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

santagada at codespeak.net santagada at codespeak.net
Tue May 22 02:53:48 CEST 2007


Author: santagada
Date: Tue May 22 02:53:46 2007
New Revision: 43549

Modified:
   pypy/dist/pypy/lang/js/astbuilder.py
   pypy/dist/pypy/lang/js/jsgrammar.txt
   pypy/dist/pypy/lang/js/operations.py
   pypy/dist/pypy/lang/js/test/test_interp.py
Log:
lots of tests running, implemented ifs and whiles

Modified: pypy/dist/pypy/lang/js/astbuilder.py
==============================================================================
--- pypy/dist/pypy/lang/js/astbuilder.py	(original)
+++ pypy/dist/pypy/lang/js/astbuilder.py	Tue May 22 02:53:46 2007
@@ -44,6 +44,10 @@
         '!=': operations.Ne,
         '!==': operations.StrictNe,
         '===': operations.StrictEq,
+        '>': operations.Gt,
+        '>=': operations.Ge,
+        '<': operations.Lt,
+        '<=': operations.Le,
         '.': operations.Member,
         '[': operations.Member,
     }
@@ -54,6 +58,7 @@
         '-': operations.UMinus,
         '++': operations.Increment,
         '--': operations.Decrement,
+        'typeof': operations.Typeof,
     }
     LISTOP_TO_CLS = {
         '[': operations.Array,
@@ -114,6 +119,7 @@
     visit_equalityexpression = binaryop
     visit_logicalorexpression = binaryop
     visit_logicalandexpression = binaryop
+    visit_relationalexpression = binaryop
     
     def visit_memberexpression(self, node):
         if isinstance(node.children[0], Symbol) and \
@@ -158,6 +164,22 @@
         return self.LISTOP_TO_CLS[op.additional_info](pos, l)
     visit_arrayliteral = listop # XXX elision
     visit_objectliteral = listop
+
+    def visit_block(self, node):
+        op = node.children[0]
+        pos = self.get_pos(op)
+        l = [self.dispatch(child) for child in node.children[1:]]
+        return operations.Block(pos, l)
+
+    def visit_arguments(self, node):
+        pos = self.get_pos(node)
+        nodes = [self.dispatch(child) for child in node.children[1:]]
+        return operations.ArgumentList(pos, nodes)
+
+    def visit_variabledeclarationlist(self, node):
+        pos = self.get_pos(node)
+        nodes = [self.dispatch(child) for child in node.children]
+        return operations.VariableDeclList(pos, nodes)
     
     def visit_propertynameandvalue(self, node):
         pos = self.get_pos(node)
@@ -174,6 +196,16 @@
         pos = self.get_pos(node)
         body = self.dispatch(node.children[0])
         return operations.Program(pos, body)
+
+    def visit_variablestatement(self, node):
+        pos = self.get_pos(node)
+        body = self.dispatch(node.children[0])
+        return operations.Variable(pos, body)
+
+    def visit_throwstatement(self, node):
+        pos = self.get_pos(node)
+        exp = self.dispatch(node.children[0])
+        return operations.Throw(pos, exp)
         
     def visit_sourceelements(self, node):
         pos = self.get_pos(node)
@@ -188,17 +220,7 @@
     
     def visit_expressionstatement(self, node):
         return self.dispatch(node.children[0])
-    
-    def visit_variablestatement(self, node):
-        pos = self.get_pos(node)
-        body = self.dispatch(node.children[0])
-        return operations.Variable(pos, body)
-    
-    def visit_variabledeclarationlist(self, node):
-        pos = self.get_pos(node)
-        nodes = [self.dispatch(child) for child in node.children]
-        return operations.VariableDeclList(pos, nodes)
-    
+            
     def visit_variabledeclaration(self, node):
         pos = self.get_pos(node)
         identifier = self.dispatch(node.children[0])
@@ -213,12 +235,7 @@
         left = self.dispatch(node.children[0])
         right = self.dispatch(node.children[1])
         return operations.Call(pos, left, right)
-    
-    def visit_arguments(self, node):
-        pos = self.get_pos(node)
-        nodes = [self.dispatch(child) for child in node.children[1:]]
-        return operations.ArgumentList(pos, nodes)
-    
+        
     def visit_assignmentexpression(self, node):
         pos = self.get_pos(node)
         left = self.dispatch(node.children[0])
@@ -228,12 +245,7 @@
     
     def visit_functiondeclaration(self, node):
         pos = self.get_pos(node)
-        
-    def visit_throwstatement(self, node):
-        pos = self.get_pos(node)
-        exp = self.dispatch(node.children[0])
-        return operations.Throw(pos, exp)
-    
+            
     def visit_emptystatement(self, node):
         return operations.astundef
     
@@ -245,3 +257,19 @@
             val = self.dispatch(node.children[0])
             return operations.New(pos, val)
     
+    def visit_ifstatement(self, node):
+        pos = self.get_pos(node)
+        condition = self.dispatch(node.children[0])
+        ifblock =  self.dispatch(node.children[1])
+        if len(node.children) > 2:
+            elseblock =  self.dispatch(node.children[2])
+        else:
+            elseblock = operations.astundef
+        return operations.If(pos, condition, ifblock, elseblock)
+    
+    def visit_iterationstatement(self, node):
+        pos = self.get_pos(node)
+        if node.children[0].additional_info == 'while':
+            condition = self.dispatch(node.children[1])
+            block = self.dispatch(node.children[2])
+            return operations.While(pos, condition, block)

Modified: pypy/dist/pypy/lang/js/jsgrammar.txt
==============================================================================
--- pypy/dist/pypy/lang/js/jsgrammar.txt	(original)
+++ pypy/dist/pypy/lang/js/jsgrammar.txt	Tue May 22 02:53:46 2007
@@ -13,7 +13,7 @@
                 | <statement>
                 ;
 
-statement   : <block> [";"]?
+statement   : <block>
             | <variablestatement> [";"]
             | <emptystatement>
             | <expressionstatement> [";"]
@@ -29,7 +29,7 @@
             | <trystatement>
             ;
 
-block   : ["{"] >statementlist<? ["}"]
+block   : "{" >statementlist<? ["}"]
         ;
 
 statementlist   : statement >statementlist<

Modified: pypy/dist/pypy/lang/js/operations.py
==============================================================================
--- pypy/dist/pypy/lang/js/operations.py	(original)
+++ pypy/dist/pypy/lang/js/operations.py	Tue May 22 02:53:46 2007
@@ -91,6 +91,12 @@
     def decision(self, ctx, op1, op2):
         raise NotImplementedError
 
+class Undefined(Statement):
+    def execute(self, ctx):
+        return w_Undefined
+
+astundef = Undefined(Position())
+
 class PropertyInit(BinaryOp):
     pass
 
@@ -138,6 +144,7 @@
 
 class Block(Statement):
     def __init__(self, pos, nodes):
+        self.pos = pos
         self.nodes = nodes
     
     def execute(self, ctx):
@@ -286,12 +293,10 @@
     opcode = "THIS"
 
 class If(Statement):
-    opcode = 'IF'
-
-    def __init__(self, pos, t):
-        self.condition = get_obj(t, 'condition')
-        self.thenPart = get_obj(t, 'thenPart')
-        self.elsePart = get_obj(t, 'elsePart')
+    def __init__(self, pos, condition, thenpart, elsepart=astundef):
+        self.condition = condition
+        self.thenPart = thenpart
+        self.elsePart = elsepart
 
     def execute(self, ctx):
         temp = self.condition.eval(ctx).GetValue()
@@ -870,10 +875,6 @@
             return W_String("undefined")
         return W_String(val.GetValue().type())
 
-class Undefined(Statement):
-    def execute(self, ctx):
-        return None
-
 class VariableDeclaration(Expression):
     def __init__(self, pos, identifier, expr=None):
         self.pos = pos
@@ -931,9 +932,10 @@
 
 
 class WhileBase(Statement):
-    def __init__(self, pos, t):
-        self.condition = get_obj(t, 'condition')
-        self.body = get_obj(t, 'body')
+    def __init__(self, pos, condition, body):
+        self.pos = pos
+        self.condition = condition
+        self.body = body
 
 class Do(WhileBase):
     opcode = 'DO'
@@ -956,8 +958,6 @@
                     continue
     
 class While(WhileBase):
-    opcode = 'WHILE'
-    
     def execute(self, ctx):
         while self.condition.eval(ctx).ToBoolean():
             try:
@@ -967,6 +967,7 @@
                     break
                 elif e.type == 'continue':
                     continue
+    
 
 class ForIn(Statement):
     def __init__(self, pos, t):
@@ -1030,5 +1031,3 @@
     def eval(self, ctx):
         return W_Number(+self.expr.eval(ctx).GetValue().ToNumber())
     
-
-astundef = Undefined(Position())

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	Tue May 22 02:53:46 2007
@@ -5,9 +5,9 @@
 import py
 
 from pypy.lang.js import interpreter
-from pypy.lang.js.jsparser import parse
-from pypy.lang.js.interpreter import *
-from pypy.lang.js.jsobj import W_Number, W_Object, ExecutionContext
+from pypy.lang.js.operations import AEC, Number, Position, Plus
+from pypy.lang.js.jsobj import W_Number, W_Object, \
+    ExecutionContext, W_Root, ThrowException, w_Null
 
 def test_simple():
     n1 = Number(Position(), 2.0)
@@ -23,10 +23,10 @@
     js_int = interpreter.Interpreter()
     try:
         if isinstance(code, str):
-            js_int.run(load_source(code))
+            js_int.run(interpreter.load_source(code))
         else:
             for codepiece in code:
-                js_int.run(load_source(codepiece))
+                js_int.run(interpreter.load_source(codepiece))
     except ThrowException, excpt:
         l.append("uncaught exception: "+str(excpt.exception.ToString()))
     print l, assval
@@ -37,7 +37,7 @@
     interpreter.writer = l.append
     jsint = interpreter.Interpreter()
     try:
-        jsint.run(load_source(code))
+        jsint.run(interpreter.load_source(code))
     except ThrowException, excpt:
         l.append("uncaught exception: "+str(excpt.exception.ToString()))
     print l, prints
@@ -49,11 +49,13 @@
 def assertv(code, value):
     jsint = interpreter.Interpreter()
     try:
-        code_val = jsint.run(load_source(code)).GetValue()
+        code_val = jsint.run(interpreter.load_source(code)).GetValue()
     except ThrowException, excpt:
         code_val = excpt
     print code_val, value
-    if isinstance(value, bool):
+    if isinstance(value, W_Root):
+        assert AEC(jsint.global_context, code_val, value) == True
+    elif isinstance(value, bool):
         assert code_val.ToBoolean() == value
     elif isinstance(value, int):
         assert code_val.ToInt32() == value
@@ -61,11 +63,6 @@
         assert code_val.ToNumber() == value
     else:
         assert code_val.ToString() == value
-
-def assert_result(code, result):
-    inter = interpreter.Interpreter()
-    r = inter.run(load_source(code))
-    assert r.ToString() == result.ToString()
     
 def test_interp_parse():
     yield assertv, "1+1;", 2
@@ -210,9 +207,8 @@
     """, ["3"])
 
 def test_block():
-    py.test.skip("not ready yet")
-    assert_result("{ 5};", W_Number(5))
-    assert_result("{3; 5};", W_Number(5))
+    assertv("{5;}", W_Number(5))
+    assertv("{3; 5;}", W_Number(5))
 
 def test_try_catch_finally():
     py.test.skip("not ready yet")
@@ -229,7 +225,6 @@
     """, ["3", "5"])
     
 def test_if_then():
