[pypy-svn] r33982 - in pypy/dist/pypy/lang/js: . test

fijal at codespeak.net fijal at codespeak.net
Tue Oct 31 19:27:00 CET 2006


Author: fijal
Date: Tue Oct 31 19:26:57 2006
New Revision: 33982

Added:
   pypy/dist/pypy/lang/js/scope.py   (contents, props changed)
   pypy/dist/pypy/lang/js/test/test_scope.py   (contents, props changed)
Log:
Oops.


Added: pypy/dist/pypy/lang/js/scope.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/lang/js/scope.py	Tue Oct 31 19:26:57 2006
@@ -0,0 +1,56 @@
+
+class Scope(object):
+    def __init__(self, parent=None):
+        # string --> W_Root
+        self.dict_w = {}
+        self.parent = parent
+    
+    def set(self, name, w_obj):
+        self.dict_w[name] = w_obj
+    
+    def get(self, name):
+        return self.dict_w[name]
+
+    def has(self, name):
+        return name in self.dict_w
+
+class ScopeManager(object):
+    def __init__(self):
+        self.current_scope = Scope(None)
+    
+    def enter_scope(self):
+        self.current_scope = Scope(self.current_scope)
+        return self.current_scope
+    
+    def leave_scope(self):
+        self.current_scope = self.current_scope.parent
+    
+    def get_variable(self, name, scope=None):
+        if scope is None:
+            scope = self.current_scope
+        while 1:
+            try:
+                return scope.get(name)
+            except KeyError:
+                scope = scope.parent
+                if scope is None:
+                    raise NameError("Name %s not defined" % name)
+    
+    def set_variable(self, name, w_value, scope=None):
+        if scope is None:
+            scope = self.current_scope
+        while 1:
+            if scope.has(name):
+                scope.set(name, w_value)
+                return
+            if scope.parent is None:
+                scope.set(name, w_value)
+                return
+            scope = scope.parent
+
+    def add_variable(self, name, w_value, scope=None):
+        if scope is None:
+            scope = self.current_scope
+        scope.set(name, w_value)
+
+scope_manager = ScopeManager()

Added: pypy/dist/pypy/lang/js/test/test_scope.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/lang/js/test/test_scope.py	Tue Oct 31 19:26:57 2006
@@ -0,0 +1,36 @@
+
+""" test scope
+"""
+
+from pypy.lang.js.scope import ScopeManager
+
+
+def test_scope_manager_search():
+    sm = ScopeManager()
+    sm.enter_scope()
+    sm.add_variable("5", 5)
+    assert sm.get_variable("5") == 5
+    sm.enter_scope()
+    assert sm.get_variable("5") == 5
+    sm.add_variable("5", 6)
+    assert sm.get_variable("5") == 6
+    sm.leave_scope()
+    assert sm.get_variable("5") == 5
+
+def test_scope_manager_overlay():
+    sm = ScopeManager()
+    sm.enter_scope()
+    sm.add_variable("5", 5)
+    assert sm.get_variable("5") == 5
+    sm.enter_scope()
+    sm.add_variable("5", 6)
+    assert sm.get_variable("5") == 6
+
+def test_scope_manager_updown():
+    sm = ScopeManager()
+    sm.enter_scope()
+    sm.add_variable("5", 5)
+    sm.enter_scope()
+    sm.add_variable("5", 6)
+    sm.leave_scope()
+    assert sm.get_variable("5") == 5



More information about the Pypy-commit mailing list