[pypy-svn] r43458 - in pypy/dist/pypy/lang/js: . test
santagada at codespeak.net
santagada at codespeak.net
Thu May 17 17:45:04 CEST 2007
Author: santagada
Date: Thu May 17 17:45:04 2007
New Revision: 43458
Modified:
pypy/dist/pypy/lang/js/astbuilder.py
pypy/dist/pypy/lang/js/operations.py
pypy/dist/pypy/lang/js/test/test_parser.py
Log:
removed from_tree and made a position object (maybe I can just grab the one on the symbol)
Modified: pypy/dist/pypy/lang/js/astbuilder.py
==============================================================================
--- pypy/dist/pypy/lang/js/astbuilder.py (original)
+++ pypy/dist/pypy/lang/js/astbuilder.py Thu May 17 17:45:04 2007
@@ -20,40 +20,37 @@
'{': operations.ObjectInit,
}
- def get_instance(self, node, cls):
+ def get_pos(self, node):
value = ''
source_pos = None
if isinstance(node, Symbol):
- source_pos = node.token.source_pos
value = node.additional_info
+ source_pos = node.token.source_pos
else:
curr = node.children[0]
while not isinstance(curr, Symbol):
if len(curr.children):
curr = curr.children[0]
else:
- source_pos = None
break
else:
+ value = curr.additional_info
source_pos = curr.token.source_pos
# XXX some of the source positions are not perfect
- return cls(None,
- value,
+ return operations.Position(
source_pos.lineno,
source_pos.columnno,
source_pos.columnno + len(value))
def visit_DECIMALLITERAL(self, node):
- result = self.get_instance(node, operations.Number)
- result.num = float(result.value)
- return result
+ pos = self.get_pos(node)
+ number = operations.Number(pos, float(node.additional_info))
+ return number
def string(self,node):
- print node.additional_info
- result = self.get_instance(node, operations.String)
- result.strval = node.additional_info[1:-1] #XXX should do unquoting
- return result
+ pos = self.get_pos(node)
+ return operations.String(pos, node.additional_info)
visit_DOUBLESTRING = string
visit_SINGLESTRING = string
@@ -62,11 +59,9 @@
left = self.dispatch(node.children[0])
for i in range((len(node.children) - 1) // 2):
op = node.children[i * 2 + 1]
- result = self.get_instance(
- op, self.BINOP_TO_CLS[op.additional_info])
+ pos = self.get_pos(op)
right = self.dispatch(node.children[i * 2 + 2])
- result.left = left
- result.right = right
+ result = self.BINOP_TO_CLS[op.additional_info](pos, left, right)
left = result
return left
visit_additiveexpression = binaryop
@@ -74,48 +69,41 @@
def visit_unaryexpression(self, node):
op = node.children[0]
- result = self.get_instance(
- op, self.UNOP_TO_CLS[op.additional_info])
+ pos = self.get_pos(op)
child = self.dispatch(node.children[1])
- result.expr = child
- result.postfix = False
- return result
+ return self.UNOP_TO_CLS[op.additional_info](pos, child)
def listop(self, node):
op = node.children[0]
- result = self.get_instance(
- op, self.LISTOP_TO_CLS[op.additional_info])
+ pos = self.get_pos(op)
l = [self.dispatch(child) for child in node.children[1:]]
- result.list = l
- return result
+ return self.LISTOP_TO_CLS[op.additional_info](pos, l)
visit_arrayliteral = listop
visit_objectliteral = listop
def visit_propertynameandvalue(self, node):
- result = self.get_instance(
- node, operations.PropertyInit)
- result.left = self.dispatch(node.children[0])
- result.right = self.dispatch(node.children[1])
- return result
+ pos = self.get_pos(node)
+ left = self.dispatch(node.children[0])
+ right = self.dispatch(node.children[1])
+ return operations.PropertyInit(pos,left,right)
def visit_IDENTIFIERNAME(self, node):
- result = self.get_instance(node, operations.Identifier)
- result.name = node.additional_info
- result.initializer = operations.astundef #XXX this is uneded now
- print result
- return result
+ pos = self.get_pos(node)
+ name = node.additional_info
+ initializer = operations.astundef #XXX this is uneded now
+ return operations.Identifier(pos, name, initializer)
def visit_program(self, node):
- result = self.get_instance(node, operations.Program)
- result.body = self.dispatch(node.children[0])
- return result
+ pos = self.get_pos(node)
+ body = self.dispatch(node.children[0])
+ return operations.Program(pos, body)
def visit_sourceelements(self, node):
- result = self.get_instance(node, operations.Script)
- result.var_decl = None #XXX TODO
- result.func_decl = None #XXX TODO
- result.nodes = [self.dispatch(child) for child in node.children]
- return result
+ pos = self.get_pos(node)
+ var_decl = None #XXX TODO
+ func_decl = None #XXX TODO
+ nodes = [self.dispatch(child) for child in node.children]
+ return operations.Script(pos, var_decl, func_decl, nodes)
def visit_expressionstatement(self, node):
return self.dispatch(node.children[0])
Modified: pypy/dist/pypy/lang/js/operations.py
==============================================================================
--- pypy/dist/pypy/lang/js/operations.py (original)
+++ pypy/dist/pypy/lang/js/operations.py Thu May 17 17:45:04 2007
@@ -10,40 +10,18 @@
from pypy.rlib.rarithmetic import r_uint, intmask
from constants import unescapedict, SLASH
+class Position(object):
+ def __init__(self, lineno=-1, start=-1, end=-1):
+ self.lineno = lineno
+ self.start = start
+ self.end = end
+
+
class Node(object):
"""
- Node is the base class for all the other nodes, the opcode parameter
- is used to match the AST operation to the efective execution node.
+ Node is the base class for all the other nodes.
"""
- opcode = None
- def __init__(self, t=None, value='', lineno=0, start=0, end=0):
- """
- Not to be overriden by subclasses, this method thakes the basic node
- information from the AST needed for tracing and debuging. if you want
- to override behavior for the creation of a node do it on the from_tree
- call.
- """
- if t is None:
- self.value = value
- self.lineno = lineno
- self.start = start
- self.end = end
- else:
- self.type = get_string(t, 'type')
- self.value = get_string(t, 'value')
- self.lineno = int(get_string(t, 'lineno'))
-
- try:
- self.start = int(get_string(t, 'start'))
- except ValueError, e:
- self.start = 0
- try:
- self.end = int(get_string(t, 'end'))
- except Exception, e:
- self.end = 0
- self.from_tree(t)
-
- def from_tree(self, t):
+ def __init__(self, pos):
"""
Initializes the content from the AST specific for each node type
"""
@@ -71,7 +49,8 @@
return "<ASTop %s %s >"%(self.opcode, self.value)
class Statement(Node):
- pass
+ def __init__(self, pos):
+ self.pos = pos
class Expression(Statement):
def eval(self, ctx):
@@ -81,18 +60,21 @@
return self.eval(ctx)
class ListOp(Expression):
- def from_tree(self, t):
- self.list = get_objects(t)
+ def __init__(self, pos, nodelist):
+ self.pos = pos
+ self.nodelist = nodelist
class UnaryOp(Expression):
- def from_tree(self, t):
- self.expr = get_obj(t, '0')
- self.postfix = bool(get_string(t, 'postfix'))
+ def __init__(self, pos, expr, postfix=False):
+ self.pos = pos
+ self.expr = expr
+ self.postfix = postfix
class BinaryOp(Expression):
- def from_tree(self, t):
- self.left = get_obj(t,'0')
- self.right = get_obj(t, '1')
+ def __init__(self, pos, left, right):
+ self.pos = pos
+ self.left = left
+ self.right = right
class BinaryComparisonOp(BinaryOp):
def eval(self, ctx):
@@ -112,25 +94,17 @@
def decision(self, ctx, op1, op2):
raise NotImplementedError
-class BinaryLogicOp(BinaryOp):
- pass
-
class PropertyInit(BinaryOp):
- opcode = 'PROPERTY_INIT'
+ pass
-class Array(ListOp):
- opcode = 'ARRAY_INIT'
-
+class Array(ListOp):
def eval(self, ctx):
array = W_Array()
- for i in range(len(self.list)):
- array.Put(str(i), self.list[i].eval(ctx).GetValue())
+ for i in range(len(self.nodelist)):
+ array.Put(str(i), self.nodelist[i].eval(ctx).GetValue())
return array
-
-class Assign(BinaryOp):
- opcode = 'ASSIGN'
-
+class Assign(BinaryOp):
def eval(self, ctx):
v1 = self.left.eval(ctx)
v3 = self.right.eval(ctx).GetValue()
@@ -159,9 +133,7 @@
return val
class Block(Statement):
- opcode = 'BLOCK'
-
- def from_tree(self, t):
+ def __init__(self, pos, t):
self.nodes = get_objects(t)
def execute(self, ctx):
@@ -203,7 +175,7 @@
return W_Number(op1^op2)
class Unconditional(Statement):
- def from_tree(self, t):
+ def __init__(self, pos, t):
pieces = get_string(t, 'target').split(',')
self.targtype = pieces[0]
self.targlineno = pieces[1]
@@ -256,7 +228,7 @@
class Conditional(Expression):
opcode = 'CONDITIONAL'
- def from_tree(self, t):
+ def __init__(self, pos, t):
self.logicalexpr = get_obj(t, '0')
self.trueop = get_obj(t, '1')
self.falseop = get_obj(t, '2')
@@ -278,7 +250,7 @@
class Function(Expression):
opcode = 'FUNCTION'
- def from_tree(self, t):
+ def __init__(self, pos, t):
self.name = get_string(t, 'name')
self.body = get_obj(t, 'body')
params = get_string(t, 'params')
@@ -296,9 +268,9 @@
class Identifier(Expression):
opcode = 'IDENTIFIER'
- def from_tree(self, t):
- self.name = get_string(t,'value')
- self.initializer = get_obj(t, 'initializer')
+ def __init__(self, pos, name, initializer):
+ self.name = name
+ self.initializer = initializer
def __str__(self):
return "<id %s init: %s>"%(str(self.name), str(self.initializer))
@@ -318,7 +290,7 @@
class If(Statement):
opcode = 'IF'
- def from_tree(self, t):
+ def __init__(self, pos, t):
self.condition = get_obj(t, 'condition')
self.thenPart = get_obj(t, 'thenPart')
self.elsePart = get_obj(t, 'elsePart')
@@ -362,8 +334,7 @@
else:
return -1
-class Or(BinaryLogicOp):
- opcode = 'OR'
+class Or(BinaryOp):
def eval(self, ctx):
s2 = self.left.eval(ctx).GetValue()
@@ -372,7 +343,7 @@
s4 = self.right.eval(ctx).GetValue()
return s4
-class And(BinaryLogicOp):
+class And(BinaryOp):
opcode = 'AND'
def eval(self, ctx):
@@ -612,7 +583,7 @@
thing = self.expr.eval(ctx)
val = thing.GetValue()
x = val.ToNumber()
- resl = Plus().mathop(ctx, W_Number(x), W_Number(1))
+ resl = Plus.mathop(ctx, W_Number(x), W_Number(1))
thing.PutValue(resl, ctx)
if self.postfix:
return val
@@ -630,7 +601,7 @@
thing = self.expr.eval(ctx)
val = thing.GetValue()
x = val.ToNumber()
- resl = Plus().mathop(ctx, W_Number(x), W_Number(-1))
+ resl = Plus.mathop(ctx, W_Number(x), W_Number(-1))
thing.PutValue(resl, ctx)
if self.postfix:
return val
@@ -667,9 +638,8 @@
return result
class Plus(BinaryNumberOp):
- opcode = 'PLUS'
-
- def mathop(self, ctx, nleft, nright):
+ @staticmethod
+ def mathop(ctx, nleft, nright):
if isinstance(nleft, W_String) or isinstance(nright, W_String):
sleft = nleft.ToString()
sright = nright.ToString()
@@ -680,16 +650,12 @@
return W_Number(fleft + fright)
class Mult(BinaryNumberOp):
- opcode = 'MUL'
-
def mathop(self, ctx, nleft, nright):
fleft = nleft.ToNumber()
fright = nright.ToNumber()
return W_Number(fleft * fright)
class Mod(BinaryNumberOp):
- opcode = 'MOD'
-
def mathop(self, ctx, nleft, nright):
fleft = nleft.ToInt32()
fright = nright.ToInt32()
@@ -716,7 +682,7 @@
class Null(Expression):
opcode = 'NULL'
- def from_tree(self, t):
+ def __init__(self, pos, t):
pass
def eval(self, ctx):
@@ -749,11 +715,10 @@
args = self.right.eval(ctx).get_args()
return x.Construct(ctx=ctx, args=args)
-class Number(Expression):
- opcode = 'NUMBER'
-
- def from_tree(self, t):
- self.num = float(get_string(t, 'value'))
+class Number(Expression):
+ def __init__(self, pos, num):
+ assert isinstance(num, float)
+ self.num = num
def eval(self, ctx):
return W_Number(self.num)
@@ -761,8 +726,8 @@
class String(Expression):
opcode = 'STRING'
- def from_tree(self, t):
- self.strval = self.string_unquote(get_string(t, 'value'))
+ def __init__(self, pos, strval):
+ self.strval = self.string_unquote(strval)
def eval(self, ctx):
return W_String(self.strval)
@@ -778,17 +743,13 @@
#removing the begining quotes (" or \')
if string.startswith('"'):
singlequote = False
- if stop < 0:
- print stop
- raise JsSyntaxError()
- internalstring = string[1:stop]
else:
singlequote = True
- stop -= 1
- if stop < 0:
- print stop
- raise JsSyntaxError()
- internalstring = string[2:stop]
+
+ if stop < 0:
+ print stop
+ raise JsSyntaxError()
+ internalstring = string[1:stop]
for c in internalstring:
if last == SLASH:
@@ -807,8 +768,8 @@
def eval(self, ctx):
w_obj = W_Object()
- for prop in self.list:
- name = prop.left.value
+ for prop in self.nodelist:
+ name = prop.left.name
w_expr = prop.right.eval(ctx).GetValue()
w_obj.Put(name, w_expr)
return w_obj
@@ -823,28 +784,10 @@
"""
Script nodes are found on each function declaration and in global code
"""
- opcode = 'SCRIPT'
-
- def from_tree(self, t):
- f = get_tree_item(t, 'funDecls')
- if f.symbol == "dict":
- func_decl = [from_tree(f),]
- elif f.symbol == "list":
- func_decl = [from_tree(x) for x in f.children]
- else:
- func_decl = []
-
- v = get_tree_item(t, 'varDecls')
- if v.symbol == "dict":
- var_decl = [from_tree(v),]
- elif v.symbol == "list":
- var_decl = [from_tree(x) for x in v.children]
- else:
- var_decl = []
-
- self.nodes = get_objects(t)
+ def __init__(self, pos, var_decl, func_decl, nodes):
self.var_decl = var_decl
self.func_decl = func_decl
+ self.nodes = nodes
def execute(self, ctx):
for var in self.var_decl:
@@ -869,6 +812,9 @@
raise
class Program(Statement):
+ def __init__(self, pos, body):
+ self.pos = pos
+ self.body = body
def execute(self, ctx):
return self.body.execute(self, ctx)
@@ -876,7 +822,7 @@
class Semicolon(Statement):
opcode = 'SEMICOLON'
- def from_tree(self, t):
+ def __init__(self, pos, t):
self.expr = get_obj(t, 'expression')
def execute(self, ctx):
@@ -887,7 +833,7 @@
class Return(Statement):
opcode = 'RETURN'
- def from_tree(self, t):
+ def __init__(self, pos, t):
self.expr = get_obj(t, 'value')
def execute(self, ctx):
@@ -899,7 +845,7 @@
class Throw(Statement):
opcode = 'THROW'
- def from_tree(self, t):
+ def __init__(self, pos, t):
self.exception = get_obj(t, 'exception')
def execute(self, ctx):
@@ -908,7 +854,7 @@
class Try(Statement):
opcode = 'TRY'
- def from_tree(self, t):
+ def __init__(self, pos, t):
self.tryblock = get_obj(t, 'tryBlock')
self.finallyblock = get_obj(t, 'finallyBlock')
catch = get_tree_item(t, 'catchClauses')
@@ -959,7 +905,7 @@
class Vars(Statement):
opcode = 'VAR'
- def from_tree(self, t):
+ def __init__(self, pos, t):
self.nodes = get_objects(t)
def execute(self, ctx):
@@ -980,7 +926,7 @@
class With(Statement):
opcode = "WITH"
- def from_tree(self, t):
+ def __init__(self, pos, t):
self.object = get_obj(t, 'object')
self.body = get_obj(t, 'body')
@@ -996,7 +942,7 @@
class WhileBase(Statement):
- def from_tree(self, t):
+ def __init__(self, pos, t):
self.condition = get_obj(t, 'condition')
self.body = get_obj(t, 'body')
@@ -1034,9 +980,7 @@
continue
class ForIn(Statement):
- opcode = 'FOR_IN'
-
- def from_tree(self, t):
+ def __init__(self, pos, t):
self.object = get_obj(t, 'object')
self.body = get_obj(t, 'body')
self.iterator = get_obj(t, 'iterator')
@@ -1057,9 +1001,7 @@
continue
class For(Statement):
- opcode = 'FOR'
-
- def from_tree(self, t):
+ def __init__(self, pos, t):
self.setup = get_obj(t, 'setup')
self.condition = get_obj(t, 'condition')
self.update = get_obj(t, 'update')
@@ -1079,7 +1021,7 @@
continue
class Boolean(Expression):
- def from_tree(self, t):
+ def __init__(self, pos, t):
if self.opcode == 'TRUE':
self.bool = True
else:
@@ -1089,71 +1031,22 @@
return W_Boolean(self.bool)
class BTrue(Boolean):
- opcode = 'TRUE'
+ pass
class BFalse(Boolean):
- opcode = 'FALSE'
+ pass
class Not(UnaryOp):
- opcode = 'NOT'
-
def eval(self, ctx):
return W_Boolean(not self.expr.eval(ctx).GetValue().ToBoolean())
class UMinus(UnaryOp):
- opcode = 'UNARY_MINUS'
-
def eval(self, ctx):
return W_Number(-self.expr.eval(ctx).GetValue().ToNumber())
-class UPlus(UnaryOp):
- opcode = 'UNARY_PLUS'
-
+class UPlus(UnaryOp):
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 astundef
-
-def get_string(t, string):
- simb = get_tree_item(t, string)
- if isinstance(simb, Symbol):
- return str(simb.additional_info)
- else:
- return ''
-
-def get_objects(t):
- item = get_tree_item(t, 'length')
- if item is None:
- return []
- lgt = int(item.additional_info)
- output = [get_obj(t, str(i)) for i in range(lgt)]
- return output
-
-def get_tree_item(t, name):
- for x in t.children:
- if isinstance(x.children[0], Symbol):
- if x.children[0].additional_info == name:
- return x.children[1]
- return None
-
-opcodedict = {}
-for i in locals().values():
- if isinstance(i, type(Node)) and issubclass(i, Node):
- if i.opcode is not None:
- opcodedict[i.opcode] = i
-
-def from_tree(t):
- if t is None:
- return None
- opcode = get_string(t, 'type')
- if opcode in opcodedict:
- return opcodedict[opcode](t)
- else:
- raise NotImplementedError("Dont know how to handle %s" % opcode)
+astundef = Undefined(Position())
Modified: pypy/dist/pypy/lang/js/test/test_parser.py
==============================================================================
--- pypy/dist/pypy/lang/js/test/test_parser.py (original)
+++ pypy/dist/pypy/lang/js/test/test_parser.py Thu May 17 17:45:04 2007
@@ -290,19 +290,18 @@
ast = self.to_ast(s)
return ast.eval(global_context())
- def test_get_instance(self):
+ def test_get_pos(self):
from pypy.lang.js import operations
from pypy.rlib.parsing.tree import Symbol
astb = ASTBuilder()
t = self.parse('6')
assert isinstance(t, Symbol)
- op = astb.get_instance(t, operations.Node)
- assert op.value == '6'
- assert op.lineno == 0
+ pos = astb.get_pos(t)
+ assert pos.lineno == 0
t = self.parse('[1,]')
assert not isinstance(t, Symbol)
- op = astb.get_instance(t, operations.Node)
- assert op.lineno == 0
+ pos = astb.get_pos(t)
+ assert pos.start == 0
def test_primaryexpression(self):
w_num = self.eval_expr('(6)')
@@ -330,7 +329,7 @@
assert w_str.ToString() == 'hello world'
-class TestToAST(BaseGrammarTest):
+class TestToASTProgram(BaseGrammarTest):
def setup_class(cls):
cls.parse = parse_func()
@@ -339,5 +338,6 @@
def test_simple(self):
self.to_ast("1;")
+ #self.to_ast("var x=1;")
#self.to_ast("print(1+1);")
More information about the Pypy-commit
mailing list