From 1dae0a4b544418496dedda6b97bd02249824df6d Mon Sep 17 00:00:00 2001 From: "Samuel D. Crow" Date: Tue, 16 Mar 2021 17:01:28 -0500 Subject: [PATCH] started build script and went on a massive bug hunt --- Makefile | 40 ++++++++++++ yab2cpp.cpp | 41 ++++++------ yab2cpp.h | 145 +++++++++++++++++++++++------------------- yabCodeStructures.cpp | 143 +++++++++++++++++++---------------------- yabDataStructures.cpp | 142 +++++++++++++++++------------------------ yabFunctions.cpp | 87 +++++++++++++++++-------- 6 files changed, 322 insertions(+), 276 deletions(-) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..fa9720a --- /dev/null +++ b/Makefile @@ -0,0 +1,40 @@ +#Makefile for yab2cpp +#by Samuel D. Crow + +#presently architecture-neutral + +CC=g++ +LD=g++ +#release build +#FLAGS=-c -Os -std=c++11 +#LDFLAGS= + +#debug build +FLAGS=-c -g -Og -std=c++11 +LDFLAGS= + +AR=ar r +LD=g++ + +default: yab2cpp + +yabDataStructures.o: yabDataStructures.cpp yab2cpp.h + $(CC) $(FLAGS) -o yabDataStructures.o yabDataStructures.cpp + +yabCodeStructures.o: yabCodeStructures.cpp yab2cpp.h + $(CC) $(FLAGS) -o yabCodeStructures.o yabCodeStructures.cpp + +yabFunctions.o: yabFunctions.cpp yab2cpp.h + $(CC) $(FLAGS) -o yabFunctions.o yabFunctions.cpp + +yab2cpp.o: yab2cpp.cpp yab2cpp.h + $(CC) $(FLAGS) -o yab2cpp.o yab2cpp.cpp + +BASIC_framework.a: yabDataStructures.o yabCodeStructures.o yabFunctions.o + $(AR) yabDataStructures.o yabCodeStructures.o yabFunctions.o + +yab2cpp: BASIC_framework.a yab2cpp.o + $(LD) $(LDFLAGS) -o buildyab2cpp yab2cpp.o -lBASIC_framework + +clean: + rm -f *.o yab2cpp BASIC_framework.a diff --git a/yab2cpp.cpp b/yab2cpp.cpp index 0bfbe7a..59bb356 100644 --- a/yab2cpp.cpp +++ b/yab2cpp.cpp @@ -22,9 +22,10 @@ ofstream logfile; ofstream varNames; /* private prototypes */ -void helpText(string &); +void helpText(string); +void setup(); +void compile(); void shutDown(); -void logger(string &); /* process command line parameters */ int main(int argc, char *argv[]) @@ -98,29 +99,35 @@ void setUp() if (mode & COMPILE) { /* compile mode */ - output_cpp=new ofstream("build/output.cpp"); - funcs_h=new ofstream ("functions.h"); - consts_h=new ofstream("consts.h"); - heap_h=new ofstream("heap.h"); + output_cpp.open("output/output.cpp"); + funcs_h.open("output/functions.h"); + consts_h.open("output/consts.h"); + heap_h.open("output/heap.h"); output_cpp << "#include \n#include \"consts.h\"\n" << "#include \"heap.h\"\n#include \"functions.h\"\n" << "unsigned int state=start;\nint run(){\nwhile (state>=start){\n" << "switch(state){\ncase start:" << endl; if (mode & DEBUG) { - varNames=new ofstream("varnames.txt"); + varNames.open("varnames.txt"); } } if (mode & DUMP) { /* dump identifier mode */ - logfile=fopen("parse.log","w"); + logfile.open("parse.log"); logger("Setup complete."); } } +void error(enum COMPILE_ERRORS e) +{ + errorLevel=e; + exit(1); +} + /* write a note in the logfile */ -void logger(string &contents) +void logger(string s) { unsigned int count; if (mode & DEBUG) @@ -131,14 +138,14 @@ void logger(string &contents) logfile << '\t'; --count; } - logfile << contents << endl; + logfile << s << endl; } } /* shutdown the compiler and exit */ void shutDown() { - if (errorLevel != E_OK) cerr << "\nERROR: " << COMPILEERRORNAMES[errorLevel] << "\n\n" << endl; + if (errorLevel != E_OK) cerr << "\nERROR: " << COMPILE_ERROR_NAMES[errorLevel] << "\n\n" << endl; if (fn::isCallStackEmpty()) { logger("Stack was empty"); @@ -146,26 +153,22 @@ void shutDown() else { logger("Dumping stack."); - if (mode & DUMP && logfile != NULL) + if (mode & DUMP && (logfile)) { - fn::dumpCallStack(logfile); + fn::dumpCallStack(); } } operands::dumpVars(); label::dumpLabels(); output_cpp << "}\n}return state;\n}"<< endl; - } } /* open files and compile */ void compile() { setUp(); - - /* parse */ - ctx = mb_create(NULL); - while(mb_parse(ctx, NULL)){logger("done");} - mb_destroy(ctx); + + shutDown(); } diff --git a/yab2cpp.h b/yab2cpp.h index d482d0f..d0d40b9 100644 --- a/yab2cpp.h +++ b/yab2cpp.h @@ -13,6 +13,8 @@ #include #include #include +#include +#include using namespace std; @@ -90,7 +92,7 @@ enum TYPES T_FLOATCALL_ARRAY, T_STRINGCALL_ARRAY, T_VOIDCALL -} +}; /* list of all kinds of other code structures */ enum CODES { @@ -194,6 +196,7 @@ enum OPERATORS /* global prototype */ void error(enum COMPILE_ERRORS err); +void logger(string s); /* internal states used by the parser */ class operands @@ -201,21 +204,23 @@ class operands enum TYPES type; unsigned int id; static unsigned int nextID; - static unordered_map globals; + static unordered_map> globals; static unordered_map strConst; + /* private constructor for parameter passing only */ + explicit operands(unsigned int id, enum TYPES t); public: enum TYPES getType() const {return type;} unsigned int getID() const {return id;} - static operands *findGlobal(string &s); - static void dumpVars(ostream &out); - static unsigned int getOrCreateStr(string &s); - static operands *createConst(string &s, enum TYPES t); - static operands *getOrCreateGlobal(string &s, enum TYPES t); + static shared_ptrfindGlobal(string &s); + static void dumpVars(); + static shared_ptrgetOrCreateStr(string s); + static shared_ptrcreateConst(string s, enum TYPES t); + static shared_ptrgetOrCreateGlobal(string &s, enum TYPES t); enum TYPES getSimpleVarType(); void generateBox(ostream &scope); - virtual string &boxName(); + virtual string boxName(); enum TYPES coerceTypes(); explicit operands(enum TYPES t); @@ -226,27 +231,27 @@ public: /* expression can be terminal or non-terminal */ class expression { - operands *op; - expression *left; - expression *right; + shared_ptrop; + shared_ptrleft; + shared_ptrright; enum OPERATORS oper; public: enum OPERATORS getOp() const {return oper;} - expression *getLeft() const {return left;} - expression *getRight() const {return right;} + shared_ptrgetLeft() const {return left;} + shared_ptrgetRight() const {return right;} bool isBinOp(); - operands *evaluate(); - operands *stringEval(); + shared_ptrevaluate(); + shared_ptrstringEval(shared_ptrl, shared_ptrr); /* r is NULL for unary operators */ - expression(expression *l, enum OPERATORS o, expression *r=NULL) + expression(shared_ptrl, enum OPERATORS o, shared_ptrr=NULL) { this->left=l; this->right=r; this->oper=o; } - expression(operands x) + expression(shared_ptrx) { op=x; oper=O_TERM; @@ -259,11 +264,11 @@ public: class codeType { enum CODES type; - static list nesting; + static list >nesting; public: enum CODES getType() const {return this->type;} - static codeType *getCurrent(); + static shared_ptr getCurrent(); virtual void close(); virtual void generateBreak()=0; @@ -277,25 +282,27 @@ class label { unsigned int id; static unsigned int nextID; - static unordered_map lookup; + static unordered_map > lookup; public: - static dumpLabels(ostream &v); + static void dumpLabels(); unsigned int getID() const {return id;} void generateJumpTo(); - void generateOnNSkip(list >dest); - void generateOnNTo(expression *e); - void generateCondJump(expression *e); + /* pass generateOnNSkip as second paramater + to generateOnNTo or generateOnNSub */ + unsigned int generateOnNSkip(list >&dest); + static void generateOnNTo(shared_ptre, unsigned int skip); + void generateCondJump(shared_ptre); void generate(); - static label *find(string &s); + static shared_ptr