Started enforcing style guide. Fixed all warnings.

This commit is contained in:
Samuel D. Crow
2021-03-23 15:25:59 -05:00
parent 68bf3cab0f
commit fbb60f4e3d
3 changed files with 86 additions and 52 deletions

View File

@@ -32,6 +32,7 @@ extern ofstream varNames;
extern unordered_map<string, shared_ptr<variable> >globals; extern unordered_map<string, shared_ptr<variable> >globals;
extern unordered_map<string, shared_ptr<variable> >locals; extern unordered_map<string, shared_ptr<variable> >locals;
extern unordered_map<string, shared_ptr<variable> >statics; extern unordered_map<string, shared_ptr<variable> >statics;
/* /*
** list of all compiler errors ** list of all compiler errors
** **
@@ -189,6 +190,7 @@ public:
{} {}
}; };
/* constant operands */
class constOp:public operands class constOp:public operands
{ {
/* box is defined once in the constructor */ /* 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 class expression
{ {
shared_ptr<operands>op; shared_ptr<operands>op;
@@ -221,15 +223,19 @@ public:
bool isBinOp(); bool isBinOp();
shared_ptr<operands>evaluate(); 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 */ /* 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->left=l;
this->right=r; this->right=r;
this->oper=o; this->oper=o;
} }
/* Terminal expression node */
expression(shared_ptr<operands>x) expression(shared_ptr<operands>x)
{ {
op=x; op=x;
@@ -290,14 +296,19 @@ public:
/* if statement */ /* if statement */
class ifStatement:public codeType class ifStatement:public codeType
{ {
shared_ptr<label>redo; /* for continue command */ /* for continue command */
shared_ptr<label>done; /* for break or after "then" condition */ shared_ptr<label>redo;
shared_ptr<label>chain; /* For elsif command */ /* for break or after "then" condition */
shared_ptr<label>done;
/* For elsif command */
shared_ptr<label>chain;
public: public:
void generateContinue(); void generateContinue();
virtual void generateBreak() override; virtual void generateBreak() override;
void alternative(shared_ptr<expression>e=NULL); /* enable else or elsif condition */ /* enable else or elsif condition */
virtual void close() override; /* end if */ void alternative(shared_ptr<expression>e=NULL);
/* end if */
virtual void close() override;
explicit ifStatement(shared_ptr<expression>e); explicit ifStatement(shared_ptr<expression>e);
virtual ~ifStatement() virtual ~ifStatement()
@@ -365,7 +376,8 @@ class arrayType:public variable
public: public:
virtual string &boxName(list<unsigned int>indexes); 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() virtual ~arrayType()
{} {}
}; };
@@ -375,13 +387,14 @@ class forLoop:public codeType
shared_ptr<variable>var; shared_ptr<variable>var;
shared_ptr<variable>startTemp; shared_ptr<variable>startTemp;
shared_ptr<variable>stopTemp; shared_ptr<variable>stopTemp;
whileLoop *infrastructure; shared_ptr<whileLoop>infrastructure;
shared_ptr<expression>step; shared_ptr<expression>step;
public: public:
virtual void generateBreak(); virtual void generateBreak();
virtual void close(); 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() virtual ~forLoop()
{} {}
}; };
@@ -413,7 +426,8 @@ public:
int getNumParams() const {return this->params.size();} int getNumParams() const {return this->params.size();}
void addParameter(shared_ptr<variable>); void addParameter(shared_ptr<variable>);
shared_ptr<operands>generateCall(string &name, list<shared_ptr<operands> >&paramList); shared_ptr<operands>generateCall(string &name,
list<shared_ptr<operands> >&paramList);
void generateReturn(shared_ptr<expression>expr); void generateReturn(shared_ptr<expression>expr);
void generateReturn(); void generateReturn();
virtual void generateBreak(); virtual void generateBreak();

View File

@@ -31,7 +31,8 @@ void label::dumpLabels()
varNames << "Global Labels\n\n"; varNames << "Global Labels\n\n";
for(auto iter=lookup.begin(); iter!=lookup.end(); ++iter) 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; varNames << endl;
} }
@@ -113,7 +114,8 @@ void ifStatement::alternative(shared_ptr<expression>e)
if(e!=NULL) if(e!=NULL)
{ {
this->chain=shared_ptr<label>(new label()); 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); chain->generateCondJump(f);
} }
} }
@@ -195,8 +197,9 @@ forLoop::forLoop(shared_ptr<variable>v,
stopTemp->assignment(stop); stopTemp->assignment(stop);
/* if (v<stopTemp) */ /* if (v<stopTemp) */
shared_ptr<ifStatement>c=shared_ptr<ifStatement>(new ifStatement( shared_ptr<ifStatement>c=shared_ptr<ifStatement>(new ifStatement(
shared_ptr<expression>(new expression(shared_ptr<expression>(new expression(v)), shared_ptr<expression>(new expression(shared_ptr<expression>(
O_LESS, shared_ptr<expression>(new expression(stopTemp)))))); new expression(v)), O_LESS, shared_ptr<expression>(
new expression(stopTemp))))));
/* startTemp=v;*/ /* startTemp=v;*/
startTemp->assignment(shared_ptr<expression>(new expression(v))); startTemp->assignment(shared_ptr<expression>(new expression(v)));
/* else */ /* else */
@@ -216,9 +219,10 @@ forLoop::forLoop(shared_ptr<variable>v,
shared_ptr<expression>(new expression(startTemp)))); shared_ptr<expression>(new expression(startTemp))));
shared_ptr<expression>stopper=shared_ptr<expression>(new expression( shared_ptr<expression>stopper=shared_ptr<expression>(new expression(
stopper1, O_AND, stopper2)); stopper1, O_AND, stopper2));
this->infrastructure=new whileLoop(shared_ptr<expression>(new expression( shared_ptr<whileLoop>infrastructure=shared_ptr<whileLoop>(new whileLoop(
stopper, O_UNEQUAL, shared_ptr<expression>(new expression( shared_ptr<expression>(new expression(stopper, O_UNEQUAL,
shared_ptr<constOp>(new constOp("0", T_INT))))))); shared_ptr<expression>(new expression(
shared_ptr<constOp>(new constOp("0", T_INT))))))));
if (stepVal) if (stepVal)
{ {
step=stepVal; step=stepVal;

View File

@@ -23,7 +23,10 @@ ofstream &scope::operator<<(ostream &in)
case S_GLOBAL: case S_GLOBAL:
case S_STATIC: case S_STATIC:
return heap_h; return heap_h;
default:
break;
} }
error(E_INTERNAL);
} }
/* methods for operands */ /* methods for operands */
@@ -43,6 +46,8 @@ enum TYPES operands::getSimpleVarType()
case T_STRINGCALL_ARRAY: case T_STRINGCALL_ARRAY:
case T_STRINGVAR: case T_STRINGVAR:
return T_STRINGVAR; return T_STRINGVAR;
default:
break;
} }
error(E_UNASSIGNABLE_TYPE); error(E_UNASSIGNABLE_TYPE);
} }
@@ -156,14 +161,17 @@ bool expression::isBinOp()
{ {
switch (this->getOp()) switch (this->getOp())
{ {
/* fallthrough for multiselect */
case O_NEGATE: case O_NEGATE:
case O_NOT: case O_NOT:
case O_INVERT: case O_INVERT:
case O_INT_TO_FLOAT: case O_INT_TO_FLOAT:
return false; return false;
break; default:
return true;
} }
return true; /* unreachable code */
error(E_INTERNAL);
} }
shared_ptr<operands>expression::evaluate() shared_ptr<operands>expression::evaluate()
@@ -181,12 +189,14 @@ shared_ptr<operands>expression::evaluate()
enum TYPES rt=r->getSimpleVarType(); enum TYPES rt=r->getSimpleVarType();
if (lt==T_INTVAR && rt==T_FLOATVAR) 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; lt=T_FLOATVAR;
} }
if (lt==T_FLOATVAR && rt==T_INTVAR) 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; rt=T_FLOATVAR;
} }
if (lt!=rt)error(E_TYPE_MISMATCH); if (lt!=rt)error(E_TYPE_MISMATCH);
@@ -197,9 +207,15 @@ shared_ptr<operands>expression::evaluate()
t=l->getSimpleVarType(); t=l->getSimpleVarType();
r=NULL; r=NULL;
} }
if (t==T_STRINGVAR) return expression::stringEval(l, r);
switch (this->getOp()) 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: case O_INVERT:
this->op=shared_ptr<operands>(new operands(t)); this->op=shared_ptr<operands>(new operands(t));
this->op->generateBox(scopeVar); this->op->generateBox(scopeVar);
@@ -226,69 +242,82 @@ shared_ptr<operands>expression::evaluate()
if (t!=T_INTVAR) error(E_TYPE_MISMATCH); if (t!=T_INTVAR) error(E_TYPE_MISMATCH);
this->op=shared_ptr<operands>(new operands(T_INTVAR)); this->op=shared_ptr<operands>(new operands(T_INTVAR));
this->op->generateBox(scopeVar); 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; break;
case O_DIVIDE: case O_DIVIDE:
this->op=shared_ptr<operands>(new operands(t)); this->op=shared_ptr<operands>(new operands(t));
this->op->generateBox(scopeVar); 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; break;
case O_PLUS: case O_PLUS:
this->op=shared_ptr<operands>(new operands(t)); this->op=shared_ptr<operands>(new operands(t));
this->op->generateBox(scopeVar); 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; break;
case O_MINUS: case O_MINUS:
this->op=shared_ptr<operands>(new operands(t)); this->op=shared_ptr<operands>(new operands(t));
this->op->generateBox(scopeVar); 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; break;
case O_MULTIPLY: case O_MULTIPLY:
this->op=shared_ptr<operands>(new operands(t)); this->op=shared_ptr<operands>(new operands(t));
this->op->generateBox(scopeVar); 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; break;
case O_OR: case O_OR:
if (t!=T_INTVAR) error(E_TYPE_MISMATCH); if (t!=T_INTVAR) error(E_TYPE_MISMATCH);
this->op=shared_ptr<operands>(new operands(T_INTVAR)); this->op=shared_ptr<operands>(new operands(T_INTVAR));
this->op->generateBox(scopeVar); 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; break;
case O_AND: case O_AND:
if (t!=T_INTVAR) error(E_TYPE_MISMATCH); if (t!=T_INTVAR) error(E_TYPE_MISMATCH);
this->op=shared_ptr<operands>(new operands(T_INTVAR)); this->op=shared_ptr<operands>(new operands(T_INTVAR));
this->op->generateBox(scopeVar); 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; break;
case O_GREATER: case O_GREATER:
this->op=shared_ptr<operands>(new operands(T_INTVAR)); this->op=shared_ptr<operands>(new operands(T_INTVAR));
this->op->generateBox(scopeVar); 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; break;
case O_LESS: case O_LESS:
this->op=shared_ptr<operands>(new operands(T_INTVAR)); this->op=shared_ptr<operands>(new operands(T_INTVAR));
this->op->generateBox(scopeVar); 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; break;
case O_GREATER_EQUAL: case O_GREATER_EQUAL:
this->op=shared_ptr<operands>(new operands(T_INTVAR)); this->op=shared_ptr<operands>(new operands(T_INTVAR));
this->op->generateBox(scopeVar); 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; break;
case O_LESS_EQUAL: case O_LESS_EQUAL:
this->op=shared_ptr<operands>(new operands(T_INTVAR)); this->op=shared_ptr<operands>(new operands(T_INTVAR));
this->op->generateBox(scopeVar); 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; break;
case O_EQUAL: case O_EQUAL:
this->op=shared_ptr<operands>(new operands(T_INTVAR)); this->op=shared_ptr<operands>(new operands(T_INTVAR));
this->op->generateBox(scopeVar); 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; break;
case O_UNEQUAL: case O_UNEQUAL:
this->op=shared_ptr<operands>(new operands(T_INTVAR)); this->op=shared_ptr<operands>(new operands(T_INTVAR));
this->op->generateBox(scopeVar); 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; break;
default: default:
errorLevel=E_INTERNAL; errorLevel=E_INTERNAL;
@@ -300,20 +329,6 @@ shared_ptr<operands>expression::evaluate()
return this->op; 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 definitions */
variable::variable(enum SCOPES s, string &name, enum TYPES t):operands(t) 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(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, name, t)); return shared_ptr<variable>(new variable(scopeGlobal?S_GLOBAL:S_LOCAL,
name, t));
} }
void variable::assignment(shared_ptr<expression>value) void variable::assignment(shared_ptr<expression>value)