diff parser/Parser.d @ 45:9bc660cbdbec new_gen

If statements are back Also fixed a bug in the codegen preventing return in the else branch, now it is optional. Also found an issue with the way we are generating our llvm from ifs - it doesn't mean anything but the code looks ugly. if (cond_1) if (cond_2) statement; return 0; Becomes: br cond_1, then, merge then: br cond_2 then2, merge2 merge: ret 0 then2: statements merge2: br merge This is because we use appendBasicBlock on the function
author Anders Halager <halager@gmail.com>
date Wed, 23 Apr 2008 16:43:42 +0200
parents 495188f9078e
children 90fb4fdfefdd
line wrap: on
line diff
--- a/parser/Parser.d	Wed Apr 23 00:57:45 2008 +0200
+++ b/parser/Parser.d	Wed Apr 23 16:43:42 2008 +0200
@@ -86,8 +86,32 @@
                 require(Tok.Seperator);
                 return action.actOnReturnStmt(ret, exp);
 
+            /*
+               if (cond)
+                single statement | compound statement
+               [else
+                single statement | compound statement]
+             */
             case Tok.If:
-                return null;
+                Token _if = lexer.next();
+
+                require(Tok.OpenParentheses);
+                Exp cond = parseExpression();
+                require(Tok.CloseParentheses);
+
+                Stmt thenB = parseSingleOrCompoundStatement();
+
+                // if there is no else part we use the if as token, to have
+                // something than can be passed along
+                Token _else = _if;
+                Stmt elseB;
+                if (lexer.peek.type == Tok.Else)
+                {
+                    _else = lexer.next;
+                    elseB = parseSingleOrCompoundStatement();
+                }
+
+                return action.actOnIfStmt(_if, cond, thenB, _else, elseB);
 
             case Tok.While:
                 return null;
@@ -142,6 +166,16 @@
     }
 
     /**
+      Parse either a block, or a single statement as allowed after if, while
+      and for.
+     */
+    Stmt parseSingleOrCompoundStatement()
+    {
+        if (lexer.peek.type == Tok.OpenBrace)
+            return parseCompoundStatement();
+        return parseStatement();
+    }
+    /**
       Parses a function-body or similar, expects { to be current token.
       
       Will consume both the starting { and ending }