From 4943489628d2913ad4a29bd8b43cbb8bfba02ada Mon Sep 17 00:00:00 2001 From: "Samuel D. Crow" Date: Fri, 12 Mar 2021 21:57:53 -0600 Subject: [PATCH] added loop constructs and a few missing methods elsewhere --- yab2cpp.cpp | 135 +++++++++++++++++++++++++++++++++++++++++++++++++++- yab2cpp.h | 127 +++++++++++++++++++++++------------------------- 2 files changed, 192 insertions(+), 70 deletions(-) diff --git a/yab2cpp.cpp b/yab2cpp.cpp index 052c125..1cc4c4a 100644 --- a/yab2cpp.cpp +++ b/yab2cpp.cpp @@ -173,16 +173,39 @@ static void operands::dumpVars(ostream &out) } out << endl; } -unsigned int operands::getOrCreateStr(string &s, ostream &k) +unsigned int operands::getOrCreateStr(ostream &k, string &s) { auto iter=constStr.find(s); if (iter!=constStr.end()) return iter->second; ++nextID; - k << "const string sk" << nextID << "=\"" << s << "\";\n"; + k << "const string k" << nextID << "=\"" << s << "\";\n"; constStr[s]=nextID; return nextID; } +unsigned int operands::createConst(ostream &k, string &s, enum TYPES t) +{ + operands *me=new operands(t); + if (t==T_INT) + { + k << "const int k"; + } + else + { + if (t==T_FLOAT) + { + k << "const double k"; + } + else + { + errorLevel=E_TYPE_MISMATCH; + exit(1); + } + } + k << me->getID() << "=" << s << ";\n"; + return me; +} + enum TYPES operands::getSimpleVarType() { switch type @@ -559,11 +582,119 @@ void conditional::close(ostream &out) exit(1); } this->done->generate(); +} + +conditional::~conditional() +{ delete this->done; delete this->redo; } /* Loop definitions */ +repeatLoop::repeatLoop(ostream &out):codeType(T_REPEATLOOP) +{ + this->loopStart=new label(); + this->loopEnd=new label(); + loopStart->generate(out;) +} + +void repeatLoop::generateBreak(ostream &out) +{ + loopEnd->generateJumpTo(out); +} + +void repeatLoop::close(ostream &out, expression *e) +{ + expression *f=new expression(e, O_NOT); + loopStart->generateCondJump(out, f); + loopEnd->generate(out); +} + +repeatLoop::~repeatLoop() +{ + delete loopStart; + delete loopEnd; +} + +doLoop::doLoop(ostream &out):codeType(T_DOLOOP) +{ + this->loopStart=new label(); + this->loopEnd=new label(); + loopStart->generate(out;) +} + +void doLoop::generateBreak(ostream &out) +{ + loopEnd->generateJumpTo(out); +} + +void doLoop::close(ostream &out) +{ + this->loopStart->generateJumpTo(out); + this->loopEnd->generate(out); +} + +doLoop::~doLoop() +{ delete loopStart; + delete loopEnd; +} + +whileLoop::whileLoop(ostream &out, expression *e):codeType(T_WHILELOOP) +{ + loopContinue=new label(); + loopStart=new label(); + loopEnd=new label(); + cond=e; + loopStart->generateJumpTo(out); + loopContinue->generate(out); +} + +void whileLoop::generateBreak(ostream &out) +{ + loopEnd->generateJumpTo(out); +} + +void whileLoop::close(ostream &out) +{ + loopStart->generate(out); + loopContinue->generateJumpCond(out, cond); + loopEnd->generate(out); +} + +whileLoop::~whileLoop() +{ + delete loopStart; + delete loopContinue; + delete loopEnd; +} + +/* TODO: make the stopper into a full range check */ +forLoop::forLoop(ostream &out, ostream &k, variable *v, expression *start, expression *stop, expression *stepVal=NULL):codeType(T_FORLOOP) +{ + expression *stopper=new expression(new expression (v), O_UNEQUAL, stop); + v->assignment(out, start); + infrastructure=new whileLoop(out, stopper); + if (stepVal) + { + step=stepVal; + } + else + { + step=new expression(operands::createConst(k, "1", T_INT)); + } +} + +void forLoop::generateBreak(ostream &out) +{ + infrastructure->generateBreak(out); +} + +void forLoop::close(ostream &out) +{ + expression *stepper=new expression(new expression(v), O_PLUS, step); + v->assignment(out, stepper) + infrastructure->close(ostream &out); +} /* function definitions */ diff --git a/yab2cpp.h b/yab2cpp.h index dd06cd8..5ca3ded 100644 --- a/yab2cpp.h +++ b/yab2cpp.h @@ -168,7 +168,8 @@ public: unsigned int getID() const {return id;} static void dumpVars(ostream &out); - static unsigned int getOrCreateStr(string &s); + static unsigned int getOrCreateStr(ostream &k, string &s); + static operands *createConst(ostream &k, string &s, enum TYPES t); enum TYPES getSimpleVarType(); void generateBox(ostream &out); @@ -202,6 +203,11 @@ public: this->right=r; this->oper=o; } + expression(operands x) + { + op=x; + oper=O_TERM; + } virtual ~expression(); }; @@ -218,11 +224,11 @@ public: codeType *getCurrent(); virtual void close(); + virtual void generateBreak(ostream &out)=0; explicit codeType(enum CODES t); virtual ~codeType() - { - } + {} }; class label @@ -257,83 +263,88 @@ public: /* if statement */ class conditional:public codeType { - shared_ptr