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

jlg at codespeak.net jlg at codespeak.net
Mon Jun 25 19:41:06 CEST 2007


Author: jlg
Date: Mon Jun 25 19:41:04 2007
New Revision: 44519

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:
objects - W_Procedure added, evaluation refactoring

Modified: pypy/dist/pypy/lang/scheme/TODO.txt
==============================================================================
--- pypy/dist/pypy/lang/scheme/TODO.txt	(original)
+++ pypy/dist/pypy/lang/scheme/TODO.txt	Mon Jun 25 19:41:04 2007
@@ -10,15 +10,18 @@
 - symbols, variables and execution context
   global dict for symbols _obarray_
 
-Do next
--------
-
 - there is need to distinct identifier from symbol
   (define var 11)
   (symbol? var) -> #f
   (symbol? 'var) -> #t
+
+Do next
+-------
+
 - control structures
 - functions
+- implement key funcions
+  (apply, reduce, mapcar and so on)
 
 Do in some future
 -----------------

Modified: pypy/dist/pypy/lang/scheme/object.py
==============================================================================
--- pypy/dist/pypy/lang/scheme/object.py	(original)
+++ pypy/dist/pypy/lang/scheme/object.py	Mon Jun 25 19:41:04 2007
@@ -1,21 +1,5 @@
 import autopath
 
-class ExecutionContext(object):
-    """Execution context implemented as a dict.
-
-    { "IDENTIFIER": W_Root }
-    """
-    def __init__(self, scope):
-        assert scope is not None
-        self.scope = scope
-
-    def get(self, name):
-        # shouldn't neme be instance of sth like W_Identifier
-        return self.scope.get(name, None)
-
-    def put(self, name, obj):
-        self.scope[name] = obj
-
 class W_Root(object):
     def to_string(self):
         return ''
@@ -44,14 +28,14 @@
 
     def eval(self, ctx):
 
-        if ctx is not None:
-            w_obj = ctx.get(self.name)
-            if w_obj is not None:
-                return w_obj.eval(ctx)
-
-        try:
-            return OPERATION_MAP[self.name]
-        except KeyError:
+        if ctx is None:
+            ctx = ExecutionContext()
+
+        w_obj = ctx.get(self.name)
+        if w_obj is not None:
+            return w_obj.eval(ctx)
+        else:
+            #reference to undefined identifier
             raise NotImplementedError
 
 class W_Boolean(W_Root):
@@ -117,7 +101,8 @@
         self.cdr = cdr
 
     def to_string(self):
-        return "(" + self.car.to_string() + " . " + self.cdr.to_string() + ")"
+        return "(" + self.car.to_string() + " . " \
+                + self.cdr.to_string() + ")"
 
     def eval(self, ctx):
         oper = self.car.eval(ctx)
@@ -132,6 +117,17 @@
 #not sure though any operations should exist here
 #it its very similar to operation.add
 #############################
+class W_Procedure(W_Root):
+
+    def __init__(self, pname=""):
+        self.pname = pname
+
+    def to_string(self):
+        return "#<procedure:%s>" % (self.pname,)
+
+    def eval(self, ctx, lst=None):
+        raise NotImplementedError
+
 def add_lst(ctx, lst):
     return apply_lst(ctx, lambda x, y: x + y, lst)
 
@@ -158,6 +154,14 @@
     else:
         return W_Float(acc)
 
+class Add(W_Procedure):
+    def eval(self, ctx):
+        return add_lst
+
+class Mul(W_Procedure):
+    def eval(self, ctx):
+        return mul_lst
+
 ######################################
 # dict mapping operations to callables
 # callables must have 2 arguments
@@ -166,7 +170,22 @@
 #######################################
 OPERATION_MAP = \
     {
-        '+': add_lst,
-        '*': mul_lst,
+        '+': Add("+"),
+        '*': Mul("*"),
     }
 
+class ExecutionContext(object):
+    """Execution context implemented as a dict.
+
+    { "IDENTIFIER": W_Root }
+    """
+    def __init__(self, scope=OPERATION_MAP):
+        assert scope is not None
+        self.scope = scope
+
+    def get(self, name):
+        return self.scope.get(name, None)
+
+    def put(self, name, obj):
+        self.scope[name] = obj
+

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	Mon Jun 25 19:41:04 2007
@@ -36,26 +36,26 @@
     return parse(expr).eval(None)
 
 def test_numerical():
-    w_num = eval_noctx('(+ 4)')
+    w_num = eval_noctx("(+ 4)")
     assert w_num.to_number() == 4
-    w_num = eval_noctx('(+ 4 -5)')
+    w_num = eval_noctx("(+ 4 -5)")
     assert w_num.to_number() == -1
-    w_num = eval_noctx('(+ 4 -5 6.1)')
+    w_num = eval_noctx("(+ 4 -5 6.1)")
     assert w_num.to_number() == 5.1
 
-    w_num = eval_noctx('(* 4)')
+    w_num = eval_noctx("(* 4)")
     assert w_num.to_number() == 4
-    w_num = eval_noctx('(* 4 -5)')
+    w_num = eval_noctx("(* 4 -5)")
     assert w_num.to_number() == -20
-    w_num = eval_noctx('(* 4 -5 6.1)')
+    w_num = eval_noctx("(* 4 -5 6.1)")
     assert w_num.to_number() == (4 * -5 * 6.1)
 
 def test_numerical_nested():
-    w_num = eval_noctx('(+ 4 (* (+ 5) 6) (+ 1 2))')
+    w_num = eval_noctx("(+ 4 (* (+ 5) 6) (+ 1 2))")
     assert w_num.to_number() == 37
 
 def test_ctx_simple():
-    ctx = ExecutionContext({})
+    ctx = ExecutionContext()
     ctx.put("v1", W_Fixnum(4))
     ctx.put("v2", W_Fixnum(5))
 



More information about the Pypy-commit mailing list