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

jlg at codespeak.net jlg at codespeak.net
Wed Jul 11 09:22:46 CEST 2007


Author: jlg
Date: Wed Jul 11 09:22:45 2007
New Revision: 44915

Modified:
   pypy/dist/pypy/lang/scheme/TODO.txt
   pypy/dist/pypy/lang/scheme/object.py
   pypy/dist/pypy/lang/scheme/test/test_eval.py
Log:
let macro added

Modified: pypy/dist/pypy/lang/scheme/TODO.txt
==============================================================================
--- pypy/dist/pypy/lang/scheme/TODO.txt	(original)
+++ pypy/dist/pypy/lang/scheme/TODO.txt	Wed Jul 11 09:22:45 2007
@@ -1,18 +1,20 @@
 Do now
 ------
 
+- lambda called with wrong number of args issue
 - RPythonize (during EP sprint)
 
 Do next
 -------
 
-- definitions: let, let*, letrec
+- definitions: let*, letrec
 
 - symbols vs identifier, which name is better
   - global dict for symbols _obarray_
 
 - implement key funcions
   (apply, reduce, mapcar and so on)
+  # reduce will not make into python 3.0 ;)
 
 - comparison: < > eq? eqv?
 
@@ -22,8 +24,9 @@
 Here starts the real fun!
 
 - macros
-- proper tail-recursion
-  - mutulally recursive procedures (r5rs p.12)
 - delayed evaluation
 - continuations
+- proper tail-recursion
+  - mutulally recursive procedures (r5rs p.12)
+- switch to bytecode enberation + eval istead of evaluating AST
 

Modified: pypy/dist/pypy/lang/scheme/object.py
==============================================================================
--- pypy/dist/pypy/lang/scheme/object.py	(original)
+++ pypy/dist/pypy/lang/scheme/object.py	Wed Jul 11 09:22:45 2007
@@ -182,9 +182,6 @@
 
     def procedure(self, ctx, lst):
         #ctx is a caller context, which is joyfully ignored
-        
-        #if len(lst) != len(self.args):
-        #    raise "Wrong argument count"
 
         local_ctx = self.closure.copy()
 
@@ -196,10 +193,6 @@
             else:
                 local_ctx.put(name, lst[idx])
 
-        #vars = zip(self.args, lst)
-        #for (name, val) in vars:
-        #    local_ctx.put(name, val)
-
         body_expression = self.body
         body_result = None
         while not isinstance(body_expression, W_Nil):
@@ -318,6 +311,26 @@
         w_body = lst.cdr #.car
         return W_Lambda(w_args, w_body, ctx.copy())
 
+class Let(W_Macro):
+    def eval(slef, ctx, lst):
+        local_ctx = ctx.copy()
+        w_formal = lst.car
+        while not isinstance(w_formal, W_Nil):
+            name = w_formal.car.car.to_string()
+            #evaluate the values in caller ctx
+            val = w_formal.car.cdr.car.eval(ctx)
+            local_ctx.put(name, val)
+            w_formal = w_formal.cdr
+
+        body_expression = lst.cdr
+        body_result = None
+        while not isinstance(body_expression, W_Nil):
+            body_result = body_expression.car.eval(local_ctx)
+            body_expression = body_expression.cdr
+
+        return body_result
+
+
 def Literal(sexpr):
     return W_Pair(W_Identifier('quote'), W_Pair(sexpr, W_Nil()))
 
@@ -356,6 +369,7 @@
         'set!': Sete,
         'if': MacroIf,
         'lambda': Lambda,
+        'let': Let,
         'quote': Quote,
     }
 

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	Wed Jul 11 09:22:45 2007
@@ -318,3 +318,11 @@
     assert w_lst.cdr.cdr.cdr.car.to_string() == "a"
     assert isinstance(w_lst.cdr.cdr.cdr.cdr, W_Nil)
 
+def test_let():
+    ctx = ExecutionContext()
+    w_global = W_Fixnum(0)
+    ctx.put("var", w_global)
+    w_result = eval_expr(ctx, "(let ((var 42) (x (+ 2 var))) (+ var x))")
+    assert w_result.to_number() == 44
+    assert ctx.get("var") is w_global
+



More information about the Pypy-commit mailing list