[Avida-SVN] r1794 - in development: Avida.xcodeproj source/script source/targets/avida-s

brysonda at myxo.css.msu.edu brysonda at myxo.css.msu.edu
Thu Jul 12 11:53:26 PDT 2007


Author: brysonda
Date: 2007-07-12 14:53:26 -0400 (Thu, 12 Jul 2007)
New Revision: 1794

Added:
   development/source/script/cASTDumpVisitor.cc
   development/source/script/cASTDumpVisitor.h
Modified:
   development/Avida.xcodeproj/project.pbxproj
   development/source/script/ASTree.cc
   development/source/script/ASTree.h
   development/source/script/cASTVisitor.h
   development/source/script/cParser.cc
   development/source/script/cParser.h
   development/source/targets/avida-s/main.cc
Log:
AS: Add the beginnings of a basic cASTDumpVisitor that dumps the abstract syntax tree to cout.  Will properly dump parse basic arithmetic expressions, variable declarations, and assignments.

Modified: development/Avida.xcodeproj/project.pbxproj
===================================================================
--- development/Avida.xcodeproj/project.pbxproj	2007-07-12 17:23:06 UTC (rev 1793)
+++ development/Avida.xcodeproj/project.pbxproj	2007-07-12 18:53:26 UTC (rev 1794)
@@ -193,6 +193,7 @@
 		70B651B70BEA9AEC002472ED /* unit-tests in CopyFiles */ = {isa = PBXBuildFile; fileRef = 70B6514C0BEA6FAD002472ED /* unit-tests */; };
 		70D046610C32EA90000614E7 /* cBirthChamber.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = 702D4F3908DA61E2007BA469 /* cBirthChamber.h */; };
 		70DCAC9C097AF7C0002F8733 /* primitive.cc in Sources */ = {isa = PBXBuildFile; fileRef = 70DCAC9B097AF7C0002F8733 /* primitive.cc */; };
+		70F9FC100C469DD70083B788 /* cASTDumpVisitor.cc in Sources */ = {isa = PBXBuildFile; fileRef = 70F9FC0F0C469DC10083B788 /* cASTDumpVisitor.cc */; };
 /* End PBXBuildFile section */
 
 /* Begin PBXBuildRule section */
@@ -808,6 +809,8 @@
 		70F7DE710929678E009E311D /* cGenotype_BirthData.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = cGenotype_BirthData.h; sourceTree = "<group>"; };
 		70F7DE730929678E009E311D /* cGenotype_TestData.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = cGenotype_TestData.h; sourceTree = "<group>"; };
 		70F7DE76092967A8009E311D /* cGenotypeBatch.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = cGenotypeBatch.h; sourceTree = "<group>"; };
+		70F9FC0E0C469DC10083B788 /* cASTDumpVisitor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = cASTDumpVisitor.h; sourceTree = "<group>"; };
+		70F9FC0F0C469DC10083B788 /* cASTDumpVisitor.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = cASTDumpVisitor.cc; sourceTree = "<group>"; };
 		70FB86A908BFAFEC00BDF589 /* CMakeLists.txt */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = text; path = CMakeLists.txt; sourceTree = "<group>"; };
 		DCC30FCF0762539D008F7A48 /* Doxyfile */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = text; path = Doxyfile; sourceTree = "<group>"; };
 		DCC3109C0762539E008F7A48 /* avida.cc */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = avida.cc; sourceTree = "<group>"; };
@@ -1197,6 +1200,8 @@
 				702F52DE0992FD8000B2B507 /* cScriptObject.h */,
 				702F52DF0992FD8000B2B507 /* cScriptObject.cc */,
 				70E130E30C4551E900CE9249 /* cASTVisitor.h */,
+				70F9FC0E0C469DC10083B788 /* cASTDumpVisitor.h */,
+				70F9FC0F0C469DC10083B788 /* cASTDumpVisitor.cc */,
 			);
 			path = script;
 			sourceTree = "<group>";
@@ -1967,6 +1972,7 @@
 				7023ECDC0C0A44CC00362B9C /* cScriptObject.cc in Sources */,
 				7023ECDD0C0A44CE00362B9C /* cSymbolTable.cc in Sources */,
 				704368D60C31991500A05ABA /* ASTree.cc in Sources */,
