[pypy-svn] r44917 - in pypy/dist/pypy/lang/scheme: . test

fijal at codespeak.net fijal at codespeak.net
Wed Jul 11 10:07:48 CEST 2007


Author: fijal
Date: Wed Jul 11 10:07:47 2007
New Revision: 44917

Added:
   pypy/dist/pypy/lang/scheme/interactive.py   (contents, props changed)
   pypy/dist/pypy/lang/scheme/test/test_interactive.py   (contents, props changed)
Log:
Add a simple scheme interactive interpreter


Added: pypy/dist/pypy/lang/scheme/interactive.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/lang/scheme/interactive.py	Wed Jul 11 10:07:47 2007
@@ -0,0 +1,33 @@
+#!/usr/bin/env python
+""" Interactive (untranslatable) version of the pypy
+scheme interpreter
+"""
+
+from pypy.lang.scheme.object import ExecutionContext
+from pypy.lang.scheme.ssparser import parse
+import os, sys
+
+def check_parens(s):
+    return s.count("(") == s.count(")")
+
+def interactive():
+    print "PyPy Scheme interpreter"
+    ctx = ExecutionContext()
+    to_exec = ""
+    cont = False
+    while 1:
+        if cont:
+            ps = '.. '
+        else:
+            ps = '-> '
+        sys.stdout.write(ps)
+        to_exec += sys.stdin.readline()
+        if check_parens(to_exec):
+            print parse(to_exec).eval(ctx)
+            to_exec = ""
+            cont = False
+        else:
+            cont = True
+
+if __name__ == '__main__':
+    interactive()

Added: pypy/dist/pypy/lang/scheme/test/test_interactive.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/lang/scheme/test/test_interactive.py	Wed Jul 11 10:07:47 2007
@@ -0,0 +1,32 @@
+
+from pypy.lang.scheme.interactive import check_parens
+import py
+import re
+import sys
+
+def test_paren():
+    assert check_parens("(((  ))())")
+    assert not check_parens("(x()x")
+
+class TestInteractive:
+    def _spawn(self, *args, **kwds):
+        try:
+            import pexpect
+        except ImportError, e:
+            py.test.skip(str(e))
+        kwds.setdefault('timeout', 10)
+        print 'SPAWN:', args, kwds
+        child = pexpect.spawn(*args, **kwds)
+        child.logfile = sys.stdout
+        return child
+
+    def spawn(self, argv=[]):
+        path = py.magic.autopath()/".."/".."/"interactive.py"
+        return self._spawn(str(path), argv)
+
+    def test_interactive(self):
+        child = self.spawn()
+        child.expect("->")
+        child.sendline("(+ 1 2)")
+        child.expect("3W")
+



More information about the Pypy-commit mailing list