[pypy-svn] r13545 - in pypy/dist/pypy/rpython: . test
hpk at codespeak.net
hpk at codespeak.net
Fri Jun 17 18:19:55 CEST 2005
Author: hpk
Date: Fri Jun 17 18:19:55 2005
New Revision: 13545
Modified:
pypy/dist/pypy/rpython/llinterp.py
pypy/dist/pypy/rpython/test/test_llinterp.py
Log:
(cfbolz,hpk)
- introduced LLFrame class to allow recursive calls
(and handle local variable bindings more effectively
as a side effect)
- i am using stricter vim-settings which will probably
generate large diffs in the future
Modified: pypy/dist/pypy/rpython/llinterp.py
==============================================================================
--- pypy/dist/pypy/rpython/llinterp.py (original)
+++ pypy/dist/pypy/rpython/llinterp.py Fri Jun 17 18:19:55 2005
@@ -18,6 +18,21 @@
self.bindings = {}
self.typer = typer
+ def getgraph(self, func):
+ return self.flowgraphs[func]
+
+ def eval_function(self, func, args=()):
+ graph = self.getgraph(func)
+ llframe = LLFrame(graph, args, self)
+ return llframe.eval()
+
+class LLFrame(object):
+ def __init__(self, graph, args, llinterpreter):
+ self.graph = graph
+ self.args = args
+ self.llinterpreter = llinterpreter
+ self.bindings = {}
+
# _______________________________________________________
# variable setters/getters helpers
@@ -60,13 +75,11 @@
# _______________________________________________________
# evaling functions
- def eval_function(self, func, args=()):
- graph = self.flowgraphs[func]
- return self.eval_graph(graph,args)
-
- def eval_graph(self, graph, args=()):
- log.graph("evaluating", graph.name)
+ def eval(self):
+ graph = self.graph
+ log.frame("evaluating", graph.name)
nextblock = graph.startblock
+ args = self.args
while 1:
self.fillvars(nextblock, args)
nextblock, args = self.eval_block(nextblock)
@@ -108,7 +121,7 @@
elif catch_exception:
link = block.exits[0]
if e:
- exdata = self.typer.getexceptiondata()
+ exdata = self.llinterpreter.typer.getexceptiondata()
cls, inst = e.args
for link in block.exits[1:]:
assert issubclass(link.exitcase, Exception)
@@ -152,8 +165,11 @@
def op_direct_call(self, f, *args):
if hasattr(f._obj, 'graph'):
- return self.eval_graph(f._obj.graph, args)
- return self.eval_function(f._obj._callable, args)
+ graph = f._obj.graph
+ else:
+ graph = self.llinterpreter.getgraph(f._obj._callable)
+ frame = self.__class__(graph, args, self.llinterpreter)
+ return frame.eval()
def op_malloc(self, obj):
return malloc(obj)
Modified: pypy/dist/pypy/rpython/test/test_llinterp.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_llinterp.py (original)
+++ pypy/dist/pypy/rpython/test/test_llinterp.py Fri Jun 17 18:19:55 2005
@@ -1,19 +1,19 @@
import py
from pypy.rpython.lltype import typeOf
-from pypy.rpython.rtyper import RPythonTyper
+from pypy.rpython.rtyper import RPythonTyper
from pypy.rpython.llinterp import LLInterpreter, LLException
-from pypy.translator.translator import Translator
+from pypy.translator.translator import Translator
from pypy.rpython.lltype import pyobjectptr
# switch on logging of interp to show more info on failing tests
-def setup_module(mod):
+def setup_module(mod):
mod.logstate = py.log._getstate()
- py.log.setconsumer("llinterp", py.log.STDOUT)
+ py.log.setconsumer("llinterp", py.log.STDOUT)
-def teardown_module(mod):
- py.log._setstate(mod.logstate)
+def teardown_module(mod):
+ py.log._setstate(mod.logstate)
def find_exception(exc):
assert isinstance(exc, LLException)
@@ -25,7 +25,7 @@
if func(pyobjectptr(cls)).typeptr == klass:
return cls
-def gengraph(func, argtypes=[]):
+def gengraph(func, argtypes=[]):
t = Translator(func)
t.annotate(argtypes)
global typer # we need it for find_exception
@@ -35,28 +35,30 @@
t.checkgraphs()
return t, typer
-def interpret(func, values):
+def interpret(func, values, view=False):
t, typer = gengraph(func, [type(x) for x in values])
+ if view:
+ t.view()
interp = LLInterpreter(t.flowgraphs, typer)
- res = interp.eval_function(func, values)
- return res
+ res = interp.eval_function(func, values)
+ return res
#__________________________________________________________________
-# tests
-
-def test_int_ops():
+# tests
+
+def test_int_ops():
res = interpret(number_ops, [3])
- assert res == 4
+ assert res == 4
-def test_float_ops():
+def test_float_ops():
res = interpret(number_ops, [3.5])
- assert res == 4.5
+ assert res == 4.5
-def test_ifs():
+def test_ifs():
res = interpret(simple_ifs, [0])
- assert res == 43
+ assert res == 43
res = interpret(simple_ifs, [1])
- assert res == 42
+ assert res == 42
def test_raise():
res = interpret(raise_exception, [41])
@@ -87,32 +89,42 @@
assert find_exception(info.value) is ValueError
def test_call_raise_intercept():
- res = interpret(call_raise_intercept, [41])
+ res = interpret(call_raise_intercept, [41], view=False)
assert res == 41
res = interpret(call_raise_intercept, [42])
assert res == 42
info = raises(LLException, interpret, call_raise_intercept, [43])
assert find_exception(info.value) is TypeError
-def test_while_simple():
+def test_while_simple():
res = interpret(while_simple, [3])
assert res == 6
-def test_number_comparisons():
- for t in float, int:
+def test_number_comparisons():
+ for t in float, int:
val1 = t(3)
val2 = t(4)
gcres = interpret(comparisons, [val1, val2])
res = [getattr(gcres, x) for x in typeOf(gcres).TO._names]
assert res == [True, True, False, True, False, False]
-def test_some_builtin():
- def f(i, j):
- x = range(i)
+def test_some_builtin():
+ def f(i, j):
+ x = range(i)
return x[j-1]
res = interpret(f, [10, 7])
assert res == 6
+def test_recursion_does_not_overwrite_my_variables():
+ def f(i):
+ j = i + 1
+ if i > 0:
+ f(i-1)
+ return j
+
+ res = interpret(f, [4])
+ assert res == 5
+
#
#__________________________________________________________________
#
@@ -125,35 +137,35 @@
for i in range(3):
assert res.items[i] == i+1
#__________________________________________________________________
-# example functions for testing the LLInterpreter
+# example functions for testing the LLInterpreter
_snap = globals().copy()
-def number_ops(i):
+def number_ops(i):
j = i + 2
- k = j * 2
+ k = j * 2
m = k / 2
return m - 1
-def comparisons(x, y):
- return (x < y,
- x <= y,
- x == y,
- x != y,
- #x is None,
- #x is not None,
- x >= y,
- x > y,
+def comparisons(x, y):
+ return (x < y,
+ x <= y,
+ x == y,
+ x != y,
+ #x is None,
+ #x is not None,
+ x >= y,
+ x > y,
)
-def simple_ifs(i):
- if i:
- return 42
- else:
- return 43
+def simple_ifs(i):
+ if i:
+ return 42
+ else:
+ return 43
-def while_simple(i):
+def while_simple(i):
sum = 0
- while i > 0:
+ while i > 0:
sum += i
i -= 1
return sum
@@ -184,21 +196,21 @@
except ValueError:
raise TypeError
#__________________________________________________________________
-# interactive playing
+# interactive playing
-if __name__ == '__main__':
+if __name__ == '__main__':
try:
import rlcompleter2 as _rl2
- _rl2.setup()
- except ImportError:
+ _rl2.setup()
+ except ImportError:
pass
t, typer = gengraph(number_ops, [int])
interp = LLInterpreter(t.flowgraphs, typer)
res = interp.eval_function(number_ops, [3])
assert res == number_ops(3)
- for name, value in globals().items():
- if name not in _snap and name[0] != '_':
+ for name, value in globals().items():
+ if name not in _snap and name[0] != '_':
print "%20s: %s" %(name, value)
More information about the Pypy-commit
mailing list