Started enforcing style guide. Fixed all warnings.
This commit is contained in:
38
yab2cpp.h
38
yab2cpp.h
@@ -32,6 +32,7 @@ extern ofstream varNames;
|
||||
extern unordered_map<string, shared_ptr<variable> >globals;
|
||||
extern unordered_map<string, shared_ptr<variable> >locals;
|
||||
extern unordered_map<string, shared_ptr<variable> >statics;
|
||||
|
||||
/*
|
||||
** list of all compiler errors
|
||||
**
|
||||
@@ -189,6 +190,7 @@ public:
|
||||
{}
|
||||
};
|
||||
|
||||
/* constant operands */
|
||||
class constOp:public operands
|
||||
{
|
||||
/* box is defined once in the constructor */
|
||||
@@ -207,7 +209,7 @@ public:
|
||||
{}
|
||||
};
|
||||
|
||||
/* expression can be terminal or non-terminal */
|
||||
/* expression can be terminal or non-terminal node */
|
||||
class expression
|
||||
{
|
||||
shared_ptr<operands>op;
|
||||
@@ -221,15 +223,19 @@ public:
|
||||
|
||||
bool isBinOp();
|
||||
shared_ptr<operands>evaluate();
|
||||
shared_ptr<operands>stringEval(shared_ptr<operands>l, shared_ptr<operands>r);
|
||||
shared_ptr<operands>stringEval(shared_ptr<operands>l,
|
||||
shared_ptr<operands>r);
|
||||
|
||||
/* r is NULL for unary operators */
|
||||
expression(shared_ptr<expression>l, enum OPERATORS o, shared_ptr<expression>r=NULL)
|
||||
expression(shared_ptr<expression>l, enum OPERATORS o,
|
||||
shared_ptr<expression>r=NULL)
|
||||
{
|
||||
this->left=l;
|
||||
this->right=r;
|
||||
this->oper=o;
|
||||
}
|
||||
|
||||
/* Terminal expression node */
|
||||
expression(shared_ptr<operands>x)
|
||||
{
|
||||
op=x;
|
||||
@@ -290,14 +296,19 @@ public:
|
||||
/* if statement */
|
||||
class ifStatement:public codeType
|
||||
{
|
||||
shared_ptr<label>redo; /* for continue command */
|
||||
shared_ptr<label>done; /* for break or after "then" condition */
|
||||
shared_ptr<label>chain; /* For elsif command */
|
||||
/* for continue command */
|
||||
shared_ptr<label>redo;
|
||||
/* for break or after "then" condition */
|
||||
shared_ptr<label>done;
|
||||
/* For elsif command */
|
||||
shared_ptr<label>chain;
|
||||
public:
|
||||
void generateContinue();
|
||||
virtual void generateBreak() override;
|
||||
void alternative(shared_ptr<expression>e=NULL); /* enable else or elsif condition */
|
||||
virtual void close() override; /* end if */
|
||||
/* enable else or elsif condition */
|
||||
void alternative(shared_ptr<expression>e=NULL);
|
||||
/* end if */
|
||||
virtual void close() override;
|
||||
|
||||
explicit ifStatement(shared_ptr<expression>e);
|
||||
virtual ~ifStatement()
|
||||
@@ -365,7 +376,8 @@ class arrayType:public variable
|
||||
public:
|
||||
virtual string &boxName(list<unsigned int>indexes);
|
||||
|
||||
explicit arrayType(string &name, enum TYPES t, list<unsigned int>dim);/*:variable(scope, name, t);*/
|
||||
explicit arrayType(string &name, enum TYPES t, list<unsigned int>dim);
|
||||
/*:variable(scope, name, t);*/
|
||||
virtual ~arrayType()
|
||||
{}
|
||||
};
|
||||
@@ -375,13 +387,14 @@ class forLoop:public codeType
|
||||
shared_ptr<variable>var;
|
||||
shared_ptr<variable>startTemp;
|
||||
shared_ptr<variable>stopTemp;
|
||||
whileLoop *infrastructure;
|
||||
shared_ptr<whileLoop>infrastructure;
|
||||
shared_ptr<expression>step;
|
||||
public:
|
||||
virtual void generateBreak();
|
||||
virtual void close();
|
||||
|
||||
explicit forLoop(shared_ptr<variable>v, shared_ptr<expression>start, shared_ptr<expression>stop, shared_ptr<expression>stepVal=NULL);
|
||||
explicit forLoop(shared_ptr<variable>v, shared_ptr<expression>start,
|
||||
shared_ptr<expression>stop, shared_ptr<expression>stepVal=NULL);
|
||||
virtual ~forLoop()
|
||||
{}
|
||||
};
|
||||
@@ -413,7 +426,8 @@ public:
|
||||
int getNumParams() const {return this->params.size();}
|
||||
void addParameter(shared_ptr<variable>);
|
||||
|
||||
shared_ptr<operands>generateCall(string &name, list<shared_ptr<operands> >¶mList);
|
||||
shared_ptr<operands>generateCall(string &name,
|
||||
list<shared_ptr<operands> >¶mList);
|
||||
void generateReturn(shared_ptr<expression>expr);
|
||||
void generateReturn();
|
||||
virtual void generateBreak();
|
||||
|
||||
@@ -31,7 +31,8 @@ void label::dumpLabels()
|
||||
varNames << "Global Labels\n\n";
|
||||
for(auto iter=lookup.begin(); iter!=lookup.end(); ++iter)
|
||||
{
|
||||
varNames << "label " << iter->first << " has ID " << iter->second->getID() << "\n" ;
|
||||
varNames << "label " << iter->first << " has ID "
|
||||
<< iter->second->getID() << "\n" ;
|
||||
}
|
||||
varNames << endl;
|
||||
}
|
||||
@@ -113,7 +114,8 @@ void ifStatement::alternative(shared_ptr<expression>e)
|
||||
if(e!=NULL)
|
||||
{
|
||||
this->chain=shared_ptr<label>(new label());
|
||||
shared_ptr<expression>f=shared_ptr<expression>(new expression(e,O_NOT));
|
||||
shared_ptr<expression>f=shared_ptr<expression>(
|
||||
new expression(e,O_NOT));
|
||||
chain->generateCondJump(f);
|
||||
}
|
||||
}
|
||||
@@ -195,8 +197,9 @@ forLoop::forLoop(shared_ptr<variable>v,
|
||||
stopTemp->assignment(stop);
|
||||
/* if (v<stopTemp) */
|
||||
shared_ptr<ifStatement>c=shared_ptr<ifStatement>(new ifStatement(
|
||||
shared_ptr<expression>(new expression(shared_ptr<expression>(new expression(v)),
|
||||
O_LESS, shared_ptr<expression>(new expression(stopTemp))))));
|
||||
shared_ptr<expression>(new expression(shared_ptr<expression>(
|
||||
new expression(v)), O_LESS, shared_ptr<expression>(
|
||||
new expression(stopTemp))))));
|
||||
/* startTemp=v;*/
|
||||
startTemp->assignment(shared_ptr<expression>(new expression(v)));
|
||||
/* else */
|
||||
@@ -216,9 +219,10 @@ forLoop::forLoop(shared_ptr<variable>v,
|
||||
shared_ptr<expression>(new expression(startTemp))));
|
||||
shared_ptr<expression>stopper=shared_ptr<expression>(new expression(
|
||||
stopper1, O_AND, stopper2));
|
||||
this->infrastructure=new whileLoop(shared_ptr<expression>(new expression(
|
||||
stopper, O_UNEQUAL, shared_ptr<expression>(new expression(
|
||||
shared_ptr<constOp>(new constOp("0", T_INT)))))));
|
||||
shared_ptr<whileLoop>infrastructure=shared_ptr<whileLoop>(new whileLoop(
|
||||
shared_ptr<expression>(new expression(stopper, O_UNEQUAL,
|
||||
shared_ptr<expression>(new expression(
|
||||
shared_ptr<constOp>(new constOp("0", T_INT))))))));
|
||||
if (stepVal)
|
||||
{
|
||||
step=stepVal;
|
||||
|
||||
@@ -23,7 +23,10 @@ ofstream &scope::operator<<(ostream &in)
|
||||
case S_GLOBAL:
|
||||
case S_STATIC:
|
||||
return heap_h;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
error(E_INTERNAL);
|
||||
}
|
||||
|
||||
/* methods for operands */
|
||||
@@ -43,6 +46,8 @@ enum TYPES operands::getSimpleVarType()
|
||||
case T_STRINGCALL_ARRAY:
|
||||
case T_STRINGVAR:
|
||||
return T_STRINGVAR;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
error(E_UNASSIGNABLE_TYPE);
|
||||
}
|
||||
@@ -156,14 +161,17 @@ bool expression::isBinOp()
|
||||
{
|
||||
switch (this->getOp())
|
||||
{
|
||||
/* fallthrough for multiselect */
|
||||
case O_NEGATE:
|
||||
case O_NOT:
|
||||
case O_INVERT:
|
||||
case O_INT_TO_FLOAT:
|
||||
return false;
|
||||
break;
|
||||
default:
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
/* unreachable code */
|
||||
error(E_INTERNAL);
|
||||
}
|
||||
|
||||
shared_ptr<operands>expression::evaluate()
|
||||
@@ -181,12 +189,14 @@ shared_ptr<operands>expression::evaluate()
|
||||
enum TYPES rt=r->getSimpleVarType();
|
||||
if (lt==T_INTVAR && rt==T_FLOATVAR)
|
||||
{
|
||||
l=shared_ptr<operands>((new expression(shared_ptr<expression>(new expression(l)), O_INT_TO_FLOAT))->evaluate());
|
||||
l=shared_ptr<operands>((new expression(shared_ptr<expression>(
|
||||
new expression(l)), O_INT_TO_FLOAT))->evaluate());
|
||||
lt=T_FLOATVAR;
|
||||
}
|
||||
if (lt==T_FLOATVAR && rt==T_INTVAR)
|
||||
{
|
||||
r=shared_ptr<operands>((new expression(shared_ptr<expression>(new expression(r)), O_INT_TO_FLOAT))->evaluate());
|
||||
r=shared_ptr<operands>((new expression(shared_ptr<expression>(
|
||||
new expression(r)), O_INT_TO_FLOAT))->evaluate());
|
||||
rt=T_FLOATVAR;
|
||||
}
|
||||
if (lt!=rt)error(E_TYPE_MISMATCH);
|
||||
@@ -197,9 +207,15 @@ shared_ptr<operands>expression::evaluate()
|
||||
t=l->getSimpleVarType();
|
||||
r=NULL;
|
||||
}
|
||||
if (t==T_STRINGVAR) return expression::stringEval(l, r);
|
||||
switch (this->getOp())
|
||||
{
|
||||
case O_STRING_CONCAT:
|
||||
if (t!=T_STRINGVAR) error(E_BAD_SYNTAX);
|
||||
this->op=shared_ptr<operands>(new operands(T_STRINGVAR));
|
||||
this->op->generateBox(scopeGlobal?S_GLOBAL:S_LOCAL);
|
||||
output_cpp << this->op->boxName() << "=" << l->boxName()
|
||||
<< "+" << r->boxName();
|
||||
break;
|
||||
case O_INVERT:
|
||||
this->op=shared_ptr<operands>(new operands(t));
|
||||
this->op->generateBox(scopeVar);
|
||||
@@ -226,69 +242,82 @@ shared_ptr<operands>expression::evaluate()
|
||||
if (t!=T_INTVAR) error(E_TYPE_MISMATCH);
|
||||
this->op=shared_ptr<operands>(new operands(T_INTVAR));
|
||||
this->op->generateBox(scopeVar);
|
||||
output_cpp << this->op->boxName() << "=" << l->boxName() << "%" << r->boxName() << ";\n";
|
||||
output_cpp << this->op->boxName() << "=" << l->boxName()
|
||||
<< "%" << r->boxName() << ";\n";
|
||||
break;
|
||||
case O_DIVIDE:
|
||||
this->op=shared_ptr<operands>(new operands(t));
|
||||
this->op->generateBox(scopeVar);
|
||||
output_cpp << this->op->boxName() << "=" << l->boxName() << "/" << r->boxName() << ";\n";
|
||||
output_cpp << this->op->boxName() << "=" << l->boxName()
|
||||
<< "/" << r->boxName() << ";\n";
|
||||
break;
|
||||
case O_PLUS:
|
||||
this->op=shared_ptr<operands>(new operands(t));
|
||||
this->op->generateBox(scopeVar);
|
||||
output_cpp << this->op->boxName() << "=" << l->boxName() << "+" << r->boxName() << ";\n";
|
||||
output_cpp << this->op->boxName() << "=" << l->boxName()
|
||||
<< "+" << r->boxName() << ";\n";
|
||||
break;
|
||||
case O_MINUS:
|
||||
this->op=shared_ptr<operands>(new operands(t));
|
||||
this->op->generateBox(scopeVar);
|
||||
output_cpp << this->op->boxName() << "=" << l->boxName() << "-" << r->boxName() << ";\n";
|
||||
output_cpp << this->op->boxName() << "=" << l->boxName()
|
||||
<< "-" << r->boxName() << ";\n";
|
||||
break;
|
||||
case O_MULTIPLY:
|
||||
this->op=shared_ptr<operands>(new operands(t));
|
||||
this->op->generateBox(scopeVar);
|
||||
output_cpp << this->op->boxName() << "=" << l->boxName() << "*" << r->boxName() << ";\n";
|
||||
output_cpp << this->op->boxName() << "=" << l->boxName()
|
||||
<< "*" << r->boxName() << ";\n";
|
||||
break;
|
||||
case O_OR:
|
||||
if (t!=T_INTVAR) error(E_TYPE_MISMATCH);
|
||||
this->op=shared_ptr<operands>(new operands(T_INTVAR));
|
||||
this->op->generateBox(scopeVar);
|
||||
output_cpp << this->op->boxName() << "=" << l->boxName() << "|" << r->boxName() << ";\n";
|
||||
output_cpp << this->op->boxName() << "=" << l->boxName()
|
||||
<< "|" << r->boxName() << ";\n";
|
||||
break;
|
||||
case O_AND:
|
||||
if (t!=T_INTVAR) error(E_TYPE_MISMATCH);
|
||||
this->op=shared_ptr<operands>(new operands(T_INTVAR));
|
||||
this->op->generateBox(scopeVar);
|
||||
output_cpp << this->op->boxName() << "=" << l->boxName() << "&" << r->boxName() << ";\n";
|
||||
output_cpp << this->op->boxName() << "=" << l->boxName()
|
||||
<< "&" << r->boxName() << ";\n";
|
||||
break;
|
||||
case O_GREATER:
|
||||
this->op=shared_ptr<operands>(new operands(T_INTVAR));
|
||||
this->op->generateBox(scopeVar);
|
||||
output_cpp << this->op->boxName() << "=(" << l->boxName() << ">" << r->boxName() << ")?-1:0;\n";
|
||||
output_cpp << this->op->boxName() << "=(" << l->boxName()
|
||||
<< ">" << r->boxName() << ")?-1:0;\n";
|
||||
break;
|
||||
case O_LESS:
|
||||
this->op=shared_ptr<operands>(new operands(T_INTVAR));
|
||||
this->op->generateBox(scopeVar);
|
||||
output_cpp << this->op->boxName() << "=(" << l->boxName() << "<" << r->boxName() << ")?-1:0;\n";
|
||||
output_cpp << this->op->boxName() << "=(" << l->boxName()
|
||||
<< "<" << r->boxName() << ")?-1:0;\n";
|
||||
break;
|
||||
case O_GREATER_EQUAL:
|
||||
this->op=shared_ptr<operands>(new operands(T_INTVAR));
|
||||
this->op->generateBox(scopeVar);
|
||||
output_cpp << this->op->boxName() << "=(" << l->boxName() << ">=" << r->boxName() << ")?-1:0;\n";
|
||||
output_cpp << this->op->boxName() << "=(" << l->boxName()
|
||||
<< ">=" << r->boxName() << ")?-1:0;\n";
|
||||
break;
|
||||
case O_LESS_EQUAL:
|
||||
this->op=shared_ptr<operands>(new operands(T_INTVAR));
|
||||
this->op->generateBox(scopeVar);
|
||||
output_cpp << this->op->boxName() << "=(" << l->boxName() << "<=" << r->boxName() << ")?-1:0;\n";
|
||||
output_cpp << this->op->boxName() << "=(" << l->boxName()
|
||||
<< "<=" << r->boxName() << ")?-1:0;\n";
|
||||
break;
|
||||
case O_EQUAL:
|
||||
this->op=shared_ptr<operands>(new operands(T_INTVAR));
|
||||
this->op->generateBox(scopeVar);
|
||||
output_cpp << this->op->boxName() << "=(" << l->boxName() << "==" << r->boxName() << ")?-1:0;\n";
|
||||
output_cpp << this->op->boxName() << "=(" << l->boxName()
|
||||
<< "==" << r->boxName() << ")?-1:0;\n";
|
||||
break;
|
||||
case O_UNEQUAL:
|
||||
this->op=shared_ptr<operands>(new operands(T_INTVAR));
|
||||
this->op->generateBox(scopeVar);
|
||||
output_cpp << this->op->boxName() << "=(" << l->boxName() << "!=" << r->boxName() << ")?-1:0;\n";
|
||||
output_cpp << this->op->boxName() << "=(" << l->boxName()
|
||||
<< "!=" << r->boxName() << ")?-1:0;\n";
|
||||
break;
|
||||
default:
|
||||
errorLevel=E_INTERNAL;
|
||||
@@ -300,20 +329,6 @@ shared_ptr<operands>expression::evaluate()
|
||||
return this->op;
|
||||
}
|
||||
|
||||
shared_ptr<operands> expression::stringEval(shared_ptr<operands>l, shared_ptr<operands>r)
|
||||
{
|
||||
if (this->getOp()==O_STRING_CONCAT)
|
||||
{
|
||||
this->op=shared_ptr<operands>(new operands(T_STRINGVAR));
|
||||
this->op->generateBox(scopeGlobal?S_GLOBAL:S_LOCAL);
|
||||
output_cpp << this->op->boxName() << "=" << l->boxName() << "+" << r->boxName();
|
||||
}
|
||||
else error(E_INTERNAL);
|
||||
/* convert expression into single operand */
|
||||
this->oper=O_TERM;
|
||||
return this->op;
|
||||
}
|
||||
|
||||
/* variable definitions */
|
||||
variable::variable(enum SCOPES s, string &name, enum TYPES t):operands(t)
|
||||
{
|
||||
@@ -350,7 +365,8 @@ shared_ptr<variable> 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<variable>(new variable(scopeGlobal?S_GLOBAL:S_LOCAL, name, t));
|
||||
return shared_ptr<variable>(new variable(scopeGlobal?S_GLOBAL:S_LOCAL,
|
||||
name, t));
|
||||
}
|
||||
|
||||
void variable::assignment(shared_ptr<expression>value)
|
||||
|
||||
Reference in New Issue
Block a user