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

stephan at codespeak.net stephan at codespeak.net
Sat Nov 4 13:19:28 CET 2006


Author: stephan
Date: Sat Nov  4 13:19:25 2006
New Revision: 34163

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) while, less than and errorfix in abstract ls operator

Modified: pypy/dist/pypy/lang/js/astgen.py
==============================================================================
--- pypy/dist/pypy/lang/js/astgen.py	(original)
+++ pypy/dist/pypy/lang/js/astgen.py	Sat Nov  4 13:19:25 2006
@@ -79,6 +79,8 @@
     def __init__(self, nodes):
         self.nodes = nodes
 
+class Lt(BinaryOperator): pass
+
 class Number(Node):
     def __init__(self, num):
         self.num = num
@@ -135,6 +137,11 @@
         self.nodes = nodes
         [scope_manager.add_variable(id.name, w_Undefined) for id in nodes]
 
+class While(Node):
+    def __init__(self, condition, body):
+        self.condition = condition
+        self.body = body
+
 def getlist(d):
     if 'length' not in d:
         return []
@@ -188,6 +195,8 @@
         return Index(from_dict(d['0']), from_dict(d['1']))
     elif tp == 'LIST':
         return List(getlist(d))
+    elif tp == 'LT':
+        return Lt(from_dict(d['0']), from_dict(d['1']))
     elif tp == 'NUMBER':
         return Number(float(d['value']))
     elif tp == 'OBJECT_INIT':
@@ -220,5 +229,9 @@
         return Try(from_dict(d['tryBlock']), catchblock, finallyblock, catchparam)
     elif tp == 'VAR':
         return Vars(getlist(d))
+    elif tp == 'WHILE':
+        body = from_dict(d['body'])
+        condition = from_dict(d['condition'])
+        return While(condition, body)
     else:
         raise NotImplementedError("Dont know how to handler %s" % tp)

Modified: pypy/dist/pypy/lang/js/interpreter.py
==============================================================================
--- pypy/dist/pypy/lang/js/interpreter.py	(original)
+++ pypy/dist/pypy/lang/js/interpreter.py	Sat Nov  4 13:19:25 2006
@@ -95,7 +95,9 @@
 
 def AbstractRelationalLt(value1, value2):
     # TODO: really implement the algorithm
-    return value1<value2
+    v1 = value1.ToPrimitive().ToNumber()
+    v2 = value2.ToPrimitive().ToNumber()
+    return v1<v2
 
 class __extend__(Gt):
     def call(self, context = None):
@@ -106,6 +108,12 @@
         else:
             return W_Boolean(False)
 
+class __extend__(Lt):
+    def call(self, context = None):
+        left = self.left.call(context).GetValue()
+        right = self.right.call(context).GetValue()
+        return W_Boolean(AbstractRelationalLt(left, right))
+
 class __extend__(Index):
     def call(self, context=None):
         w_obj = self.left.call(context).GetValue()
@@ -227,3 +235,8 @@
         for var in self.nodes:
             var.call(context)
 
+class __extend__(While):
+    def call(self, context=None):
+        while self.condition.call(context).ToBoolean():
+            self.body.call(context)
+

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	Sat Nov  4 13:19:25 2006
@@ -200,3 +200,13 @@
     def test_gt(self):
         self.assert_prints(parse_d("print(1>0)"),["true"])
         self.assert_prints(parse_d("print(0>1)"),["false"])
+
+    def test_while(self):
+        self.assert_prints(parse_d("""
+        i = 0;
+        while (i<3) {
+            print(i);
+            i = i+1;
+        }
+        print(i);
+        """), ["0","1","2","3"])



More information about the Pypy-commit mailing list