+				70F9FC100C469DD70083B788 /* cASTDumpVisitor.cc in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};

Modified: development/source/script/ASTree.cc
===================================================================
--- development/source/script/ASTree.cc	2007-07-12 17:23:06 UTC (rev 1793)
+++ development/source/script/ASTree.cc	2007-07-12 18:53:26 UTC (rev 1794)
@@ -43,6 +43,7 @@
 
 void cASTFunctionCall::Accept(cASTVisitor& visitor) { visitor.visitFunctionCall(*this); }
 void cASTLiteral::Accept(cASTVisitor& visitor) { visitor.visitLiteral(*this); }
+void cASTLiteralArray::Accept(cASTVisitor& visitor) { visitor.visitLiteralArray(*this); }
 void cASTVariableReference::Accept(cASTVisitor& visitor) { visitor.visitVariableReference(*this); }
 
 

Modified: development/source/script/ASTree.h
===================================================================
--- development/source/script/ASTree.h	2007-07-12 17:23:06 UTC (rev 1793)
+++ development/source/script/ASTree.h	2007-07-12 18:53:26 UTC (rev 1794)
@@ -78,9 +78,13 @@
   
 public:
   cASTAssignment(const cString& var) : m_var(var), m_expr(NULL) { ; }
+  ~cASTAssignment() { delete m_expr; }
   
-  void SetExpression(cASTNode* expr) { delete m_expr; m_expr = expr; }
+  inline const cString& GetVariable() { return m_var; }
   
+  inline void SetExpression(cASTNode* expr) { delete m_expr; m_expr = expr; }
+  inline cASTNode* GetExpression() { return m_expr; }
+  
   void Accept(cASTVisitor& visitor);
 };
 
@@ -97,7 +101,8 @@
   cASTStatementList() { ; }
   ~cASTStatementList();
   
-  void AddNode(cASTNode* n) { m_nodes.PushRear(n); }
+  inline void AddNode(cASTNode* n) { m_nodes.PushRear(n); }
+  inline tListIterator<cASTNode> Iterator() { return tListIterator<cASTNode>(m_nodes); }
   
   void Accept(cASTVisitor& visitor);
 };
@@ -164,8 +169,12 @@
   
 public:
   cASTVariableDefinition(ASType_t type, const cString& var) : m_type(type), m_var(var), m_assign(NULL) { ; }
+  ~cASTVariableDefinition() { delete m_assign; }
   
-  void SetAssignment(cASTNode* assign) { delete m_assign; m_assign = assign; }
+  inline ASType_t GetType() { return m_type; }
+  inline const cString& GetVariable() { return m_var; }
+  inline void SetAssignmentExpression(cASTNode* assign) { delete m_assign; m_assign = assign; }
+  inline cASTNode* GetAssignmentExpression() { return m_assign; }
   
   void Accept(cASTVisitor& visitor);
 };
@@ -186,8 +195,11 @@
   cASTExpressionBinary(ASToken_t op, cASTNode* l, cASTNode* r) : m_op(op), m_left(l), m_right(r) { ; }
   ~cASTExpressionBinary() { delete m_left; delete m_right; }
   
-  void SetLeft(cASTNode* left) { m_left = left; }
-  void SetRight(cASTNode* right) { m_right = right; }
+  inline ASToken_t GetOperator() { return m_op; }
+  inline void SetLeft(cASTNode* left) { m_left = left; }
+  inline cASTNode* GetLeft() { return m_left; }
+  inline void SetRight(cASTNode* right) { m_right = right; }
+  inline cASTNode* GetRight() { return m_right; }
   
   void Accept(cASTVisitor& visitor);
 };
@@ -202,9 +214,11 @@
 public:
   cASTExpressionUnary(ASToken_t op, cASTNode* e) : m_op(op), m_expr(e) { ; }
   ~cASTExpressionUnary() { delete m_expr; }
+
+  inline ASToken_t GetOperator() { return m_op; }
+  inline void SetExpression(cASTNode* expr) { m_expr = expr; }
+  inline cASTNode* GetExpression() { return m_expr; }
   
-  void SetExpression(cASTNode* expr) { m_expr = expr; }
-  
   void Accept(cASTVisitor& visitor);
 };
 
@@ -226,15 +240,37 @@
 class cASTLiteral : public cASTNode
 {
 private:
-  ASToken_t m_type;
+  ASType_t m_type;
+  cString m_value;
   
 public:
-  cASTLiteral(ASToken_t t) : m_type(t) { ; }
+  cASTLiteral(ASType_t t, const cString& v) : m_type(t), m_value(v) { ; }
   
+  inline ASType_t GetType() { return m_type; }
+  inline const cString& GetValue() { return m_value; }
+  
   void Accept(cASTVisitor& visitor);
 };
 
 
+class cASTLiteralArray : public cASTNode
+{
+private:
+  ASType_t m_type;
+  cASTNode* m_value;
+  
+public:
+  cASTLiteralArray(ASType_t t, cASTNode* v) : m_type(t), m_value(v) { ; }
+  ~cASTLiteralArray() { delete m_value; }
+  
+  
+  inline ASType_t GetType() { return m_type; }
+  inline cASTNode* GetValue() { return m_value; }
+  
+  void Accept(cASTVisitor& visitor);
+};
+
+
 class cASTVariableReference : public cASTNode
 {
 private:

Added: development/source/script/cASTDumpVisitor.cc
===================================================================
--- development/source/script/cASTDumpVisitor.cc	                        (rev 0)
+++ development/source/script/cASTDumpVisitor.cc	2007-07-12 18:53:26 UTC (rev 1794)
@@ -0,0 +1,209 @@
+/*
+ *  cASTDumpVisitor.cc
+ *  Avida
+ *
+ *  Created by David on 7/12/07.
+ *  Copyright 2007 Michigan State University. All rights reserved.
+ *
+ *
+ *  This program is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU General Public License
+ *  as published by the Free Software Foundation; version 2
+ *  of the License.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ */
+
+#include "cASTDumpVisitor.h"
+
+#include <iostream>
+
+using namespace std;
+
+
+inline void cASTDumpVisitor::indent()
+{
+  for (int i = 0; i < m_depth; i++) cout << "  ";
+}
+
+void cASTDumpVisitor::printToken(ASToken_t token)
+{
+  switch (token) {
+    case AS_TOKEN_OP_BIT_NOT:   cout << '~';  break;
+    case AS_TOKEN_OP_BIT_AND:   cout << '&';  break;
+    case AS_TOKEN_OP_BIT_OR:    cout << '|';  break;
+    case AS_TOKEN_OP_LOGIC_NOT: cout << '!';  break;
+    case AS_TOKEN_OP_LOGIC_AND: cout << "&&"; break;
+    case AS_TOKEN_OP_LOGIC_OR:  cout << "||"; break;
+    case AS_TOKEN_OP_ADD:       cout << '+';  break;
+    case AS_TOKEN_OP_SUB:       cout << '-';  break;
+    case AS_TOKEN_OP_MUL:       cout << '*';  break;
+    case AS_TOKEN_OP_DIV:       cout << '/';  break;
+    case AS_TOKEN_OP_MOD:       cout << '%';  break;
+    case AS_TOKEN_OP_EQ:        cout << "=="; break;
+    case AS_TOKEN_OP_LE:        cout << "<="; break;
+    case AS_TOKEN_OP_GE:        cout << ">="; break;
+    case AS_TOKEN_OP_LT:        cout << '<';  break;
+    case AS_TOKEN_OP_GT:        cout << '>';  break;
+    case AS_TOKEN_OP_NEQ:       cout << "!="; break;
+    case AS_TOKEN_ARR_RANGE:    cout << ':';  break;
+    case AS_TOKEN_ARR_EXPAN:    cout << '^';  break;
+    default:                    cout << '?';  break;
+  }
+}
+
+const char* cASTDumpVisitor::mapType(ASType_t type)
+{
+  switch (type) {
+    case AS_TYPE_ARRAY:       return "array";
+    case AS_TYPE_CHAR:        return "char";
+    case AS_TYPE_FLOAT:       return "float";
+    case AS_TYPE_INT:         return "int";
+    case AS_TYPE_MATRIX:      return "matrix";
+    case AS_TYPE_STRING:      return "string";
+    case AS_TYPE_VOID:        return "void";
+    case AS_TYPE_OBJECT_REF:  return "object";
+    
+    case AS_TYPE_INVALID:
+    default:
+      return "*INVALID*";
+  }
+}
+
+
+void cASTDumpVisitor::visitAssignment(cASTAssignment& node)
+{
+  m_depth++;
+  indent();
+  cout << node.GetVariable() << endl;
+  m_depth--;
+  
+  indent();
+  cout << "=" << endl;
+
+  m_depth++;
+  node.GetExpression()->Accept(*this);
+  m_depth--;
+}
+
+void cASTDumpVisitor::visitStatementList(cASTStatementList& node)
+{
+  tListIterator<cASTNode> it(node.Iterator());
+  
+  indent();
+  cout << "stmtlist:" << endl;
+
+  m_depth++;
+  cASTNode* stmt = NULL;
+  while ((stmt = it.Next())) {
+    stmt->Accept(*this);
+  }
+  m_depth--;
+}
+
+
+
+void cASTDumpVisitor::visitForeachBlock(cASTForeachBlock& node)
+{
+  
+}
+
+
+void cASTDumpVisitor::visitIfBlock(cASTIfBlock& node)
+{
+  
+}
+
+
+void cASTDumpVisitor::visitWhileBlock(cASTWhileBlock& node)
+{
+  
+}
+
+
+
+void cASTDumpVisitor::visitFunctionDefinition(cASTFunctionDefinition& node)
+{
+  
+}
+
+
+void cASTDumpVisitor::visitVariableDefinition(cASTVariableDefinition& node)
+{
+  indent();
+  cout << mapType(node.GetType()) << " " << node.GetVariable() << endl;
+  
+  if (node.GetAssignmentExpression()) {
+    m_depth++;
+    indent();
+    cout << "=" << endl;
+    
+    m_depth++;
+    node.GetAssignmentExpression()->Accept(*this);
+
+    m_depth -= 2;
+  }  
+}
+
+
+
+void cASTDumpVisitor::visitExpressionBinary(cASTExpressionBinary& node)
+{
+  m_depth++;
+  node.GetLeft()->Accept(*this);
+  m_depth--;
+  
+  indent();
+  printToken(node.GetOperator());
+  cout << endl;
+  
+  m_depth++;
+  node.GetRight()->Accept(*this);
+  m_depth--;  
+}
+
+
+void cASTDumpVisitor::visitExpressionUnary(cASTExpressionUnary& node)
+{
+  indent();
+  printToken(node.GetOperator());
+  cout << endl;
+  
+  m_depth++;
+  node.GetExpression()->Accept(*this);
+  m_depth--;
+}
+
+
+
+void cASTDumpVisitor::visitFunctionCall(cASTFunctionCall& node)
+{
+  
+}
+
+
+void cASTDumpVisitor::visitLiteral(cASTLiteral& node)
+{
+  indent();
+  cout << "(" << mapType(node.GetType()) << ") " << node.GetValue() << endl;
+}
+
+
+void cASTDumpVisitor::visitLiteralArray(cASTLiteralArray& node)
+{
+
+}
+
+
+void cASTDumpVisitor::visitVariableReference(cASTVariableReference& node)
+{
+  
+}

