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

jlg at codespeak.net jlg at codespeak.net
Thu Jun 28 16:29:14 CEST 2007


Author: jlg
Date: Thu Jun 28 16:29:14 2007
New Revision: 44602

Modified:
   pypy/dist/pypy/lang/scheme/object.py
   pypy/dist/pypy/lang/scheme/test/test_eval.py
Log:
(if <cond> <then> <else>) macro implemented

Modified: pypy/dist/pypy/lang/scheme/object.py
==============================================================================
--- pypy/dist/pypy/lang/scheme/object.py	(original)
+++ pypy/dist/pypy/lang/scheme/object.py	Thu Jun 28 16:29:14 2007
@@ -190,6 +190,21 @@
     def eval(self, ctx):
         return define
 
+def macro_if(ctx, lst):
+    w_condition = lst.car
+    w_then = lst.cdr.car
+    w_else = lst.cdr.cdr.car
+
+    w_cond_val = w_condition.eval(ctx)
+    if w_cond_val.to_boolean() is True:
+        return w_then.eval(ctx)
+    else:
+        return w_else.eval(ctx)
+
+class MacroIf(W_Procedure):
+    def eval(self, ctx):
+        return macro_if
+
 ######################################
 # dict mapping operations to callables
 # callables must have 2 arguments
@@ -201,6 +216,7 @@
         '+': Add("+"),
         '*': Mul("*"),
         'define': Define("define"),
+        'if': MacroIf("if"),
     }
 
 class ExecutionContext(object):

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	Thu Jun 28 16:29:14 2007
@@ -80,3 +80,12 @@
     eval_expr(ctx, "(define v2 3.1)")
     w_num = eval_expr(ctx, "(+ 1 v1 v2)")
     assert w_num.to_number() == 46.1
+
+def test_if_simple():
+    ctx = ExecutionContext()
+    w_t = eval_expr(ctx, "(if #t #t #f)")
+    assert w_t.to_boolean() is True
+    w_f = eval_expr(ctx, "(if #f #t #f)")
+    assert w_f.to_boolean() is False
+
+



More information about the Pypy-commit mailing list