fixed bugs and added enough runtime to execute first code after the compiler segfaults at shutdown

This commit is contained in:
Samuel D. Crow
2021-03-24 18:17:33 -05:00
parent 7c5bf76778
commit 48505d1ef8
11 changed files with 152 additions and 79 deletions

2
.gitignore vendored
View File

@@ -5,7 +5,9 @@ bison.output
yab yab
*.o *.o
*.so *.so
*.log
.vscode/* .vscode/*
yab2cpp yab2cpp
tester tester
output/* output/*
runtime/main

View File

@@ -5,8 +5,9 @@ CFLAGS += -Os
LFLAGS := LFLAGS :=
ODIR := build ODIR := build
RDIR := output
YABCODESTRUCTURES_SOURCE_DEPS := yabCodeStructures.cpp yab2cpp.h yab2cpp.cpp tester.cpp YABCODESTRUCTURES_SOURCE_DEPS := yabCodeStructures.cpp yab2cpp.h yab2cpp.cpp tester.cpp runtime/runtime.h
YAB2CPP_SOURCE_DEPS := yab2cpp.cpp yab2cpp.h tester.cpp YAB2CPP_SOURCE_DEPS := yab2cpp.cpp yab2cpp.h tester.cpp
YABDATASTRUCTURES_SOURCE_DEPS := yabDataStructures.cpp yab2cpp.h yab2cpp.cpp tester.cpp YABDATASTRUCTURES_SOURCE_DEPS := yabDataStructures.cpp yab2cpp.h yab2cpp.cpp tester.cpp
YABFUNCTIONS_SOURCE_DEPS := yabFunctions.cpp yab2cpp.h yab2cpp.cpp tester.cpp YABFUNCTIONS_SOURCE_DEPS := yabFunctions.cpp yab2cpp.h yab2cpp.cpp tester.cpp
@@ -17,15 +18,18 @@ all: binaries
$(ODIR): $(ODIR):
@mkdir $(ODIR) @mkdir $(ODIR)
$(RDIR):
@mkdir $(RDIR)
binaries: bin_yab2cpp bin_tester binaries: bin_yab2cpp bin_tester
YAB2CPP_OBJECT_DEPS := $(ODIR)/yabCodeStructures.o $(ODIR)/yabFunctions.o $(ODIR)/yabDataStructures.o $(ODIR)/yabIO.o $(ODIR)/yab2cpp.o YAB2CPP_OBJECT_DEPS := $(ODIR)/yabCodeStructures.o $(ODIR)/yabFunctions.o $(ODIR)/yabDataStructures.o $(ODIR)/yabIO.o $(ODIR)/yab2cpp.o
TESTER_OBJECT_DEPS := $(ODIR)/yabCodeStructures.o $(ODIR)/yabFunctions.o $(ODIR)/yabDataStructures.o $(ODIR)/yabIO.o $(ODIR)/tester.o TESTER_OBJECT_DEPS := $(ODIR)/yabCodeStructures.o $(ODIR)/yabFunctions.o $(ODIR)/yabDataStructures.o $(ODIR)/yabIO.o $(ODIR)/tester.o
bin_tester: $(ODIR) $(TESTER_OBJECT_DEPS) bin_tester: $(RDIR) $(ODIR) $(TESTER_OBJECT_DEPS)
$(CC) -o tester $(TESTER_OBJECT_DEPS) $(LFLAGS) $(CC) -o tester $(TESTER_OBJECT_DEPS) $(LFLAGS)
bin_yab2cpp: $(ODIR) $(YAB2CPP_OBJECT_DEPS) bin_yab2cpp: $(RDIR) $(ODIR) $(YAB2CPP_OBJECT_DEPS)
$(CC) -o yab2cpp $(YAB2CPP_OBJECT_DEPS) $(LFLAGS) $(CC) -o yab2cpp $(YAB2CPP_OBJECT_DEPS) $(LFLAGS)
$(ODIR)/yabCodeStructures.o: $(ODIR) $(YABCODESTRUCTURES_SOURCE_DEPS) $(ODIR)/yabCodeStructures.o: $(ODIR) $(YABCODESTRUCTURES_SOURCE_DEPS)
@@ -48,4 +52,5 @@ $(ODIR)/yabFunctions.o: $(ODIR) $(YABFUNCTIONS_SOURCE_DEPS)
.PHONY: clean .PHONY: clean
clean: clean:
rm -rf build/* yab2cpp tester rm -rf build/* output/* yab2cpp tester

28
runtime/Makefile Normal file
View File

@@ -0,0 +1,28 @@
CC := g++
CFLAGS := -Wall
CFLAGS += -std=c++11
CFLAGS += -Os
LFLAGS :=
all: binaries
binaries: bin_main
SRC := ../output/
OUTPUT_DEPS := $(SRC)consts.h $(SRC)heap.h $(SRC)functions.h runtime.h $(SRC)output.cpp
MAIN_OBJ_DEPS := output.o main.o
bin_main: $(MAIN_OBJ_DEPS)
$(CC) -o main $(MAIN_OBJ_DEPS)
main.o: $(OUTPUT_DEPS)
$(CC) -o main.o -c main.cpp
output.o: $(OUTPUT_DEPS)
$(CC) -I$(SRC) -o output.o -c $(SRC)output.cpp
.PHONY: clean
clean:
rm -rf *.o main

16
runtime/main.cpp Normal file
View File

@@ -0,0 +1,16 @@
/*
** Practice runtime for Yab2Cpp
**
** by Samuel D. Crow
*/
#include "runtime.h"
int main(int argc, char *argv[])
{
unsigned int ret=run();
if (ret!=EXIT)
{
return 1;
}
return 0;
}

