changeset 166:56334292a29e

- Implemented parseForStatement(). - Fix: checked for wrong parentheses in parseWhile- and DoWhileStatement().
author aziz
date Fri, 13 Jul 2007 23:23:05 +0000
parents 797074d143d7
children 141b908e32b6
files trunk/src/Parser.d trunk/src/Statements.d
diffstat 2 files changed, 72 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/trunk/src/Parser.d	Fri Jul 13 23:02:01 2007 +0000
+++ b/trunk/src/Parser.d	Fri Jul 13 23:23:05 2007 +0000
@@ -1176,6 +1176,31 @@
     return new ScopeStatement(s);
   }
 
+  /+
+    NoScopeStatement:
+        NonEmptyStatement
+        BlockStatement
+    BlockStatement:
+        { }
+        { StatementList }
+  +/
+  Statement parseNoScopeStatement()
+  {
+    Statement s;
+    if (token.type == T.LBrace)
+    {
+      nT();
+      auto ss = new Statements();
+      while (token.type != T.RBrace && token.type != T.EOF)
+        ss ~= parseStatement();
+      require(T.RBrace);
+      s = ss;
+    }
+    else
+      s = parseStatement();
+    return s;
+  }
+
   Statement parseIfStatement()
   {
     assert(token.type == T.If);
@@ -1224,9 +1249,9 @@
   {
     assert(token.type == T.While);
     nT();
-    require(T.RParen);
+    require(T.LParen);
     auto condition = parseExpression();
-    require(T.LParen);
+    require(T.RParen);
     return new WhileStatement(condition, parseScopeStatement());
   }
 
@@ -1236,10 +1261,43 @@
     nT();
     auto doBody = parseScopeStatement();
     require(T.While);
-    require(T.RParen);
+    require(T.LParen);
     auto condition = parseExpression();
+    require(T.RParen);
+    return new DoWhileStatement(condition, doBody);
+  }
+
+  Statement parseForStatement()
+  {
+    assert(token.type == T.For);
+    nT();
     require(T.LParen);
-    return new DoWhileStatement(condition, doBody);
+
+    Statement init, forBody;
+    Expression condition, increment;
+
+    if (token.type != T.Semicolon)
+    {
+      init = parseNoScopeStatement();
+      require(T.Semicolon);
+    }
+    else
+      nT();
+    if (token.type != T.Semicolon)
+    {
+      condition = parseExpression();
+      require(T.Semicolon);
+    }
+    else
+      nT();
+    if (token.type != T.RParen)
+    {
+      increment = parseExpression();
+      require(T.RParen);
+    }
+    else
+      nT();
+    return new ForStatement(init, condition, increment, forBody);
   }
 
   /+++++++++++++++++++++++++++++
--- a/trunk/src/Statements.d	Fri Jul 13 23:02:01 2007 +0000
+++ b/trunk/src/Statements.d	Fri Jul 13 23:23:05 2007 +0000
@@ -96,7 +96,16 @@
 
 class ForStatement : Statement
 {
-
+  Statement init;
+  Expression condition, increment;
+  Statement forBody;
+  this(Statement init, Expression condition, Expression increment, Statement forBody)
+  {
+    this.init = init;
+    this.condition = condition;
+    this.increment = increment;
+    this.forBody = forBody;
+  }
 }
 
 class ForeachStatement : Statement