[pypy-commit] lang-js default: scope explicit tracks variable declaration

stepahn noreply at buildbot.pypy.org
Fri Dec 28 11:32:02 CET 2012


Author: Stephan <stephan at stzal.com>
Branch: 
Changeset: r107:6de3a8596bd9
Date: 2011-07-06 11:49 +0200
http://bitbucket.org/pypy/lang-js/changeset/6de3a8596bd9/

Log:	scope explicit tracks variable declaration

diff --git a/js/astbuilder.py b/js/astbuilder.py
--- a/js/astbuilder.py
+++ b/js/astbuilder.py
@@ -7,6 +7,7 @@
 class Scope(object):
     def __init__(self):
         self.local_variables = []
+        self.declared_variables = []
 
     def __repr__(self):
         return 'Scope ' + repr(self.local_variables)
@@ -15,6 +16,12 @@
         if not self.is_local(identifier) == True:
             self.local_variables.append(identifier)
 
+    def declare_local(self, identifier):
+        if not self.is_local(identifier) == True:
+            self.add_local(identifier)
+            if not identifier in self.declared_variables:
+                self.declared_variables.append(identifier)
+
     def is_local(self, identifier):
         return identifier in self.local_variables
 
@@ -37,9 +44,9 @@
     def end_scope(self):
         self.scopes.pop()
 
-    def identifiers(self):
+    def declarations(self):
         if self.scope_present():
-            return self.current_scope().local_variables
+            return self.current_scope().declared_variables
         return []
 
     def is_local(self, identifier):
@@ -52,6 +59,10 @@
         if self.scope_present():
             self.current_scope().add_local(identifier)
 
+    def declare_local(self, identifier):
+        if self.scope_present():
+            self.current_scope().declare_local(identifier)
+
     def get_local(self, identifier):
         return self.current_scope().get_local(identifier)
 
@@ -308,7 +319,7 @@
             node = self.dispatch(child)
             if node is not None:
                 nodes.append(node)
-        var_decl = self.scopes.identifiers()
+        var_decl = self.scopes.declarations()
         if not var_decl:
             var_decl = self.varlists.pop().keys()
         else:
@@ -344,7 +355,7 @@
         pos = self.get_pos(node)
         identifier = self.dispatch(node.children[0])
         identifier_name = identifier.get_literal()
-        self.scopes.add_local(identifier_name)
+        self.scopes.declare_local(identifier_name)
         self.varlists[-1][identifier_name] = None
         if len(node.children) > 1:
             expr = self.dispatch(node.children[1])
diff --git a/js/test/test_locals.py b/js/test/test_locals.py
--- a/js/test/test_locals.py
+++ b/js/test/test_locals.py
@@ -33,3 +33,20 @@
     assert scopes.get_local('b') == 0
     py.test.raises(ValueError, scopes.get_local, 'a')
 
+def test_scopes_declare_local():
+    scopes = Scopes()
+    scopes.new_scope()
+    assert scopes.declarations() == []
+    assert scopes.is_local('a') is False
+    assert scopes.is_local('b') is False
+    assert scopes.is_local('c') is False
+    scopes.declare_local('a')
+    assert scopes.is_local('a') is True
+    assert scopes.is_local('b') is False
+    scopes.add_local('b')
+    assert scopes.is_local('b') is True
+    assert scopes.get_local('a') == 0
+    assert scopes.get_local('b') == 1
+    py.test.raises(ValueError, scopes.get_local, 'c')
+    assert scopes.declarations() == ['a']
+


More information about the pypy-commit mailing list