-    py.test.skip("not ready yet")
     assertp("""
     if (1) {
         print(1);
@@ -237,42 +232,39 @@
     """, "1")
 
 def test_if_then_else():
-    py.test.skip("not ready yet")
-    assert_prints("""
+    assertp("""
     if (0) {
         print(1);
     } else {
         print(2);
     }
-    """, ["2"])
+    """, "2")
 
 def test_compare():
-    py.test.skip("not ready yet")
-    assert_prints("print(1>0);",["true"])
-    assert_prints("print(0>1);",["false"])
-    assert_prints("print(0>0);",["false"])
-    assert_prints("print(1<0);",["false"])
-    assert_prints("print(0<1);",["true"])
-    assert_prints("print(0<0);",["false"])
-    assert_prints("print(1>=0);",["true"])
-    assert_prints("print(1>=1);",["true"])
-    assert_prints("print(1>=2);",["false"])
-    assert_prints("print(0<=1);",["true"])
-    assert_prints("print(1<=1);",["true"])
-    assert_prints("print(1<=0);",["false"])
-    assert_prints("print(0==0);",["true"])
-    assert_prints("print(1==1);",["true"])
-    assert_prints("print(0==1);",["false"])
-    assert_prints("print(0!=1);",["true"])
-    assert_prints("print(1!=1);",["false"])
+    yield assertv, "1>0;", True
+    yield assertv, "0>1;", False
+    yield assertv, "0>0;", False
+    yield assertv, "1<0;", False
+    yield assertv, "0<1;", True
+    yield assertv, "0<0;", False
+    yield assertv, "1>=0;", True
+    yield assertv, "1>=1;", True
+    yield assertv, "1>=2;", False
+    yield assertv, "0<=1;", True
+    yield assertv, "1<=1;", True
+    yield assertv, "1<=0;", False
+    yield assertv, "0==0;", True
+    yield assertv, "1==1;", True
+    yield assertv, "0==1;", False
+    yield assertv, "0!=1;", True
+    yield assertv, "1!=1;", False
 
 def test_binary_op():
-    assert_prints("print(0||0); print(1||0);",["0", "1"])
-    assert_prints("print(0&&1); print(1&&1);",["0", "1"])
+    yield assertp, "print(0||0); print(1||0);", ["0", "1"]
+    yield assertp, "print(0&&1); print(1&&1);", ["0", "1"]
 
 def test_while():
-    py.test.skip("not ready yet")
-    assert_prints("""
+    assertp("""
     i = 0;
     while (i<3) {
         print(i);
@@ -373,11 +365,10 @@
     print('out');""", ["out"])
 
 def test_typeof():
-    py.test.skip("not ready yet")
-    assert_result("""
+    assertv("""
     var x = 3;
     typeof x == 'number';
-    """, W_Boolean(True))
+    """, True)
     
 def test_semicolon():
     assertp(';', [])
@@ -451,7 +442,7 @@
     assert_prints(""" "'t'"; """, [])
     
 def test_null():
-    assert_result("null;", w_Null)
+    assertv("null;", w_Null)
 
 def test_void():
     py.test.skip("not ready yet")



More information about the Pypy-commit mailing list