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

fijal at codespeak.net fijal at codespeak.net
Mon Oct 30 16:15:57 CET 2006


Author: fijal
Date: Mon Oct 30 16:15:56 2006
New Revision: 33907

Modified:
   pypy/dist/pypy/lang/js/astgen.py
   pypy/dist/pypy/lang/js/test/test_interp.py
Log:
(santagada, fijal) - First JS code is working and interpreted.


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 16:15:56 2006
@@ -48,6 +48,26 @@
     def __init__(self, nodes):
         self.nodes = nodes
 
-#class Print(Node):
-#    def __init__(self, expr):
-#        
+def getlist(d):
+    lgt = int(d['length'])
+    output = [from_dict(d[str(i)]) for i in range(lgt)]
+    return output
+
+def from_dict(d):
+    if d['type'] == 'SCRIPT':
+        # XXX: Cannot parse it right now
+        return Script(getlist(d), [], [])
+    elif d['type'] == 'SEMICOLON':
+        return Semicolon(from_dict(d['expression']))
+    elif d['type'] == 'NUMBER':
+        return Number(int(d['value']))
+    elif d['type'] == 'IDENTIFIER':
+        return Identifier(d['value'])
+    elif d['type'] == 'LIST':
+        return List(getlist(d))
+    elif d['type'] == 'CALL':
+        return Call(from_dict(d['0']), from_dict(d['1']))
+    elif d['type'] == 'PLUS':
+        return Plus(from_dict(d['0']), from_dict(d['1']))
+    else:
+        raise NotImplementedError("Dont know how to handler %s" % d['type'])

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 16:15:56 2006
@@ -1,6 +1,7 @@
 
 from pypy.lang.js.astgen import *
 from pypy.lang.js import interpreter
+from pypy.lang.js.parser import parse
 
 import sys
 from StringIO import StringIO
@@ -16,3 +17,18 @@
     Script([Semicolon(Call(Identifier('print'), List([Number(1), Number(2)])))],[],[]).call()
     assert s.getvalue() == '1,2\n'
     sys.stdout = oldstdout
+
+class TestInterp(object):
+    def assert_prints(self, code, assval):
+        s = StringIO()
+        oldstdout = sys.stdout
+        sys.stdout = s
+        code.call()
+        assert s.getvalue() == assval
+        sys.stdout = oldstdout
+    
+    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")
+



More information about the Pypy-commit mailing list