Added: development/source/script/cASTDumpVisitor.h
===================================================================
--- development/source/script/cASTDumpVisitor.h	                        (rev 0)
+++ development/source/script/cASTDumpVisitor.h	2007-07-12 18:53:26 UTC (rev 1794)
@@ -0,0 +1,66 @@
+/*
+ *  cASTDumpVisitor.h
+ *  Avida
+ *
+ *  Created by David on 7/12/07.
+ *  Copyright 2007 Michigan State University. All rights reserved.
+ *
+ *
+ *  This program is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU General Public License
+ *  as published by the Free Software Foundation; version 2
+ *  of the License.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ */
+
+#ifndef cASTDumpVisitor_h
+#define cASTDumpVisitor_h
+
+#ifndef cASTVisitor_h
+#include "cASTVisitor.h"
+#endif
+
+
+class cASTDumpVisitor : public cASTVisitor
+{
+private:
+  int m_depth;
+  
+public:
+  cASTDumpVisitor() : m_depth(0) { ; }
+  
+  void visitAssignment(cASTAssignment&);
+  
+  void visitStatementList(cASTStatementList&);
+  
+  void visitForeachBlock(cASTForeachBlock&);
+  void visitIfBlock(cASTIfBlock&);
+  void visitWhileBlock(cASTWhileBlock&);
+  
+  void visitFunctionDefinition(cASTFunctionDefinition&);
+  void visitVariableDefinition(cASTVariableDefinition&);
+  
+  void visitExpressionBinary(cASTExpressionBinary&);
+  void visitExpressionUnary(cASTExpressionUnary&);
+  
+  void visitFunctionCall(cASTFunctionCall&);
+  void visitLiteral(cASTLiteral&);
+  void visitLiteralArray(cASTLiteralArray&);
+  void visitVariableReference(cASTVariableReference&);
+
+private:
+  inline void indent();
+  void printToken(ASToken_t token);
+  const char* mapType(ASType_t type);
+};
+
+#endif

