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

santagada at codespeak.net santagada at codespeak.net
Fri Nov 3 15:29:58 CET 2006


Author: santagada
Date: Fri Nov  3 15:29:57 2006
New Revision: 34108

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:
(stephan, santagada) Grouping, comma operator, throwing exceptions


Modified: pypy/dist/pypy/lang/js/astgen.py
==============================================================================
--- pypy/dist/pypy/lang/js/astgen.py	(original)
+++ pypy/dist/pypy/lang/js/astgen.py	Fri Nov  3 15:29:57 2006
@@ -9,13 +9,6 @@
 #    def __init__(self, lineno = 1):
 #        self.lineno = lineno
 
-#    def getlist(d):
-#        lgt = int(d['length'])
-#        output = [d[str(i)] for i in range(lgt)]
-#        return output
-#    getlist = staticmethod(getlist)
-
-        
 class Array(Node):
     def __init__(self, items=()):
         self.items = items
@@ -30,6 +23,11 @@
         self.identifier = identifier
         self.arglist = arglist
 
+class Comma(Node):
+    def __init__(self, left, right):
+        self.left = left
+        self.right = right
+
 class Dot(Node):
     def __init__(self, left, right):
         self.left = left
@@ -42,7 +40,12 @@
         self.scope = scope
         #w_obj = W_Object({}, function=self)
         #self.scope = Scope(copy(scope.dict))
-    
+
+class Group(Node):
+    """The Group class."""
+    def __init__(self, expr):
+        self.expr = expr
+
 class Identifier(Node):
     def __init__(self, name, initialiser):
         self.name = name
@@ -93,6 +96,12 @@
     def __init__(self, strval):
         self.strval = strval
 
+class Throw(Node):
+    """The Throw class."""
+    def __init__(self, exception):
+        self.exception = exception
+
+
 class Vars(Node):
     def __init__(self, nodes):
         self.nodes = nodes
@@ -149,6 +158,12 @@
         return f
     elif tp == 'RETURN':
         return Return(from_dict(d['value']))
+    elif tp == 'THROW':
+        return Throw(from_dict(d['exception']))
+    elif tp == 'GROUP':
+        return Group(from_dict(d['0']))
+    elif tp == 'COMMA':
+        return Comma(from_dict(d['0']),from_dict(d['1']))
     elif tp == 'VAR':
         return Vars(getlist(d))
     else:

Modified: pypy/dist/pypy/lang/js/interpreter.py
==============================================================================
--- pypy/dist/pypy/lang/js/interpreter.py	(original)
+++ pypy/dist/pypy/lang/js/interpreter.py	Fri Nov  3 15:29:57 2006
@@ -11,6 +11,11 @@
     def __init__(self, value):
         self.value = value
 
+class ThrowException(Exception):
+    def __init__(self, exception):
+        self.exception = exception
+
+
 class __extend__(Array):
     def call(self, context):
         d = dict(enumerate(self.items))
@@ -42,6 +47,11 @@
             scope_manager.current_scope = backup_scope
             return retval
 
+class __extend__(Comma):
+    def call(self, context=None):
+        self.left.call(context)
+        return self.right.call(context)
+
 class __extend__(Dot):
     def call(self, context=None):
         w_obj = self.left.call(context).GetValue().ToObject()
@@ -65,6 +75,11 @@
     def get_literal(self):
         return self.name
 
+class __extend__(Group):
+    """The __extend__ class."""
+    def call(self, context = None):
+        return self.expr.call(context)
+
 class __extend__(Index):
     def call(self, context=None):
         w_obj = self.left.call(context).GetValue()
@@ -128,7 +143,6 @@
             ncontext.assign(item, temp)
         
         w_Arguments = W_Arguments(dict([(str(x),y) for x,y in enumerate(args)]))
-        print w_Arguments.dict_w
         ncontext.assign('arguments', w_Arguments)
         
         try:
@@ -153,6 +167,10 @@
     def call(self, context=None):
         raise ExecutionReturned(self.expr.call(context))
 
+class __extend__(Throw):
+    def call(self, context=None):
+        raise ThrowException(self.exception.call(context))
+    
 class __extend__(Vars):
     def call(self, context=None):
         for var 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	Fri Nov  3 15:29:57 2006
@@ -2,6 +2,7 @@
 from pypy.lang.js.astgen import *
 from pypy.lang.js import interpreter
 from pypy.lang.js.parser import parse
+from pypy.lang.js.interpreter import ThrowException
 import py.test
 
 import sys
@@ -23,7 +24,10 @@
     def assert_prints(self, code, assval):
         l = []
         interpreter.writer = l.append
-        code.call()
+        try:
+            code.call()
+        except ThrowException, excpt:
+            l.append("uncaught exception: "+str(excpt.exception))
         assert l == assval
     
     def test_interp_parse(self):
@@ -112,7 +116,6 @@
         """), ["test"])
 
     def test_function_arguments(self):
-        #py.test.skip('not ready yet')
         self.assert_prints(parse_d("""
         x = function () {
                 r = arguments[0];
@@ -136,3 +139,23 @@
         print(x);
         """), ["[]"])
 
+    def test_throw(self):
+        self.assert_prints(parse_d("throw(3)"), ["uncaught exception: 3"])
+        
+    def test_group(self):
+        self.assert_prints(parse_d("print((2+1))"), ["3"])
+
+    def test_comma(self):
+        self.assert_prints(parse_d("print((500,3))"), ["3"])
+    
+    def test_try_catch(self):
+        py.test.skip('Under Construction')
+        self.assert_prints(parse_d("""
+        try {
+            throw(3);
+        }
+        catch (x) {
+            print(x);
+        }
+        """), ["3"])
+        
\ No newline at end of file



More information about the Pypy-commit mailing list