Anonymous avatar Anonymous committed dde7124

Enough of the evaluator to correctly run "Hello, world!".

Comments (0)

Files changed (3)

eg/hello.xoo

-a := 65
-print char a
-print string "Hello, world!"
-print string "The value of a is ";
-print a;
-print "!"
+a := 65
+print char a
+print string "Hello, world!"
+print string "The value of a is ";
+print a;
+print "!"
         return 'AST(%r,value=%r)' % (self.type, self.value)
 
 
-def eval_xoomonk(ast):
+def eval_xoomonk(ast, state):
     type = ast.type
     if type == 'Program':
-        pass
+        for node in ast.children:
+            eval_xoomonk(node, state)
     elif type == 'Assignment':
         pass
     elif type == 'PrintString':
-        pass
+        sys.stdout.write(ast.value)
     elif type == 'PrintChar':
-        pass
+        value = eval_xoomonk(ast.children[0], state)
+        sys.stdout.write(chr(value))
     elif type == 'Print':
-        pass
+        value = eval_xoomonk(ast.children[0], state)
+        sys.stdout.write(str(value))
     elif type == 'Newline':
-        pass
+        eval_xoomonk(ast.children[0], state)
+        sys.stdout.write('\n')
     elif type == 'Ref':
         pass
     else:
         self.type = None
         self.scan()
 
-    def scan_pattern(self, pattern, type):
+    def scan_pattern(self, pattern, type, token_group=1, rest_group=2):
         pattern = r'^(' + pattern + r')(.*?)$'
         match = re.match(pattern, self.text, re.DOTALL)
         if not match:
             return False
         else:
             self.type = type
-            self.token = match.group(1)
-            self.text = match.group(2)
+            self.token = match.group(token_group)
+            self.text = match.group(rest_group)
             #print >>sys.stderr, "(%r/%s->%r)" % (self.token, self.type, self.text)
             return True
 
             return
         if self.scan_pattern(r'\d+', 'integer literal'):
             return
-        if self.scan_pattern(r'\".*?\"', 'string literal'):
+        if self.scan_pattern(r'\"(.*?)\"', 'string literal',
+                             token_group=2, rest_group=3):
             return
         if self.scan_pattern(r'\w+', 'identifier'):
             return
 
 def main(argv):
     optparser = OptionParser(__doc__)
+    optparser.add_option("-a", "--show-ast",
+                         action="store_true", dest="show_ast", default=False,
+                         help="show parsed AST before evaluation")
     optparser.add_option("-t", "--test",
                          action="store_true", dest="test", default=False,
                          help="run test cases and exit")
     file = open(args[0])
     text = file.read()
     file.close()
-    #print repr(text)
     p = Parser(text)
     ast = p.program()
-    result = eval_xoomonk(ast)
+    if options.show_ast:
+        print repr(ast)
+    result = eval_xoomonk(ast, {})
     sys.exit(0)
 
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.