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

jlg at codespeak.net jlg at codespeak.net
Tue Jul 3 14:19:23 CEST 2007


Author: jlg
Date: Tue Jul  3 14:19:22 2007
New Revision: 44694

Modified:
   pypy/dist/pypy/lang/scheme/object.py
   pypy/dist/pypy/lang/scheme/test/test_eval.py
Log:
W_Macro != W_Procedure; lambda prototype added

Modified: pypy/dist/pypy/lang/scheme/object.py
==============================================================================
--- pypy/dist/pypy/lang/scheme/object.py	(original)
+++ pypy/dist/pypy/lang/scheme/object.py	Tue Jul  3 14:19:22 2007
@@ -123,6 +123,11 @@
     def to_string(self):
         return "()"
 
+class W_Lambda(W_Root):
+    def __init__(self, args, body):
+        self.args = args
+        self.body = body
+
 ##
 # operations
 ##
@@ -139,6 +144,16 @@
     def oper(self, ctx, lst):
         raise NotImplementedError
 
+class W_Macro(W_Root):
+    def __init__(self, pname=""):
+        self.pname = pname
+
+    def to_string(self):
+        return "#<macro:%s>" % (self.pname,)
+
+    def eval(self, ctx, lst=None):
+        raise NotImplementedError
+
 def apply_lst(ctx, fun, lst):
     acc = None
 
@@ -173,17 +188,17 @@
     def oper(self, ctx, lst):
         return apply_lst(ctx, self.multiplier, lst)
 
-class Define(W_Procedure):
-    def oper(self, ctx, lst):
+class Define(W_Macro):
+    def eval(self, ctx, lst):
         w_identifier = lst.car
         assert isinstance(w_identifier, W_Identifier)
 
         w_val = lst.cdr.car.eval(ctx)
-        ctx.put(w_identifier.name, w_val)
+        ctx.set(w_identifier.name, w_val)
         return w_val
 
-class MacroIf(W_Procedure):
-    def oper(self, ctx, lst):
+class MacroIf(W_Macro):
+    def eval(self, ctx, lst):
         w_condition = lst.car
         w_then = lst.cdr.car
         if isinstance(lst.cdr.cdr, W_Nil):
@@ -214,6 +229,12 @@
         w_pair = lst.car.eval(ctx)
         return w_pair.cdr
 
+class Lambda(W_Macro):
+    def eval(self, ctx, lst):
+        w_args = lst.car
+        w_body = lst.cdr.car
+        return W_Lambda(w_args, w_body)
+
 ##
 # Location()
 ##
@@ -236,6 +257,7 @@
         'cons': Cons,
         'car': Car,
         'cdr': Cdr,
+        'lambda': Lambda,
     }
 
 OPERATION_MAP = {}

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	Tue Jul  3 14:19:22 2007
@@ -2,7 +2,7 @@
 from pypy.lang.scheme.ssparser import parse
 from pypy.lang.scheme.object import W_Boolean, W_Fixnum, W_Float, W_String
 from pypy.lang.scheme.object import W_Nil, W_Pair, W_Symbol, W_Identifier
-from pypy.lang.scheme.object import W_Procedure
+from pypy.lang.scheme.object import W_Procedure, W_Lambda
 from pypy.lang.scheme.object import ExecutionContext
 from pypy.lang.scheme.operation import mul, add
 
@@ -144,3 +144,8 @@
     w_cddr = eval_noctx("(cdr (cdr (cons 1 (cons 2 3))))")
     assert w_cddr.to_number() == 3
 
+def test_lambda_definition():
+    ctx = ExecutionContext()
+    w_lambda = eval_expr(ctx, "(lambda () 12)")
+    assert isinstance(w_lambda, W_Lambda)
+



More information about the Pypy-commit mailing list