diff --git a/yab2cpp.h b/yab2cpp.h index 9c7b93e..d26493e 100644 --- a/yab2cpp.h +++ b/yab2cpp.h @@ -53,7 +53,8 @@ enum COMPILE_ERRORS { E_GOSUB_CANNOT_RETURN_VALUE, E_SUBROUTINE_NOT_FOUND, E_TOO_MANY_PARAMETERS, - E_UNASSIGNABLE_TYPE + E_UNASSIGNABLE_TYPE, + E_UNDIMENSIONED_ARRAY }; extern enum COMPILE_ERRORS errorLevel; @@ -363,6 +364,7 @@ public: static shared_ptrgetOrCreateVar(string &name, enum TYPES t); void assignment(shared_ptrvalue); + /* always call generateBox() after new variable() */ variable(enum SCOPES s, string &name, enum TYPES t); variable(); ~variable() @@ -373,8 +375,9 @@ class arrayType:public variable { list dimensions; public: + string generateBox(enum SCOPES s); virtual string boxName(listindexes); - virtual string boxName(){error(E_TYPE_MISMATCH);} + virtual string boxName(){error(E_UNDIMENSIONED_ARRAY);} explicit arrayType(string &name, enum TYPES t, listdim); /*:variable(scope, name, t);*/ diff --git a/yabDataStructures.cpp b/yabDataStructures.cpp index 97dffc9..bbd2cc2 100644 --- a/yabDataStructures.cpp +++ b/yabDataStructures.cpp @@ -352,7 +352,6 @@ variable::variable(enum SCOPES s, string &name, enum TYPES t):operands(t) default: error(E_INTERNAL); } - this->generateBox(s); } shared_ptr variable::getOrCreateVar(string &name, enum TYPES t) @@ -365,8 +364,10 @@ shared_ptr variable::getOrCreateVar(string &name, enum TYPES t) if(i!=statics.end())return i->second; } if (globals.find(name)!=globals.end())return globals[name]; - return shared_ptr(new variable(scopeGlobal?S_GLOBAL:S_LOCAL, - name, t)); + shared_ptrv=shared_ptr(new variable( + scopeGlobal?S_GLOBAL:S_LOCAL, name, t)); + v->generateBox(scopeGlobal?S_GLOBAL:S_LOCAL); + return v; } void variable::assignment(shared_ptrvalue) @@ -396,3 +397,48 @@ void variable::assignment(shared_ptrvalue) break; } } + +string arrayType::boxName(listindexes) +{ + ostringstream out; + string buf; + auto i=indexes.begin(); + out << 'v' << this->getID(); + while (i!=indexes.end()) + { + out << '[' << *i << ']'; + } + out.str(buf); + out.clear(); + return buf; +} + +string arrayType::generateBox(enum SCOPES s) +{ + ostringstream out; + string buf; + switch (this->getType()) + { + case T_STRINGCALL_ARRAY: + out << "string "; + break; + case T_INTCALL_ARRAY: + out << "int "; + break; + case T_FLOATCALL_ARRAY: + out << "double "; + break; + default: + error(E_INTERNAL); + } + out << boxName(this->dimensions) << ";\n"; + out.str(buf); + out.clear(); + return buf; +} + +arrayType::arrayType(string &name, enum TYPES t, listdim): + variable(S_GLOBAL, name, t) +{ + this->dimensions=dim; +} \ No newline at end of file