diff --git a/yab2cpp.cpp b/yab2cpp.cpp index 1cc4c4a..1e83273 100644 --- a/yab2cpp.cpp +++ b/yab2cpp.cpp @@ -1,18 +1,25 @@ +/* +** Yab2Cpp +** +** Transpiler by Samuel D. Crow +** +** Based on Yab +** +*/ #include "yab2cpp.h" -enum COMPILEERRORS errorLevel=E_OK; +enum COMPILE_ERRORS errorLevel=E_OK; unsigned int mode=0; unsigned int indentLevel=0; bool scopeGlobal=true; -extern ofstream output_cpp; -extern ofstream funcs_h; -extern ofstream heap_h; -extern ofstream consts_h; -extern ofstream logfile; -extern ofstream varNames; - -extern ifstream src; +ifstream src; +ofstream output_cpp; +ofstream funcs_h; +ofstream heap_h; +ofstream consts_h; +ofstream logfile; +ofstream varNames; /* private prototypes */ void helpText(string &); @@ -173,6 +180,7 @@ static void operands::dumpVars(ostream &out) } out << endl; } + unsigned int operands::getOrCreateStr(ostream &k, string &s) { auto iter=constStr.find(s); @@ -288,6 +296,16 @@ void operands::generateBox(ostream &out) out << this->getID() << ";\n"; } +operands *operands::getOrCreateGlobal(ostream &heap, string &s, enum TYPES t) +{ + operands op*=operands::globals->find(s); + if (op==globals.end()) + { + op=new variable(heap, s, t); + } + return op; +} + void operands::boxName(ostream &out) { switch (this->getType()) @@ -444,259 +462,64 @@ expression::~expression() } } -/* base class of all the code structure types */ -codeType::codeType(enum CODES t) +/* variable definitions */ +variable::variable(ostream &scope, string &name, enum TYPES t):operands(t) { - this->id= ++nextID; - nesting.push_back(this); - this->type=t; + this->generateBox(scope); } -codeType *codeType::getCurrent() +variable *variable::getOrCreateVarName(ostream &func, ostream &heap, string &name, enum TYPES t) { - return nesting.back; -} - -void codeType::close() -{ - nesting.pop_back(); -} - -/* label definitions and helper routines */ - -label *label::find(string &s) -{ - auto ret=lookup.find(s); - return(ret==lookup.end()?NULL:ret->second); -} - -void label::dumpLabels(ostream &v) -{ - v << "Global Labels\n\n"; - for(auto iter=lookup.begin(); iter!=lookup.end(); ++iter) + variable *v; + if (!scopeGlobal) { - v << "label " << iter->first << " has ID " << iter->second->getID() << "\n" ; + fn *currentFunc=fn::getCurrentSub(); + v=fn::getOrCreateVar(func, heap, t, name, false); + return v; } - v << endl; + return reinterpret_castoperands::getOrCreateGlobal(heap, name); } -void label::generateJumpTo(ostream &out) +void variable::assignment(expression *value) { - out << "state=" << this->getID() << ";\nbreak;\n"; -} - -void label::generateOnNSkip(ostream &k, list