Commits

Maciej Fijalkowski  committed c8561e9

BinOp

  • Participants
  • Parent commits 1101265

Comments (0)

Files changed (2)

File kermit/grammar.txt

 
+IGNORE: "[ \t\n]";
 DECIMAL: "-?0|[1-9][0-9]*";
+ADD_SYMBOL: "[-+]";
 
 main: statement* [EOF];
 
 statement: expr ";";
 
-expr: DECIMAL;
+expr: atom ADD_SYMBOL expr | atom;
+
+atom: DECIMAL;

File kermit/sourceparser.py

         return Stmt(self.visit_expr(node.children[0]))
 
     def visit_expr(self, node):
+        if len(node.children) == 1:
+            return self.visit_atom(node.children[0])
+        return BinOp(node.children[1].additional_info,
+                     self.visit_atom(node.children[0]),
+                     self.visit_expr(node.children[2]))
+
+    def visit_atom(self, node):
         chnode = node.children[0]
         if chnode.symbol == 'DECIMAL':
             return ConstantInt(int(chnode.additional_info))