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

santagada at codespeak.net santagada at codespeak.net
Wed Jan 24 14:51:48 CET 2007


Author: santagada
Date: Wed Jan 24 14:51:46 2007
New Revision: 37261

Modified:
   pypy/dist/pypy/lang/js/interpreter.py
   pypy/dist/pypy/lang/js/jsobj.py
   pypy/dist/pypy/lang/js/test/test_interp.py
Log:
all tests passing... and the code is much cleanner


Modified: pypy/dist/pypy/lang/js/interpreter.py
==============================================================================
--- pypy/dist/pypy/lang/js/interpreter.py	(original)
+++ pypy/dist/pypy/lang/js/interpreter.py	Wed Jan 24 14:51:46 2007
@@ -45,7 +45,7 @@
         raise NotImplementedError
     
     def __str__(self):
-        return "<astop %s %s >"%(self.opcode, self.value)
+        return "<ASTop %s %s >"%(self.opcode, self.value)
 
 class Statement(Node):
     pass
@@ -63,7 +63,7 @@
         
 class UnaryOp(Expression):
     def from_tree(self, t):
-        self.expr = get_Obj(t, '0')
+        self.expr = get_obj(t, '0')
 
 class BinaryOp(Expression):
     def from_tree(self, t):
@@ -218,7 +218,7 @@
 class Block(Statement):
     opcode = 'BLOCK'
 
-    def from_tree(t):
+    def from_tree(self, t):
         self.nodes = get_objects(t)        
 
     def execute(self, ctx):
@@ -235,9 +235,10 @@
 
 class Unconditional(Statement):
     def from_tree(self, t):
-        self.targtype, 
-        self.targlineno, 
-        self.targstart = get_string(t, 'target').split(',')
+        pieces = get_string(t, 'target').split(',')
+        self.targtype = pieces[0] 
+        self.targlineno = pieces[1]
+        self.targstart = pieces[2]
         
 class Break(Unconditional):
     opcode = 'BREAK'
@@ -305,10 +306,7 @@
 
     def from_tree(self, t):
         self.name = get_string(t, 'name')
-                
-        self.params = params
         self.body = get_obj(t, 'body')
-        
         params = get_string(t, 'params')
         if params == '':
             self.params = []
@@ -325,18 +323,14 @@
     def from_tree(self, t):
         self.name = get_string(t,'value')
         self.initializer = get_obj(t, 'initializer')
-
-    def __init__(self, name, initialiser=None):
-        self.name = name
-        self.initialiser = initialiser
         
     def __str__(self):
-        return "<id %s init: %s>"%(str(self.name), str(self.initialiser))
+        return "<id %s init: %s>"%(str(self.name), str(self.initializer))
     
     def eval(self, ctx):
-        if self.initialiser is not None:
+        if self.initializer is not astundef:
             ref = ctx.resolve_identifier(self.name)
-            ref.PutValue(self.initialiser.eval(ctx).GetValue(), ctx)
+            ref.PutValue(self.initializer.eval(ctx).GetValue(), ctx)
         return ctx.resolve_identifier(self.name)
     
     def get_literal(self):
@@ -364,7 +358,7 @@
     opcode = 'GROUP'
     
     def eval(self, ctx):
