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

jlg at codespeak.net jlg at codespeak.net
Fri Jul 20 11:22:44 CEST 2007


Author: jlg
Date: Fri Jul 20 11:22:44 2007
New Revision: 45209

Modified:
   pypy/dist/pypy/lang/scheme/TODO.txt
   pypy/dist/pypy/lang/scheme/execution.py
   pypy/dist/pypy/lang/scheme/object.py
   pypy/dist/pypy/lang/scheme/ssparser.py
   pypy/dist/pypy/lang/scheme/test/test_eval.py
   pypy/dist/pypy/lang/scheme/test/test_parser.py
Log:
set-car, set-cdr return value is now W_Undefined()

Modified: pypy/dist/pypy/lang/scheme/TODO.txt
==============================================================================
--- pypy/dist/pypy/lang/scheme/TODO.txt	(original)
+++ pypy/dist/pypy/lang/scheme/TODO.txt	Fri Jul 20 11:22:44 2007
@@ -6,9 +6,6 @@
 Do next
 -------
 
-- symbols vs identifier, which name is better
-  - global dict for symbols _obarray_
-
 - implement key functions
   (apply, reduce, mapcar and so on)
   # reduce will not make into python 3.0 ;)

Modified: pypy/dist/pypy/lang/scheme/execution.py
==============================================================================
--- pypy/dist/pypy/lang/scheme/execution.py	(original)
+++ pypy/dist/pypy/lang/scheme/execution.py	Fri Jul 20 11:22:44 2007
@@ -46,6 +46,7 @@
         'set!': Sete,
         'if': MacroIf,
         'lambda': Lambda,
+        'begin': Begin,
         'let': Let,
         'let*': LetStar,
         'letrec': Letrec,

Modified: pypy/dist/pypy/lang/scheme/object.py
==============================================================================
--- pypy/dist/pypy/lang/scheme/object.py	(original)
+++ pypy/dist/pypy/lang/scheme/object.py	Fri Jul 20 11:22:44 2007
@@ -52,7 +52,14 @@
     def eval_tr(self, ctx):
         return (self, None)
 
+class W_Undefined(W_Root):
+    def to_string(self):
+        return "#<undefined>"
+
 class W_Symbol(W_Root):
+    #class dictionary for symbol storage
+    obarray = {}
+
     def __init__(self, val):
         self.name = val
 
@@ -66,6 +73,22 @@
         w_obj = ctx.get(self.name)
         return (w_obj, None)
 
+    def eq_symbol(self, w_symb):
+        return w_symb is self
+
+def symbol(name):
+    #use this to create new symbols, it stores all symbols
+    #in W_Symbol.obarray dict
+    #if already in obarray return it 
+    name = name.lower()
+    w_symb = W_Symbol.obarray.get(name, None)
+    if w_symb is None:
+        w_symb = W_Symbol(name)
+        W_Symbol.obarray[name] = w_symb
+
+    assert isinstance(w_symb, W_Symbol)
+    return w_symb
+
 class W_Boolean(W_Root):
     def __init__(self, val):
         self.boolval = bool(val)
@@ -462,24 +485,24 @@
         return w_pair.cdr
 
 class SetCar(W_Procedure):
-    def procedure(self, crx, lst):
+    def procedure(self, ctx, lst):
         w_pair = lst[0]
         w_obj = lst[1]
         if not isinstance(w_pair, W_Pair):
             raise WrongArgType(w_pair, "Pair")
 
         w_pair.car = w_obj
-        return w_obj
+        return W_Undefined()
 
 class SetCdr(W_Procedure):
-    def procedure(self, crx, lst):
+    def procedure(self, ctx, lst):
         w_pair = lst[0]
         w_obj = lst[1]
         if not isinstance(w_pair, W_Pair):
             raise WrongArgType(w_pair, "Pair")
 
         w_pair.cdr = w_obj
-        return w_obj #unspec
+        return W_Undefined()
 
 class Quit(W_Procedure):
     def procedure(self, ctx, lst):
@@ -620,9 +643,14 @@
         w_body = lst.cdr
         return W_Lambda(w_args, w_body, ctx)
 
+class Begin(W_Macro):
+    def call_tr(self, ctx, lst):
+        #begin uses eval_body, so it is tail-recursive aware
+        return self.eval_body(ctx, lst)
+
 class Let(W_Macro):
     def call_tr(self, ctx, lst):
-        """let uses eval_body, so it is tail-recursive aware"""
+        #let uses eval_body, so it is tail-recursive aware
         if not isinstance(lst, W_Pair):
             raise SchemeSyntaxError
         local_ctx = ctx.copy()

Modified: pypy/dist/pypy/lang/scheme/ssparser.py
==============================================================================
--- pypy/dist/pypy/lang/scheme/ssparser.py	(original)
+++ pypy/dist/pypy/lang/scheme/ssparser.py	Fri Jul 20 11:22:44 2007
@@ -1,7 +1,7 @@
 import autopath
 from pypy.rlib.parsing.pypackrat import PackratParser
 from pypy.rlib.parsing.makepackrat import BacktrackException, Status
-from pypy.lang.scheme.object import W_Pair, W_Integer, W_String, W_Symbol, \
+from pypy.lang.scheme.object import W_Pair, W_Integer, W_String, symbol, \
         W_Nil, W_Boolean, W_Real, quote, qq, unquote, unquote_splicing
 
 def str_unquote(s):
@@ -27,7 +27,7 @@
     SYMBOL:
         c = `[\+\-\*\^\?a-zA-Z!<=>_~/$%&:][\+\-\*\^\?a-zA-Z0-9!<=>_~/$%&:]*`
         IGNORE*
-        return {W_Symbol(c)};
+        return {symbol(c)};
 
     FIXNUM:
         c = `\-?(0|([1-9][0-9]*))`

Modified: pypy/dist/pypy/lang/scheme/test/test_eval.py
==============================================================================
--- pypy/dist/pypy/lang/scheme/test/test_eval.py	(original)
+++ pypy/dist/pypy/lang/scheme/test/test_eval.py	Fri Jul 20 11:22:44 2007
@@ -387,6 +387,14 @@
     assert w_lst.cdr.cdr.cdr.car.to_string() == "a"
     assert isinstance(w_lst.cdr.cdr.cdr.cdr, W_Nil)
 
+def test_begin():
+    ctx = ExecutionContext()
+    w_global = W_Integer(0)
+    ctx.put("var", w_global)
+    w_result = eval_expr(ctx, "(begin (set! var 11) (+ var 33))")
+    assert w_result.to_number() == 44
+    assert ctx.get("var").to_number() == 11
+
 def test_let():
     ctx = ExecutionContext()
     w_global = W_Integer(0)

Modified: pypy/dist/pypy/lang/scheme/test/test_parser.py
==============================================================================
--- pypy/dist/pypy/lang/scheme/test/test_parser.py	(original)
+++ pypy/dist/pypy/lang/scheme/test/test_parser.py	Fri Jul 20 11:22:44 2007
@@ -94,7 +94,7 @@
     t = parse_sexpr("(" + char + ")")
     assert isinstance(t, W_Pair)
     assert isinstance(t.car, W_Symbol)
-    assert unwrap(t.car) == char
+    assert unwrap(t.car) == char.lower()
     assert isinstance(t.cdr, W_Nil)
 
 def test_truth_values():



More information about the Pypy-commit mailing list