diff --git a/.gitignore b/.gitignore index db3ecb0..5a2f8de 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ bison.output yab *.o *.so +.vscode/* diff --git a/Makefile b/Makefile index fa9720a..8a832d3 100644 --- a/Makefile +++ b/Makefile @@ -5,16 +5,16 @@ CC=g++ LD=g++ + #release build #FLAGS=-c -Os -std=c++11 -#LDFLAGS= +#LDFLAGS=LDFLAGS=-std=c++11 #debug build FLAGS=-c -g -Og -std=c++11 -LDFLAGS= +LDFLAGS=-std=c++11 AR=ar r -LD=g++ default: yab2cpp @@ -30,11 +30,13 @@ yabFunctions.o: yabFunctions.cpp yab2cpp.h 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 +#BASIC_framework.a: yabDataStructures.o yabCodeStructures.o yabFunctions.o +# $(AR) BASIC_framework.a yabDataStructures.o yabCodeStructures.o yabFunctions.o -yab2cpp: BASIC_framework.a yab2cpp.o - $(LD) $(LDFLAGS) -o buildyab2cpp yab2cpp.o -lBASIC_framework +#yab2cpp: BASIC_framework.a yab2cpp.o +yab2cpp: yab2cpp.o yabCodeStructures.o yabDataStructures.o yabFunctions.o + $(LD) $(LDFLAGS) -o yab2cpp yab2cpp.o yabCodeStructures.o yabDataStructures.o yabFunctions.o +#BASIC_framework.a clean: rm -f *.o yab2cpp BASIC_framework.a diff --git a/yab2cpp.cpp b/yab2cpp.cpp index 59bb356..cf83516 100644 --- a/yab2cpp.cpp +++ b/yab2cpp.cpp @@ -8,6 +8,60 @@ */ #include "yab2cpp.h" +/* These correspond to the enum COMPILE_ERRORS. */ +const char *COMPILE_ERROR_NAMES[]={ + "no error", + "incorrect syntax", + "wrong type", + "failed allocation", + "stack underflow", + "internal compiler error", + "duplicated label", + "previous subroutine didn't end", + "value returned from gosub call", + "undefined subroutine name", + "too many parameters in function call", + "value cannot be assigned" +}; + +/* These correspond to the types of enum TYPES. */ +const string TYPENAMES[]={ + "unknown", + "none", + "string constant", + "integer constant", + "floating point constant", + "string variable", + "integer variable", + "floating point variable", + "string array or function", + "integer array or function", + "floating point array or function", + "string array or function", + "function" +}; + +const string CODETYPES[]={ + "print sequence", + "print segment", + "while loop", + "for loop", + "repeat loop", + "do loop", + "if statement", + "procedure statement", + "function statement", + "assignment", + "label", + "parameter list or array index", + "data item", + "function returning string", + "function returning floating point", + "function returning integer", + "function returning nothing", + "function" +}; + enum COMPILE_ERRORS errorLevel=E_OK; unsigned int mode=0; unsigned int indentLevel=0; diff --git a/yab2cpp.h b/yab2cpp.h index d0d40b9..34a9be4 100644 --- a/yab2cpp.h +++ b/yab2cpp.h @@ -55,22 +55,6 @@ extern unsigned int mode; extern unsigned int indentLevel; extern bool scopeGlobal; -/* These correspond to the enum COMPILE_ERRORS. */ -const char *COMPILE_ERROR_NAMES[]={ - "no error", - "incorrect syntax", - "wrong type", - "failed allocation", - "stack underflow", - "internal compiler error", - "duplicated label", - "previous subroutine didn't end", - "value returned from gosub call", - "undefined subroutine name", - "too many parameters in function call", - "value cannot be assigned" -}; - /* flags used internally by the compiler (must be powers of 2) */ #define COMPILE 1 @@ -117,44 +101,6 @@ enum CODES T_UNKNOWNFUNC }; -/* These correspond to the types of enum TYPES. */ -const string TYPENAMES[]={ - "unknown", - "none", - "string constant", - "integer constant", - "floating point constant", - "string variable", - "integer variable", - "floating point variable", - "string array or function", - "integer array or function", - "floating point array or function", - "string array or function", - "function" -}; - -const string CODETYPES[]={ - "print sequence", - "print segment", - "while loop", - "for loop", - "repeat loop", - "do loop", - "if statement", - "procedure statement", - "function statement", - "assignment", - "label", - "parameter list or array index", - "data item", - "function returning string", - "function returning floating point", - "function returning integer", - "function returning nothing", - "function" -}; - typedef union { double d; @@ -170,6 +116,14 @@ enum SEPARATORS S_LINEFEED }; +enum SCOPES +{ + S_UNKNOWN, + S_LOCAL, + S_STATIC, + S_GLOBAL +}; + enum OPERATORS { O_PLUS, @@ -199,27 +153,37 @@ void error(enum COMPILE_ERRORS err); void logger(string s); /* internal states used by the parser */ +class scope:public ofstream +{ + enum SCOPES myscope; +public: + ofstream &operator<<(ostream &in); + enum SCOPES getScope() const {return myscope;} + + scope(enum SCOPES s){myscope=s;} + ~scope() + {} +}; + class operands { enum TYPES type; unsigned int id; static unsigned int nextID; - static unordered_map> globals; - static unordered_map strConst; /* 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_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); + void generateBox(enum SCOPES s); virtual string boxName(); enum TYPES coerceTypes(); @@ -228,6 +192,24 @@ public: {} }; +class constOp:public operands +{ + /* box is defined once in the constructor */ + string box; + static unordered_map strConst; + + /* const for id that exists already */ + void processConst(unsigned int i); + /* const that must be defined still */ + void processConst(const string &s); +public: + virtual string boxName(){return box;} + + constOp(const string &s, enum TYPES t); + ~constOp() + {} +}; + /* expression can be terminal or non-terminal */ class expression { @@ -251,9 +233,9 @@ public: this->right=r; this->oper=o; } - expression(shared_ptrx) + expression(operands *x) { - op=x; + op=shared_ptr(x); oper=O_TERM; } /*TODO: Recycle temporary variables when not in debug mode*/ @@ -370,12 +352,12 @@ public: class variable:public operands { - ostream &myScope; + enum SCOPES myScope; public: static shared_ptrgetOrCreateVar(string &name, enum TYPES t); void assignment(shared_ptrvalue); - explicit variable(ostream &scope, string &name, enum TYPES t); + explicit variable(enum SCOPES s, string &name, enum TYPES t); variable(); ~variable() {} @@ -409,18 +391,20 @@ public: class fn:codeType { - static unordered_map >locals; - static unordered_map >statics; + friend variable; static unordered_map >functions; static list > callStack; static unsigned int nextID; list >params; unsigned int id; enum TYPES kind; + shared_ptrrc; shared_ptr