Yab2Cpp/yab2cpp.cpp

173 lines
3.5 KiB
C++
Raw Normal View History

/*
** Yab2Cpp
**
** Transpiler by Samuel D. Crow
**
** Based on Yab
**
*/
2021-03-11 21:22:40 -06:00
#include "yab2cpp.h"
enum COMPILE_ERRORS errorLevel=E_OK;
2021-03-11 21:22:40 -06:00
unsigned int mode=0;
unsigned int indentLevel=0;
bool scopeGlobal=true;
ifstream src;
ofstream output_cpp;
ofstream funcs_h;
ofstream heap_h;
ofstream consts_h;
ofstream logfile;
ofstream varNames;
2021-03-11 21:22:40 -06:00
/* private prototypes */
void helpText(string &);
void shutDown();
void logger(string &);
/* process command line parameters */
int main(int argc, char *argv[])
{
atexit(shutDown);
switch (argc)
{
case 1:
mode=COMPILE;
cout << "\nCompile initiated." << endl;
compile();
break;
case 2:
if (argv[1][0]=='-')
{
switch (argv[1][1])
{
case 'd':
cout << "\nIdentifier dump initiated." << endl;
mode=DUMP;
compile();
break;
case 'v':
cout << "\n" << argv[0] << " version "
<< VER_MAJOR << "." << VER_MINOR << "." << VER_RELEASE << endl;
break;
case 'V':
cout << "\nVerbose compile initiated." << endl;
mode=DUMP|COMPILE;
compile();
break;
case 'D':
cout << "\nCompiler debug and dump mode initiated." << endl;
mode=DUMP|DEBUG;
compile();
break;
case 'G':
cout << "\nDebug, dump and compile initiated." << endl;
mode=DUMP|DEBUG|COMPILE;
compile();
break;
default:
helpText(argv[0]);
break;
}
}
break;
default:
helpText(argv[0]);
break;
}
return 0;
}
/* print the help text to stdout */
void helpText(string &commandname)
{
cout << commandname << "[-d|D|V|v|G] < filename.mb\n" <<
"Compiles filename.mb by default unless a flag is specified.\n" <<
"\n The optional flags are as follows:\n" <<
"-d is a dump of build to the parse.log file.\n" <<
"-D is a dump of identifiers and logged build.\n" <<
"-V is for a verbose build where the compiler logs and compiles.\n" <<
"-v prints the version and exits.\n\n" <<
"-G activates dump, debug and compile all at once.\n" << endl;
}
/* open files and initialize them*/
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 << "#include <runtime.h>\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");
}
}
if (mode & DUMP)
{
/* dump identifier mode */
logfile=fopen("parse.log","w");
logger("Setup complete.");
}
}
/* write a note in the logfile */
void logger(string &contents)
{
unsigned int count;
if (mode & DEBUG)
{
count=indentLevel;
while (count > 0)
{
logfile << '\t';
--count;
}
logfile << contents << endl;
}
}
/* shutdown the compiler and exit */
void shutDown()
{
if (errorLevel != E_OK) cerr << "\nERROR: " << COMPILEERRORNAMES[errorLevel] << "\n\n" << endl;
if (fn::isCallStackEmpty())
{
logger("Stack was empty");
}
else
{
logger("Dumping stack.");
if (mode & DUMP && logfile != NULL)
{
fn::dumpCallStack(logfile);
}
}
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();
}