[pypy-svn] r52417 - in pypy/branch/jit-refactoring/pypy/lang/js: . test
fijal at codespeak.net
fijal at codespeak.net
Wed Mar 12 16:03:34 CET 2008
Author: fijal
Date: Wed Mar 12 16:03:33 2008
New Revision: 52417
Modified:
pypy/branch/jit-refactoring/pypy/lang/js/jscode.py
pypy/branch/jit-refactoring/pypy/lang/js/operations.py
pypy/branch/jit-refactoring/pypy/lang/js/test/test_parser.py
Log:
Basic expressions.
Modified: pypy/branch/jit-refactoring/pypy/lang/js/jscode.py
==============================================================================
--- pypy/branch/jit-refactoring/pypy/lang/js/jscode.py (original)
+++ pypy/branch/jit-refactoring/pypy/lang/js/jscode.py Wed Mar 12 16:03:33 2008
@@ -52,6 +52,12 @@
def decision(self, ctx, op1, op2):
raise NotImplementedError
+class BaseBinaryOperation(Opcode):
+ pass
+
+class BaseUnaryOperation(Opcode):
+ pass
+
class Undefined(Opcode):
def eval(self, ctx):
return w_Undefined
@@ -137,6 +143,39 @@
def __repr__(self):
return 'LOAD_OBJECT %r' % (self.listofnames,)
+class SUB(BaseBinaryOperation):
+ pass
+
+class ADD(BaseBinaryOperation):
+ pass
+
+class MUL(BaseBinaryOperation):
+ pass
+
+class DIV(BaseBinaryOperation):
+ pass
+
+class MOD(BaseBinaryOperation):
+ pass
+
+class UPLUS(BaseUnaryOperation):
+ pass
+
+class UMINUS(BaseUnaryOperation):
+ pass
+
+class PREINCR(BaseUnaryOperation):
+ pass
+
+class POSTINCR(BaseUnaryOperation):
+ pass
+
+class PREDECR(BaseUnaryOperation):
+ pass
+
+class POSTDECR(BaseUnaryOperation):
+ pass
+
OpcodeMap = {}
for name, value in locals().items():
Modified: pypy/branch/jit-refactoring/pypy/lang/js/operations.py
==============================================================================
--- pypy/branch/jit-refactoring/pypy/lang/js/operations.py (original)
+++ pypy/branch/jit-refactoring/pypy/lang/js/operations.py Wed Mar 12 16:03:33 2008
@@ -68,6 +68,10 @@
self.expr = expr
self.postfix = postfix
+ def emit(self, bytecode):
+ self.expr.emit(bytecode)
+ bytecode.emit(self.operation_name)
+
class BinaryOp(Expression):
def __init__(self, pos, left, right):
self.pos = pos
@@ -609,12 +613,21 @@
r3 = r1.GetBase()
r4 = r1.GetPropertyName()
return W_Boolean(r3.Delete(r4))
-
-class Increment(UnaryOp):
+class BaseIncrementDecrement(UnaryOp):
+ def emit(self, bytecode):
+ self.expr.emit(bytecode)
+ if self.postfix:
+ bytecode.emit('POST' + self.operation_name)
+ else:
+ bytecode.emit('PRE' + self.operation_name)
+
+class Increment(BaseIncrementDecrement):
"""
++value (prefix) and value++ (postfix)
"""
+ operation_name = 'INCR'
+
def eval(self, ctx):
# XXX write down fast version
thing = self.expr.eval(ctx)
@@ -628,10 +641,12 @@
return resl
-class Decrement(UnaryOp):
+class Decrement(BaseIncrementDecrement):
"""
same as increment --value and value --
"""
+ operation_name = 'DECR'
+
def eval(self, ctx):
# XXX write down hot path
thing = self.expr.eval(ctx)
@@ -734,26 +749,30 @@
return W_FloatNumber(fleft - fright)
class Plus(BinaryNumberOp):
+ operation_name = 'ADD'
def mathop(self, ctx, n1, n2):
return plus(ctx, n1, n2)
class Mult(BinaryNumberOp):
+ operation_name = 'MUL'
def mathop(self, ctx, n1, n2):
return mult(ctx, n1, n2)
class Mod(BinaryNumberOp):
+ operation_name = 'MOD'
def mathop(self, ctx, n1, n2):
return mod(ctx, n1, n2)
-
class Division(BinaryNumberOp):
def mathop(self, ctx, n1, n2):
return division(ctx, n1, n2)
class Sub(BinaryNumberOp):
+ operation_name = 'SUB'
+
def mathop(self, ctx, n1, n2):
return sub(ctx, n1, n2)
@@ -1138,6 +1157,8 @@
class UMinus(UnaryOp):
+ operation_name = 'UMINUS'
+
def eval(self, ctx):
res = self.expr.eval(ctx)
if isinstance(res, W_IntNumber):
@@ -1147,6 +1168,8 @@
return W_FloatNumber(-self.expr.eval(ctx).GetValue().ToNumber())
class UPlus(UnaryOp):
+ operation_name = 'UPLUS'
+
def eval(self, ctx):
res = self.expr.eval(ctx)
if isinstance(res, W_BaseNumber):
Modified: pypy/branch/jit-refactoring/pypy/lang/js/test/test_parser.py
==============================================================================
--- pypy/branch/jit-refactoring/pypy/lang/js/test/test_parser.py (original)
+++ pypy/branch/jit-refactoring/pypy/lang/js/test/test_parser.py Wed Mar 12 16:03:33 2008
@@ -341,17 +341,33 @@
py.test.raises(ParseError, self.check, '1=2', [])
def test_expression(self):
- py.test.skip("Not yet")
- w_num = self.eval_expr('1 - 1 - 1')
- assert w_num.ToNumber() == -1
- w_num = self.eval_expr('-(6 * (6 * 6)) + 6 - 6')
- assert w_num.ToNumber() == -216
- w_num = self.eval_expr('++5')
- assert w_num.ToNumber() == 6
- w_num = self.eval_expr('--5')
- assert w_num.ToNumber() == 4
- w_str = self.eval_expr('"hello "+\'world\'')
- assert w_str.ToString(self.ctx) == 'hello world'
+ self.check('1 - 1 - 1', [
+ 'LOAD_INTCONSTANT 1',
+ 'LOAD_INTCONSTANT 1',
+ 'SUB',
+ 'LOAD_INTCONSTANT 1',
+ 'SUB'])
+ self.check('-(6 * (6 * 6)) + 6 - 6', [
+ 'LOAD_INTCONSTANT 6',
+ 'LOAD_INTCONSTANT 6',
+ 'LOAD_INTCONSTANT 6',
+ 'MUL',
+ 'MUL',
+ 'UMINUS',
+ 'LOAD_INTCONSTANT 6',
+ 'ADD',
+ 'LOAD_INTCONSTANT 6',
+ 'SUB'])
+ self.check('++5', [
+ 'LOAD_INTCONSTANT 5',
+ 'PREINCR'])
+ self.check('5--', [
+ 'LOAD_INTCONSTANT 5',
+ 'POSTDECR'])
+ self.check('"hello " + \'world\'',
+ ['LOAD_STRINGCONSTANT "hello "',
+ 'LOAD_STRINGCONSTANT "world"',
+ 'ADD'])
from pypy.lang.js.jsparser import parse
More information about the Pypy-commit
mailing list