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

santagada at codespeak.net santagada at codespeak.net
Tue Jan 2 22:03:35 CET 2007


Author: santagada
Date: Tue Jan  2 22:03:29 2007
New Revision: 36112

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:
binary and comparison operations ready


Modified: pypy/dist/pypy/lang/js/astgen.py
==============================================================================
--- pypy/dist/pypy/lang/js/astgen.py	(original)
+++ pypy/dist/pypy/lang/js/astgen.py	Tue Jan  2 22:03:29 2007
@@ -14,6 +14,16 @@
         self.left = left
         self.right = right
 
+class BinaryLogicOperator(Node):
+    """super class for binary operators"""
+    def __init__(self, left, right):
+        self.left = left
+        self.right = right
+
+class Or(BinaryLogicOperator): pass
+
+class And(BinaryLogicOperator): pass
+
 
 class Array(Node):
     def __init__(self, items=()):
@@ -44,6 +54,8 @@
         self.left = left
         self.right = right
 
+class Eq(BinaryOperator): pass
+
 class Function(Node):
     def __init__(self, name, params, body):
         self.name = name
@@ -54,6 +66,8 @@
     def __init__(self, expr):
         self.expr = expr
 
+class Ge(BinaryOperator): pass
+
 class Gt(BinaryOperator): pass
 
 class Identifier(Node):
@@ -69,6 +83,8 @@
         self.thenPart = thenPart
         self.elsePart = elsePart
 
+class In(BinaryOperator): pass
+
 class Index(Node):
     def __init__(self, left, expr):
         self.left = left
@@ -78,8 +94,14 @@
     def __init__(self, nodes):
         self.nodes = nodes
 
+class Le(BinaryOperator): pass
+
 class Lt(BinaryOperator): pass
 
+class Minus(BinaryOperator):pass
+
+class Ne(BinaryOperator):pass
+
 class New(Node):
     def __init__(self, identifier):
         self.identifier = identifier
@@ -187,6 +209,12 @@
         return Comma(from_dict(d['0']),from_dict(d['1']))
     elif tp == 'DOT':
         return Dot(from_dict(d['0']), from_dict(d['1']))
+    elif tp == 'EQ':
+        return Eq(from_dict(d['0']), from_dict(d['1']))
+    elif tp == 'OR':
+        return Or(from_dict(d['0']), from_dict(d['1']))
+    elif tp == 'AND':
+        return And(from_dict(d['0']), from_dict(d['1']))
     elif tp == 'FUNCTION':        
         name = d.get('name', '')
         body = from_dict(d['body'])
@@ -198,6 +226,8 @@
         return f
     elif tp == 'GROUP':
         return Group(from_dict(d['0']))
+    elif tp == 'GE':
+        return Ge(from_dict(d['0']), from_dict(d['1']))
     elif tp == 'GT':
         return Gt(from_dict(d['0']), from_dict(d['1']))
     elif tp == 'IDENTIFIER':
@@ -213,12 +243,20 @@
         else:
             elsePart = from_dict(d['elsePart'])
         return If(condition,thenPart,elsePart)
+    elif tp == 'IN':
+        return In(from_dict(d['0']), from_dict(d['1']))
     elif tp == 'INDEX':
         return Index(from_dict(d['0']), from_dict(d['1']))
     elif tp == 'LIST':
         return List(getlist(d))
+    elif tp == 'LE':
+        return Le(from_dict(d['0']), from_dict(d['1']))
     elif tp == 'LT':
         return Lt(from_dict(d['0']), from_dict(d['1']))
+    elif tp == 'MINUS':
+        return Minus(from_dict(d['0']), from_dict(d['1']))
+    elif tp == 'NE':
+        return Ne(from_dict(d['0']), from_dict(d['1']))
     elif tp == 'NEW':
         return New(d['0']['value'])
     elif tp == 'NUMBER':

Modified: pypy/dist/pypy/lang/js/interpreter.py
==============================================================================
--- pypy/dist/pypy/lang/js/interpreter.py	(original)
+++ pypy/dist/pypy/lang/js/interpreter.py	Tue Jan  2 22:03:29 2007
@@ -124,29 +124,88 @@
     else:
         pass 
 
-class __extend__(Gt):
-    def call(self, ctx = None):
+class __extend__(Or):
+    def call(self, ctx):
+        s2 = self.left.call(ctx).GetValue()
+        if s2.ToBoolean():
+            return s2
+        s4 = self.right.call(ctx).GetValue()
+        return s4
+
+class __extend__(And):
+    def call(self, ctx):
         s2 = self.left.call(ctx).GetValue()
+        if not s2.ToBoolean():
+            return s2
         s4 = self.right.call(ctx).GetValue()
-        s5 = ARC(s4, s2)
+        return s4
+
+
+class __extend__(BinaryOperator):
+    def call(self, ctx):
+        s2 = self.left.call(ctx).GetValue()
+        s4 = self.right.call(ctx).GetValue()
+        return self.decision(s2, s4)
+
+class __extend__(Ge):
+    def decision(self, op1, op2):
+        s5 = ARC(op1, op2)
+        if s5 is None or s5:
+            return W_Boolean(False)
+        else:
+            return W_Boolean(True)
+
+class __extend__(Gt):
+    def decision(self, op1, op2):
+        s5 = ARC(op2, op1)
         if s5 is None:
             return W_Boolean(False)
         else:
             return W_Boolean(s5)
 
