-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathl-inference-frontend.cpp
93 lines (76 loc) · 1.65 KB
/
l-inference-frontend.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
#include <iostream>
#include <fstream>
#include <sstream>
#include "ast/expression.h"
#include "TypeInference.h"
using namespace std;
int yylex();
int yyparse();
extern int yy_scan_string(const char* c);
void (*parser_error_fn)(string);
int curr_lineno;
Expression* res_expr;
string int_to_string(long i) {
char temp[100];
sprintf(temp, "%ld", i);
string res = temp;
return res;
}
long int string_to_int(const string & s) {
stringstream ss;
ss<< s;
long int res;
ss >> res;
return res;
}
void parse(const string & s, void (*write_fn)(string)) {
curr_lineno = 1;
res_expr = NULL;
parser_error_fn = write_fn;
yy_scan_string(s.c_str());
if(yyparse()!=0) {
res_expr = NULL;
}
}
void report_error(string c) {
cout << c << endl;
}
int main(int argc, char** argv) {
if(argc <=1) {
cout << "No filename given " << endl;
return -1;
}
string filename;
bool print_ast = false;
if(argc == 2)
filename = argv[1];
else {
filename = argv[2];
string flags = argv[1];
if(flags == "-ast") print_ast = true;
}
ifstream file(filename.c_str());
if(!file.is_open()) {
cout << "File \"" << filename << "\" cannot be opened." << endl;
return -1;
}
string res;
bool first = true;
while(!file.eof()) {
if(!first) res+="\n";
first = false;
string temp;
std::getline(file, temp);
res+=temp;
}
parse(res, report_error);
if(print_ast && res_expr != NULL) {
cout << "****************** AST ******************" << endl;
cout << res_expr->to_string() << endl;
cout << "*****************************************" << endl;
}
if(res_expr != NULL) {
cout << res_expr->to_string() << endl;
TypeInference ti(res_expr);
}
}