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

brysonda at myxo.css.msu.edu brysonda at myxo.css.msu.edu
Sun Mar 9 18:59:31 PDT 2008


Author: brysonda
Date: 2008-03-09 21:59:31 -0400 (Sun, 09 Mar 2008)
New Revision: 2443

Modified:
   development/source/script/ASTree.cc
   development/source/script/ASTree.h
   development/source/script/AvidaScript.h
   development/source/script/cASTVisitor.h
   development/source/script/cDirectInterpretASTVisitor.cc
   development/source/script/cDirectInterpretASTVisitor.h
   development/source/script/cDumpASTVisitor.cc
   development/source/script/cDumpASTVisitor.h
   development/source/script/cParser.cc
   development/source/script/cSemanticASTVisitor.cc
   development/source/script/cSemanticASTVisitor.h
Log:
AS:
Add basic parse and semantic support for object assignment expressions (including array indexed assignments).

Modified: development/source/script/ASTree.cc
===================================================================
--- development/source/script/ASTree.cc	2008-03-09 20:41:07 UTC (rev 2442)
+++ development/source/script/ASTree.cc	2008-03-10 01:59:31 UTC (rev 2443)
@@ -28,7 +28,10 @@
 
 
 void cASTAssignment::Accept(cASTVisitor& visitor) { visitor.VisitAssignment(*this); }
+void cASTArgumentList::Accept(cASTVisitor& visitor) { visitor.VisitArgumentList(*this); }
+void cASTObjectAssignment::Accept(cASTVisitor& visitor) { visitor.VisitObjectAssignment(*this); }
 
+
 void cASTReturnStatement::Accept(cASTVisitor& visitor) { visitor.VisitReturnStatement(*this); }
 void cASTStatementList::Accept(cASTVisitor& visitor) { visitor.VisitStatementList(*this); }
 
@@ -43,7 +46,6 @@
 void cASTExpressionBinary::Accept(cASTVisitor& visitor) { visitor.VisitExpressionBinary(*this); }
 void cASTExpressionUnary::Accept(cASTVisitor& visitor) { visitor.VisitExpressionUnary(*this); }
 
-void cASTArgumentList::Accept(cASTVisitor& visitor) { visitor.VisitArgumentList(*this); }
 void cASTFunctionCall::Accept(cASTVisitor& visitor) { visitor.VisitFunctionCall(*this); }
 void cASTLiteral::Accept(cASTVisitor& visitor) { visitor.VisitLiteral(*this); }
 void cASTLiteralArray::Accept(cASTVisitor& visitor) { visitor.VisitLiteralArray(*this); }

Modified: development/source/script/ASTree.h
===================================================================
--- development/source/script/ASTree.h	2008-03-09 20:41:07 UTC (rev 2442)
+++ development/source/script/ASTree.h	2008-03-10 01:59:31 UTC (rev 2443)
@@ -95,6 +95,7 @@
 
 class cASTAssignment;
 class cASTArgumentList;
+class cASTObjectAssignment;
 
 class cASTReturnStatement;
 class cASTStatementList;
@@ -165,8 +166,28 @@
 };
 
 
+class cASTObjectAssignment : public cASTNode
+{
+private:
+  cASTNode* m_trgt;
+  cASTNode* m_expr;
 
+public:
+  cASTObjectAssignment(const cASFilePosition& fp, cASTNode* trgt)
+    : cASTNode(fp), m_trgt(trgt), m_expr(NULL) { ; }
+  ~cASTObjectAssignment() { delete m_expr; }
+  
+  inline cASTNode* GetTarget() { return m_trgt; }
 
+  inline void SetExpression(cASTNode* expr) { delete m_expr; m_expr = expr; }
+  inline cASTNode* GetExpression() { return m_expr; }
+  
+  void Accept(cASTVisitor& visitor);
+};
+
+
+
+
 // --------  Block Nodes  --------
 
 class cASTReturnStatement : public cASTNode
@@ -530,7 +551,6 @@
 private:
   cASTNode* m_object;
   cString m_name;
