Fixed longstanding bugs and cleaned up code
This commit is contained in:
parent
bf70fd087b
commit
6a3c37b02c
40
tester.cpp
40
tester.cpp
@ -276,10 +276,10 @@ void shutDown()
|
||||
|
||||
variableType *v;
|
||||
printSegment *print;
|
||||
fn *func;
|
||||
operands *o;
|
||||
expression *e;
|
||||
list<operands *>p;
|
||||
|
||||
void testInt()
|
||||
{
|
||||
logger("testInt started");
|
||||
@ -331,7 +331,7 @@ void testFunc()
|
||||
logger("testFunc started");
|
||||
string name=string("square");
|
||||
o = operands::createOp(T_FLOATVAR);
|
||||
func=fn::declare(name, T_FLOATFUNC, o);
|
||||
fn *func=fn::declare(name, T_FLOATFUNC, o);
|
||||
name=string("radius");
|
||||
func->addParameter(name, T_FLOATVAR);
|
||||
logger("param added");
|
||||
@ -349,10 +349,43 @@ void testFunc()
|
||||
logger("call generated");
|
||||
e=new expression(o);
|
||||
print=new printSegment(e);
|
||||
print->generate();
|
||||
o->dispose();
|
||||
delete print;
|
||||
p.clear();
|
||||
logger("testFunc cleared");
|
||||
}
|
||||
|
||||
void testIf()
|
||||
{
|
||||
logger("testIf started");
|
||||
string name=string("check");
|
||||
logger("name declared");
|
||||
fn *func2=fn::declare(name, T_UNKNOWNFUNC);
|
||||
logger("check fn declared");
|
||||
string name2=string("checker");
|
||||
func2->addParameter(name2, T_FLOATVAR);
|
||||
logger("checker added");
|
||||
o=operands::createOp(T_FLOATVAR);
|
||||
e=new expression(new expression(o), O_LESS, new expression(new constOp("0.0", T_FLOAT)));
|
||||
ifStatement *i=new ifStatement(e);
|
||||
print=new printSegment(new expression(new constOp("less", T_STRING)));
|
||||
print->generate();
|
||||
delete print;
|
||||
logger("testFunc cleared");
|
||||
i->alternative();
|
||||
print=new printSegment(new expression(new constOp("greater or equal", T_STRING)));
|
||||
print->generate();
|
||||
delete print;
|
||||
i->close();
|
||||
func2->close();
|
||||
delete i;
|
||||
p.push_back(new constOp("0.0", T_FLOAT));
|
||||
func2->generateCall(name, p);
|
||||
p.clear();
|
||||
p.push_back(new constOp("-0.1", T_FLOAT));
|
||||
func2->generateCall(name, p);
|
||||
p.clear();
|
||||
logger("testIf cleared");
|
||||
}
|
||||
|
||||
void testForLoop()
|
||||
@ -384,6 +417,7 @@ void compile()
|
||||
testString();
|
||||
testFloat();
|
||||
testFunc();
|
||||
testIf();
|
||||
testForLoop();
|
||||
logger("generating end");
|
||||
label::generateEnd();
|
||||
|
@ -286,7 +286,7 @@ public:
|
||||
label(){this->id = ++nextID;}
|
||||
label(string &s)
|
||||
{
|
||||
label();
|
||||
this->id = ++nextID;
|
||||
label::lookup[s]=unique_ptr<label>(this);
|
||||
}
|
||||
|
||||
@ -411,7 +411,7 @@ class fn
|
||||
unsigned int id;
|
||||
enum CODES type;
|
||||
enum TYPES kind;
|
||||
operands *rc;
|
||||
operands *rc; // Return Code
|
||||
label *startAddr;
|
||||
label *skipDef;
|
||||
/* stamdard constructor called by declare */
|
||||
|
@ -100,7 +100,7 @@ void label::generateCondJump(expression *e)
|
||||
if (o->getType()==T_INT||o->getType()==T_INTVAR)
|
||||
{
|
||||
output_cpp<< "if(" << o->boxName()
|
||||
<< "!=0)state=" << this->getID() << ";\nbreak;\n";
|
||||
<< "!=0){state=" << this->getID() << ";break;}\n";
|
||||
o->dispose();
|
||||
delete e;
|
||||
return;
|
||||
|
@ -75,6 +75,8 @@ void fn::addParameter(string &name, enum TYPES t)
|
||||
/* TODO needs to be broken into smaller pieces */
|
||||
operands *fn::generateCall(string &name, list<operands *>¶mList)
|
||||
{
|
||||
static unsigned int callID;
|
||||
unsigned int callEnumerator;
|
||||
auto v=params.begin();
|
||||
operands *current;
|
||||
label *retAddr=new label();
|
||||
@ -88,12 +90,13 @@ operands *fn::generateCall(string &name, list<operands *>¶mList)
|
||||
error(E_TOO_MANY_PARAMETERS);
|
||||
}
|
||||
/* TODO CHECK THIS */
|
||||
callEnumerator = ++callID;
|
||||
heap_h << "struct f" << g->getID()
|
||||
<< " *sub" << this->getID() << ";\n";
|
||||
<< " *sub" << callEnumerator << ";\n";
|
||||
output_cpp << " sub" << this->getID()
|
||||
<< "= new f" << g->getID()
|
||||
<< "(" << retAddr->getID() << ");\n"
|
||||
<< "callStack = sub" << this->getID() << ";\n";
|
||||
<< "callStack = sub" << callEnumerator << ";\n";
|
||||
|
||||
/* TODO Make parameter processing a separate function */
|
||||
while(paramList.size()>0)
|
||||
@ -243,10 +246,11 @@ void fn::close()
|
||||
|
||||
fn *fn::declare(string &s, enum CODES t, operands *returnCode)
|
||||
{
|
||||
/* sd is the skipDef of this function */
|
||||
label *sd=new label();
|
||||
/*check for nesting error */
|
||||
/* check for nesting error */
|
||||
if (!scopeGlobal) error(E_END_FUNCTION);
|
||||
/*check if this function name is already used*/
|
||||
/* check if this function name is already used */
|
||||
if (fn::functions.find(s)!=fn::functions.end()) error(E_DUPLICATE_SYMBOL);
|
||||
logger("declaration name cleared");
|
||||
sd->generateJumpTo();
|
||||
@ -281,6 +285,7 @@ fn::fn(enum CODES t, operands *returnCode)
|
||||
startAddr->generate();
|
||||
}
|
||||
|
||||
/* Destructor is called only at the end of the unique pointer's existence */
|
||||
fn::~fn()
|
||||
{
|
||||
delete startAddr;
|
||||
|
Loading…
x
Reference in New Issue
Block a user