-        return self.exp.eval(ctx)
+        return self.expr.eval(ctx)
 
 def ARC(ctx, x, y):
     """
@@ -411,6 +405,8 @@
     
     def decision(self, ctx, op1, op2):
         s5 = ARC(ctx, op1, op2)
+        if DEBUG:
+            print ">= ARC result:", s5
         if s5 in (-1, 1):
             return W_Boolean(False)
         else:
@@ -421,6 +417,8 @@
     
     def decision(self, ctx, op1, op2):
         s5 = ARC(ctx, op2, op1)
+        if DEBUG:
+            print "> ARC result:", s5
         if s5 == -1:
             return W_Boolean(False)
         else:
@@ -491,7 +489,7 @@
         thing = self.expr.eval(ctx)
         val = thing.GetValue()
         x = val.ToNumber()
-        resl = Plus(None, None).decision(ctx, W_Number(x), W_Number(1))
+        resl = Plus().mathop(ctx, W_Number(x), W_Number(1))
         thing.PutValue(resl, ctx)
         return val
 
@@ -540,6 +538,9 @@
 class Null(Expression):
     opcode = 'NULL'
     
+    def from_tree(self, t):
+        pass
+    
     def eval(self, ctx):
         return w_Null            
 
@@ -558,7 +559,9 @@
     def eval(self, ctx):
         w_obj = W_Object()
         for prop in self.list:
-            name = prop.left.get_literal()
+            if DEBUG:
+                print prop.left
+            name = prop.left.value
             w_expr = prop.right.eval(ctx).GetValue()
             w_obj.Put(name, w_expr)
         return w_obj
@@ -586,7 +589,7 @@
             return W_Number(fleft + fright)
 
 class Mult(BinaryNumberOp):
-    opcode = 'MULT'
+    opcode = 'MUL'
     
     def mathop(self, ctx, nleft, nright):
         fleft = nleft.ToNumber()
@@ -664,7 +667,7 @@
         return self.expr.execute(ctx)
 
 class String(Expression):
-    code = 'STRING'
+    opcode = 'STRING'
     
     def from_tree(self, t):
         self.strval = get_string(t, 'value')
@@ -738,7 +741,7 @@
     opcode = 'TYPEOF'
     
     def eval(self, ctx):
-        val = self.exp.eval(ctx)
+        val = self.expr.eval(ctx)
         if isinstance(val, W_Reference) and val.GetBase() is None:
             return W_String("undefined")
         return W_String(val.GetValue().type())
@@ -803,7 +806,7 @@
                     continue
     
 class Boolean(Expression):
-    def from_tree(selt, t):
+    def from_tree(self, t):
         if self.opcode == 'TRUE':
             self.bool = True
         else:
@@ -812,10 +815,10 @@
     def eval(self, ctx):
         return W_Boolean(self.bool)
 
-class True(Boolean):
+class BTrue(Boolean):
     opcode = 'TRUE'
 
-class False(Boolean):
+class BFalse(Boolean):
     opcode = 'FALSE'
 
 class Not(UnaryOp):
@@ -830,16 +833,17 @@
     def eval(self, ctx):
         return W_Number(-self.expr.eval(ctx).GetValue().ToNumber())
 
+astundef = Undefined()
 def get_obj(t, objname):
     item = get_tree_item(t, objname)
     if isinstance(item, Nonterminal):
         return from_tree(item)
     else:
-        return Undefined()
+        return astundef
 
 def get_string(t, string):
         simb = get_tree_item(t, string)
-        if simb is not None:
+        if isinstance(simb, Symbol):
             return simb.additional_info
         else:
             return ''

Modified: pypy/dist/pypy/lang/js/jsobj.py
==============================================================================
--- pypy/dist/pypy/lang/js/jsobj.py	(original)
+++ pypy/dist/pypy/lang/js/jsobj.py	Wed Jan 24 14:51:46 2007
@@ -1,6 +1,6 @@
 # encoding: utf-8
 
-DEBUG = False
+DEBUG = True
 
 class SeePage(NotImplementedError):
     pass
@@ -253,6 +253,8 @@
         self.callfuncbi = callfuncbi
 
     def Call(self, ctx, args=[], this = None):
+        if DEBUG:
+            print args
         return self.callfuncbi(ctx, args, this)
         
     def type(self):
@@ -326,10 +328,12 @@
 
 class W_Boolean(W_Primitive):
     def __init__(self, boolval):
+        if DEBUG:
+            print boolval
         self.boolval = bool(boolval)
 
     def ToString(self):
-        if self.boolval:
+        if self.boolval == True:
             return "true"
         return "false"
     
@@ -343,6 +347,9 @@
 
     def type(self):
         return 'boolean'
+        
+    def __repr__(self):
+        return "<W_Bool "+str(self.boolval)+" >"
 
 class W_String(W_Primitive):
     def __init__(self, strval):

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	Wed Jan 24 14:51:46 2007
@@ -9,7 +9,7 @@
 from pypy.lang.js.interpreter import *
 from pypy.lang.js.jsobj import W_Number, W_Object, ExecutionContext
 
-py.test.skip('WIP')
+
 def js_is_on_path():
     if py.path.local.sysfind("js") is None:
         py.test.skip("js binary not found")
@@ -18,7 +18,14 @@
 
 class TestInterp(object):
     def test_simple(self):
-        assert Plus(Number(3), Number(4)).eval(ExecutionContext()).floatval == 7
+        p = Plus()
+        n1 = Number()
+        n2 = Number()
+        n1.num = 2
+        n2.num = 4
+        p.left = n1
+        p.right = n2
+        assert p.eval(ExecutionContext()).GetValue().ToNumber() == 6
         l = []
         interpreter.writer = l.append
         # Script([Semicolon(Call(Identifier('print', None), 



More information about the Pypy-commit mailing list