[pypy-svn] r36369 - in pypy/dist/pypy/lang/js: . test
santagada at codespeak.net
santagada at codespeak.net
Tue Jan 9 16:31:30 CET 2007
Author: santagada
Date: Tue Jan 9 16:31:28 2007
New Revision: 36369
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:
finishing the machinery to have a dynamic interpreter
Modified: pypy/dist/pypy/lang/js/interpreter.py
==============================================================================
--- pypy/dist/pypy/lang/js/interpreter.py (original)
+++ pypy/dist/pypy/lang/js/interpreter.py Tue Jan 9 16:31:28 2007
@@ -7,11 +7,21 @@
# TODO Add line info for debug
# def __init__(self, lineno = 1):
# self.lineno = lineno
- pass
+ def eval(self, ctx):
+ raise NotImplementedError
-class Statement(Node):
def execute(self, ctx):
raise NotImplementedError
+
+ def get_literal(self):
+ raise NotImplementedError
+
+ def get_args(self, ctx):
+ raise NotImplementedError
+
+
+class Statement(Node):
+ pass
class Expression(Statement):
def eval(self, ctx):
@@ -31,6 +41,9 @@
s2 = self.left.eval(ctx).GetValue()
s4 = self.right.eval(ctx).GetValue()
return self.decision(ctx, s2, s4)
+
+ def decision(self, ctx, op1, op2):
+ raise NotImplementedError
class BinaryLogicOp(BinaryOp):
@@ -40,35 +53,27 @@
def writer(x):
print x
+def load_source(script_source):
+ temp_tree = parse(script_source)
+ return from_tree(temp_tree)
+
+def load_bytecode(bytecode):
+ temp_tree = parse_bytecode(bytecode)
+ return from_tree(temp_tree)
+
class Interpreter(object):
"""Creates a js interpreter"""
- def __init__(self, script_source=None):
+ def __init__(self):
self.w_Object = W_Object() #creating Object
self.w_Global = W_Object()
self.w_Global.Prototype = self.w_Object
self.w_Global.Put('prototype', W_String('Object'))
self.w_Global.Put('Object', self.w_Object)
self.global_context = global_context(self.w_Global)
- if script_source is not None:
- self.load_source(script_source)
-
- def load_source(self, script_source):
- """load a source script text to the interpreter"""
- temp_tree = parse(script_source)
- self.script = from_tree(temp_tree)
-
- def append_source(self, script_source):
- temp_tree = parse(script_source)
- newscript = from_tree(temp_tree)
- self.script.append_script(newscript)
-
- def load_bytecode(self, bytecode):
- temp_tree = parse_bytecode(bytecode)
- self.script = from_tree(temp_tree)
- def run(self):
+ def run(self, script):
"""run the interpreter"""
- return self.script.execute(self.global_context)
+ return script.execute(self.global_context)
class PropertyInit(Node):
def __init__(self, name, value):
Modified: pypy/dist/pypy/lang/js/jsobj.py
==============================================================================
--- pypy/dist/pypy/lang/js/jsobj.py (original)
+++ pypy/dist/pypy/lang/js/jsobj.py Tue Jan 9 16:31:28 2007
@@ -55,9 +55,18 @@
def ToNumber(self):
return NaN
- # def get_literal(self):
- # return self.ToString()
- #
+ def Get(self, P):
+ raise NotImplementedError
+
+ def Put(self, P, V, DontDelete=False, ReadOnly=False, DontEnum=False, Internal=False):
+ raise NotImplementedError
+
+ def PutValue(self, w, ctx):
+ raise NotImplementedError
+
+ def Call(self, ctx, args=[], this=None):
+ raise NotImplementedError
+
def __str__(self):
return self.ToString()
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 Tue Jan 9 16:31:28 2007
@@ -28,16 +28,20 @@
def assert_prints(self, code, assval):
l = []
interpreter.writer = l.append
- js_int = interpreter.Interpreter(code)
+ js_int = interpreter.Interpreter()
try:
- js_int.run()
+ if isinstance(code, str):
+ js_int.run(load_source(code))
+ else:
+ for codepiece in code:
+ js_int.run(load_source(codepiece))
except ThrowException, excpt:
l.append("uncaught exception: "+str(excpt.exception))
assert l == assval
def assert_result(self, code, result):
- inter = interpreter.Interpreter(code)
- r = inter.run()
+ inter = interpreter.Interpreter()
+ r = inter.run(load_source(code))
assert r.ToString() == result.ToString()
def test_interp_parse(self):
@@ -283,5 +287,15 @@
print("z" in x);
""", ["true", "false"])
+ def test_append_code(self):
+ self.assert_prints(["""
+ var x; x=3;
+ """, """
+ print(x);
+ z = 2;
+ ""","""
+ print(z)
+ """]
+ ,["3", "2"])
More information about the Pypy-commit
mailing list