[pypy-svn] r66136 - pypy/branch/pyjitpl5/pypy/jit/tl

arigo at codespeak.net arigo at codespeak.net
Tue Jul 7 10:47:33 CEST 2009


Author: arigo
Date: Tue Jul  7 10:47:31 2009
New Revision: 66136

Added:
   pypy/branch/pyjitpl5/pypy/jit/tl/tla.py   (contents, props changed)
Log:
Start tla.py, "toy language for academics" (thanks antonio for
the name :-)


Added: pypy/branch/pyjitpl5/pypy/jit/tl/tla.py
==============================================================================
--- (empty file)
+++ pypy/branch/pyjitpl5/pypy/jit/tl/tla.py	Tue Jul  7 10:47:31 2009
@@ -0,0 +1,94 @@
+
+from pypy.rlib.jit import JitDriver
+
+
+class W_Object:
+    pass
+
+
+class W_IntObject(W_Object):
+
+    def __init__(self, intvalue):
+        self.intvalue = intvalue
+
+    def is_true(self):
+        return self.intvalue != 0
+
+    def add(self, w_other):
+        if isinstance(w_other, W_IntObject):
+            sum = self.intvalue + other.intvalue
+            return W_IntObject(sum)
+        else:
+            raise OperationError
+
+
+class OperationError:
+    pass
+
+# ____________________________________________________________
+
+CONST_INT = 1
+POP       = 2
+ADD       = 3
+RETURN    = 4
+
+# ____________________________________________________________
+
+
+class Frame(object):
+    _virtualizable2_ = ['stackpos', 'stack[*]']
+    
+    def __init__(self, bytecode):
+        self.bytecode = bytecode
+        self.stack = [None] * 8
+        self.stackpos = 0
+
+    def push(self, w_x):
+        self.stack[self.stackpos] = w_x
+        self.stackpos += 1
+
+    def pop(self):
+        self.stackpos -= 1
+        assert self.stackpos >= 0
+        return self.stack[self.stackpos]
+
+    def interp(self):
+        bytecode = self.bytecode
+        pc = 0
+
+        while pc < len(bytecode):
+            opcode = ord(bytecode[pc])
+            pc += 1
+
+            if opcode == CONST_INT:
+                value = ord(bytecode[pc])
+                pc += 1
+                w_z = W_IntObject(value)
+                self.push(w_z)
+
+            elif opcode == POP:
+                self.pop()
+
+            elif opcode == ADD:
+                w_y = self.pop()
+                w_x = self.pop()
+                w_z = w_x.add(w_y)
+                self.push(w_z)
+
+            elif opcode == JUMP_IF:
+                target = ord(bytecode[pc])
+                pc += 1
+                w_x = self.pop()
+                if w_x.is_true():
+                    pc = target
+
+            elif opcode == RETURN:
+                w_x = self.pop()
+                return w_x
+
+
+def run(bytecode, w_arg):
+    frame = Frame(bytecode)
+    frame.push(w_arg)
+    w_result = frame.interp()
+    return w_result



More information about the Pypy-commit mailing list