-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcompiler.cpp
More file actions
155 lines (140 loc) · 4.22 KB
/
compiler.cpp
File metadata and controls
155 lines (140 loc) · 4.22 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <getopt.h>
#include <iostream>
#include <fstream>
#include <map>
#include "lexer.hpp"
#include "syntaxer.hpp"
#include "semantixer.hpp"
#include "interpreter.hpp"
using namespace std;
class ArgReader {
private:
int argc;
char **argv;
map < char , string > parsed_args;
const char required[1] = {'f'};
void readArgs() {
int opt;
while (true) {
opt = getopt(argc, argv, "l:s:S:f:h");
switch(opt) {
case 'f':
parsed_args['f'] = optarg;
continue;
case 'l':
parsed_args['l'] = optarg;
continue;
case 's':
parsed_args['s'] = optarg;
continue;
case 'S':
parsed_args['S'] = optarg;
continue;
case 'h':
printHelp();
exit(1);
case -1:
for(auto c:required) {
if (parsed_args[c] == "") {
printHelp();
exit(1);
}
}
break;
}
break;
}
}
public:
ArgReader(int argc, char **argv) {
this->argc = argc;
this->argv = argv;
readArgs();
}
string getArg(char arg) {
return parsed_args[arg];
}
void printHelp() {
printf("Usage: ./compiler -f <program.dl> [Options] \n");
printf("Options: \n\
\t-f <program.dl>:\tSource code to be compiled. This is a required argument\n\
\t[-l <output>]:\tPrint the output of lexer to <output> file.\n\
\t[-s <output>]:\tPrint the output of syntax analyzer to <output> file.\n\
\t[-S <output>]:\tPrint the output of semantic analyzer to <output> file.\n");
}
};
vector < Token > *tokenized_code;
int main(int argc, char **argv) {
ArgReader args(argc, argv);
string file_name = args.getArg('f');
ifstream file(file_name);
if(!file.good()) {
cout<<"Error: file "<<file_name<<" doesn't exists\n";
exit(1);
}
Lexer lexer(file);
tokenized_code = lexer.get_tokens();
if (tokenized_code == NULL) {
lexer.print_errors();
exit(2);
}
if (args.getArg('l') != "") {
lexer.print(args.getArg('l'));
}
Syntaxer syntaxer(tokenized_code);
Node* root = syntaxer.get_root();
if (root == NULL) {
syntaxer.print_errors();
exit(3);
}
if (args.getArg('s') != "") {
syntaxer.print(args.getArg('s'), root);
}
Semantixer semantixer(root);
root = semantixer.traverse();
if (root == NULL) {
semantixer.print_errors();
exit(4);
}
if (args.getArg('S') != "") {
semantixer.print(args.getArg('S'));
}
int prog_params = semantixer.prog_params;
Node* param_root = NULL;
if(prog_params) {
cout<<"Enter prog params:\n";
Lexer param_lexer(cin);
vector < Token > * param_token = param_lexer.get_tokens();
if (param_token == NULL) {
param_lexer.print_errors();
exit(2);
}
for(auto token: *param_token) {
if(token.type == ATOM || (token.type == KEYWORD && token.content != "quote")) {
cout<<"Error: Params can't have atoms or keywords\n";
exit(2);
}
}
Syntaxer param_syntaxer(param_token);
param_root = param_syntaxer.get_root();
if (param_root == NULL) {
param_syntaxer.print_errors();
exit(3);
}
Semantixer param_semantixer(param_root,false);
param_root = param_semantixer.traverse();
if (param_root == NULL) {
param_semantixer.print_errors();
exit(4);
}
param_semantixer.print("sample.sem");
if((int)param_root->children.size() != prog_params) {
cout<<"Error: Prog has "<<prog_params<<" params but got "<<(int)param_root->children.size()<<endl;
exit(4);
}
}
Interpreter interpreter(root,param_root);
interpreter.interpret();
file.close();
return 0;
}