xlang/bootstrap/main.cc

65 lines
1.3 KiB
C++
Raw Normal View History

#include <stdlib.h>
#include <unistd.h>
#include <emit.hh>
2023-01-05 20:12:15 +00:00
#include <error.hh>
#include <iostream>
#include <string>
2023-01-06 02:56:33 +00:00
#include <typecheck.hh>
#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);
auto *tree = parser.file();
2023-01-05 20:12:15 +00:00
if (errorlistener.hasError()) {
exit(1);
}
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);
return 0;
}