2023-01-04 22:32:55 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <emit.hh>
|
2023-01-05 20:12:15 +00:00
|
|
|
#include <error.hh>
|
2023-01-04 22:32:55 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
2023-01-06 02:56:33 +00:00
|
|
|
#include <typecheck.hh>
|
2023-01-04 22:32:55 +00:00
|
|
|
#include <xlangLexer.h>
|
|
|
|
#include <xlangParser.h>
|
|
|
|
|
|
|
|
static void usage() {
|
|
|
|
std::cerr << getprogname() << " [-o output.ssa] input.x" << std::endl;
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
int opt;
|
|
|
|
std::string inputfile;
|
|
|
|
std::string outputfile;
|
|
|
|
|
|
|
|
while ((opt = getopt(argc, argv, "o:")) != -1) {
|
|
|
|
switch (opt) {
|
|
|
|
case 'o':
|
|
|
|
outputfile = optarg;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
if (argc != 1) {
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
inputfile = argv[0];
|
|
|
|
if (!outputfile.length()) {
|
|
|
|
outputfile = "/dev/stdout";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ifstream inputstream{inputfile};
|
|
|
|
antlr4::ANTLRInputStream input{inputstream};
|
|
|
|
xlang::xlangLexer lexer{&input};
|
|
|
|
antlr4::CommonTokenStream tokens{&lexer};
|
|
|
|
xlang::xlangParser parser{&tokens};
|
2023-01-05 20:12:15 +00:00
|
|
|
xlang::ErrorListener errorlistener{inputfile};
|
|
|
|
|
|
|
|
parser.removeErrorListeners();
|
|
|
|
parser.addErrorListener(&errorlistener);
|
2023-01-04 22:32:55 +00:00
|
|
|
auto *tree = parser.file();
|
2023-01-05 20:12:15 +00:00
|
|
|
if (errorlistener.hasError()) {
|
|
|
|
exit(1);
|
|
|
|
}
|
2023-01-04 22:32:55 +00:00
|
|
|
|
2023-01-06 02:56:33 +00:00
|
|
|
xlang::TypeCheckVisitor typecheck{errorlistener};
|
|
|
|
typecheck.visitFile(tree);
|
|
|
|
if (errorlistener.hasError()) {
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2023-01-05 16:44:30 +00:00
|
|
|
xlang::EmitVisitor emit{outputfile};
|
|
|
|
emit.visitFile(tree);
|
2023-01-04 22:32:55 +00:00
|
|
|
return 0;
|
|
|
|
}
|