[pypy-commit] lang-scheme default: Implemented external representaions of objects and write function

boemmels noreply at buildbot.pypy.org
Sun Nov 27 00:55:28 CET 2011


Author: Juergen Boemmels <boemmels at web.de>
Branch: 
Changeset: r14:5a01480a6507
Date: 2011-11-27 00:54 +0100
http://bitbucket.org/pypy/lang-scheme/changeset/5a01480a6507/

Log:	Implemented external representaions of objects and write function

diff --git a/scheme/object.py b/scheme/object.py
--- a/scheme/object.py
+++ b/scheme/object.py
@@ -34,6 +34,9 @@
     def to_string(self):
         return ''
 
+    def to_repr(self):
+        return "#<unknown>"
+
     def to_boolean(self):
         return True
 
@@ -60,9 +63,11 @@
     equal = eqv
 
 class W_Undefined(W_Root):
-    def to_string(self):
+    def to_repr(self):
         return "#<undefined>"
 
+    to_string = to_repr
+
 w_undefined = W_Undefined()
 
 class W_Symbol(W_Root):
@@ -72,9 +77,11 @@
     def __init__(self, val):
         self.name = val
 
-    def to_string(self):
+    def to_repr(self):
         return self.name
 
+    to_string = to_repr
+
     def eval_tr(self, ctx):
         w_obj = ctx.get(self.name)
         return (w_obj, None)
@@ -98,11 +105,13 @@
     def __init__(self, val):
         self.boolval = bool(val)
 
-    def to_string(self):
+    def to_repr(self):
         if self.boolval:
             return "#t"
         return "#f"
 
+    to_string = to_repr
+
     def to_boolean(self):
         return self.boolval
 
@@ -120,6 +129,17 @@
     def to_string(self):
         return self.strval
 
+    def to_repr(self):
+        str_lst = ["\""]
+        for ch in self.strval:
+            if ch in ["\"", "\\"]:
+                str_lst.append("\\" + ch)
+            else:
+                str_lst.append(ch)
+
+        str_lst.append("\"")
+        return ''.join(str_lst)
+
     def __repr__(self):
         return "<W_String \"" + self.strval + "\">"
 
@@ -130,6 +150,9 @@
     def to_string(self):
         return self.chrval
 
+    def to_repr(self):
+        return "#\\" + self.chrval
+
     def __repr__(self):
         return "<W_Character #\\" + self.chrval + ">"
 
@@ -141,6 +164,10 @@
     def to_string(self):
         return str(self.realval)
 
+    def to_repr(self):
+        # return repr(self.realval)
+        return str(float(self.realval))
+
     def to_number(self):
         return self.to_float()
 
@@ -184,6 +211,10 @@
     def to_string(self):
         return str(self.intval)
 
+    def to_repr(self):
+        #return repr(self.intval)
+        return str(int(self.intval))
+
     def to_number(self):
         return self.to_fixnum()
 
@@ -213,9 +244,11 @@
     def __repr__(self):
         return "<W_Nil ()>"
 
-    def to_string(self):
+    def to_repr(self):
         return "()"
 
+    to_string = to_repr
+
     def eval_cf(self, ctx, caller, cont, elst=[], enum=0):
         raise SchemeSyntaxError
 
@@ -239,12 +272,24 @@
             return car + " " + cdr.to_lstring()
         elif cdr is w_nil: #end of proper list
             return car
+        else: #end proper list with dotted
+            return car + " . " + cdr.to_string()
 
-        #end proper list with dotted
-        return car + " . " + cdr.to_string()
+    def to_repr(self):
+        return "(" + self.to_lrepr() + ")"
+
+    def to_lrepr(self):
+        car = self.car.to_repr()
+        cdr = self.cdr
+        if isinstance(cdr, W_Pair): #still proper list
+            return car + " " + cdr.to_lrepr()
+        elif cdr is w_nil: #end of proper list
+            return car
+        else: #end proper list with dotted
+            return car + " . " + cdr.to_repr()
 
     def __repr__(self):
