From e5d08cc31ba9007b2ee01bdd6e5cf81fcb63a704 Mon Sep 17 00:00:00 2001 From: "Samuel D. Crow" Date: Fri, 19 Mar 2021 19:26:10 -0500 Subject: [PATCH] added static initializers and now it builds and links! --- yab2cpp.cpp | 20 ++++++------ yab2cpp.h | 44 ++++++++++++--------------- yabCodeStructures.cpp | 45 ++++++++++++--------------- yabDataStructures.cpp | 71 ++++++++++++++----------------------------- yabFunctions.cpp | 37 +++++++++++++++++----- 5 files changed, 102 insertions(+), 115 deletions(-) diff --git a/yab2cpp.cpp b/yab2cpp.cpp index 5009831..aeace66 100644 --- a/yab2cpp.cpp +++ b/yab2cpp.cpp @@ -8,6 +8,10 @@ */ #include "yab2cpp.h" +unordered_map >globals; +unordered_map >locals; +unordered_map >statics; + /* These correspond to the enum COMPILE_ERRORS. */ const char *COMPILE_ERROR_NAMES[]={ "no error", @@ -200,19 +204,17 @@ void logger(string s) void shutDown() { if (errorLevel != E_OK) cerr << "\nERROR: " << COMPILE_ERROR_NAMES[errorLevel] << "\n\n" << endl; - if (fn::isCallStackEmpty()) + logger("Dumping stack."); + if (mode & DUMP && (logfile)) { - logger("Stack was empty"); + fn::dumpCallStack(); } - else + varNames << "Global Variables\n"; + for(auto iter=globals.begin(); iter!=globals.end(); ++iter) { - logger("Dumping stack."); - if (mode & DUMP && (logfile)) - { - fn::dumpCallStack(); - } + varNames << "variable " << iter->first << " has ID " << iter->second << "\n"; } - operands::dumpVars(); + varNames << endl; label::dumpLabels(); output_cpp << "}\n}return state;\n}"<< endl; } diff --git a/yab2cpp.h b/yab2cpp.h index 86b790a..127bc67 100644 --- a/yab2cpp.h +++ b/yab2cpp.h @@ -22,13 +22,16 @@ using namespace std; #define VER_MINOR 0 #define VER_RELEASE 1 +class variable; extern ofstream output_cpp; extern ofstream funcs_h; extern ofstream heap_h; extern ofstream consts_h; extern ofstream logfile; extern ofstream varNames; - +extern unordered_map >globals; +extern unordered_map >locals; +extern unordered_map >statics; /* ** list of all compiler errors ** @@ -151,7 +154,7 @@ enum OPERATORS /* global prototype */ [[noreturn]] void error(enum COMPILE_ERRORS err); void logger(string s); - + /* internal states used by the parser */ class scope:public ofstream { @@ -172,16 +175,10 @@ class operands static unsigned int nextID; /* private constructor for parameter passing only */ explicit operands(unsigned int id, enum TYPES t); -protected: - static unordered_map> globals; public: enum TYPES getType() const {return type;} unsigned int getID() const {return id;} - static shared_ptrfindGlobal(string &s); - static void dumpVars(); - static shared_ptrgetOrCreateGlobal(string &s, enum TYPES t); - enum TYPES getSimpleVarType(); void generateBox(enum SCOPES s); virtual string boxName(); @@ -233,26 +230,24 @@ public: this->right=r; this->oper=o; } - expression(operands *x) + expression(shared_ptrx) { - op=shared_ptr(x); + op=x; oper=O_TERM; } /*TODO: Recycle temporary variables when not in debug mode*/ - virtual ~expression(); + virtual ~expression() + {} }; /* parent class of all code types */ class codeType { enum CODES type; - static list >nesting; public: enum CODES getType() const {return this->type;} - static shared_ptr getCurrent(); - - virtual void close(); + virtual void close()=0; virtual void generateBreak()=0; explicit codeType(enum CODES t); @@ -283,8 +278,9 @@ public: label(){this->id = ++nextID;} label(string &s) { + unordered_map >lookup; label(); - lookup[s]=shared_ptr