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

stephan at codespeak.net stephan at codespeak.net
Sat Nov 4 12:32:31 CET 2006


Author: stephan
Date: Sat Nov  4 12:32:29 2006
New Revision: 34160

Modified:
   pypy/dist/pypy/lang/js/astgen.py
   pypy/dist/pypy/lang/js/interpreter.py
   pypy/dist/pypy/lang/js/jsobj.py
   pypy/dist/pypy/lang/js/test/test_interp.py
Log:
(stephan, santagada) implemented booleans, if-then-else and gt 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 12:32:29 2006
@@ -9,6 +9,13 @@
 #    def __init__(self, lineno = 1):
 #        self.lineno = lineno
 
+class BinaryOperator(Node):
+    """super class for binary operators"""
+    def __init__(self, left, right):
+        self.left = left
+        self.right = right
+
+
 class Array(Node):
     def __init__(self, items=()):
         self.items = items
@@ -50,11 +57,19 @@
     def __init__(self, expr):
         self.expr = expr
 
+class Gt(BinaryOperator): pass
+
 class Identifier(Node):
     def __init__(self, name, initialiser):
         self.name = name
         self.initialiser = initialiser
 
+class If(Node):
+    def __init__(self, condition, thenPart=None, elsePart=None):
+        self.condition = condition
+        self.thenPart = thenPart
+        self.elsePart = elsePart
+
 class Index(Node):
     def __init__(self, left, expr):
         self.left = left
@@ -72,10 +87,7 @@
     def __init__(self, properties):
         self.properties = properties
 
-class Plus(Node):
-    def __init__(self, left, right):
-        self.left = left
-        self.right = right
+class Plus(BinaryOperator):pass
 
 class PropertyInit(Node):
     def __init__(self, name, value):
@@ -106,7 +118,7 @@
         self.exception = exception
 
 class Try(Node):
-    """The Try class."""
+    # TODO: rewrite to use 'Undefined'
     def __init__(self, tryblock, catchblock, finallyblock, catchparam):
         self.tryblock = tryblock
         self.catchblock = catchblock
@@ -114,6 +126,10 @@
         self.catchparam = catchparam
 
 
+class Undefined(Node):
+    def __init__(self):
+        pass
+
 class Vars(Node):
     def __init__(self, nodes):
         self.nodes = nodes
@@ -133,53 +149,64 @@
     if d is None:
         return d
     tp = d['type']
-    if tp == 'SCRIPT':
-        # XXX: Cannot parse it right now
-        return Script(getlist(d), [], [])
-    elif tp == 'ARRAY_INIT':
+    if tp == 'ARRAY_INIT':
         return Array(getlist(d))
-    elif tp == 'SEMICOLON':
-        return Semicolon(from_dict(d['expression']))
-    elif tp == 'NUMBER':
-        return Number(float(d['value']))
-    elif tp == 'IDENTIFIER':
-        return Identifier(d['value'], from_dict(d.get('initializer', None)))
-    elif tp == 'LIST':
-        return List(getlist(d))
-    elif tp == 'CALL':
-        return Call(from_dict(d['0']), from_dict(d['1']))
-    elif tp == 'PLUS':
-        return Plus(from_dict(d['0']), from_dict(d['1']))
     elif tp == 'ASSIGN':
         return Assign(from_dict(d['0']), from_dict(d['1']))
-    elif tp == 'STRING':
-        return String(d['value'])
-    elif tp == 'PROPERTY_INIT':
-        return PropertyInit(from_dict(d['0']), from_dict(d['1']))
-    elif tp == 'OBJECT_INIT':
-        return ObjectInit(getlist(d))
+    elif tp == 'BLOCK':
+        return Block(getlist(d))
+    elif tp == 'CALL':
+        return Call(from_dict(d['0']), from_dict(d['1']))
+    elif tp == 'COMMA':
+        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 == 'INDEX':
-        return Index(from_dict(d['0']), from_dict(d['1']))
     elif tp == 'FUNCTION':
         scope = scope_manager.enter_scope()
         body = from_dict(d['body'])
         f = Function(d['params'].split(','), body, scope)
         scope_manager.leave_scope()
         return f
+    elif tp == 'GROUP':
+        return Group(from_dict(d['0']))
+    elif tp == 'GT':
+        return Gt(from_dict(d['0']), from_dict(d['1']))
+    elif tp == 'IDENTIFIER':
+        return Identifier(d['value'], from_dict(d.get('initializer', None)))
+    elif tp == 'IF':
+        condition = from_dict(d['condition'])
+        if d['thenPart'] == 'null':
+            thenPart = Undefined()
+        else:
+            thenPart = from_dict(d['thenPart'])
+        if d['elsePart'] == 'null':
+            elsePart = Undefined()
+        else:
+            elsePart = from_dict(d['elsePart'])
+        return If(condition,thenPart,elsePart)
+    elif tp == 'INDEX':
+        return Index(from_dict(d['0']), from_dict(d['1']))
+    elif tp == 'LIST':
+        return List(getlist(d))
+    elif tp == 'NUMBER':
+        return Number(float(d['value']))
+    elif tp == 'OBJECT_INIT':
+        return ObjectInit(getlist(d))
+    elif tp == 'PLUS':
+        return Plus(from_dict(d['0']), from_dict(d['1']))
+    elif tp == 'PROPERTY_INIT':
+        return PropertyInit(from_dict(d['0']), from_dict(d['1']))
     elif tp == 'RETURN':
         return Return(from_dict(d['value']))
