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

jlg at codespeak.net jlg at codespeak.net
Wed May 23 16:00:23 CEST 2007


Author: jlg
Date: Wed May 23 16:00:23 2007
New Revision: 43573

Modified:
   pypy/dist/pypy/lang/scheme/object.py   (contents, props changed)
   pypy/dist/pypy/lang/scheme/ssparser.py   (contents, props changed)
   pypy/dist/pypy/lang/scheme/test/test_object.py   (contents, props changed)
   pypy/dist/pypy/lang/scheme/test/test_parser.py   (contents, props changed)
Log:
tabs removed

Modified: pypy/dist/pypy/lang/scheme/object.py
==============================================================================
--- pypy/dist/pypy/lang/scheme/object.py	(original)
+++ pypy/dist/pypy/lang/scheme/object.py	Wed May 23 16:00:23 2007
@@ -1,30 +1,84 @@
+import autopath
 
 class W_Root(object):
+    def to_string(self):
+        return ''
 
-	def is_pair(self):
-		return False
+    def to_boolean(self):
+        return False
 
-	def to_string(self):
-		return ''
+    def __str__(self):
+        return self.to_string() + "W"
 
-	def to_boolean(self):
-		return False
-
-	def __str__(self):
-		return self.to_string()
+    def __repr__(self):
+        return "<W_Root " + self.to_string + " >"
 
 class W_Boolean(W_Root):
-	pass
+    def __init__(self, val):
+        self.boolval = bool(val)
+
+    def to_string(self):
+        if self.boolval:
+            return "#t"
+        return "#f"
+
+    def to_boolean(self):
+        return self.boolval
+
+    def __repr__(self):
+        return "<W_Boolean " + str(self.boolval) + " >"
+
+class W_String(W_Root):
+    def __init__(self, val):
+        self.strval = val
+
+    def to_string(self):
+        return self.strval
+
+    def __repr__(self):
+        return "<W_String " + self.strval + " >"
+
+class W_Fixnum(W_Root):
+    def __init__(self, val):
+        self.fixnumval = int(val)
+
+    def to_string(self):
+        return str(self.fixnumval)
+
+    def to_number(self):
+        return self.to_fixnum()
+
+    def to_fixnum(self):
+        return self.fixnumval
+
+    def to_float(self):
+        return float(self.fixnumval)
+
+class W_Float(W_Root):
+    def __init__(self, val):
+        self.floatval = float(val)
+
+    def to_string(self):
+        return str(self.floatval)
+
+    def to_number(self):
+        return self.to_float()
+
+    def to_fixnum(self):
+        return int(self.floatval)
+
+    def to_float(self):
+        return self.floatval
 
-class W_False(W_Boolean):
+class W_Pair(W_Root):
+    def __init__(self, car, cdr):
+        self.car = car
+        self.cdr = cdr
 
-	def to_string(self):
-		return "#f"
+    def to_string(self):
+        return "(" + self.car.to_string() + " . " + self.cdr.to_string() + ")"
 
-class W_True(W_Boolean):
+class W_Nil(W_Root):
+    def to_string(self):
+        return "()"
 
-	def to_boolean(self):
-		return True
-		
-	def to_string(self):
-		return "#t"

Modified: pypy/dist/pypy/lang/scheme/ssparser.py
==============================================================================
--- pypy/dist/pypy/lang/scheme/ssparser.py	(original)
+++ pypy/dist/pypy/lang/scheme/ssparser.py	Wed May 23 16:00:23 2007
@@ -1,6 +1,7 @@
 import autopath
 from pypy.rlib.parsing.ebnfparse import parse_ebnf, make_parse_function
 from pypy.rlib.parsing.parsing import ParseError
+from pypy.rlib.parsing.tree import RPythonVisitor
 
 DEBUG = False
 
