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

santagada at codespeak.net santagada at codespeak.net
Fri Jun 1 21:07:02 CEST 2007


Author: santagada
Date: Fri Jun  1 21:06:59 2007
New Revision: 43978

Modified:
   pypy/dist/pypy/lang/js/operations.py
   pypy/dist/pypy/lang/js/test/test_interp.py
Log:
implemented string comparisons for the logical ops, did some tests for it and
then removed the static methods from the binary math ops


Modified: pypy/dist/pypy/lang/js/operations.py
==============================================================================
--- pypy/dist/pypy/lang/js/operations.py	(original)
+++ pypy/dist/pypy/lang/js/operations.py	Fri Jun  1 21:06:59 2007
@@ -74,8 +74,6 @@
 class BinaryOp(Expression):
     def __init__(self, pos, left, right):
         self.pos = pos
-        assert isinstance(left, Node)
-        assert isinstance(right, Node)
         self.left = left
         self.right = right
     
@@ -109,7 +107,7 @@
 class PropertyInit(BinaryOp):
     pass
 
-class Array(ListOp):    
+class Array(ListOp):
     def eval(self, ctx):
         array = W_Array()
         for i in range(len(self.nodes)):
@@ -121,7 +119,7 @@
         self.pos = pos
         self.left = left
         self.right = right
-        self.type = atype    
+        self.type = atype
 
     def eval(self, ctx):
         v1 = self.left.eval(ctx)
@@ -187,7 +185,8 @@
         return W_Number(op1|op2)
     
 
-class BitwiseXor(BinaryBitwiseOp):    
+
+class BitwiseXor(BinaryBitwiseOp):
     def decision(self, ctx, op1, op2):
         return W_Number(op1^op2)
     
@@ -207,7 +206,8 @@
         raise ExecutionReturned('continue', None, None)
     
 
-class Call(BinaryOp):    
+
+class Call(BinaryOp):
     def eval(self, ctx):
         r1 = self.left.eval(ctx)
         r2 = self.right.eval(ctx)
@@ -323,7 +323,6 @@
     Implements the Abstract Relational Comparison x < y
     Still not fully to the spec
     """
-    # TODO complete the funcion with strings comparison
     s1 = x.ToPrimitive(ctx, 'Number')
     s2 = y.ToPrimitive(ctx, 'Number')
     if not (isinstance(s1, W_String) and isinstance(s2, W_String)):
@@ -336,6 +335,12 @@
         else:
             return 0
     else:
+        s4 = s1.ToString()
+        s5 = s2.ToString()
+        if s4 < s5:
+            return 1
+        if s4 == s5:
+            return 0
         return -1
 
 class Or(BinaryOp):
@@ -566,7 +571,7 @@
         thing = self.expr.eval(ctx)
         val = thing.GetValue()
         x = val.ToNumber()
-        resl = Plus.mathop(ctx, W_Number(x), W_Number(1))
+        resl = plus(ctx, W_Number(x), W_Number(1))
         thing.PutValue(resl, ctx)
         if self.postfix:
             return val
@@ -584,7 +589,7 @@
         thing = self.expr.eval(ctx)
         val = thing.GetValue()
         x = val.ToNumber()
-        resl = Plus.mathop(ctx, W_Number(x), W_Number(-1))
+        resl = sub(ctx, W_Number(x), W_Number(1))
         thing.PutValue(resl, ctx)
         if self.postfix:
             return val
@@ -617,6 +622,9 @@
         nright = self.right.eval(ctx).GetValue().ToPrimitive(ctx, 'Number')
         result = self.mathop(ctx, nleft, nright)
         return result
+    
+    def mathop(self, ctx, n1, n2):
+        raise NotImplementedError
 
 def plus(ctx, nleft, nright):
     if isinstance(nleft, W_String) or isinstance(nright, W_String):
@@ -650,28 +658,33 @@
 
 
 class Plus(BinaryNumberOp):
-    mathop = staticmethod(plus)
-    
+    def mathop(self, ctx, n1, n2):
+        return plus(ctx, n1, n2)
+
 
 class Mult(BinaryNumberOp):
-    mathop = staticmethod(mult)
-    
+    def mathop(self, ctx, n1, n2):
+        return mult(ctx, n1, n2)
+
 
 class Mod(BinaryNumberOp):
-    mathop = staticmethod(mod)
-    
+    def mathop(self, ctx, n1, n2):
+        return mod(ctx, n1, n2)
+
 
 class Division(BinaryNumberOp):
-    mathop = staticmethod(division)
-    
+    def mathop(self, ctx, n1, n2):
+        return division(ctx, n1, n2)
+
 
 class Sub(BinaryNumberOp):
-    mathop = staticmethod(sub)
-    
+    def mathop(self, ctx, n1, n2):
+        return sub(ctx, n1, n2)
+
 
-class Null(Expression):    
+class Null(Expression):
     def eval(self, ctx):
-        return w_Null            
+        return w_Null
 
 
 ##############################################################################
@@ -697,7 +710,8 @@
         return x.Construct(ctx=ctx, args=args)
     
 
-class Number(Expression):    
+
+class Number(Expression):
     def __init__(self, pos, num):
         self.pos = pos
         assert isinstance(num, float)
@@ -737,7 +751,7 @@
                 temp.append(unescapeseq)
                 last = unescapeseq
                 continue
-            if c != SLASH:        
+            if c != SLASH:
                 temp.append(c)
             last = c
         return ''.join(temp)
@@ -999,7 +1013,7 @@
 
 class For(Statement):
     def __init__(self, pos, setup, condition, update, body):
-        self.pos = pos        
+        self.pos = pos
         self.setup = setup
         self.condition = condition
         self.update = update
@@ -1036,7 +1050,7 @@
     def eval(self, ctx):
         return W_Number(-self.expr.eval(ctx).GetValue().ToNumber())
 
-class UPlus(UnaryOp):    
+class UPlus(UnaryOp):
     def eval(self, ctx):
         return W_Number(+self.expr.eval(ctx).GetValue().ToNumber())
     

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 Jun  1 21:06:59 2007
@@ -233,6 +233,11 @@
     yield assertv, "0!=1;", True
     yield assertv, "1!=1;", False
 
+def test_string_compare():
+    yield assertv, "'aaa' > 'a';", True
+    yield assertv, "'aaa' < 'a';", False
+    yield assertv, "'a' > 'a';", False
+
 def test_binary_op():
     yield assertp, "print(0||0); print(1||0);", ["0", "1"]
     yield assertp, "print(0&&1); print(1&&1);", ["0", "1"]



More information about the Pypy-commit mailing list