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

brysonda at myxo.css.msu.edu brysonda at myxo.css.msu.edu
Sat Mar 1 16:17:13 PST 2008


Author: brysonda
Date: 2008-03-01 19:17:13 -0500 (Sat, 01 Mar 2008)
New Revision: 2406

Modified:
   development/source/script/ASTree.h
   development/source/script/AvidaScript.h
   development/source/script/cSemanticASTVisitor.cc
   development/source/script/cSemanticASTVisitor.h
Log:
AS: Function calls.

Modified: development/source/script/ASTree.h
===================================================================
--- development/source/script/ASTree.h	2008-03-01 17:01:27 UTC (rev 2405)
+++ development/source/script/ASTree.h	2008-03-02 00:17:13 UTC (rev 2406)
@@ -359,6 +359,7 @@
   
   inline void AddNode(cASTVariableDefinition* n) { m_nodes.PushRear(n); }
   inline tListIterator<cASTVariableDefinition> Iterator() { return tListIterator<cASTVariableDefinition>(m_nodes); }
+  inline cASTVariableDefinition* GetFirst() { return m_nodes.GetFirst(); }
   
   inline int GetSize() const { return m_nodes.GetSize(); }
   

Modified: development/source/script/AvidaScript.h
===================================================================
--- development/source/script/AvidaScript.h	2008-03-01 17:01:27 UTC (rev 2405)
+++ development/source/script/AvidaScript.h	2008-03-02 00:17:13 UTC (rev 2406)
@@ -117,11 +117,14 @@
   AS_SEMANTIC_WARN_UNREACHABLE,
   AS_SEMANTIC_WARN__LAST,
   
+  AS_SEMANTIC_ERR_ARGUMENT_DEFAULT_REQUIRED,
+  AS_SEMANTIC_ERR_ARGUMENT_MISSING_REQUIRED,
   AS_SEMANTIC_ERR_CANNOT_CAST,
-  AS_SEMANTIC_ERR_FUNCTION_DEFAULT_REQUIRED,
+  AS_SEMANTIC_ERR_FUNCTION_CALL_SIGNATURE_MISMATCH,
   AS_SEMANTIC_ERR_FUNCTION_REDEFINITION,
   AS_SEMANTIC_ERR_FUNCTION_RTYPE_MISMATCH,
   AS_SEMANTIC_ERR_FUNCTION_SIGNATURE_MISMATCH,
+  AS_SEMANTIC_ERR_FUNCTION_UNDECLARED,
   AS_SEMANTIC_ERR_FUNCTION_UNDEFINED,
   AS_SEMANTIC_ERR_TOO_MANY_ARGUMENTS,
   AS_SEMANTIC_ERR_UNDEFINED_TYPE_OP,

Modified: development/source/script/cSemanticASTVisitor.cc
===================================================================
--- development/source/script/cSemanticASTVisitor.cc	2008-03-01 17:01:27 UTC (rev 2405)
+++ development/source/script/cSemanticASTVisitor.cc	2008-03-02 00:17:13 UTC (rev 2406)
@@ -227,7 +227,7 @@
         vd->Accept(*this);
         if (def_val_start) {
           if (!vd->GetAssignmentExpression())
-            SEMANTIC_ERROR(FUNCTION_DEFAULT_REQUIRED, (const char*)vd->GetName(), (const char*)node.GetName());
+            SEMANTIC_ERROR(ARGUMENT_DEFAULT_REQUIRED, (const char*)vd->GetName(), (const char*)node.GetName());
         } else {
           if (vd->GetAssignmentExpression()) def_val_start = true;
         }
@@ -482,7 +482,59 @@
 
 void cSemanticASTVisitor::visitFunctionCall(cASTFunctionCall& node)
 {
-  // @TODO - function call
+  int fun_id = -1;
+  bool global = false;
+  if (lookupFunction(node.GetName(), fun_id, global)) {
+    cASTVariableDefinitionList* sig = (global ? m_global_symtbl : m_cur_symtbl)->GetFunctionSignature(fun_id);
+    
+    // Check function parameters for match to the signature
+    cASTArgumentList* args = node.GetArguments();
+    if (args) {
+      if (sig && args->GetSize() <= sig->GetSize()) {
+        
+        bool err = false;
+        tListIterator<cASTVariableDefinition> sit = sig->Iterator();
+        tListIterator<cASTNode> cit = node.GetArguments()->Iterator();
+        cASTNode* an = NULL;
+        while ((an = cit.Next())) {
+          an->Accept(*this);
+          
+          ASType_t in_type = an->GetType();
+          ASType_t out_type = sit.Next()->GetType();
+          if (valid_cast[in_type][out_type]) { 
+            if ((in_type == TYPE(FLOAT) && out_type == TYPE(INT)) || (in_type == TYPE(INT) && out_type == TYPE(CHAR))) 
+              SEMANTIC_WARNING(LOSS_OF_PRECISION, mapType(in_type), mapType(out_type)); 
+          } else { 
+            if (!err) {
+              SEMANTIC_ERROR(FUNCTION_CALL_SIGNATURE_MISMATCH, (const char*)node.GetName());
+              err = true;
+            }
+            SEMANTIC_ERROR(CANNOT_CAST, mapType(in_type), mapType(out_type)); 
+          }
+        }
+        
+        if (!sit.AtEnd() && !sit.Next()->GetAssignmentExpression()) 
+          SEMANTIC_ERROR(ARGUMENT_MISSING_REQUIRED, (const char*)sit.Get()->GetName());
+      } else {
+        SEMANTIC_ERROR(FUNCTION_CALL_SIGNATURE_MISMATCH, (const char*)node.GetName());
+      }
+    } else {
+      if (sig && sig->GetSize() && !sig->GetFirst()->GetAssignmentExpression()) {
+        SEMANTIC_ERROR(FUNCTION_CALL_SIGNATURE_MISMATCH, (const char*)node.GetName());
+        tListIterator<cASTVariableDefinition> it = sig->Iterator();
+        cASTVariableDefinition* vd = NULL;
+        while ((vd == it.Next())) {
+          if (!vd->GetAssignmentExpression()) SEMANTIC_ERROR(ARGUMENT_MISSING_REQUIRED, (const char*)vd->GetName());
+          else break;
+        }
+      }
+    }
+    
+    node.SetFunc(fun_id, global);
+    node.SetType(m_cur_symtbl->GetFunctionRType(fun_id));
+  } else {
+    SEMANTIC_ERROR(FUNCTION_UNDECLARED, (const char*)node.GetName());
+  }
 }
 
 
