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

fijal at codespeak.net fijal at codespeak.net
Mon Oct 30 17:28:31 CET 2006


Author: fijal
Date: Mon Oct 30 17:28:30 2006
New Revision: 33914

Modified:
   pypy/dist/pypy/lang/js/astgen.py
   pypy/dist/pypy/lang/js/interpreter.py
   pypy/dist/pypy/lang/js/test/test_interp.py
Log:
(santagada, fijal, arigo) - Added global scope for js interp


Modified: pypy/dist/pypy/lang/js/astgen.py
==============================================================================
--- pypy/dist/pypy/lang/js/astgen.py	(original)
+++ pypy/dist/pypy/lang/js/astgen.py	Mon Oct 30 17:28:30 2006
@@ -22,6 +22,11 @@
 #        return Script(self.getlist(d), d['varDecl'], d['funcDecl'])
 #    from_dict = staticmethod(from_dict)
 
+class Assign(Node):
+    def __init__(self, identifier, expr):
+        self.identifier = identifier
+        self.expr = expr
+
 class Semicolon(Node):
     def __init__(self, expr):
         self.expr = expr
@@ -54,20 +59,23 @@
     return output
 
 def from_dict(d):
-    if d['type'] == 'SCRIPT':
+    tp = d['type']
+    if tp == 'SCRIPT':
         # XXX: Cannot parse it right now
         return Script(getlist(d), [], [])
-    elif d['type'] == 'SEMICOLON':
+    elif tp == 'SEMICOLON':
         return Semicolon(from_dict(d['expression']))
-    elif d['type'] == 'NUMBER':
+    elif tp == 'NUMBER':
         return Number(int(d['value']))
-    elif d['type'] == 'IDENTIFIER':
+    elif tp == 'IDENTIFIER':
         return Identifier(d['value'])
-    elif d['type'] == 'LIST':
+    elif tp == 'LIST':
         return List(getlist(d))
-    elif d['type'] == 'CALL':
+    elif tp == 'CALL':
         return Call(from_dict(d['0']), from_dict(d['1']))
-    elif d['type'] == 'PLUS':
+    elif tp == 'PLUS':
         return Plus(from_dict(d['0']), from_dict(d['1']))
+    elif tp == 'ASSIGN':
+        return Assign(from_dict(d['0']), from_dict(d['1']))
     else:
-        raise NotImplementedError("Dont know how to handler %s" % d['type'])
+        raise NotImplementedError("Dont know how to handler %s" % tp)

Modified: pypy/dist/pypy/lang/js/interpreter.py
==============================================================================
--- pypy/dist/pypy/lang/js/interpreter.py	(original)
+++ pypy/dist/pypy/lang/js/interpreter.py	Mon Oct 30 17:28:30 2006
@@ -1,28 +1,43 @@
 
 from pypy.lang.js.astgen import *
+from pypy.lang.js.context import ExecutionContext
+
+def writer(x):
+    print x
+
+class __extend__(Assign):
+    def call(self, context):
+        val = self.expr.call(context)
+        context.assign(self.identifier.name, val)
+        return val
 
 class __extend__(Number):
-    def call(self):
+    def call(self, context):
         return self.num
 
 class __extend__(Plus):
-    def call(self):
-        return self.left.call() + self.right.call()
+    def call(self, context=None):
+        return self.left.call(context) + self.right.call(context)
 
 class __extend__(Semicolon):
-    def call(self):
-        self.expr.call()
+    def call(self, context=None):
+        self.expr.call(context)
+
+class __extend__(Identifier):
+    def call(self, context=None):
+        return context.access(self.name)
 
 class __extend__(Script):
-    def call(self):
+    def call(self, context=None):
+        new_context = ExecutionContext(context)
         for node in self.nodes:
-            node.call()
+            node.call(new_context)
 
 class __extend__(Call):
-    def call(self):
+    def call(self, context=None):
         assert self.identifier.name == 'print'
-        print ",".join([str(i) for i in self.arglist.call()])
+        writer(",".join([str(i) for i in self.arglist.call(context)]))
 
 class __extend__(List):
-    def call(self):
-        return [node.call() for node in self.nodes]
+    def call(self, context=None):
+        return [node.call(context) for node in self.nodes]

Modified: pypy/dist/pypy/lang/js/test/test_interp.py
==============================================================================
--- pypy/dist/pypy/lang/js/test/test_interp.py	(original)
+++ pypy/dist/pypy/lang/js/test/test_interp.py	Mon Oct 30 17:28:30 2006
@@ -6,29 +6,29 @@
 import sys
 from StringIO import StringIO
 
-def test_simple():
-    assert Plus(Number(3), Number(4)).call() == 7
-#    s = Script([Semicolon(Plus(Number(3), Number(4)))], [], [])
-#    s.call()
-    s = StringIO()
-    oldstdout = sys.stdout
-    sys.stdout = s
-
-    Script([Semicolon(Call(Identifier('print'), List([Number(1), Number(2)])))],[],[]).call()
-    assert s.getvalue() == '1,2\n'
-    sys.stdout = oldstdout
+def parse_d(code):
+    return from_dict(parse(code))
 
 class TestInterp(object):
+    def test_simple(self):
+        assert Plus(Number(3), Number(4)).call() == 7
+        #    s = Script([Semicolon(Plus(Number(3), Number(4)))], [], [])
+        #    s.call()
+        l = []
+        interpreter.writer = l.append
+        Script([Semicolon(Call(Identifier('print'), List([Number(1), Number(2)])))],[],[]).call()
+        assert l == ['1,2']
+
     def assert_prints(self, code, assval):
-        s = StringIO()
-        oldstdout = sys.stdout
-        sys.stdout = s
+        l = []
+        interpreter.writer = l.append
         code.call()
-        assert s.getvalue() == assval
-        sys.stdout = oldstdout
+        assert l == assval
     
     def test_interp_parse(self):
-        self.assert_prints(from_dict(parse("print(1+1)")), "2\n")
-        self.assert_prints(from_dict(parse("print(1+2+3); print(1)")), "6\n1\n")
-        self.assert_prints(from_dict(parse("print(1,2,3);\n")), "1,2,3\n")
+        self.assert_prints(parse_d("print(1+1)"), ["2"])
+        self.assert_prints(parse_d("print(1+2+3); print(1)"), ["6", "1"])
+        self.assert_prints(parse_d("print(1,2,3);\n"), ["1,2,3"])
 
+    def test_var_assign(self):
+        self.assert_prints(parse_d("x=3;print(x);"), ["3"])



More information about the Pypy-commit mailing list