2021-03-13 23:49:58 +01:00
|
|
|
/*
|
|
|
|
** Yab2Cpp
|
|
|
|
**
|
|
|
|
** Transpiler by Samuel D. Crow
|
|
|
|
**
|
|
|
|
** Based on Yab
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
#include "yab2cpp.h"
|
|
|
|
|
2021-03-20 01:26:10 +01:00
|
|
|
/* static initializers */
|
2021-03-29 05:35:35 +02:00
|
|
|
unordered_map<string, unique_ptr<fn> > fn::functions;
|
|
|
|
list<fn *>fn::callStack;
|
2021-03-25 00:17:33 +01:00
|
|
|
unsigned int fn::nextID=0;
|
2021-03-16 23:01:28 +01:00
|
|
|
|
2021-03-13 23:49:58 +01:00
|
|
|
/* function definitions */
|
2021-03-20 01:26:10 +01:00
|
|
|
void fn::dumpCallStack()
|
|
|
|
{
|
|
|
|
auto i=callStack.rbegin();
|
|
|
|
if (i==callStack.rend())
|
|
|
|
{
|
|
|
|
logfile << "call stack was empty\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
do
|
|
|
|
{
|
|
|
|
logfile << (*i)->funcName << "\n";
|
|
|
|
++i;
|
|
|
|
} while(i!=callStack.rend());
|
|
|
|
}
|
|
|
|
|
2021-03-29 05:35:35 +02:00
|
|
|
fn *fn::getCurrentSub()
|
2021-03-13 23:49:58 +01:00
|
|
|
{
|
2021-03-16 23:01:28 +01:00
|
|
|
return callStack.back();
|
2021-03-13 23:49:58 +01:00
|
|
|
}
|
|
|
|
|
2021-03-29 05:35:35 +02:00
|
|
|
void fn::generateOnNSub(expression *e, unsigned int skip)
|
2021-03-13 23:49:58 +01:00
|
|
|
{
|
2021-03-29 05:35:35 +02:00
|
|
|
label *r=new label();
|
|
|
|
fn *self=new fn(r);
|
2021-03-15 20:04:53 +01:00
|
|
|
fn::callStack.push_back(self);
|
2021-03-16 23:01:28 +01:00
|
|
|
label::generateOnNTo(e, skip);
|
2021-03-15 20:04:53 +01:00
|
|
|
r->generate();
|
2021-03-13 23:49:58 +01:00
|
|
|
}
|
|
|
|
|
2021-03-29 05:35:35 +02:00
|
|
|
void fn::generateGosub(label *sub)
|
2021-03-13 23:49:58 +01:00
|
|
|
{
|
2021-03-29 05:35:35 +02:00
|
|
|
label *r=new label();
|
|
|
|
fn *self=new fn(r);
|
2021-03-15 20:04:53 +01:00
|
|
|
fn::callStack.push_back(self);
|
|
|
|
sub->generateJumpTo();
|
|
|
|
r->generate();
|
|
|
|
}
|
|
|
|
|
2021-03-29 05:35:35 +02:00
|
|
|
fn::fn(label *gosub):codeType(T_GOSUB)
|
2021-03-15 20:04:53 +01:00
|
|
|
{
|
2021-03-20 01:26:10 +01:00
|
|
|
this->funcName="unnamed gosub";
|
2021-03-15 20:04:53 +01:00
|
|
|
this->ret=gosub;
|
|
|
|
}
|
|
|
|
|
2021-03-29 05:35:35 +02:00
|
|
|
fn *fn::getSub(string &name)
|
2021-03-15 20:04:53 +01:00
|
|
|
{
|
2021-03-16 23:01:28 +01:00
|
|
|
auto iter=fn::functions.find(name);
|
2021-03-29 05:35:35 +02:00
|
|
|
if(iter==fn::functions.end()) return nullptr;
|
|
|
|
return iter->second.get();
|
2021-03-15 20:04:53 +01:00
|
|
|
}
|
|
|
|
|
2021-03-29 05:35:35 +02:00
|
|
|
operands *fn::generateCall(string &name, list<operands *>¶mList)
|
2021-03-15 20:04:53 +01:00
|
|
|
{
|
|
|
|
auto v=params.begin();
|
2021-03-29 05:35:35 +02:00
|
|
|
operands *current;
|
|
|
|
fn *g=fn::getSub(name);
|
|
|
|
if (g==nullptr)
|
2021-03-15 20:04:53 +01:00
|
|
|
{
|
2021-03-16 23:01:28 +01:00
|
|
|
error(E_SUBROUTINE_NOT_FOUND);
|
2021-03-15 20:04:53 +01:00
|
|
|
}
|
|
|
|
if (paramList.size()>params.size())
|
|
|
|
{
|
2021-03-16 23:01:28 +01:00
|
|
|
error(E_TOO_MANY_PARAMETERS);
|
2021-03-15 20:04:53 +01:00
|
|
|
}
|
2021-03-16 23:01:28 +01:00
|
|
|
/* TODO CHECK THIS */
|
|
|
|
output_cpp << "struct f" << g->getID()
|
|
|
|
<< "*sub" << this->getID()
|
|
|
|
<< "= new struct f" << g->getID()
|
|
|
|
<< "();\n";
|
2021-03-18 00:56:14 +01:00
|
|
|
|
|
|
|
/* TODO Make parameter processing a separate function */
|
2021-03-15 20:04:53 +01:00
|
|
|
while(paramList.size()>0)
|
|
|
|
{
|
2021-03-16 23:01:28 +01:00
|
|
|
current= paramList.front();
|
2021-03-15 20:04:53 +01:00
|
|
|
paramList.pop_front();
|
2021-03-16 23:01:28 +01:00
|
|
|
if(current->getSimpleVarType()!=(*v)->getType())
|
2021-03-15 20:04:53 +01:00
|
|
|
{
|
2021-03-16 23:01:28 +01:00
|
|
|
error(E_TYPE_MISMATCH);
|
|
|
|
}
|
2021-03-29 05:35:35 +02:00
|
|
|
(*v)->assignment(new expression(current));
|
2021-03-16 23:01:28 +01:00
|
|
|
++v;
|
|
|
|
}
|
|
|
|
/* pad remaining unassigned variables with empty values */
|
|
|
|
while (v!=params.end())
|
|
|
|
{
|
|
|
|
switch ((*v)->getType())
|
|
|
|
{
|
|
|
|
case T_FLOATVAR:
|
2021-03-29 05:35:35 +02:00
|
|
|
(*v)->assignment(new expression(new constOp("0.0", T_FLOAT)));
|
2021-03-16 23:01:28 +01:00
|
|
|
break;
|
|
|
|
case T_INTVAR:
|
2021-03-29 05:35:35 +02:00
|
|
|
(*v)->assignment(new expression(new constOp("0", T_INT)));
|
2021-03-16 23:01:28 +01:00
|
|
|
break;
|
|
|
|
case T_STRINGVAR:
|
2021-03-29 05:35:35 +02:00
|
|
|
(*v)->assignment(new expression(new constOp("", T_STRING)));
|
2021-03-16 23:01:28 +01:00
|
|
|
default:
|
2021-03-18 00:56:14 +01:00
|
|
|
error(E_INTERNAL);
|
2021-03-15 20:04:53 +01:00
|
|
|
}
|
|
|
|
++v;
|
|
|
|
}
|
2021-03-18 00:56:14 +01:00
|
|
|
g->startAddr->generateJumpTo();
|
|
|
|
g->ret->generate();
|
|
|
|
return g->rc;
|
2021-03-15 20:04:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void fn::generateReturn()
|
|
|
|
{
|
2021-03-29 05:35:35 +02:00
|
|
|
fn *c=getCurrentSub();
|
2021-03-15 20:04:53 +01:00
|
|
|
switch(c->getType())
|
|
|
|
{
|
|
|
|
case T_UNKNOWNFUNC:
|
2021-03-16 23:01:28 +01:00
|
|
|
/* set return type to NONE */
|
2021-03-15 20:04:53 +01:00
|
|
|
this->kind=T_NONE;
|
|
|
|
/*fallthrough*/
|
|
|
|
case T_GOSUB:
|
|
|
|
c->ret->generateJumpTo();
|
|
|
|
fn::callStack.pop_back();
|
|
|
|
break;
|
|
|
|
default:
|
2021-03-18 22:18:54 +01:00
|
|
|
error(E_TYPE_MISMATCH);
|
2021-03-15 20:04:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-29 05:35:35 +02:00
|
|
|
void fn::generateReturn(expression *expr)
|
2021-03-15 20:04:53 +01:00
|
|
|
{
|
2021-03-18 00:56:14 +01:00
|
|
|
this->rc=expr->evaluate();
|
|
|
|
this->kind=rc->getSimpleVarType();
|
2021-03-15 20:04:53 +01:00
|
|
|
this->ret->generateJumpTo();
|
|
|
|
fn::callStack.pop_back();
|
2021-03-29 05:35:35 +02:00
|
|
|
delete expr;
|
2021-03-15 20:04:53 +01:00
|
|
|
switch (this->getType())
|
|
|
|
{
|
|
|
|
case T_UNKNOWNFUNC:
|
2021-03-18 00:56:14 +01:00
|
|
|
return;
|
2021-03-15 20:04:53 +01:00
|
|
|
case T_STRINGFUNC:
|
2021-03-18 00:56:14 +01:00
|
|
|
if (kind!=T_STRINGVAR) error(E_TYPE_MISMATCH);
|
|
|
|
return;
|
2021-03-15 20:04:53 +01:00
|
|
|
case T_INTFUNC:
|
2021-03-18 00:56:14 +01:00
|
|
|
if (kind!=T_INTVAR&&kind!=T_FLOATVAR) error(E_TYPE_MISMATCH);
|
|
|
|
return;
|
2021-03-15 20:04:53 +01:00
|
|
|
case T_FLOATFUNC:
|
2021-03-18 00:56:14 +01:00
|
|
|
if(kind!=T_FLOATVAR) error(E_TYPE_MISMATCH);
|
|
|
|
return;
|
2021-03-15 20:04:53 +01:00
|
|
|
case T_GOSUB:
|
2021-03-18 00:56:14 +01:00
|
|
|
error(E_GOSUB_CANNOT_RETURN_VALUE);
|
2021-03-15 20:04:53 +01:00
|
|
|
default:
|
2021-03-18 00:56:14 +01:00
|
|
|
error(E_BAD_SYNTAX);
|
2021-03-15 20:04:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* not allowed in function definitions directly */
|
|
|
|
void fn::generateBreak()
|
|
|
|
{
|
|
|
|
error(E_BAD_SYNTAX);
|
|
|
|
}
|
|
|
|
|
|
|
|
void fn::close()
|
|
|
|
{
|
|
|
|
/* check if no returns and no return type */
|
|
|
|
enum CODES t=this->getType();
|
|
|
|
if (this->kind==T_UNKNOWN&&
|
|
|
|
(t==T_UNKNOWNFUNC||t==T_VOIDFUNC))
|
|
|
|
{
|
|
|
|
/* generate a typeless return */
|
2021-03-16 23:01:28 +01:00
|
|
|
this->generateReturn();
|
2021-03-15 20:04:53 +01:00
|
|
|
}
|
|
|
|
funcs_h << "};\n";
|
2021-03-20 01:26:10 +01:00
|
|
|
locals.clear();
|
|
|
|
statics.clear();
|
2021-03-15 20:04:53 +01:00
|
|
|
this->params.clear();
|
|
|
|
scopeGlobal=true;
|
|
|
|
}
|
|
|
|
|
2021-03-29 05:35:35 +02:00
|
|
|
fn::fn(string &s, enum CODES t, operands *returnCode):codeType(t)
|
2021-03-15 20:04:53 +01:00
|
|
|
{
|
2021-03-18 00:56:14 +01:00
|
|
|
/*check for nesting error */
|
2021-03-15 20:04:53 +01:00
|
|
|
if (!scopeGlobal) error(E_END_FUNCTION);
|
2021-03-18 00:56:14 +01:00
|
|
|
/*check if this function name is already used*/
|
2021-03-16 23:01:28 +01:00
|
|
|
if (fn::functions.find(s)!=fn::functions.end()) error(E_DUPLICATE_SYMBOL);
|
2021-03-20 01:26:10 +01:00
|
|
|
this->funcName=s;
|
2021-03-15 20:04:53 +01:00
|
|
|
this->id= ++nextID;
|
2021-03-18 00:56:14 +01:00
|
|
|
/*define storage for locals*/
|
2021-03-15 20:04:53 +01:00
|
|
|
funcs_h << "struct f" << this->id <<"\n{\n";
|
2021-03-18 00:56:14 +01:00
|
|
|
/*define label space for return*/
|
2021-03-29 05:35:35 +02:00
|
|
|
this->ret=new label();
|
2021-03-18 00:56:14 +01:00
|
|
|
/*allocate function name*/
|
2021-03-29 05:35:35 +02:00
|
|
|
fn::functions[s]=unique_ptr<fn>(this);
|
2021-03-18 00:56:14 +01:00
|
|
|
/* initiate local scope */
|
2021-03-15 20:04:53 +01:00
|
|
|
scopeGlobal=false;
|
2021-03-18 00:56:14 +01:00
|
|
|
/*keep track of where the return code will be sent to*/
|
|
|
|
this->rc=returnCode;
|
|
|
|
/*allocate and generate start address label*/
|
2021-03-29 05:35:35 +02:00
|
|
|
this->startAddr=new label();
|
2021-03-18 00:56:14 +01:00
|
|
|
startAddr->generate();
|
2021-03-13 23:49:58 +01:00
|
|
|
}
|