@@ -12,17 +13,30 @@
 '''
 
 try:
-	regexs, rules, ToAST = parse_ebnf(grammar)
+    regexs, rules, ToAST = parse_ebnf(grammar)
 except ParseError, e:
-	#print e.nice_error_message()
-	raise
+    #print e.nice_error_message()
+    raise
 
 parsef = make_parse_function(regexs, rules, eof=True)
 
 def parse(code):
-	t = parsef(code) 
-	tree = t.visit(ToAST())
-	if DEBUG:
-		ToAST().transform(t).view()
-	return tree
+    t = parsef(code) 
+    tree = t.visit(ToAST())[0]
+    if DEBUG:
+        ToAST().transform(t).view()
+    return tree
+
+class ASTBuilder(RPythonVisitor):
+
+    def visit_STRING(self, node):
+        print node.symbol + ":" + node.additional_info
+
+    def visit_IDENTIFIER(self, node):
+        print node.symbol + ":" + node.additional_info
+
+    def visit_sexpr(self, node):
+        print node.symbol + ":("
+        nodes = [self.dispatch(child) for child in node.children]
+        print ")"
 

Modified: pypy/dist/pypy/lang/scheme/test/test_object.py
==============================================================================
--- pypy/dist/pypy/lang/scheme/test/test_object.py	(original)
+++ pypy/dist/pypy/lang/scheme/test/test_object.py	Wed May 23 16:00:23 2007
@@ -1,12 +1,39 @@
 from pypy.lang.scheme.object import *
 
 def test_false():
-	w_false = W_False()
-	assert w_false.to_boolean() is False
-	assert isinstance(w_false, W_Boolean)
+    w_false = W_Boolean(False)
+    assert w_false.to_boolean() is False
 
 def test_true():
-	w_true = W_True()
-	assert w_true.to_boolean() is True
-	assert isinstance(w_true, W_Boolean)
+    w_true = W_Boolean(True)
+    assert w_true.to_boolean() is True
 
+def test_string():
+    str = "Hello World!"
+    w_str = W_String(str)
+    assert str == w_str.to_string()
+    
+def test_fixnum():
+    num = 12345
+    w_num = W_Fixnum(num)
+    assert num == w_num.to_fixnum()
+    assert float(num) == w_num.to_float()
+
+def test_float():
+    num = 12345.567
+    w_num = W_Float(num)
+    assert num == w_num.to_float()
+    assert int(num) == w_num.to_fixnum()
+
+def test_pair():
+    c1 = W_Fixnum(1)
+    c2 = W_String("c2")
+    c3 = W_Float(0.3)
+    c4 = W_Nil()
+    p2 = W_Pair(c3, c4)
+    p1 = W_Pair(c2, p2)
+    p = W_Pair(c1, p1)
+    assert p.car == c1
+    assert p.cdr.car == c2
+    assert p.cdr.cdr.car == c3
+    assert p.cdr.cdr.cdr == c4

Modified: pypy/dist/pypy/lang/scheme/test/test_parser.py
==============================================================================
--- pypy/dist/pypy/lang/scheme/test/test_parser.py	(original)
+++ pypy/dist/pypy/lang/scheme/test/test_parser.py	Wed May 23 16:00:23 2007
@@ -2,35 +2,41 @@
 from pypy.rlib.parsing.parsing import Symbol, Nonterminal
 
 def test_simple_sexpr():
-	#parse simple sexpr
-	t = parse(r'''(+ 1 2)''')
-	assert isinstance(t[0], Nonterminal) 
-	assert len(t[0].children) == 3
+    #parse simple sexpr
+    t = parse(r'''(+ 1 2)''')
+    assert isinstance(t, Nonterminal) 
+    assert len(t.children) == 3
 
 def test_string():
-	#parse string
-	t = parse(r'''"don't beleive \"them\""''')
-	assert isinstance(t[0], Symbol)
+    #parse string
+    t = parse(r'''"don't beleive \"them\""''')
+    assert isinstance(t, Symbol)
 
 def test_complex_sexpr():
-	#parse more complex sexpr
-	t = parse(r'''
-		(define (fac n) ; comment
-			(if (< n 2) n
-				(* (fac (- n 1)) n)))
-		''')
-	assert isinstance(t[0], Nonterminal)
-	assert len(t[0].children) == 3
-	assert isinstance(t[0].children[0], Symbol)
-	assert isinstance(t[0].children[1], Nonterminal)
-	assert isinstance(t[0].children[2], Nonterminal)
+    #parse more complex sexpr
+    t = parse(r'''
+        (define (fac n) ; comment
+            (if (< n 2) n
+                (* (fac (- n 1)) n)))
+        ''')
+    assert isinstance(t, Nonterminal)
+    assert len(t.children) == 3
+    assert isinstance(t.children[0], Symbol)
+    assert isinstance(t.children[1], Nonterminal)
+    assert isinstance(t.children[2], Nonterminal)
 
 def test_ident_gen():
-	ch_list = "+-*/azAZ09<=>-_~!$%&:?^"
-	for char in ch_list:
-		yield check_ident_ch, char
+    ch_list = "+-*/azAZ09<=>-_~!$%&:?^"
+    for char in ch_list:
+        yield check_ident_ch, char
 
 def check_ident_ch(char):
-	t = parse("(" + char + ")")
-	assert isinstance(t[0], Nonterminal)
-	assert isinstance(t[0].children[0], Symbol)
+    t = parse("(" + char + ")")
+    assert isinstance(t, Nonterminal)
+    assert isinstance(t.children[0], Symbol)
+
+def test_ast():
+    t = parse(r'''(define var "(+ 1 2 (sqrt 4))")''')
+    astb = ASTBuilder()
+    astb.dispatch(t)
+    #assert False



More information about the Pypy-commit mailing list