-  ASType_t m_type;
   
 public:
   cASTObjectReference(const cASFilePosition& fp, cASTNode* object, const cString& name)
@@ -540,8 +560,7 @@
   cASTNode* GetObject() { return m_object; }
   inline const cString& GetName() { return m_name; }
   
-  ASType_t GetType() const { return m_type; }
-  inline void SetType(ASType_t type) { m_type = type; }
+  ASType_t GetType() const { return AS_TYPE_OBJECT_REF; }
   
   void Accept(cASTVisitor& visitor);
 };

Modified: development/source/script/AvidaScript.h
===================================================================
--- development/source/script/AvidaScript.h	2008-03-09 20:41:07 UTC (rev 2442)
+++ development/source/script/AvidaScript.h	2008-03-10 01:59:31 UTC (rev 2443)
@@ -129,6 +129,7 @@
   AS_SEMANTIC_ERR_FUNCTION_SIGNATURE_MISMATCH,
   AS_SEMANTIC_ERR_FUNCTION_UNDECLARED,
   AS_SEMANTIC_ERR_FUNCTION_UNDEFINED,
+  AS_SEMANTIC_ERR_INVALID_ASSIGNMENT_TARGET,
   AS_SEMANTIC_ERR_TOO_MANY_ARGUMENTS,
   AS_SEMANTIC_ERR_UNDEFINED_TYPE_OP,
   AS_SEMANTIC_ERR_UNPACK_WILD_NONARRAY,

Modified: development/source/script/cASTVisitor.h
===================================================================
--- development/source/script/cASTVisitor.h	2008-03-09 20:41:07 UTC (rev 2442)
+++ development/source/script/cASTVisitor.h	2008-03-10 01:59:31 UTC (rev 2443)
@@ -36,6 +36,8 @@
 
   
   virtual void VisitAssignment(cASTAssignment&) = 0;
+  virtual void VisitArgumentList(cASTArgumentList&) = 0;
+  virtual void VisitObjectAssignment(cASTObjectAssignment&) = 0;  
   
   virtual void VisitReturnStatement(cASTReturnStatement&) = 0;
   virtual void VisitStatementList(cASTStatementList&) = 0;
@@ -51,7 +53,6 @@
   virtual void VisitExpressionBinary(cASTExpressionBinary&) = 0;
   virtual void VisitExpressionUnary(cASTExpressionUnary&) = 0;
 
-  virtual void VisitArgumentList(cASTArgumentList&) = 0;
   virtual void VisitFunctionCall(cASTFunctionCall&) = 0;
   virtual void VisitLiteral(cASTLiteral&) = 0;
   virtual void VisitLiteralArray(cASTLiteralArray&) = 0;

Modified: development/source/script/cDirectInterpretASTVisitor.cc
===================================================================
--- development/source/script/cDirectInterpretASTVisitor.cc	2008-03-09 20:41:07 UTC (rev 2442)
+++ development/source/script/cDirectInterpretASTVisitor.cc	2008-03-10 01:59:31 UTC (rev 2443)
@@ -96,6 +96,21 @@
 }
 
 
+void cDirectInterpretASTVisitor::VisitArgumentList(cASTArgumentList& node)
+{
+  // Should never recurse into here.  Argument lists are processed by their owners as needed.
+  INTERPRET_ERROR(INTERNAL);
+}
+
+
+void cDirectInterpretASTVisitor::VisitObjectAssignment(cASTObjectAssignment& node)
+{
+  // @TODO - handle object assignment
+  INTERPRET_ERROR(INTERNAL);
+}
+
+
+
 void cDirectInterpretASTVisitor::VisitReturnStatement(cASTReturnStatement& node)
 {
   node.GetExpression()->Accept(*this);
@@ -600,7 +615,7 @@
       }
       break;
 
-    case TOKEN(IDX_OPEN):
+    case TOKEN(IDX_OPEN): // @TODO - handle indexing
       break;
       
     default:
@@ -657,12 +672,6 @@
 }
 
 
