added array handling
This commit is contained in:
@@ -53,7 +53,8 @@ enum COMPILE_ERRORS {
|
|||||||
E_GOSUB_CANNOT_RETURN_VALUE,
|
E_GOSUB_CANNOT_RETURN_VALUE,
|
||||||
E_SUBROUTINE_NOT_FOUND,
|
E_SUBROUTINE_NOT_FOUND,
|
||||||
E_TOO_MANY_PARAMETERS,
|
E_TOO_MANY_PARAMETERS,
|
||||||
E_UNASSIGNABLE_TYPE
|
E_UNASSIGNABLE_TYPE,
|
||||||
|
E_UNDIMENSIONED_ARRAY
|
||||||
};
|
};
|
||||||
|
|
||||||
extern enum COMPILE_ERRORS errorLevel;
|
extern enum COMPILE_ERRORS errorLevel;
|
||||||
@@ -363,6 +364,7 @@ public:
|
|||||||
static shared_ptr<variable>getOrCreateVar(string &name, enum TYPES t);
|
static shared_ptr<variable>getOrCreateVar(string &name, enum TYPES t);
|
||||||
|
|
||||||
void assignment(shared_ptr<expression>value);
|
void assignment(shared_ptr<expression>value);
|
||||||
|
/* always call generateBox() after new variable() */
|
||||||
variable(enum SCOPES s, string &name, enum TYPES t);
|
variable(enum SCOPES s, string &name, enum TYPES t);
|
||||||
variable();
|
variable();
|
||||||
~variable()
|
~variable()
|
||||||
@@ -373,8 +375,9 @@ class arrayType:public variable
|
|||||||
{
|
{
|
||||||
list<unsigned int> dimensions;
|
list<unsigned int> dimensions;
|
||||||
public:
|
public:
|
||||||
|
string generateBox(enum SCOPES s);
|
||||||
virtual string boxName(list<unsigned int>indexes);
|
virtual string boxName(list<unsigned int>indexes);
|
||||||
virtual string boxName(){error(E_TYPE_MISMATCH);}
|
virtual string boxName(){error(E_UNDIMENSIONED_ARRAY);}
|
||||||
|
|
||||||
explicit arrayType(string &name, enum TYPES t, list<unsigned int>dim);
|
explicit arrayType(string &name, enum TYPES t, list<unsigned int>dim);
|
||||||
/*:variable(scope, name, t);*/
|
/*:variable(scope, name, t);*/
|
||||||
|
|||||||
@@ -352,7 +352,6 @@ variable::variable(enum SCOPES s, string &name, enum TYPES t):operands(t)
|
|||||||
default:
|
default:
|
||||||
error(E_INTERNAL);
|
error(E_INTERNAL);
|
||||||
}
|
}
|
||||||
this->generateBox(s);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
shared_ptr<variable> variable::getOrCreateVar(string &name, enum TYPES t)
|
shared_ptr<variable> variable::getOrCreateVar(string &name, enum TYPES t)
|
||||||
@@ -365,8 +364,10 @@ shared_ptr<variable> variable::getOrCreateVar(string &name, enum TYPES t)
|
|||||||
if(i!=statics.end())return i->second;
|
if(i!=statics.end())return i->second;
|
||||||
}
|
}
|
||||||
if (globals.find(name)!=globals.end())return globals[name];
|
if (globals.find(name)!=globals.end())return globals[name];
|
||||||
return shared_ptr<variable>(new variable(scopeGlobal?S_GLOBAL:S_LOCAL,
|
shared_ptr<variable>v=shared_ptr<variable>(new variable(
|
||||||
name, t));
|
scopeGlobal?S_GLOBAL:S_LOCAL, name, t));
|
||||||
|
v->generateBox(scopeGlobal?S_GLOBAL:S_LOCAL);
|
||||||
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
void variable::assignment(shared_ptr<expression>value)
|
void variable::assignment(shared_ptr<expression>value)
|
||||||
@@ -396,3 +397,48 @@ void variable::assignment(shared_ptr<expression>value)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
string arrayType::boxName(list<unsigned int>indexes)
|
||||||
|
{
|
||||||
|
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, list<unsigned int>dim):
|
||||||
|
variable(S_GLOBAL, name, t)
|
||||||
|
{
|
||||||
|
this->dimensions=dim;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user