Commits

Maciej Fijalkowski  committed 140cb27

implement if

  • Participants
  • Parent commits 38539bb
  • Tags parser-complete

Comments (0)

Files changed (3)

File kermit/grammar.txt

 
 main: statement* [EOF];
 
-statement: expr ";" | VARIABLE "=" expr ";" | "while" "(" expr ")" "{" statement* "}";
+statement: expr ";" | VARIABLE "=" expr ";" | "while" "(" expr ")" "{" statement* "}" | "if" "(" expr ")" "{" statement* "}";
 
 expr: atom ADD_SYMBOL expr | atom;
 

File kermit/sourceparser.py

         self.cond = cond
         self.body = body
 
+class If(Node):
+    """ A very simple if
+    """
+    def __init__(self, cond, body):
+        self.cond = cond
+        self.body = body        
+
 class Transformer(object):
     """ Transforms AST from the obscure format given to us by the ennfparser
     to something easier to work with
             cond = self.visit_expr(node.children[2])
             stmts = self._grab_stmts(node.children[5])
             return While(cond, Block(stmts))
+        if node.children[0].additional_info == 'if':
+            cond = self.visit_expr(node.children[2])
+            stmts = self._grab_stmts(node.children[5])
+            return If(cond, Block(stmts))
         raise NotImplementedError
 
     def visit_expr(self, node):

File kermit/test/test_parser.py

 
 from kermit.sourceparser import parse, Stmt, Block, ConstantInt, BinOp,\
-     Variable, Assignment, While
+     Variable, Assignment, While, If
 
 def test_parse_basic():
     assert parse('13;') == Block([Stmt(ConstantInt(13))])
 def test_while():
     assert parse('while (1) { a = 3; }') == Block([While(ConstantInt(1),
               Block([Assignment('a', ConstantInt(3))]))])
+
+def test_if():
+    assert parse("if (1) { a; }") == Block([If(ConstantInt(1),
+                                               Block([Stmt(Variable("a"))]))])