[Avida-SVN] r1821 - development/source/script

brysonda at myxo.css.msu.edu brysonda at myxo.css.msu.edu
Wed Jul 18 20:20:43 PDT 2007


Author: brysonda
Date: 2007-07-18 23:20:43 -0400 (Wed, 18 Jul 2007)
New Revision: 1821

Modified:
   development/source/script/ASTree.h
   development/source/script/cASTDumpVisitor.cc
   development/source/script/cParser.cc
Log:
AS: ForeachBlock nodes.

Modified: development/source/script/ASTree.h
===================================================================
--- development/source/script/ASTree.h	2007-07-18 21:55:48 UTC (rev 1820)
+++ development/source/script/ASTree.h	2007-07-19 03:20:43 UTC (rev 1821)
@@ -67,7 +67,28 @@
 // -- Concrete Abstract Syntax Tree Nodes
 // ---------------------------------------------------------------------------------------------------------------------
 
+class cASTAssignment;
 
+class cASTReturnStatement;
+class cASTStatementList;
+
+class cASTForeachBlock;
+class cASTIfBlock;
+class cASTWhileBlock;
+
+class cASTFunctionDefinition;
+class cASTVariableDefinition;
+
+class cASTExpressionBinary;
+class cASTExpressionUnary;
+
+class cASTFunctionCall;
+class cASTLiteral;
+class cASTLiteralArray;
+class cASTVariableReference;
+
+
+
 // --------  Assignment Nodes  --------
 
 class cASTAssignment : public cASTNode
@@ -130,10 +151,17 @@
 class cASTForeachBlock : public cASTNode
 {
 private:
+  cASTVariableDefinition* m_var;
+  cASTNode* m_expr;
+  cASTNode* m_code;
   
 public:
-  cASTForeachBlock() { ; }
+  cASTForeachBlock(cASTVariableDefinition* v, cASTNode* e, cASTNode* c) : m_var(v), m_expr(e), m_code(c) { ; }
   
+  inline cASTVariableDefinition* GetVariable() { return m_var; }
+  inline cASTNode* GetValues() { return m_expr; }
+  inline cASTNode* GetCode() { return m_code; }
+  
   void Accept(cASTVisitor& visitor);
 };
 

Modified: development/source/script/cASTDumpVisitor.cc
===================================================================
--- development/source/script/cASTDumpVisitor.cc	2007-07-18 21:55:48 UTC (rev 1820)
+++ development/source/script/cASTDumpVisitor.cc	2007-07-19 03:20:43 UTC (rev 1821)
@@ -126,7 +126,26 @@
 
 void cASTDumpVisitor::visitForeachBlock(cASTForeachBlock& node)
 {
+  indent();
+  cout << "foreach:" << endl;
   
+  m_depth++;
+  node.GetVariable()->Accept(*this);
+  
+  indent();
+  cout << "values:" << endl;
+  m_depth++;
+  node.GetValues()->Accept(*this);
+  
+  m_depth--;
+  indent();
+  cout << "code:" << endl;
+
+  m_depth++;
+  node.GetCode()->Accept(*this);
+  m_depth--;
+  
+  m_depth--;
 }
 
 

Modified: development/source/script/cParser.cc
===================================================================
--- development/source/script/cParser.cc	2007-07-18 21:55:48 UTC (rev 1820)
+++ development/source/script/cParser.cc	2007-07-19 03:20:43 UTC (rev 1821)
@@ -674,47 +674,57 @@
 cASTNode* cParser::parseForeachStatement()
 {
   PARSE_TRACE("parseForeachStatement");
-  cASTNode* fs = NULL;
   
+  ASType_t type = AS_TYPE_INVALID;
   switch (nextToken()) {
-    case TOKEN(TYPE_ARRAY):
-    case TOKEN(TYPE_CHAR):
-    case TOKEN(TYPE_FLOAT):
-    case TOKEN(TYPE_INT):
-    case TOKEN(TYPE_MATRIX):
-    case TOKEN(TYPE_STRING):
-      break;
+    case TOKEN(TYPE_ARRAY):  type = AS_TYPE_ARRAY;  break;
+    case TOKEN(TYPE_CHAR):   type = AS_TYPE_CHAR;   break;
+    case TOKEN(TYPE_FLOAT):  type = AS_TYPE_FLOAT;  break;
+    case TOKEN(TYPE_INT):    type = AS_TYPE_INT;    break;
+    case TOKEN(TYPE_MATRIX): type = AS_TYPE_MATRIX; break;
+    case TOKEN(TYPE_STRING): type = AS_TYPE_STRING; break;
+    case TOKEN(TYPE_VOID):   type = AS_TYPE_VOID;   break;
     case TOKEN(ID):
-      if (nextToken() != TOKEN(REF)) {
+      if (peekToken() != TOKEN(REF)) {
+        nextToken();
         PARSE_UNEXPECT();
-        return fs;
+        return NULL;
       }
+      type = AS_TYPE_OBJECT_REF;
       break;
       
     default:
       PARSE_UNEXPECT();
-      return fs;
+      return NULL;
   }
   
-  if (nextToken() != TOKEN(PREC_OPEN)) {
+  if (nextToken() != TOKEN(ID)) {
     PARSE_UNEXPECT();
-    return fs;
+    return NULL;
   }
   
-  nextToken();
-  parseExpression();
+  tAutoRelease<cASTVariableDefinition> var(new cASTVariableDefinition(type, currentText()));
   
+  if (nextToken() != TOKEN(PREC_OPEN)) {
+    PARSE_UNEXPECT();
+    return NULL;
+  }  
+  nextToken(); // consume '('
+  
+  tAutoRelease<cASTNode> expr(parseExpression());
+  
   if (currentToken() != TOKEN(PREC_CLOSE)) {
     PARSE_UNEXPECT();
-    return fs;
+    return NULL;
   }
+  nextToken(); // consume ')'
   
   bool loose = false;
-  parseCodeBlock(loose);
+  tAutoRelease<cASTNode> code(parseCodeBlock(loose));
   if (!loose && currentToken() != TOKEN(CMD_ENDFOREACH)) PARSE_UNEXPECT();
-  if (!loose) nextToken();
+  if (!loose) nextToken(); // consume 'endforeach'
   
-  return fs;
+  return new cASTForeachBlock(var.Release(), expr.Release(), code.Release());
 }
 
 cASTNode* cParser::parseFunctionDefine()




More information about the Avida-cvs mailing list