From bf70fd087b1ff6e77b7568c11b6543c646396837 Mon Sep 17 00:00:00 2001 From: "Samuel D. Crow" Date: Tue, 4 May 2021 12:15:49 -0500 Subject: [PATCH] some cosmetic changes to fix warnings and forLoop --- Makefile | 6 ++--- runtime/Makefile | 6 ++--- runtime/main.cpp | 4 +-- runtime/runtime.h | 4 +-- tester.cpp | 25 ++++++++++++++--- yab2cpp.h | 8 +++--- yabCodeStructures.cpp | 37 ++++++++++++++++++++++---- yabDataStructures.cpp | 62 +++++++++++++++++++++++-------------------- 8 files changed, 102 insertions(+), 50 deletions(-) diff --git a/Makefile b/Makefile index 03f5f1b..5a006d0 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,9 @@ CC := g++ CFLAGS := -Wall CFLAGS += -std=c++11 -CFLAGS += -fno-rtti -CFLAGS += -fno-exceptions -CFLAGS += -Os +#CFLAGS += -fno-rtti +#CFLAGS += -fno-exceptions +#CFLAGS += -Os LFLAGS := ODIR := build diff --git a/runtime/Makefile b/runtime/Makefile index ea86fb2..f6ba35b 100644 --- a/runtime/Makefile +++ b/runtime/Makefile @@ -1,9 +1,9 @@ CC := g++ CFLAGS := -Wall CFLAGS += -std=c++11 -CFLAGS += -fno-rtti -CFLAGS += -fno-exceptions -CFLAGS += -Os +#CFLAGS += -fno-rtti +#CFLAGS += -fno-exceptions +#CFLAGS += -Os LFLAGS := all: binaries diff --git a/runtime/main.cpp b/runtime/main.cpp index 19fc885..4f901d1 100644 --- a/runtime/main.cpp +++ b/runtime/main.cpp @@ -5,7 +5,7 @@ */ #include "runtime.h" -struct subroutine *callStack=nullptr; +subroutine *callStack=nullptr; subroutine::subroutine(unsigned int r) { @@ -17,7 +17,7 @@ unsigned int subroutine::close() { if (callStack==nullptr) return STACK_UNDERFLOW_ERROR; unsigned int r=callStack->ret; - struct subroutine *l=callStack->called; + subroutine *l=callStack->called; delete callStack; callStack=l; return r; diff --git a/runtime/runtime.h b/runtime/runtime.h index 6066104..9432eb2 100644 --- a/runtime/runtime.h +++ b/runtime/runtime.h @@ -20,7 +20,7 @@ enum STATES:unsigned int class subroutine { - struct subroutine *called; + subroutine *called; unsigned int ret; public: @@ -30,7 +30,7 @@ public: {} }; -extern struct subroutine *callStack; +extern subroutine *callStack; /* function prototype */ unsigned int run(); diff --git a/tester.cpp b/tester.cpp index 1d97e8d..006b97a 100644 --- a/tester.cpp +++ b/tester.cpp @@ -330,11 +330,8 @@ void testFunc() { logger("testFunc started"); string name=string("square"); - logger("aloha"); o = operands::createOp(T_FLOATVAR); - logger("mahalo"); func=fn::declare(name, T_FLOATFUNC, o); - logger("funkBeat"); name=string("radius"); func->addParameter(name, T_FLOATVAR); logger("param added"); @@ -358,6 +355,27 @@ void testFunc() logger("testFunc cleared"); } +void testForLoop() +{ + logger("testForLoop started"); + string name=string("counter"); + logger("creating counter variable"); + v=variableType::getOrCreateVar(name, T_INTVAR); + logger("initializing forLoop"); + forLoop *f=new forLoop(v,new expression(new constOp("1", T_INT)), + new expression(new constOp("10", T_INT)), + new expression(new constOp("2", T_INT))); + logger("printing variable"); + print=new printSegment(new expression(v), S_LINEFEED); + print->generate(); + logger("closing testForLoop"); + f->close(); + delete f; + logger("disposing of counter variable"); + v->dispose(); + logger("testForLoop cleared"); +} + /* open files and compile */ void compile() { @@ -366,6 +384,7 @@ void compile() testString(); testFloat(); testFunc(); + testForLoop(); logger("generating end"); label::generateEnd(); /*check for nesting error */ diff --git a/yab2cpp.h b/yab2cpp.h index 8c66326..8956eab 100644 --- a/yab2cpp.h +++ b/yab2cpp.h @@ -24,6 +24,7 @@ using namespace std; #define VER_RELEASE 1 class variableType; +class expression; class fn; extern ofstream output_cpp; extern ofstream funcs_h; @@ -180,6 +181,7 @@ public: static enum TYPES getSimpleVarType(enum TYPES t); void generateBox(enum SCOPES s); + void assignment(expression *value); static operands *createOp(enum TYPES t); virtual void dispose(); }; @@ -216,6 +218,7 @@ class constOp:public operands void processConst(unsigned int i); /* const that must be defined still */ void processConst(const string &s); + void assignment(expression *v); public: virtual string boxName(){return box;} @@ -360,7 +363,6 @@ public: static variableType *cloneAttributes(variableType *v); virtual string boxName(); - void assignment(expression *value); /* always call generateBox() after new variableType() */ variableType(enum SCOPES s, string &name, enum TYPES t, fn *fnHandle); variableType(); @@ -387,8 +389,8 @@ public: class forLoop:public codeType { variableType *var; - variableType *startTemp; - variableType *stopTemp; + tempVar *startTemp; + tempVar *stopTemp; whileLoop *infrastructure; expression *step; public: diff --git a/yabCodeStructures.cpp b/yabCodeStructures.cpp index d45e49f..d9cbfd3 100644 --- a/yabCodeStructures.cpp +++ b/yabCodeStructures.cpp @@ -117,12 +117,14 @@ void label::generate() ifStatement::ifStatement(expression *e):codeType(T_IF) { + logger("creating ifStatement"); this->redo=new label(); redo->generate(); this->done=new label(); expression *f=new expression(e,O_NOT); this->chain=new label(); chain->generateCondJump(f); + logger("ifStatement created"); } void ifStatement::generateBreak() @@ -151,9 +153,11 @@ void ifStatement::alternative(expression *e) void ifStatement::close() { + logger("closing ifStatement"); /* elsif ended without else in between */ if(this->chain)error(E_BAD_SYNTAX); this->done->generate(); + logger("ifStatement closed"); } ifStatement::~ifStatement() @@ -214,12 +218,14 @@ doLoop::~doLoop() whileLoop::whileLoop(expression *e):codeType(T_WHILELOOP) { + logger("creating whileLoop"); loopContinue=new label(); loopStart=new label(); loopEnd=new label(); cond=e; loopStart->generateJumpTo(); loopContinue->generate(); + logger("whileLoop created"); } void whileLoop::generateBreak() @@ -229,9 +235,11 @@ void whileLoop::generateBreak() void whileLoop::close() { + logger("closing whileLoop"); loopStart->generate(); loopContinue->generateCondJump(cond); loopEnd->generate(); + logger("whileLoop cleared"); } whileLoop::~whileLoop() @@ -245,40 +253,57 @@ whileLoop::~whileLoop() forLoop::forLoop(variableType *v, expression *start, expression *stop, expression *stepVal):codeType(T_FORLOOP) { + logger("creating forLoop"); + startTemp=tempVar::getOrCreateVar(v->getSimpleVarType()); + stopTemp=tempVar::getOrCreateVar(v->getSimpleVarType()); + logger("forLoop assignments"); /*v=start; stopTemp=stop;*/ v->assignment(start); + logger("start evaluated"); stopTemp->assignment(stop); /* if (vassignment(new expression(v)); /* else */ + logger("ending then clause"); c->alternative(); /* startTemp=stopTemp; stopTemp=v;*/ startTemp->assignment(new expression(stopTemp)); stopTemp->assignment(new expression(v)); /* endif */ + logger("ending else clause"); c->close(); + delete c; + logger("embedded if statement cleared"); + this->var=v; + /* while (v<=stopTemp && v>=startTemp) */ + logger("generating range check for forLoop"); expression *stopper1=new expression(new expression(v), O_LESS_EQUAL, new expression(stopTemp)); expression *stopper2=new expression(new expression(v), O_GREATER_EQUAL, new expression(startTemp)); expression *stopper=new expression(stopper1, O_AND, stopper2); + logger("generating while portion of forLoop"); infrastructure=new whileLoop(new expression(stopper, O_UNEQUAL, new expression(new constOp("0", T_INT)))); if (stepVal) { + logger("explicit step value"); step=stepVal; } else { + logger("implicit step value"); /* if not present "step" is assumed to be 1 */ step=new expression(new constOp("1", T_INT)); } + logger("forLoop definition cleared"); } void forLoop::generateBreak() @@ -288,15 +313,17 @@ void forLoop::generateBreak() void forLoop::close() { + logger("closing forLoop"); /* var=var+step; */ expression *stepper=new expression(new expression(var), O_PLUS, step); var->assignment(stepper); + logger("step applied"); infrastructure->close(); + startTemp->dispose(); + stopTemp->dispose(); + delete infrastructure; + logger("forLoop closed"); } forLoop::~forLoop() -{ - delete startTemp; - delete stopTemp; - delete infrastructure; -} +{} diff --git a/yabDataStructures.cpp b/yabDataStructures.cpp index dbe7a8c..5838c48 100644 --- a/yabDataStructures.cpp +++ b/yabDataStructures.cpp @@ -121,6 +121,35 @@ string operands::boxName() } } +void operands::assignment(expression *value) +{ + operands *op=value->evaluate(); + enum TYPES t=op->getSimpleVarType(); + switch (this->getType()) + { + case T_FLOATVAR: + if (t==T_INTVAR) + { + output_cpp << this->boxName() << "=" + << "static_cast(" + << op->boxName() << ");\n"; + } + else + { + if (t!=T_FLOATVAR) error(E_TYPE_MISMATCH); + } + output_cpp << this->boxName() << "=" + << op->boxName() << ";\n"; + break; + default: + if (t!=this->getType()) error(E_TYPE_MISMATCH); + output_cpp << this->boxName() << "=" + << op->boxName() << ";\n"; + break; + } + delete value; +} + tempVar::tempVar(enum TYPES t):operands(t) { generateBox(S_GLOBAL); @@ -250,6 +279,10 @@ void constOp::processConst( const string &s) consts_h << box << "=" << s << ";\n"; } +void constOp::assignment(expression *v) +{ + error(E_BAD_SYNTAX); +} /* constructor for constOp */ constOp::constOp(const string &s, enum TYPES t):operands(t) @@ -528,35 +561,6 @@ variableType *variableType::getOrCreateVar(string &name, enum TYPES t) return v; } -void variableType::assignment(expression *value) -{ - operands *op=value->evaluate(); - enum TYPES t=op->getSimpleVarType(); - switch (this->getType()) - { - case T_FLOATVAR: - if (t==T_INTVAR) - { - output_cpp << this->boxName() << "=" - << "static_cast(" - << op->boxName() << ");\n"; - } - else - { - if (t!=T_FLOATVAR) error(E_TYPE_MISMATCH); - } - output_cpp << this->boxName() << "=" - << op->boxName() << ";\n"; - break; - default: - if (t!=this->getType()) error(E_TYPE_MISMATCH); - output_cpp << this->boxName() << "=" - << op->boxName() << ";\n"; - break; - } - delete value; -} - string arrayType::boxName(listindexes) { ostringstream out;