-        return "<W_Pair " + self.to_string() + ">"
+        return "<W_Pair " + self.to_repr() + ">"
 
     def continue_tr(self, ctx, lst, elst, cnt=True):
         oper = elst[0]
@@ -349,9 +394,11 @@
     def __init__(self, pname=""):
         self.pname = pname
 
-    def to_string(self):
+    def to_repr(self):
         return "#<primitive-procedure %s>" % (self.pname,)
 
+    to_string = to_repr
+
     def call_tr(self, ctx, lst):
         return self.continue_tr(ctx, lst, [], False)
 
diff --git a/scheme/procedure.py b/scheme/procedure.py
--- a/scheme/procedure.py
+++ b/scheme/procedure.py
@@ -382,3 +382,17 @@
         print
         return w_undefined
 
+class Write(W_Procedure):
+    _symbol_name = "write"
+
+    def procedure(self, ctx, lst):
+        if len(lst) == 1:
+            obj = lst[0]
+        elif len(lst) == 2:
+            (obj, port) = lst
+            raise NotImplementedError
+        else:
+            raise WrongArgsNumber
+
+        print obj.to_repr(),
+        return w_undefined
diff --git a/scheme/test/test_object.py b/scheme/test/test_object.py
--- a/scheme/test/test_object.py
+++ b/scheme/test/test_object.py
@@ -5,16 +5,25 @@
 def test_false():
     w_false = W_Boolean(False)
     assert w_false.to_boolean() is False
+    assert w_false.to_string() == "#f"
+    assert w_false.to_repr() == "#f"
 
 def test_true():
     w_true = W_Boolean(True)
     assert w_true.to_boolean() is True
+    assert w_true.to_string() == "#t"
+    assert w_true.to_repr() == "#t"
 
 def test_string():
     str = "Hello World!"
     w_str = W_String(str)
     assert str == w_str.to_string()
     assert w_str.to_boolean() is True
+    assert w_str.to_repr() == "\"Hello World!\""
+    str = r'''\ \\ \' " \"'''
+    w_str = W_String(str)
+    assert str == w_str.to_string()
+    assert w_str.to_repr() == r'''"\\ \\\\ \\' \" \\\""'''
     
 def test_fixnum():
     num = 12345
@@ -22,6 +31,7 @@
     assert num == w_num.to_fixnum()
     assert float(num) == w_num.to_float()
     assert w_num.to_boolean() is True
+    assert w_num.to_repr() == "12345"
 
 def test_float():
     num = 12345.567
@@ -29,6 +39,12 @@
     assert num == w_num.to_float()
     assert int(num) == w_num.to_fixnum()
     assert w_num.to_boolean() is True
+    assert w_num.to_repr() == "12345.567"
+
+def test_nil():
+    w_nil = W_Nil()
+    assert w_nil.to_boolean() is True # this is Scheme not LISP
+    assert w_nil.to_repr() == "()"
 
 def test_pair():
     c1 = W_Integer(1)
diff --git a/scheme/test/test_output.py b/scheme/test/test_output.py
--- a/scheme/test/test_output.py
+++ b/scheme/test/test_output.py
@@ -25,6 +25,7 @@
              ("(display 42)", "42"),
              ("(display \"Hello World!\")", "Hello World!"),
              ("(display '(1 2 3))", "(1 2 3)"),
+             ("(display #\\c)", "c"),
             ]
     for code, expected in tests:
         out = capture_output(lambda: eval_noctx(code))
@@ -33,3 +34,14 @@
 def test_newline():
     out = capture_output(lambda: eval_noctx("(newline)"))
     assert out == "\n"
+
+def test_write():
+    tests = [("(write 'foobar)", "foobar"),
+             ("(write 42)", "42"),
+             ("(write \"Hello World!\")", "\"Hello World!\""),
+             ("(write '(1 (0 0) \"Alice\"))", "(1 (0 0) \"Alice\")"),
+             ("(write #\\c)", "#\\c"),
+            ]
+    for code, expected in tests:
+        out = capture_output(lambda: eval_noctx(code))
+        assert out == expected


More information about the pypy-commit mailing list