forked from NesManrique/LenguajesII
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblahc.cpp
144 lines (122 loc) · 2.93 KB
/
blahc.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
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
#include <iostream>
#include <fstream>
#include <cstdio>
#include <vector>
#include "blahlog.h"
#include "blahast.h"
#include "blahsymtable.h"
#include "tac.h"
#include "spim.h"
extern int yyparse();
extern Log log;
extern Symtable table;
extern NBlock* programAST;
using namespace std;
IBlock* iblock;
struct Config{
string file;
bool printTree;
bool printTAC;
bool printSym;
string outfile;
public:
Config():outfile("out.s"),file(""),printTree(false),printTAC(false),printSym(false){}
};
void printHelp(){
cout<<"./blahc [-o file] [-a] [-s] [-t] file "<<endl;
cout<<"\t-a\tprints the AST"<<endl;
cout<<"\t-s\tprints the symbol table"<<endl;
cout<<"\t-t\tprints the TAC generated"<<endl;
cout<<"\t-o file\tgenerate spim in file file.s,"<<endl;
exit(EXIT_FAILURE);
}
void parseArgs(int argc ,char **argv, Config &opts){
if (argc==1) printHelp();
for (int i=1;i<argc;i++){
if(argv[i][0]=='-'){
switch(argv[i][1]){
case 'a':
opts.printTree=true;
break;
case 's':
opts.printSym=true;
break;
case 't':
opts.printTAC=true;
break;
case 'o':
if(i+1<argc){
opts.outfile=string(argv[i+1])+".s";
} else {
printHelp();
}
break;
}
} else{
if(opts.file!=""){
cout<<"Error: more than one file especified"<<endl;
exit(EXIT_FAILURE);
}
opts.file=string(argv[i]);
}
}
}
int main(int argc, char **argv){
Config opts;
parseArgs(argc,argv,opts);
/*Insercion de tipos basicos*/
table.insert(new TBool());
table.insert(new TInteger());
table.insert(new TFloat());
table.insert(new TUndef());
table.insert(new TError());
table.insert(new TChar());
table.insert(new TVoid());
{
vector<TType*> t;
t.push_back(table.lookupType("Integer"));
table.insert(new TFunc(string("print"),*table.lookupType("Void"),t));
t.clear();
t.push_back(table.lookupType("Float"));
table.insert(new TFunc(string("print"),*table.lookupType("Void"),t));
t.clear();
table.insert(new TFunc(string("readI"),*table.lookupType("Integer"),t));
table.insert(new TFunc(string("readF"),*table.lookupType("Float"),t));
}
if(freopen(opts.file.c_str(),"r",stdin)==NULL){
std::cerr<<"Failed to open "<<opts.file<<std::endl;
exit(EXIT_FAILURE);
};
yyparse();
if(programAST!=NULL){
if(programAST->typeChk(table)->name!="error"){
if(opts.printTree){
cout<<"===========AST========"<<endl;
programAST->print(std::cout);
}
if(opts.printSym){
cout<<"=====SymbolTable======"<<endl;
table.print(std::cout);
}
TAC tac =TAC();
if(!tac.ASTtoTAC(programAST)){
log.print(std::cout);
exit(EXIT_FAILURE);
}
if(opts.printTAC){
cout<<"=========TAC=========="<<endl;
tac.print(std::cout);
}
SPIM* spim = new SPIM(&tac);
ofstream output;
output.open(opts.outfile);
spim->print(output);
}else{
std::cout<<"Type Error"<<std::endl;
}
}else{
std::cout<<"Syntax Error"<<std::endl;
}
log.print(std::cout);
return 0;
}