22
runtime/runtime.h Normal file
View File

@@ -0,0 +1,22 @@
/*
** Practice runtime header for Yab2Cpp
**
** by Samuel D. Crow
*/
#ifndef YAB_RUNTIME
#define YAB_RUTTIME
#include <cstdio>
using namespace std;
enum STATES:unsigned int
{
EXIT,
UNDEFINED_STATE_ERROR,
START
};
/* function prototype */
unsigned int run();
#endif

View File

@@ -94,7 +94,6 @@ void shutDown();
/* process command line parameters */ /* process command line parameters */
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
atexit(shutDown);
switch (argc) switch (argc)
{ {
case 1: case 1:
@@ -152,6 +151,7 @@ int main(int argc, char *argv[])
helpText(argv[0]); helpText(argv[0]);
break; break;
} }
cout << "vamanos!" <<endl;
return 0; return 0;
} }
@@ -179,15 +179,26 @@ void setUp()
funcs_h.open("output/functions.h"); funcs_h.open("output/functions.h");
consts_h.open("output/consts.h"); consts_h.open("output/consts.h");
heap_h.open("output/heap.h"); heap_h.open("output/heap.h");
output_cpp << "#include <runtime.h>\n#include \"consts.h\"\n" output_cpp << "#include \"../runtime/runtime.h\"\n#include \"consts.h\"\n"
<< "#include \"heap.h\"\n#include \"functions.h\"\n" << "#include \"heap.h\"\n#include \"functions.h\"\n"
<< "int main(int argc, char *argv[])\n{\n" << "unsigned int state=START;\nunsigned int run(){\n"
<< "unsigned int state=start;\nint run(){\nwhile (state>=start){\n" << "while (state>=START){\n"
<< "switch(state){\ncase start:" << endl; << "switch(state){\ncase START:" << endl;
if (DUMP) }
{ else
varNames.open("varnames.txt"); {
} output_cpp.open("/dev/null");
funcs_h.open("/dev/null");
consts_h.open("/dev/null");
heap_h.open("/dev/null");
}
if (DUMP)
{
varNames.open("varnames.log");
}
else
{
varNames.open("/dev/null");
} }
if (DEBUG) if (DEBUG)
{ {
@@ -195,10 +206,15 @@ void setUp()
logfile.open("parse.log"); logfile.open("parse.log");
logger("Setup complete."); logger("Setup complete.");
} }
else
{
logfile.open("/dev/null");
}
} }
[[noreturn]] void error(enum COMPILE_ERRORS e) [[noreturn]] void error(enum COMPILE_ERRORS e)
{ {
cerr << COMPILE_ERROR_NAMES[e] << endl;
errorLevel=e; errorLevel=e;
exit(1); exit(1);
} }
@@ -220,7 +236,7 @@ void logger(string s)
if (DEBUG) if (DEBUG)
{ {
indent(); indent();
logfile << s << endl; logfile << s << "\n";
} }
} }
@@ -230,19 +246,23 @@ void shutDown()
if (errorLevel != E_OK) cerr << "\nERROR: " if (errorLevel != E_OK) cerr << "\nERROR: "
<< COMPILE_ERROR_NAMES[errorLevel] << "\n\n" << endl; << COMPILE_ERROR_NAMES[errorLevel] << "\n\n" << endl;
logger("Dumping stack."); logger("Dumping stack.");
if (DUMP && (logfile)) if (DUMP && (logfile)) fn::dumpCallStack();
if (DUMP)
{ {
fn::dumpCallStack(); varNames << "Global Variables\n";
} for(auto iter=globals.begin(); iter!=globals.end(); ++iter)
varNames << "Global Variables\n"; {
for(auto iter=globals.begin(); iter!=globals.end(); ++iter) varNames << "variable " << iter->first
{ << " has ID " << iter->second->getID() << "\n";
varNames << "variable " << iter->first }
<< " has ID " << iter->second << "\n"; varNames << endl;
}
varNames << endl;
label::dumpLabels(); label::dumpLabels();
output_cpp << "}\n}return state;\n}"<< endl; }
if (COMPILE)
{
output_cpp << "default:\nstate=UNDEFINED_STATE_ERROR;\n"
<< "break;\n}\n}\nreturn state;\n}"<< endl;
}
} }
void testInt() void testInt()
@@ -255,6 +275,7 @@ void testInt()
shared_ptr<printSegment>print=shared_ptr<printSegment>( shared_ptr<printSegment>print=shared_ptr<printSegment>(
new printSegment(shared_ptr<expression>(new expression(v)))); new printSegment(shared_ptr<expression>(new expression(v))));
print->generate(); print->generate();
label::generateEnd();
} }
/* open files and compile */ /* open files and compile */
@@ -266,4 +287,3 @@ void compile()
shutDown(); shutDown();
} }