-void cDirectInterpretASTVisitor::VisitArgumentList(cASTArgumentList& node)
-{
-  // Should never recurse into here.  Argument lists are processed by their owners as needed.
-  INTERPRET_ERROR(INTERNAL);
-}
-
 void cDirectInterpretASTVisitor::VisitFunctionCall(cASTFunctionCall& node)
 {
   // Save previous scope information
@@ -896,9 +905,22 @@
   switch (type) {
     case TYPE(ARRAY):
       return value.as_array;
+
     case TYPE(MATRIX): // @TODO - asArray 
-    case TYPE(STRING): // @TODO - asArray
       INTERPRET_ERROR(INTERNAL);
+    
+    case TYPE(STRING):
+      {
+        cString* str = value.as_string;
+        cLocalArray* arr = new cLocalArray(str->GetSize());
+        for (int i = 0; i < str->GetSize(); i++) {
+          uAnyType val;
+          val.as_char = (*str)[i];
+          arr->Set(i, TYPE(CHAR), val);
+        }
+        delete str;
+        return arr;
+      }
       
     default:
       INTERPRET_ERROR(TYPE_CAST, mapType(type), mapType(TYPE(CHAR)));

Modified: development/source/script/cDirectInterpretASTVisitor.h
===================================================================
--- development/source/script/cDirectInterpretASTVisitor.h	2008-03-09 20:41:07 UTC (rev 2442)
+++ development/source/script/cDirectInterpretASTVisitor.h	2008-03-10 01:59:31 UTC (rev 2443)
@@ -73,6 +73,8 @@
   int Interpret(cASTNode* node);
   
   void VisitAssignment(cASTAssignment&);
+  void VisitObjectAssignment(cASTObjectAssignment&);
+  void VisitArgumentList(cASTArgumentList&);
   
   void VisitReturnStatement(cASTReturnStatement&);
   void VisitStatementList(cASTStatementList&);
@@ -88,7 +90,6 @@
   void VisitExpressionBinary(cASTExpressionBinary&);
   void VisitExpressionUnary(cASTExpressionUnary&);
   
-  void VisitArgumentList(cASTArgumentList&);
   void VisitFunctionCall(cASTFunctionCall&);
   void VisitLiteral(cASTLiteral&);
   void VisitLiteralArray(cASTLiteralArray&);

Modified: development/source/script/cDumpASTVisitor.cc
===================================================================
--- development/source/script/cDumpASTVisitor.cc	2008-03-09 20:41:07 UTC (rev 2442)
+++ development/source/script/cDumpASTVisitor.cc	2008-03-10 01:59:31 UTC (rev 2443)
@@ -57,6 +57,31 @@
 }
 
 
+void cDumpASTVisitor::VisitArgumentList(cASTArgumentList& node)
+{
+  m_depth++;
+  
+  tListIterator<cASTNode> it = node.Iterator();
+  cASTNode* val = NULL;
+  while ((val = it.Next())) val->Accept(*this);
+  
+  m_depth--;
+}
+
+void cDumpASTVisitor::VisitObjectAssignment(cASTObjectAssignment& node)
+{
+  m_depth++;
+  node.GetTarget()->Accept(*this);
+  m_depth--;
+  
+  indent();
+  cout << "=" << endl;
+  
+  m_depth++;
+  node.GetExpression()->Accept(*this);
+  m_depth--;
+}
+
 void cDumpASTVisitor::VisitReturnStatement(cASTReturnStatement& node)
 {
   indent();
@@ -285,17 +310,6 @@
 }
 
 
-void cDumpASTVisitor::VisitArgumentList(cASTArgumentList& node)
-{
-  m_depth++;
-  
-  tListIterator<cASTNode> it = node.Iterator();
-  cASTNode* val = NULL;
-  while ((val = it.Next())) val->Accept(*this);
-  
-  m_depth--;
-}
-
 void cDumpASTVisitor::VisitFunctionCall(cASTFunctionCall& node)
 {
   indent();

Modified: development/source/script/cDumpASTVisitor.h
===================================================================
--- development/source/script/cDumpASTVisitor.h	2008-03-09 20:41:07 UTC (rev 2442)
+++ development/source/script/cDumpASTVisitor.h	2008-03-10 01:59:31 UTC (rev 2443)
@@ -37,6 +37,8 @@
   cDumpASTVisitor();
   
   void VisitAssignment(cASTAssignment&);
+  void VisitArgumentList(cASTArgumentList&);
+  void VisitObjectAssignment(cASTObjectAssignment&);
   
   void VisitReturnStatement(cASTReturnStatement&);
   void VisitStatementList(cASTStatementList&);
@@ -52,7 +54,6 @@
   void VisitExpressionBinary(cASTExpressionBinary&);
   void VisitExpressionUnary(cASTExpressionUnary&);
   
-  void VisitArgumentList(cASTArgumentList&);
   void VisitFunctionCall(cASTFunctionCall&);
   void VisitLiteral(cASTLiteral&);
   void VisitLiteralArray(cASTLiteralArray&);

Modified: development/source/script/cParser.cc
===================================================================
--- development/source/script/cParser.cc	2008-03-09 20:41:07 UTC (rev 2442)
+++ development/source/script/cParser.cc	2008-03-10 01:59:31 UTC (rev 2443)
@@ -331,9 +331,9 @@
   PARSE_TRACE("parseAssignment");
   cASTAssignment* an = new cASTAssignment(FILEPOS, currentText());
   
+  nextToken(); // consume id
+
   nextToken(); // consume '='
-
-  nextToken();
   cASTNode* expr = parseExpression();
   an->SetExpression(expr);
 
@@ -363,6 +363,14 @@
         if (currentToken() != TOKEN(IDX_OPEN) && currentToken() != TOKEN(DOT)) eoe = true;
       } else {
         ce.Set(new cASTObjectReference(FILEPOS, ce.Release(), name));
+        
+        if (required && currentToken() == TOKEN(ASSIGN)) {
+          cASTObjectAssignment* oa = new cASTObjectAssignment(FILEPOS, ce.Release());
+          ce.Set(oa);
+          nextToken(); // consume '='
+          oa->SetExpression(parseExpression());
+          eoe = true;
+        }
       }
     } else if (currentToken() == TOKEN(IDX_OPEN)) {
       do {
@@ -370,6 +378,14 @@
         ce.Set(new cASTExpressionBinary(FILEPOS, TOKEN(IDX_OPEN), ce.Release(), parseExpression()));
         if (currentToken() != TOKEN(IDX_CLOSE)) PARSE_UNEXPECT();
       } while (nextToken() == TOKEN(IDX_OPEN));
+      
+      if (required && currentToken() == TOKEN(ASSIGN)) {
+        cASTObjectAssignment* oa = new cASTObjectAssignment(FILEPOS, ce.Release());
+        ce.Set(oa);
+        nextToken(); // consume '='
+        oa->SetExpression(parseExpression());
+        eoe = true;
+      }      
     } else {
       if (required) { PARSE_UNEXPECT(); }
       else eoe = true;
@@ -769,7 +785,6 @@
     case TOKEN(IDX_OPEN):
       cASTNode* target = new cASTVariableReference(FILEPOS, currentText());
       nextToken(); // consume id
-      // @TODO - handle call expression assignments
       return parseCallExpression(target, true);
       break;
     case TOKEN(REF):

Modified: development/source/script/cSemanticASTVisitor.cc
===================================================================
--- development/source/script/cSemanticASTVisitor.cc	2008-03-09 20:41:07 UTC (rev 2442)
+++ development/source/script/cSemanticASTVisitor.cc	2008-03-10 01:59:31 UTC (rev 2443)
@@ -72,7 +72,7 @@
 
 cSemanticASTVisitor::cSemanticASTVisitor(cASLibrary* lib, cSymbolTable* global_symtbl, cASTNode* main)
   : m_library(lib), m_global_symtbl(global_symtbl), m_parent_scope(global_symtbl), m_fun_id(0), m_cur_symtbl(global_symtbl)
-  , m_success(true), m_fun_def(false), m_top_level(true)
+  , m_success(true), m_fun_def(false), m_top_level(true), m_call_expr(false)
 {
   // Add internal definition of the global function
   int fun_id = -1;
@@ -95,6 +95,26 @@
 }
 
 
+void cSemanticASTVisitor::VisitArgumentList(cASTArgumentList& node)
+{
+  // Should never recurse into here.  Argument lists are processed by their owners as needed.
+  SEMANTIC_ERROR(INTERNAL);
+}
+
+
+void cSemanticASTVisitor::VisitObjectAssignment(cASTObjectAssignment& node)
+{
+  m_call_expr = true;
+  node.GetTarget()->Accept(*this);
+  m_call_expr = false;
+  
+  if (node.GetTarget()->GetType() != TYPE(OBJECT_REF)) SEMANTIC_ERROR(INVALID_ASSIGNMENT_TARGET);
+  
+  node.GetExpression()->Accept(*this);
+}
+
+
+
 void cSemanticASTVisitor::VisitReturnStatement(cASTReturnStatement& node)
 {
   node.GetExpression()->Accept(*this);
@@ -342,7 +362,7 @@
     case TOKEN(IDX_OPEN):
       checkCast(node.GetLeft()->GetType(), TYPE(ARRAY));
       checkCast(node.GetRight()->GetType(), TYPE(INT));
-      node.SetType(TYPE(RUNTIME));
+      node.SetType(m_call_expr ? TYPE(OBJECT_REF) : TYPE(RUNTIME));
       break;
     case TOKEN(ARR_RANGE):
       checkCast(node.GetLeft()->GetType(), TYPE(INT));
@@ -484,12 +504,6 @@
 }
 
 
-void cSemanticASTVisitor::VisitArgumentList(cASTArgumentList& node)
-{
-  // Should never recurse into here.  Argument lists are processed by their owners as needed.
-  SEMANTIC_ERROR(INTERNAL);
-}
-
 void cSemanticASTVisitor::VisitFunctionCall(cASTFunctionCall& node)
 {
   // @TODO - somewhere in here, make sure that default value expressions are valid for this context if used
@@ -846,6 +860,9 @@
     case AS_SEMANTIC_ERR_FUNCTION_UNDEFINED:
       std::cerr << "'" << VA_ARG_STR << "()' declared but not defined" << ERR_ENDL;
       break;
+    case AS_SEMANTIC_ERR_INVALID_ASSIGNMENT_TARGET:
+      std::cerr << "invalid assignment target" << ERR_ENDL;
+      break;
     case AS_SEMANTIC_ERR_TOO_MANY_ARGUMENTS:
       std::cerr << "too many arguments" << ERR_ENDL;
       break;

Modified: development/source/script/cSemanticASTVisitor.h
===================================================================
--- development/source/script/cSemanticASTVisitor.h	2008-03-09 20:41:07 UTC (rev 2442)
+++ development/source/script/cSemanticASTVisitor.h	2008-03-10 01:59:31 UTC (rev 2443)
@@ -62,6 +62,7 @@
   bool m_success;
   bool m_fun_def;
   bool m_top_level;
+  bool m_call_expr;
 
   
   // --------  Private Constructors  --------
@@ -77,6 +78,8 @@
   
   
   void VisitAssignment(cASTAssignment&);
+  void VisitArgumentList(cASTArgumentList&);
+  void VisitObjectAssignment(cASTObjectAssignment&);
   
   void VisitReturnStatement(cASTReturnStatement&);
   void VisitStatementList(cASTStatementList&);
@@ -92,7 +95,6 @@
   void VisitExpressionBinary(cASTExpressionBinary&);
   void VisitExpressionUnary(cASTExpressionUnary&);
   
-  void VisitArgumentList(cASTArgumentList&);
   void VisitFunctionCall(cASTFunctionCall&);
   void VisitLiteral(cASTLiteral&);
   void VisitLiteralArray(cASTLiteralArray&);




More information about the Avida-cvs mailing list