Modified: development/source/script/cASTVisitor.h
===================================================================
--- development/source/script/cASTVisitor.h	2007-07-12 17:23:06 UTC (rev 1793)
+++ development/source/script/cASTVisitor.h	2007-07-12 18:53:26 UTC (rev 1794)
@@ -53,6 +53,7 @@
 
   virtual void visitFunctionCall(cASTFunctionCall&) = 0;
   virtual void visitLiteral(cASTLiteral&) = 0;
+  virtual void visitLiteralArray(cASTLiteralArray&) = 0;
   virtual void visitVariableReference(cASTVariableReference&) = 0;
 };
 

Modified: development/source/script/cParser.cc
===================================================================
--- development/source/script/cParser.cc	2007-07-12 17:23:06 UTC (rev 1793)
+++ development/source/script/cParser.cc	2007-07-12 18:53:26 UTC (rev 1794)
@@ -234,10 +234,6 @@
 }
 
 
-void cParser::Accept(cASTVisitor& visitor)
-{
-  
-}
 
 ASToken_t cParser::nextToken()
 {
@@ -573,10 +569,16 @@
   
   switch (currentToken()) {
     case TOKEN(FLOAT):
+      expr = new cASTLiteral(AS_TYPE_FLOAT, currentText());
+      break;
     case TOKEN(INT):
+      expr = new cASTLiteral(AS_TYPE_INT, currentText());
+      break;
     case TOKEN(CHAR):
+      expr = new cASTLiteral(AS_TYPE_CHAR, currentText());
+      break;
     case TOKEN(STRING):
-      expr = new cASTLiteral(currentToken());
+      expr = new cASTLiteral(AS_TYPE_STRING, currentText());
       break;
     case TOKEN(ID):
       if (peekToken() == TOKEN(PREC_OPEN)) {
@@ -611,6 +613,7 @@
         PARSE_UNEXPECT();
         return expr;
       }
+      // @todo - return literal array
       break;
       
     case TOKEN(OP_BIT_NOT):
@@ -1006,7 +1009,7 @@
     case TOKEN(ASSIGN):
       nextToken();
       cASTNode* expr = parseExpression();
-      vd->SetAssignment(expr);
+      vd->SetAssignmentExpression(expr);
       break;
     case TOKEN(PREC_OPEN):
       // @todo - array/matrix size declaration

Modified: development/source/script/cParser.h
===================================================================
--- development/source/script/cParser.h	2007-07-12 17:23:06 UTC (rev 1793)
+++ development/source/script/cParser.h	2007-07-12 18:53:26 UTC (rev 1794)
@@ -79,7 +79,7 @@
   ~cParser();
   
   bool Parse(cFile& input);
-  void Accept(cASTVisitor& visitor);
+  inline void Accept(cASTVisitor& visitor) { if (m_tree) m_tree->Accept(visitor); }
   
   
 private:

Modified: development/source/targets/avida-s/main.cc
===================================================================
--- development/source/targets/avida-s/main.cc	2007-07-12 17:23:06 UTC (rev 1793)
+++ development/source/targets/avida-s/main.cc	2007-07-12 18:53:26 UTC (rev 1794)
@@ -24,6 +24,7 @@
 
 #include "avida.h"
 #include "cASLibrary.h"
+#include "cASTDumpVisitor.h"
 #include "cFile.h"
 #include "cParser.h"
 #include "PlatformExpert.h"
@@ -41,7 +42,12 @@
   cFile file;
   if (file.Open("main.asl")) {
     if (parser->Parse(file)) {
-      std::cout << "Parse Successful" << std::endl;
+      std::cout << "Parse Successful\n" << std::endl;
+
+      cASTDumpVisitor visitor;
+      parser->Accept(visitor);
+      
+      std::cout << std::endl;
       ExitAvida(0);
     } else {
       std::cout << "Parse Failed" << std::endl;




More information about the Avida-cvs mailing list