-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
116 lines (107 loc) · 2.47 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
107
108
109
110
111
112
113
114
115
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include "astprogram.h"
extern FILE *yyin;
extern int yyparse();
// all global objects
memallocator alloc;
memory mem;
symboltable symtab;
program* parsed_program;
typelibrary typelib;
bool stage_parse;
bool stage_check;
bool stage_generate;
bool stage_run;
bool debugit;
string filename = string("");
void die()
{
printf("usage: toklas [-P | -C | -G] [-V] <file>\n");
printf(" where flags are:\n");
printf(" -P parse only\n");
printf(" -C parse and check semantics only\n");
printf(" -G stop after generating code\n");
printf(" -V print out debugging information\n");
exit(1);
}
void getOptions(int argc, char** argv)
{
if (argc < 2) {
die();
}
stage_parse = stage_check = stage_generate = stage_run = true;
debugit = false;
bool foundastage = false;
for (int i = 1 ; i< argc ; i++) {
if (string(argv[i]) == "-P") {
if (foundastage) die();
foundastage = true;
stage_check = false;
stage_generate = false;
stage_run = false;
} else if (string(argv[i]) == "-C") {
if (foundastage) die();
foundastage = true;
stage_generate = false;
stage_run = false;
} else if (string(argv[i]) == "-G") {
if (foundastage) die();
foundastage = true;
stage_run = false;
} else if (string(argv[i]) == "-V") {
debugit = true;
} else {
filename = string(argv[i]);
}
}
if (filename == "") die();
}
int main(int argc, char** argv)
{
getOptions(argc, argv);
if (stage_parse) {
if (debugit) cout << "parsing..." << endl;
yyin = fopen(filename.c_str(), "r");
int error = yyparse();
if (error) {
cout << "parse error." << endl;
exit(1);
}
if (debugit) {
cout << "output from parse:" << endl;
parsed_program->debug(0);
}
}
if (stage_check) {
if (debugit) cout << "checking semantics..." << endl;
parsed_program->createTypes();
typelib.debugTypes();
parsed_program->semanticCheck();
}
if (stage_generate) {
if (debugit) cout << "generating code..." << endl;
parsed_program->generateCode();
typelib.numberCode();
if (debugit) {
cout << "generated program was:" << endl;
typelib.debugCode();
}
}
if (stage_run) {
if (debugit) cout << "running program..." << endl;
operation* pc;
type* gt = typelib.getTypeByName("##global");
method* gtm = gt->getMethodByName("##run");
pc = gtm->code;
while((pc = pc->execute())) {
if (debugit) {
mem.debugMemory();
pc->debug(true);
}
}
if (debugit) mem.debugMemory();
}
exit(0);
}