-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
106 lines (96 loc) · 2.65 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <cstdio>
#include <unistd.h> // for getopt
#include <memory>
#include "lexer.h"
#include "parser2.h"
#include "ast.h"
#include "exceptions.h"
#include "treeprint.h"
#include "interp.h"
enum {
PRINT_TOKENS,
PRINT_AST,
EXECUTE,
};
// The execute function orchestrates the overall program logic,
// but could throw an exception if an error occurs
int execute(int argc, char **argv) {
// handle command line options
int mode = EXECUTE, opt;
while ((opt = getopt(argc, argv, "lp")) != -1) {
switch (opt) {
case 'l':
mode = PRINT_TOKENS;
break;
case 'p':
mode = PRINT_AST;
break;
default:
RuntimeError::raise("Unknown option: %c", opt);
}
}
// determine source of input
FILE *in;
const char *filename;
if (optind < argc) {
// read input from file
filename = argv[optind];
in = fopen(filename, "r");
if (!in) {
RuntimeError::raise("Could not open input file '%s'", filename);
}
} else {
filename = "<stdin>";
in = stdin;
}
// create the Lexer
std::unique_ptr<Lexer> lexer(new Lexer(in, filename));
if (mode == PRINT_TOKENS) {
// just print the tokens
bool done = false;
while (!done) {
Node *tok = lexer->peek();
if (!tok) {
done = true;
} else {
tok = lexer->next();
int kind = tok->get_tag();
std::string lexeme = tok->get_str();
printf("%d:%s:%s\n", kind, lexeme.c_str(), lexer->node_tag_to_string(tok->get_tag()).c_str());
delete tok;
}
}
} else if (mode == PRINT_AST || mode == EXECUTE) {
// Create parser and parse the input
std::unique_ptr<Parser2> parser2(new Parser2(lexer.release()));
std::unique_ptr<Node> ast(parser2->parse());
if (mode == PRINT_AST) {
// Print a text representation of the AST
ASTTreePrint tp;
tp.print(ast.get());
} else {
// Execute the program: note that the Interpreter assumes responsibility
// for deleting the AST
Interpreter interp(ast.release());
interp.analyze();
Value result = interp.execute();
printf("Result: %s\n", result.as_str().c_str());
}
}
return 0;
}
int main(int argc, char **argv) {
try {
return execute(argc, argv);
} catch (BaseException &ex) {
if (ex.has_location()) {
// the exception has Location information
const Location &loc = ex.get_loc();
fprintf(stderr, "%s:%d:%d: Error: %s\n", loc.get_srcfile().c_str(), loc.get_line(), loc.get_col(), ex.what());
} else {
// no Location
fprintf(stderr, "Error: %s\n", ex.what());
}
return 1;
}
}