# HG changeset patch # User Anders Halager # Date 1216668740 -7200 # Node ID ee202c72cd30bbdb5363da4470c0ccc59976915b # Parent 893f23a9de93013c58b577b0481c002129edccd5# Parent 393a1f47a6d2e78d56343c318cef1aefe4b36d03 Merge diff -r 893f23a9de93 -r ee202c72cd30 ast/Stmt.d --- 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) diff -r 893f23a9de93 -r ee202c72cd30 parser/Action.d --- 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; } diff -r 893f23a9de93 -r ee202c72cd30 sema/AstAction.d --- 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; diff -r 893f23a9de93 -r ee202c72cd30 sema/ScopeBuilder.d --- 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(); diff -r 893f23a9de93 -r ee202c72cd30 sema/Visitor.d --- 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) { diff -r 893f23a9de93 -r ee202c72cd30 tests/parser/for_1.d --- 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; }