changeset 153:ee202c72cd30

Merge
author Anders Halager <halager@gmail.com>
date Mon, 21 Jul 2008 21:32:20 +0200
parents 893f23a9de93 (current diff) 393a1f47a6d2 (diff)
children 0ea5d2f3e96b
files ast/Stmt.d parser/Action.d sema/AstAction.d
diffstat 6 files changed, 57 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/ast/Stmt.d	Mon Jul 21 21:30:44 2008 +0200
+++ b/ast/Stmt.d	Mon Jul 21 21:32:20 2008 +0200
@@ -21,6 +21,7 @@
     Return,
     If,
     While,
+    For,
     Switch,
 }
 
@@ -180,6 +181,27 @@
     Stmt whileBody;
 }
 
+class ForStmt : Stmt
+{
+    this(Stmt init, Exp cond, Exp incre, Stmt stmts)
+    {
+        super(StmtType.For);
+        this.init = init;
+        this.cond = cond;
+        this.incre = incre;
+        this.forBody = stmts;
+    }
+
+    override void simplify()
+    {
+        cond = cond.simplify();
+        forBody.simplify();
+    }
+
+    Exp cond, incre;
+    Stmt init, forBody;
+}
+
 class SwitchStmt : Stmt
 {
     this(SourceLocation loc, Exp target)
--- a/parser/Action.d	Mon Jul 21 21:30:44 2008 +0200
+++ b/parser/Action.d	Mon Jul 21 21:32:20 2008 +0200
@@ -288,7 +288,7 @@
 
     /**
      */
-    StmtT actOnForStmt(ref Token forTok, StmtT init, ExprT cond, ExprT incre, StmtT whileBody)
+    StmtT actOnForStmt(ref Token forTok, StmtT init, ExprT cond, ExprT incre, StmtT forBody)
     {
         return null;
     }
--- a/sema/AstAction.d	Mon Jul 21 21:30:44 2008 +0200
+++ b/sema/AstAction.d	Mon Jul 21 21:32:20 2008 +0200
@@ -196,6 +196,15 @@
         return new WhileStmt(c, b);
     }
 
+    StmtT actOnForStmt(ref Token tok, StmtT init, ExprT cond, ExprT incre, StmtT forBody)
+    {
+        Stmt i = cast(Stmt)init;
+        Exp c = cast(Exp)cond;
+        Exp inc = cast(Exp)incre;
+        Stmt b = cast(Stmt)forBody;
+        return new ForStmt(i, c, inc, b);
+    }
+
     StmtT actOnDeclStmt(DeclT decl)
     {
         Decl d = cast(Decl)decl;
--- a/sema/ScopeBuilder.d	Mon Jul 21 21:30:44 2008 +0200
+++ b/sema/ScopeBuilder.d	Mon Jul 21 21:32:20 2008 +0200
@@ -418,6 +418,14 @@
         pop(sc);
     }
 
+    override void visitForStmt(ForStmt s)
+    {
+        s.env = current();
+        auto sc = push();
+        super.visitForStmt(s);
+        pop(sc);
+    }
+
     override void visitCompoundStmt(CompoundStatement s)
     {
         s.env = current();
--- a/sema/Visitor.d	Mon Jul 21 21:30:44 2008 +0200
+++ b/sema/Visitor.d	Mon Jul 21 21:32:20 2008 +0200
@@ -70,6 +70,8 @@
                 return visitIfStmt(cast(IfStmt)stmt);
             case StmtType.While:
                 return visitWhileStmt(cast(WhileStmt)stmt);
+            case StmtType.For:
+                return visitForStmt(cast(ForStmt)stmt);
             case StmtType.Switch:
                 return visitSwitchStmt(cast(SwitchStmt)stmt);
             default:
@@ -256,6 +258,18 @@
         else
             return StmtT.init;
     }
+    
+    StmtT visitForStmt(ForStmt s)
+    {
+        visitStmt(s.init);
+        visitExp(s.cond);
+        visitExp(s.incre);
+        visitStmt(s.forBody);
+        static if (is(StmtT == void))
+            return;
+        else
+            return StmtT.init;
+    }
 
     StmtT visitSwitchStmt(SwitchStmt s)
     {
--- a/tests/parser/for_1.d	Mon Jul 21 21:30:44 2008 +0200
+++ b/tests/parser/for_1.d	Mon Jul 21 21:32:20 2008 +0200
@@ -1,8 +1,10 @@
 
 int main()
 {
+    int x = 0;
     for(int i = 0; i < 5; i = i + 1)
     {
+        x = x + i;
     }
-    return 0;
+    return x;
 }