Commits

Matt Bone committed 973add4

haha, this thing is actually working.

Comments (0)

Files changed (1)

in_py/parse_sexp.py

     def __init__(self, value):
         self.value = value
 
+
 class open_paren_token(token):
     def __add__(self, x):
         raise next_token_exception()
 
 
-class close_paren_token(open_paren_token): pass
-
+class close_paren_token(token):
+    def __add__(self, x):
+        raise next_token_exception()
+    
 
 class symbol_token(token):
     def __add__(self, x):
     top = None
     
     for token in tokens:
-        print stack
+
         if isinstance(token, open_paren_token):
             if len(stack) == 0:
                 top = list()
             if stack is None: raise syntax_exception()
 
             stack[-1].append(Number(token.value))
+            
+        else:
+            raise Exception("Unkown token %s" % token)
 
-    if len(stack) != 0: raise syntax_exception("Unbalanced Parentheses")
+    if len(stack) != 0: raise syntax_exception("Unbalanced Parentheses: %s" % len(stack))
     
     return top
 
-def sexp_to_tree():
-    pass
-
+def sexp_to_tree(sexp):
+    return parse(tokenize(sexp))
 
 import unittest
 class TestParser(unittest.TestCase):
 
         self.assertEqual(tree[0], Symbol("a"))
 
+    def test_less_simple_parse(self):
+        tokens = [open_paren_token("("),
+                  symbol_token("+"),
+                  number_token("1"),
+                  open_paren_token("("),
+                  symbol_token("+"),
+                  number_token("2"),
+                  number_token("2"),
+                  close_paren_token(")"),
+                  close_paren_token(")")
+                  ]
+        
+        tree = parse(tokens)
+
+        expected = [Symbol("+"),
+                    Number("1"),
+                    [Symbol("+"),
+                     Number("2"),
+                     Number("2"),]
+                    ]
+        self.assertEqual(tree, expected)
+
+        tree = sexp_to_tree("(+ 1 (+ 2 2))")
+        self.assertEqual(tree, expected)
     
 
 class TestTokenizer(unittest.TestCase):