[pypy-svn] r32299 - in pypy/dist/pypy/jit/tl: . test

fijal at codespeak.net fijal at codespeak.net
Thu Sep 14 11:39:39 CEST 2006


Author: fijal
Date: Thu Sep 14 11:39:37 2006
New Revision: 32299

Added:
   pypy/dist/pypy/jit/tl/braininterp.py   (contents, props changed)
   pypy/dist/pypy/jit/tl/test/test_brainfuck.py   (contents, props changed)
Log:
Added brainfuck interpreter (just to raise number of languages that can be JITed I suppose...)


Added: pypy/dist/pypy/jit/tl/braininterp.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/jit/tl/braininterp.py	Thu Sep 14 11:39:37 2006
@@ -0,0 +1,52 @@
+
+class BrainInterpreter(object):
+    def __init__(self):
+        self.table = [0] * 30000
+        self.pointer = 0
+
+    def interp_char(self, code, code_pointer, input, output):
+        char_code = code[code_pointer]
+        if char_code == '>':
+            self.pointer += 1
+        elif char_code == '<':
+            self.pointer -= 1
+        elif char_code == '+':
+            self.table[self.pointer] += 1
+        elif char_code == '-':
+            self.table[self.pointer] -= 1
+        elif char_code == '.':
+            output.write(chr(self.table[self.pointer]))
+        elif char_code == ',':
+            self.table[self.pointer] = ord(input.read(1))
+        elif char_code == '[':
+            # find corresponding ]
+            if self.table[self.pointer] == 0:
+                need = 1
+                p = code_pointer + 1
+                while need > 0:
+                    if code[p] == ']':
+                        need -= 1
+                    elif code[p] == '[':
+                        need += 1
+                    p += 1
+                return p
+        elif char_code == ']':
+            if self.table[self.pointer] != 0:
+                need = 1
+                p = code_pointer - 1
+                while need > 0:
+                    if code[p] == ']':
+                        need += 1
+                    elif code[p] == '[':
+                        need -= 1
+                    p -= 1
+                return p + 2
+        return code_pointer + 1
+
+    def interpret(self, code, input_string, output_io):
+        code_pointer = 0
+        while code_pointer < len(code):
+            code_pointer = self.interp_char(code, code_pointer,
+                input_string, output_io)
+            #print code_pointer, self.table[:5]
+        return output_io

Added: pypy/dist/pypy/jit/tl/test/test_brainfuck.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/jit/tl/test/test_brainfuck.py	Thu Sep 14 11:39:37 2006
@@ -0,0 +1,45 @@
+
+from pypy.jit.tl.braininterp import BrainInterpreter
+
+from StringIO import StringIO
+
+def run_code(code, inp):
+    inp_s = StringIO(inp)
+    out = StringIO()
+    b = BrainInterpreter()
+    b.interpret(code, inp_s, out)
+    return out.getvalue()
+
+def test_braintone():
+    assert run_code("+++.","") == chr(3)
+    assert run_code("+++>+++.","") == chr(3)
+    assert run_code("""++++++++++
+[                   The initial loop to set up useful values in the array
+   >+++++++>++++++++++>+++>+<<<<-
+]
+>++.                print 'H'
+>+.                 print 'e'
++++++++.                  'l'
+.                         'l'
++++.                      'o'
+>++.                      space
+<<+++++++++++++++.        'W'
+>.                        'o'
++++.                      'r'
+------.                   'l'
+--------.                 'd'
+>+.                       '!'
+>.                        newline
+""", "") == "Hello World!\n"
+
+    code = ",----------[----------------------.,----------]"
+    assert run_code(code, "\x0a") == ""
+    assert run_code(code, "a\x0a") == "A"
+    assert run_code(code, "aa\x0a") == "AA"
+    assert run_code(code, "lowercase\x0a") == "LOWERCASE"
+
+    add_code = ",>++++++[<-------->-],[<+>-],<.>."
+
+    assert run_code(add_code, "43 ") == "7 "
+    assert run_code(add_code, "12 ") == "3 "
+



More information about the Pypy-commit mailing list