@@ -663,7 +715,7 @@
       return false;
   }
 }
-        
+
 inline bool cSemanticASTVisitor::lookupVariable(const cString& name, int& var_id, bool& global) const
 {
   if (m_cur_symtbl->LookupVariable(name, var_id)) {
@@ -678,7 +730,21 @@
 }
 
 
+inline bool cSemanticASTVisitor::lookupFunction(const cString& name, int& fun_id, bool& global) const
+{
+  if (m_cur_symtbl->LookupFunction(name, fun_id)) {
+    global = false;
+    return true;
+  } else if (m_cur_symtbl != m_global_symtbl && m_global_symtbl->LookupFunction(name, fun_id)) {
+    global = true;
+    return true;
+  } 
+  
+  return false;
+}
 
+
+
 void cSemanticASTVisitor::reportError(ASSemanticError_t err, const cASFilePosition& fp, const int line, ...)
 {
 #define ERR_ENDL "  (cSemanticASTVisitor.cc:" << line << ")" << std::endl
@@ -704,12 +770,18 @@
       std::cerr << "unreachable statement(s)" << ERR_ENDL;
       break;
       
+    case AS_SEMANTIC_ERR_ARGUMENT_DEFAULT_REQUIRED:
+      std::cerr << "'" << VA_ARG_STR << "' argument of '" << VA_ARG_STR << "()' requires a default value due to previous"
+                << "argument defaults" << ERR_ENDL;
+      break;
+    case AS_SEMANTIC_ERR_ARGUMENT_MISSING_REQUIRED:
+      std::cerr << "required argument " << VA_ARG_STR << " not found" << ERR_ENDL;
+      break;
     case AS_SEMANTIC_ERR_CANNOT_CAST:
       std::cerr << "cannot cast " << VA_ARG_STR << " to " << VA_ARG_STR << ERR_ENDL;
       break;
-    case AS_SEMANTIC_ERR_FUNCTION_DEFAULT_REQUIRED:
-      std::cerr << "'" << VA_ARG_STR << "' argument of '" << VA_ARG_STR << "()' requires a default value due to previous"
-                << "argument defaults" << ERR_ENDL;
+    case AS_SEMANTIC_ERR_FUNCTION_CALL_SIGNATURE_MISMATCH:
+      std::cerr << "invalid call signature for '" << VA_ARG_STR << "()'" << ERR_ENDL;
       break;
     case AS_SEMANTIC_ERR_FUNCTION_REDEFINITION:
       std::cerr << "redefinition of '" << VA_ARG_STR << "()'" << ERR_ENDL;
@@ -720,6 +792,17 @@
     case AS_SEMANTIC_ERR_FUNCTION_SIGNATURE_MISMATCH:
       std::cerr << "call signature of '" << VA_ARG_STR << "()' does not match declaration" << ERR_ENDL;
       break;
+    case AS_SEMANTIC_ERR_FUNCTION_UNDECLARED:
+      {
+        cString varname = VA_ARG_STR;
+        std::cerr << "'" << varname << "()' undeclared";
+        cString nearmatch1 = m_cur_symtbl->FunctionNearMatch(varname);
+        cString nearmatch2 = m_global_symtbl->FunctionNearMatch(varname);
+        if (nearmatch1 != "") std::cerr << " - possible match '" << nearmatch1 << "()'";
+        if (nearmatch2 != "" && nearmatch1 != nearmatch2) std::cerr << " or '" << nearmatch2 << "()'";
+        std::cerr << ERR_ENDL;
+      }
+      break;
     case AS_SEMANTIC_ERR_FUNCTION_UNDEFINED:
       std::cerr << "'" << VA_ARG_STR << "()' declared but not defined" << ERR_ENDL;
       break;

Modified: development/source/script/cSemanticASTVisitor.h
===================================================================
--- development/source/script/cSemanticASTVisitor.h	2008-03-01 17:01:27 UTC (rev 2405)
+++ development/source/script/cSemanticASTVisitor.h	2008-03-02 00:17:13 UTC (rev 2406)
@@ -99,6 +99,7 @@
   inline bool validBitwiseType(ASType_t type) const;
   
   inline bool lookupVariable(const cString& name, int& var_id, bool& global) const;
+  inline bool lookupFunction(const cString& name, int& fun_id, bool& global) const;
   
   void reportError(ASSemanticError_t err, const cASFilePosition& fp, const int line, ...);
 };




More information about the Avida-cvs mailing list