+class __extend__(Le):
+    def decision(self, op1, op2):
+        s5 = ARC(op2, op1)
+        if s5 is None or s5:
+            return W_Boolean(False)
+        else:
+            return W_Boolean(True)
+
 class __extend__(Lt):
-    def call(self, ctx = None):
-        s2 = self.left.call(ctx).GetValue()
-        s4 = self.right.call(ctx).GetValue()
-        s5 = ARC(s2, s4)
-        print "< ARC result = ", s5
+    def decision(self, op1, op2):
+        s5 = ARC(op1, op2)
         if s5 is None:
             return W_Boolean(False)
         else:
             return W_Boolean(s5)
 
+def AEC(x, y):
+    """
+    Implements the Abstract Equality Comparison x == y
+    not following the specs yet
+    """
+    r = x.ToNumber() == y.ToNumber()
+    return r
+
+class __extend__(Eq):
+    def decision(self, op1, op2):
+        return W_Boolean(AEC(op1, op2))
+
+class __extend__(Ne):
+    def decision(self, op1, op2):
+        return W_Boolean(not AEC(op1, op2))
+
+
+class __extend__(In):
+    def decision(self, op1, op2):
+        if not isinstance(op2, W_Object):
+            raise ThrowException("TypeError")
+        name = op1.ToString()
+        return W_Boolean(op2.HasProperty(name))
+
+
 class __extend__(Index):
-    def call(self, ctx=None):
+    def call(self, ctx):
         w_obj = self.left.call(ctx).GetValue()
         w_member = self.expr.call(ctx).GetValue()
         w_obj = w_obj.ToObject()
@@ -158,6 +217,12 @@
         print "nodes = ", self.nodes
         return [node.call(ctx) for node in self.nodes]
 
+class __extend__(Minus):
+    def decision(self, op1, op2):
+        x = op1.ToNumber()
+        y = op2.ToNumber()
+        return W_Number(x - y)
+
 class __extend__(New):
     def call(self, ctx=None):
         obj = W_Object()
@@ -165,10 +230,8 @@
         constructor = ctx.resolve_identifier(self.identifier).GetValue()
         obj.Put('prototype', constructor.Get('prototype'))
         constructor.Call(ctx, this = obj)
-        
         return obj
 
-
 class __extend__(Number):
     def call(self, ctx):
         return W_Number(self.num)
@@ -188,13 +251,9 @@
         return w_obj
 
 class __extend__(Plus):
-    def call(self, ctx):
-        print "left", self.left.call(ctx)
-        left = self.left.call(ctx).GetValue()
-        right = self.right.call(ctx).GetValue()
-        prim_left = left.ToPrimitive('Number')
-        prim_right = right.ToPrimitive('Number')
-        # INSANE
+    def decision(self, op1, op2):
+        prim_left = op1.ToPrimitive('Number')
+        prim_right = op2.ToPrimitive('Number')
         if isinstance(prim_left, W_String) or isinstance(prim_right, W_String):
             str_left = prim_left.ToString()
             str_right = prim_right.ToString()
@@ -202,7 +261,6 @@
         else:
             num_left = prim_left.ToNumber()
             num_right = prim_right.ToNumber()
-            # XXX: obey all the rules
             return W_Number(num_left + num_right)
 
 class __extend__(Script):

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 Jan  2 22:03:29 2007
@@ -52,6 +52,9 @@
         self.assert_prints("x=3;print(x);", ["3"])
         self.assert_prints("x=3;y=4;print(x+y);", ["7"])
 
+    def test_minus(self):
+        self.assert_prints("print(2-1)", ["1"])
+    
     def test_string_var(self):
         self.assert_prints('print(\"sss\");', ["sss"])
     
@@ -82,7 +85,6 @@
         self.assert_prints('var x = 3; print(x);', ["3"])
         self.assert_prints('var x = 3; print(x+x);', ["6"])
 
-    
     def test_var_scoping(self):
         self.assert_prints("""
         var y;
@@ -213,8 +215,22 @@
         self.assert_prints("print(1<0)",["false"])
         self.assert_prints("print(0<1)",["true"])
         self.assert_prints("print(0<0)",["false"])
-
-
+        self.assert_prints("print(1>=0)",["true"])
+        self.assert_prints("print(1>=1)",["true"])
+        self.assert_prints("print(1>=2)",["false"])
+        self.assert_prints("print(0<=1)",["true"])
+        self.assert_prints("print(1<=1)",["true"])
+        self.assert_prints("print(1<=0)",["false"])
+        self.assert_prints("print(0==0)",["true"])
+        self.assert_prints("print(1==1)",["true"])
+        self.assert_prints("print(0==1)",["false"])
+        self.assert_prints("print(0!=1)",["true"])
+        self.assert_prints("print(1!=1)",["false"])
+
+    def test_binary_op(self):
+        self.assert_prints("print(0||0); print(1||0)",["0", "1"])
+        self.assert_prints("print(0&&1); print(1&&1)",["0", "1"])
+    
     def test_while(self):
         self.assert_prints("""
         i = 0;
@@ -262,13 +278,13 @@
     def test_vars(self):
         self.assert_prints("""
         var x;x=3; print(x)""", ["3"])
-        
-    def test_fun_decl(self):
-        py.test.skip("still not ready")
+
+    def test_minus(self):
         self.assert_prints("""
-        function x () { print('i work')}
-        x()
-        """, ["i work"])
+        x = {y:3};
+        print("y" in x);
+        print("z" in x);
+        """, ["true", "false"])
     
-        
+
 


More information about the pypy-svn mailing list