[Avida-SVN] r2382 - in development: Avida.xcodeproj source/script
brysonda at myxo.css.msu.edu
brysonda at myxo.css.msu.edu
Tue Feb 26 14:33:02 PST 2008
Author: brysonda
Date: 2008-02-26 17:33:02 -0500 (Tue, 26 Feb 2008)
New Revision: 2382
Added:
development/source/script/AvidaScript.cc
Modified:
development/Avida.xcodeproj/project.pbxproj
development/source/script/ASTree.h
development/source/script/AvidaScript.h
development/source/script/cASTDumpVisitor.cc
development/source/script/cASTDumpVisitor.h
development/source/script/cParser.cc
development/source/script/cSemanticASTVisitor.cc
Log:
AS Unary Expression checking.
Move cASTDumpVisitor map methods into a shared namespace.
Modified: development/Avida.xcodeproj/project.pbxproj
===================================================================
--- development/Avida.xcodeproj/project.pbxproj 2008-02-26 18:06:00 UTC (rev 2381)
+++ development/Avida.xcodeproj/project.pbxproj 2008-02-26 22:33:02 UTC (rev 2382)
@@ -184,6 +184,7 @@
7049F3720A66AD7E00640512 /* default-smt.org in CopyFiles */ = {isa = PBXBuildFile; fileRef = 7049F36E0A66AD7E00640512 /* default-smt.org */; };
7049F3730A66AD7E00640512 /* default-transsmt.org in CopyFiles */ = {isa = PBXBuildFile; fileRef = 7049F36F0A66AD7E00640512 /* default-transsmt.org */; };
704C21DC0C28C3980038F1A5 /* libtcmalloc.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 70AA941909D486AE006A24C8 /* libtcmalloc.a */; };
+ 7050E6780D74C36F008B3CA0 /* AvidaScript.cc in Sources */ = {isa = PBXBuildFile; fileRef = 7050E6770D74C36F008B3CA0 /* AvidaScript.cc */; };
70658C5A085DF67D00486BED /* libncurses.5.4.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 70658C59085DF67D00486BED /* libncurses.5.4.dylib */; };
7073972E0D725B9F003855D3 /* cSemanticASTVisitor.cc in Sources */ = {isa = PBXBuildFile; fileRef = 7073972C0D725B9D003855D3 /* cSemanticASTVisitor.cc */; };
7076FEB00D347FD000556CAF /* cAnalyzeTreeStats_CumulativeStemminess.cc in Sources */ = {isa = PBXBuildFile; fileRef = 7076FEAE0D347FD000556CAF /* cAnalyzeTreeStats_CumulativeStemminess.cc */; };
@@ -446,6 +447,7 @@
704ADBC30A6EEFC300666970 /* DriverActions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DriverActions.h; sourceTree = "<group>"; };
704ADBC40A6EEFC300666970 /* DriverActions.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = DriverActions.cc; sourceTree = "<group>"; };
704D57210AA53F8600207FC1 /* code_standards.html */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = text.html.documentation; path = code_standards.html; sourceTree = "<group>"; };
+ 7050E6770D74C36F008B3CA0 /* AvidaScript.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = AvidaScript.cc; sourceTree = "<group>"; };
705260BB0B87A7DB0007426F /* cInstLibEntry.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = cInstLibEntry.h; sourceTree = "<group>"; };
705261050B87AF5C0007426F /* cInstLib.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = cInstLib.h; sourceTree = "<group>"; };
705332480929764A006BD186 /* cSpecies.cc */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = cSpecies.cc; sourceTree = "<group>"; };
@@ -1223,6 +1225,7 @@
70E130E30C4551E900CE9249 /* cASTVisitor.h */,
70F9FC0E0C469DC10083B788 /* cASTDumpVisitor.h */,
70F9FC0F0C469DC10083B788 /* cASTDumpVisitor.cc */,
+ 7050E6770D74C36F008B3CA0 /* AvidaScript.cc */,
);
path = script;
sourceTree = "<group>";
@@ -2033,6 +2036,7 @@
704368D60C31991500A05ABA /* ASTree.cc in Sources */,
70F9FC100C469DD70083B788 /* cASTDumpVisitor.cc in Sources */,
7073972E0D725B9F003855D3 /* cSemanticASTVisitor.cc in Sources */,
+ 7050E6780D74C36F008B3CA0 /* AvidaScript.cc in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Modified: development/source/script/ASTree.h
===================================================================
--- development/source/script/ASTree.h 2008-02-26 18:06:00 UTC (rev 2381)
+++ development/source/script/ASTree.h 2008-02-26 22:33:02 UTC (rev 2382)
@@ -384,15 +384,20 @@
private:
ASToken_t m_op;
cASTNode* m_expr;
+ ASType_t m_type;
public:
- cASTExpressionUnary(const cASFilePosition& fp, ASToken_t op, cASTNode* e) : cASTNode(fp), m_op(op), m_expr(e) { ; }
+ cASTExpressionUnary(const cASFilePosition& fp, ASToken_t op, cASTNode* e)
+ : cASTNode(fp), m_op(op), m_expr(e), m_type(AS_TYPE_INVALID) { ; }
~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; }
+ ASType_t GetType() const { return m_type; }
+ inline void SetType(ASType_t type) { m_type = type; }
+
void Accept(cASTVisitor& visitor);
};
Added: development/source/script/AvidaScript.cc
===================================================================
--- development/source/script/AvidaScript.cc (rev 0)
+++ development/source/script/AvidaScript.cc 2008-02-26 22:33:02 UTC (rev 2382)
@@ -0,0 +1,74 @@
+/*
+ * AvidaScript.cc
+ * Avida
+ *
+ * Created by David on 2/26/08.
+ * Copyright 2008 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 "AvidaScript.h"
+
+
+const char* AvidaScript::mapToken(ASToken_t token)
+{
+ switch (token) {
+ case AS_TOKEN_OP_BIT_NOT: return "~";
+ case AS_TOKEN_OP_BIT_AND: return "&";
+ case AS_TOKEN_OP_BIT_OR: return "|";
+ case AS_TOKEN_OP_LOGIC_NOT: return "!";
+ case AS_TOKEN_OP_LOGIC_AND: return "&&";
+ case AS_TOKEN_OP_LOGIC_OR: return "||";
+ case AS_TOKEN_OP_ADD: return "+";
+ case AS_TOKEN_OP_SUB: return "-";
+ case AS_TOKEN_OP_MUL: return "*";
+ case AS_TOKEN_OP_DIV: return "/";
+ case AS_TOKEN_OP_MOD: return "%";
+ case AS_TOKEN_DOT: return ".";
+ case AS_TOKEN_OP_EQ: return "==";
+ case AS_TOKEN_OP_LE: return "<=";
+ case AS_TOKEN_OP_GE: return ">=";
+ case AS_TOKEN_OP_LT: return "<";
+ case AS_TOKEN_OP_GT: return ">";
+ case AS_TOKEN_OP_NEQ: return "!=";
+ case AS_TOKEN_ARR_RANGE: return ":";
+ case AS_TOKEN_ARR_EXPAN: return "^";
+ case AS_TOKEN_IDX_OPEN: return "[]";
+ default: return "?";
+ }
+}
+
+const char* AvidaScript::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*";
+ }
+}
+
+
Modified: development/source/script/AvidaScript.h
===================================================================
--- development/source/script/AvidaScript.h 2008-02-26 18:06:00 UTC (rev 2381)
+++ development/source/script/AvidaScript.h 2008-02-26 22:33:02 UTC (rev 2382)
@@ -114,6 +114,7 @@
AS_SEMANTIC_WARN_UNREACHABLE,
AS_SEMANTIC_WARN__LAST,
+ AS_SEMANTIC_ERR_UNDEFINED_TYPE_OP,
AS_SEMANTIC_ERR_VARIABLE_UNDEFINED,
AS_SEMANTIC_ERR_VARIABLE_REDEFINITION,
AS_SEMANTIC_ERR_INTERNAL,
@@ -135,4 +136,10 @@
AS_TYPE_INVALID
} ASType_t;
+
+namespace AvidaScript {
+ const char* mapToken(ASToken_t type);
+ const char* mapType(ASType_t type);
+};
+
#endif
Modified: development/source/script/cASTDumpVisitor.cc
===================================================================
--- development/source/script/cASTDumpVisitor.cc 2008-02-26 18:06:00 UTC (rev 2381)
+++ development/source/script/cASTDumpVisitor.cc 2008-02-26 22:33:02 UTC (rev 2382)
@@ -27,6 +27,7 @@
#include <iostream>
using namespace std;
+using namespace AvidaScript;
cASTDumpVisitor::cASTDumpVisitor() : m_depth(0)
@@ -40,53 +41,6 @@
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_DOT: 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;
- case AS_TOKEN_IDX_OPEN: 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++;
@@ -299,7 +253,7 @@
m_depth--;
indent();
- printToken(node.GetOperator());
+ cout << mapToken(node.GetOperator());
cout << endl;
m_depth++;
@@ -311,7 +265,7 @@
void cASTDumpVisitor::visitExpressionUnary(cASTExpressionUnary& node)
{
indent();
- printToken(node.GetOperator());
+ cout << mapToken(node.GetOperator());
cout << endl;
m_depth++;
Modified: development/source/script/cASTDumpVisitor.h
===================================================================
--- development/source/script/cASTDumpVisitor.h 2008-02-26 18:06:00 UTC (rev 2381)
+++ development/source/script/cASTDumpVisitor.h 2008-02-26 22:33:02 UTC (rev 2382)
@@ -61,8 +61,6 @@
private:
inline void indent();
- void printToken(ASToken_t token);
- const char* mapType(ASType_t type);
};
#endif
Modified: development/source/script/cParser.cc
===================================================================
--- development/source/script/cParser.cc 2008-02-26 18:06:00 UTC (rev 2381)
+++ development/source/script/cParser.cc 2008-02-26 22:33:02 UTC (rev 2382)
@@ -28,6 +28,8 @@
#include "cFile.h"
#include "tAutoRelease.h"
+using namespace AvidaScript;
+
/*
The following represents the grammar for AvidaScript in BNF, adjusted so that it is compatible with recursive descent
parsing (to be) implemented by cParser.
Modified: development/source/script/cSemanticASTVisitor.cc
===================================================================
--- development/source/script/cSemanticASTVisitor.cc 2008-02-26 18:06:00 UTC (rev 2381)
+++ development/source/script/cSemanticASTVisitor.cc 2008-02-26 22:33:02 UTC (rev 2382)
@@ -29,16 +29,21 @@
#include <cstdarg>
+using namespace AvidaScript;
-#define SEMANTIC_ERROR(code, ...) reportError(true, AS_SEMANTIC_ERR_ ## code, node.GetFilePosition(), __LINE__, __VA_ARGS__)
-#define SEMANTIC_WARNING(code, ...) reportError(false, AS_SEMANTIC_WARN_ ## code, node.GetFilePosition(), __LINE__, __VA_ARGS__)
+#define SEMANTIC_ERROR(code, ...) reportError(true, AS_SEMANTIC_ERR_ ## code, node.GetFilePosition(), __LINE__, ##__VA_ARGS__)
+#define SEMANTIC_WARNING(code, ...) reportError(false, AS_SEMANTIC_WARN_ ## code, node.GetFilePosition(), __LINE__, ##__VA_ARGS__)
+#define TOKEN(x) AS_TOKEN_ ## x
+#define TYPE(x) AS_TYPE_ ## x
+
+
cSemanticASTVisitor::cSemanticASTVisitor(cASLibrary* lib, cSymbolTable* global_symtbl)
: m_library(lib), m_global_symtbl(global_symtbl), m_parent_scope(global_symtbl), m_fun_id(0), m_cur_symtbl(global_symtbl)
{
// Add internal definition of the global function
- m_global_symtbl->AddFunction("__asmain", AS_TYPE_INT);
+ m_global_symtbl->AddFunction("__asmain", TYPE(INT));
}
void cSemanticASTVisitor::visitAssignment(cASTAssignment& node)
@@ -117,6 +122,51 @@
void cSemanticASTVisitor::visitExpressionUnary(cASTExpressionUnary& node)
{
+ node.GetExpression()->Accept(*this);
+
+ switch (node.GetOperator()) {
+ case TOKEN(OP_BIT_NOT):
+ switch (node.GetExpression()->GetType()) {
+ case TYPE(ARRAY):
+ case TYPE(MATRIX):
+ // Array and Matrix meta-op, validity must be determined at runtime
+ case TYPE(INT):
+ case TYPE(CHAR):
+ // Char and Int Okay
+
+ node.SetType(node.GetExpression()->GetType());
+ break;
+
+ default:
+ SEMANTIC_ERROR(UNDEFINED_TYPE_OP, mapToken(TOKEN(OP_BIT_NOT)), mapType(node.GetExpression()->GetType()));
+ break;
+ }
+ break;
+ case TOKEN(OP_LOGIC_NOT):
+ // All types support boolean usage
+ break;
+ case TOKEN(OP_SUB):
+ switch (node.GetExpression()->GetType()) {
+ case TYPE(ARRAY):
+ case TYPE(MATRIX):
+ // Array and Matrix meta-op, validity must be determined at runtime
+ case TYPE(CHAR):
+ case TYPE(FLOAT):
+ case TYPE(INT):
+ // Char, Float and Int Okay
+
+ node.SetType(node.GetExpression()->GetType());
+ break;
+ default:
+ SEMANTIC_ERROR(UNDEFINED_TYPE_OP, mapToken(TOKEN(OP_SUB)), mapType(node.GetExpression()->GetType()));
+ break;
+ };
+
+ break;
+ default:
+ SEMANTIC_ERROR(INTERNAL);
+ break;
+ }
}
@@ -166,23 +216,27 @@
if (err < AS_SEMANTIC_WARN__LAST) std::cerr << ": warning: ";
else std::cerr << ": error: ";
- va_list info_list;
- va_start(info_list, line);
+ va_list vargs;
+ va_start(vargs, line);
switch (err) {
case AS_SEMANTIC_WARN_UNREACHABLE:
std::cerr << "unreachable statement(s)" << ERR_ENDL;
break;
case AS_SEMANTIC_ERR_VARIABLE_UNDEFINED:
{
- cString varname = va_arg(info_list, const char*);
+ cString varname = va_arg(vargs, const char*);
std::cerr << "'" << varname << "' undefined";
cString nearmatch = m_cur_symtbl->VariableNearMatch(varname);
if (nearmatch != "") std::cerr << " - possible match '" << nearmatch << "'";
std::cerr << ERR_ENDL;
}
break;
+ case AS_SEMANTIC_ERR_UNDEFINED_TYPE_OP:
+ std::cerr << "'" << va_arg(vargs, const char*) << "' operation undefined for type '"
+ << va_arg(vargs, const char*) << "'" << ERR_ENDL;
+ break;
case AS_SEMANTIC_ERR_VARIABLE_REDEFINITION:
- std::cerr << "redefining variable '" << va_arg(info_list, const char*) << "'" << ERR_ENDL;
+ std::cerr << "redefining variable '" << va_arg(vargs, const char*) << "'" << ERR_ENDL;
break;
case AS_SEMANTIC_ERR_INTERNAL:
std::cerr << "internal semantic analysis error at cSemanticASTVisitor.cc:" << line << std::endl;
@@ -191,9 +245,12 @@
default:
std::cerr << "unknown error" << std::endl;
}
- va_end(info_list);
+ va_end(vargs);
#undef ERR_ENDL
}
-
+
#undef SEMANTIC_ERROR()
+#undef SEMANTIC_WARNING()
+#undef TOKEN()
+#undef TYPE()
More information about the Avida-cvs
mailing list