+    elif tp == 'SCRIPT':
+        # XXX: Cannot parse it right now
+        return Script(getlist(d), [], [])
+    elif tp == 'SEMICOLON':
+        return Semicolon(from_dict(d['expression']))
+    elif tp == 'STRING':
+        return String(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))
-    elif tp == 'BLOCK':
-        return Block(getlist(d))
     elif tp == 'TRY':
         finallyblock = None
         catchblock = None
@@ -191,5 +218,7 @@
             catchblock = from_dict(d['catchClauses']['block'])
             catchparam = d['catchClauses']['varName']
         return Try(from_dict(d['tryBlock']), catchblock, finallyblock, catchparam)
+    elif tp == 'VAR':
+        return Vars(getlist(d))
     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 12:32:29 2006
@@ -1,7 +1,8 @@
 
 from pypy.lang.js.astgen import *
 from pypy.lang.js.context import ExecutionContext
-from pypy.lang.js.jsobj import W_Number, W_String, W_Object, w_Undefined, W_Arguments
+from pypy.lang.js.jsobj import W_Number, W_String, W_Object 
+from pypy.lang.js.jsobj import w_Undefined, W_Arguments, W_Boolean
 from pypy.lang.js.scope import scope_manager
 
 def writer(x):
@@ -80,11 +81,31 @@
     def get_literal(self):
         return self.name
 
+class __extend__(If):
+    def call(self, context=None):
+        if self.condition.call(context).ToBoolean():
+            return self.thenPart.call(context)
+        else:
+            return self.elsePart.call(context)
+
 class __extend__(Group):
     """The __extend__ class."""
     def call(self, context = None):
         return self.expr.call(context)
 
+def AbstractRelationalLt(value1, value2):
+    # TODO: really implement the algorithm
+    return value1<value2
+
+class __extend__(Gt):
+    def call(self, context = None):
+        left = self.left.call(context).GetValue()
+        right = self.right.call(context).GetValue()
+        if left != right:
+            return W_Boolean(not AbstractRelationalLt(left, right))
+        else:
+            return W_Boolean(False)
+
 class __extend__(Index):
     def call(self, context=None):
         w_obj = self.left.call(context).GetValue()
@@ -197,6 +218,10 @@
         
         return tryresult
 
+class __extend__(Undefined):
+    def call(self, context=None):
+        return None
+
 class __extend__(Vars):
     def call(self, context=None):
         for var in self.nodes:

Modified: pypy/dist/pypy/lang/js/jsobj.py
==============================================================================
--- pypy/dist/pypy/lang/js/jsobj.py	(original)
+++ pypy/dist/pypy/lang/js/jsobj.py	Sat Nov  4 12:32:29 2006
@@ -9,6 +9,9 @@
     def GetValue(self):
         return self
 
+    def ToBoolean(self):
+        return bool(self)
+
     def ToPrimitive(self, hint=""):
         return self
 
@@ -111,13 +114,19 @@
         # XXX make NaN
         return NaN
 
+    def ToBoolean(self):
+        return False
+
 class W_Null(W_Root):
     def __str__(self):
         return "null"
 
+    def ToBoolean(self):
+        return False
+
 class W_Boolean(W_Root):
     def __init__(self, boolval):
-        self.boolval = boolval
+        self.boolval = bool(boolval)
 
     def __str__(self):
         if self.boolval:
@@ -128,6 +137,10 @@
         if self.boolval:
             return 1
         return 0
+
+    def ToBoolean(self):
+        return self.boolval
+
     
 class W_String(W_Root):
     def __init__(self, strval):
@@ -141,6 +154,9 @@
         # INSANE - should be like 'str' or so
         return self.strval
 
+    def ToBoolean(self):
+        return bool(self.strval)
+
 class W_Number(W_Root):
     def __init__(self, floatval):
         self.floatval = floatval
@@ -154,6 +170,9 @@
             return str(int(self.floatval))
         return str(self.floatval)
     
+    def ToBoolean(self):
+        return bool(self.floatval)
+
     def Get(self, name):
         return w_Undefined
 
@@ -179,5 +198,8 @@
     def ToString(self):
         raise SeePage(42)
 
+    def ToBoolean(self):
+        return bool(self.list_w)
+
 w_Undefined = W_Undefined()
 w_Null = W_Null()

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 12:32:29 2006
@@ -180,4 +180,23 @@
             print(5)
         }
         """), ["3", "5"])
-        
\ No newline at end of file
+        
+    def test_if_then(self):
+        self.assert_prints(parse_d("""
+        if (1) {
+            print(1);
+        }
+        """), ["1"])
+
+    def test_if_then_else(self):
+        self.assert_prints(parse_d("""
+        if (0) {
+            print(1);
+        } else {
+            print(2);
+        }
+        """), ["2"])
+
+    def test_gt(self):
+        self.assert_prints(parse_d("print(1>0)"),["true"])
+        self.assert_prints(parse_d("print(0>1)"),["false"])



More information about the Pypy-commit mailing list