View File

@@ -160,19 +160,6 @@ enum OPERATORS
void indent(); void indent();
void logger(string s); 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 class operands
{ {
enum TYPES type; enum TYPES type;
@@ -268,6 +255,7 @@ class label
static unordered_map<string, shared_ptr<label> > lookup; static unordered_map<string, shared_ptr<label> > lookup;
public: public:
static void dumpLabels(); static void dumpLabels();
static void generateEnd();
unsigned int getID() const {return id;} unsigned int getID() const {return id;}
@@ -450,9 +438,7 @@ class printSegment
enum SEPARATORS sep; enum SEPARATORS sep;
public: public:
void generate(); void generate();
printSegment(shared_ptr<expression>e, enum SEPARATORS s); printSegment(shared_ptr<expression>e=NULL, enum SEPARATORS s=S_LINEFEED);
printSegment(shared_ptr<expression>e) {printSegment(e, S_LINEFEED);}
printSegment() {printSegment(NULL);}
virtual ~printSegment() virtual ~printSegment()
{} {}
}; };

View File

@@ -7,10 +7,11 @@
** **
*/ */
#include "yab2cpp.h" #include "yab2cpp.h"
#include "runtime/runtime.h"
/* static initializers */ /* static initializers */
unordered_map<string, shared_ptr<label> > label::lookup; unordered_map<string, shared_ptr<label> > label::lookup;
unsigned int label::nextID; unsigned int label::nextID=START;
/* base class of all the code structure types */ /* base class of all the code structure types */
codeType::codeType(enum CODES t) codeType::codeType(enum CODES t)
@@ -53,6 +54,11 @@ void label::dumpLabels()
varNames << endl; varNames << endl;
} }
void label::generateEnd()
{
output_cpp << "state=EXIT;\nbreak;\n";
}
void label::generateJumpTo() void label::generateJumpTo()
{ {
output_cpp << "state=" << this->getID() << ";\nbreak;\n"; output_cpp << "state=" << this->getID() << ";\nbreak;\n";

View File

@@ -10,25 +10,9 @@
/* forward declaration and static initializers */ /* forward declaration and static initializers */
class fn; class fn;
unsigned int operands::nextID; unsigned int operands::nextID=0;
unordered_map<string, unsigned int> constOp::strConst; unordered_map<string, unsigned int> constOp::strConst;
/* scope methods */
ofstream &scope::operator<<(ostream &in)
{
switch (this->myscope)
{
case S_LOCAL:
return funcs_h;
case S_GLOBAL:
case S_STATIC:
return heap_h;
default:
break;
}
error(E_INTERNAL);
}
/* methods for operands */ /* methods for operands */
enum TYPES operands::getSimpleVarType() enum TYPES operands::getSimpleVarType()
{ {
@@ -62,10 +46,8 @@ operands::operands(enum TYPES t)
void operands::generateBox(enum SCOPES s) void operands::generateBox(enum SCOPES s)
{ {
string x; ostringstream ss;
scope y(s); switch (this->getType())
stringstream ss;
switch (this->getSimpleVarType())
{ {
case T_INTVAR: case T_INTVAR:
ss << "int v"; ss << "int v";
@@ -80,8 +62,19 @@ void operands::generateBox(enum SCOPES s)
error(E_TYPE_MISMATCH); error(E_TYPE_MISMATCH);
} }
ss << this->getID() << ";\n"; ss << this->getID() << ";\n";
ss.str(x); switch (s)
y << x; {
case S_LOCAL:
funcs_h << ss.str();
return;
case S_GLOBAL:
case S_STATIC:
heap_h << ss.str();
return;
default:
break;
}
error(E_INTERNAL);
} }
string operands::boxName() string operands::boxName()
@@ -94,7 +87,7 @@ string operands::boxName()
case T_INTVAR: case T_INTVAR:
case T_FLOATVAR: case T_FLOATVAR:
s << 'v' << this->getID(); s << 'v' << this->getID();
s.str(x); x=s.str();
s.clear(); s.clear();
return x; return x;
break; break;
@@ -108,7 +101,7 @@ void constOp::processConst(unsigned int i)
{ {
stringstream me; stringstream me;
me << 'k' << i; me << 'k' << i;
me.str(box); box=me.str();
} }
void constOp::processConst( const string &s) void constOp::processConst( const string &s)
@@ -401,7 +394,6 @@ void variableType::assignment(shared_ptr<expression>value)
string arrayType::boxName(list<shared_ptr<operands> >indexes) string arrayType::boxName(list<shared_ptr<operands> >indexes)
{ {
ostringstream out; ostringstream out;
string buf;
auto i=indexes.begin(); auto i=indexes.begin();
out << 'v' << this->getID(); out << 'v' << this->getID();
while (i!=indexes.end()) while (i!=indexes.end())
@@ -409,15 +401,12 @@ string arrayType::boxName(list<shared_ptr<operands> >indexes)
out << '[' << (*i)->boxName() << ']'; out << '[' << (*i)->boxName() << ']';
++i; ++i;
} }
out.str(buf); return out.str();
out.clear();
return buf;
} }
string arrayType::generateBox(enum SCOPES s) string arrayType::generateBox(enum SCOPES s)
{ {
ostringstream out; ostringstream out;
string buf;
switch (this->getType()) switch (this->getType())
{ {
case T_STRINGCALL_ARRAY: case T_STRINGCALL_ARRAY:
@@ -438,9 +427,7 @@ string arrayType::generateBox(enum SCOPES s)
out << '[' << *i << ']'; out << '[' << *i << ']';
} }
out << ";\n"; out << ";\n";
out.str(buf); return out.str();
out.clear();
return buf;
} }
arrayType::arrayType(string &name, enum TYPES t, list<unsigned int>dim): arrayType::arrayType(string &name, enum TYPES t, list<unsigned int>dim):

View File

@@ -11,7 +11,7 @@
/* static initializers */ /* static initializers */
unordered_map<string, shared_ptr<fn> > fn::functions; unordered_map<string, shared_ptr<fn> > fn::functions;
list<shared_ptr<fn> >fn::callStack; list<shared_ptr<fn> >fn::callStack;
unsigned int fn::nextID; unsigned int fn::nextID=0;
/* function definitions */ /* function definitions */
void fn::dumpCallStack() void fn::dumpCallStack()

View File

@@ -31,18 +31,19 @@ void printSegment::generate()
output_cpp << "printf(\"%f\", " << op->boxName() << ");\n"; output_cpp << "printf(\"%f\", " << op->boxName() << ");\n";
break; break;
default: default:
error(E_TYPE_MISMATCH);
break; break;
} }
} }
switch (sep) switch (sep)
{ {
case S_LINEFEED: case S_LINEFEED:
output_cpp << "puts(\"\n\");\n"; output_cpp << "puts(\"\\n\");\n";
return; return;
case S_SEMICOLON: case S_SEMICOLON:
return; return;
case S_COMMA: case S_COMMA:
output_cpp << "putc('\t');\n"; output_cpp << "putc('\\t');\n";
return; return;
default: default:
error(E_BAD_SYNTAX); error(E_BAD_SYNTAX);