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

stephan at codespeak.net stephan at codespeak.net
Wed Nov 1 17:14:09 CET 2006


Author: stephan
Date: Wed Nov  1 17:14:08 2006
New Revision: 34043

Modified:
   pypy/dist/pypy/lang/js/astgen.py
   pypy/dist/pypy/lang/js/context.py
   pypy/dist/pypy/lang/js/parser.py
   pypy/dist/pypy/lang/js/test/test_interp.py
   pypy/dist/pypy/lang/js/test/test_parser.py
Log:
(stephan, santagada) fixed single quote problem with js parser. fixed some context things (globals vs. locals)

Modified: pypy/dist/pypy/lang/js/astgen.py
==============================================================================
--- pypy/dist/pypy/lang/js/astgen.py	(original)
+++ pypy/dist/pypy/lang/js/astgen.py	Wed Nov  1 17:14:08 2006
@@ -101,6 +101,9 @@
     output = [from_dict(d[str(i)]) for i in range(lgt)]
     return output
 
+def build_interpreter(d):
+    return from_dict(d)
+
 def from_dict(d):
     if d is None:
         return d

Modified: pypy/dist/pypy/lang/js/context.py
==============================================================================
--- pypy/dist/pypy/lang/js/context.py	(original)
+++ pypy/dist/pypy/lang/js/context.py	Wed Nov  1 17:14:08 2006
@@ -1,22 +1,22 @@
 
 class ExecutionContext(object):
-    globals = {}
     
-    def __init__(self, parent=None):
-        pass
-    
-##    def __init__(self, parent = None):
-##        self.parent = parent
-##        if parent is None:
-##            self.globals = {}
-##        else:
-##            self.globals = parent.globals
-##        #self.locals = {}
+    def __init__(self, parent = None):
+        self.parent = parent
+        self.locals = {}
+        if parent is None:
+            self.globals = {}
+        else:
+            self.globals = parent.globals
+        #self.locals = {}
 
     def assign(self, name, value):
-        self.globals[name] = value
+        self.locals[name] = value
+        #self.globals[name] = value
 
     def access(self, name):
-        if name in self.globals:
+        if name in self.locals:
+            return self.locals[name]
+        elif name in self.globals:
             return self.globals[name]
         raise NameError("%s is not declared" % name)

Modified: pypy/dist/pypy/lang/js/parser.py
==============================================================================
--- pypy/dist/pypy/lang/js/parser.py	(original)
+++ pypy/dist/pypy/lang/js/parser.py	Wed Nov  1 17:14:08 2006
@@ -19,6 +19,7 @@
     jsparse = jsdir.join("jsparse.js").read()
     pipe = Popen("js", stdin=PIPE, stdout=PIPE, stderr=STDOUT)
     pipe.stdin.write(jsdefs + jsparse + "\n")
+    stripped_code = stripped_code.replace("'",r"\'")
     pipe.stdin.write("print(parse('%s'));\n" % stripped_code)
     pipe.stdin.close()
     retval = pipe.stdout.read()
@@ -29,6 +30,7 @@
 def parse(code_string):
     read_code = read_js_output(code_string)
     output = read_code.split(os.linesep)
+    print '\n'.join(output)
     try:
         code = eval("\n".join(output))
     except (SyntaxError, NameError):

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	Wed Nov  1 17:14:08 2006
@@ -2,12 +2,13 @@
 from pypy.lang.js.astgen import *
 from pypy.lang.js import interpreter
 from pypy.lang.js.parser import parse
+import py.test
 
 import sys
 from StringIO import StringIO
 
 def parse_d(code):
-    return from_dict(parse(code))
+    return build_interpreter(parse(code))
 
 class TestInterp(object):
     def test_simple(self):
@@ -101,14 +102,30 @@
         print(x(2,3,4));
         """), ["5"])
 
+    def test_function_has_var(self):
+        self.assert_prints(parse_d("""
+        x = function () {
+                var t = 'test';
+                return t;
+        };
+        print(x());
+        """), ["test"])
+
     def test_function_arguments(self):
+        #py.test.skip('not ready yet')
         self.assert_prints(parse_d("""
         x = function () {
-                var r = arguments[0];
-                var t = arguments[1];
+                r = arguments[0];
+                t = arguments[1];
                 return t + r;
         };
         print(x(2,3));
         """), ["5"])
 
 
+    def test_index(self):
+        self.assert_prints(parse_d("""
+        x = {1:"test"};
+        print(x[1]);
+        """), ["test"])
+

Modified: pypy/dist/pypy/lang/js/test/test_parser.py
==============================================================================
--- pypy/dist/pypy/lang/js/test/test_parser.py	(original)
+++ pypy/dist/pypy/lang/js/test/test_parser.py	Wed Nov  1 17:14:08 2006
@@ -19,3 +19,8 @@
     assert data['0']['body']['0']['value']['value'] == '1'
     assert sorted(data.keys()) == ['0', 'funDecls', 'length', 'lineno', \
                                   'tokenizer', 'type', 'varDecls']
+
+def test_single_quote():
+    "test if parser eats single quotes"
+    data = parse("x = '2'")
+    assert data['type'] == 'SCRIPT'



More information about the Pypy-commit mailing list