-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
190 lines (165 loc) · 4.13 KB
/
Copy pathtest.cpp
File metadata and controls
190 lines (165 loc) · 4.13 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include <iostream>
#include "compiler.h"
using namespace std;
void printInputToken(inputToken t) {
cout << "'" << t.c << "' - " << t.col << "," << t.row << endl;
}
void testInputStream(string fileName) {
InputStream is(fileName);
while (!is.eof()) {
printInputToken(is.consume());
}
}
string typeToString(Type t) {
switch (t) {
case PUNC:
return "PUNC";
case NUM:
return "NUM";
case STR:
return "STR";
case KW:
return "KW";
case JMP:
return "JMP";
case REG:
return "REG";
case OP:
return "OP";
case REF:
return "REF";
case CONV:
return "CONV";
case END:
return "END";
case ERR:
return "ERR";
default:
return "How?";
}
}
void printToken(Token t) {
cout << "Token {" << endl;
cout << " type = " << typeToString(t.type) << endl;
cout << " val = \"" << t.value << "\"" << endl;
cout << " coord = (" << t.col << ',' << t.row << ')' << endl;
cout << "}" << endl;
}
void testLexer(std::string fileName) {
cout << "======== Starting InputStream/Lexer test ========" << endl;
InputStream is(fileName);
Lexer lex(&is);
Token t = lex.read();
int i = 0;
while (t.type != END) {
// if (t.type == ERR)
printToken(t);
i++;
t = lex.read();
}
cout << i << endl;
}
string stringExpr(ExprStruct expr) {
string res = "";
res += "[" + expr.operand1.value + "]";
if (expr.operation != "")
res += " [" + expr.operation + "] [" + expr.operand2.value + "]";
return res;
}
void printJump(StatementStruct *st) {
Parser::JmpStruct jmp = *(Parser::JmpStruct *) st->ptr;
cout << "JmpStruct {" << endl;
cout << " arg1: " << jmp.arg1 << endl;
cout << " arg2: " << jmp.arg2 << endl;
cout << " instruction: " << jmp.instruction << endl;
cout << " label: " << jmp.label << endl;
cout << "};" << endl;
}
void printLoad(StatementStruct *st) {
Parser::LoadStruct load = *(Parser::LoadStruct *) st->ptr;
cout << "LoadStruct {" << endl;
cout << " reg: " << load.reg << endl;
cout << " size: " << load.load << endl;
cout << " expression: " << stringExpr(load.expr) << endl;
cout << " fromMemory: " << load.reference << endl;
cout << "};" << endl;
}
void printStore(StatementStruct *st) {
Parser::StoreStruct store = *(Parser::StoreStruct *) st->ptr;
cout << "StoreStruct {" << endl;
cout << " dest: " << stringExpr(store.dest) << endl;
cout << " val: " << store.val.value << endl;
cout << " size: " << store.store << endl;
cout << "};" << endl;
}
void printConvert(StatementStruct *st) {
Parser::ConvertStruct conv = *(Parser::ConvertStruct *) st->ptr;
cout << "ConvertStruct {" << endl;
cout << " dest: " << conv.destReg << endl;
cout << " source: " << conv.sourceReg << endl;
cout << " conversion: " << conv.conv << endl;
cout << "};" << endl;
}
void printKeyword(StatementStruct *st) {
Parser::KeyStruct kw = *(Parser::KeyStruct *) st->ptr;
cout << "KeyStruct {" << endl;
cout << " instruction: " << kw.instruction << endl;
if (kw.value != "")
cout << " value: " << kw.value << endl;
cout << "};" << endl;
}
void printStatement(StatementStruct *st) {
switch (st->type) {
case JUMP:
printJump(st);
break;
case LOAD:
printLoad(st);
break;
case STORE:
printStore(st);
break;
case CONVERT:
printConvert(st);
break;
case KEYWORD:
printKeyword(st);
break;
default:
break;
}
}
void testParser(std::string fileName) {
InputStream is(fileName);
Lexer lex(&is);
Parser parser(&lex);
std::vector<StatementStruct*> v = parser.parse();
for (int i = 0; i < v.size(); i++) {
printStatement(v.at(i));
}
}
void testCompiler(std::string fileName) {
InputStream is(fileName);
Lexer lex(&is);
Parser parser(&lex);
Compiler compiler;
compiler.Compile(&parser);
}
void printError(std::string name, std::string error, Token t) {
std::cout << "Error" << name << ": " << error << " at (" << t.col << ',' << t.row << ")\n";
}
#define ERROR(name,error,tok) \
{ \
printError(name, error, tok); \
return false; \
}
void tempo(string str1, string str2) {
cout << str1 << "," << str2 << endl;
}
int main() {
string fileName = "tests/test1.s";
// testInputStream(fileName);
// testLexer(fileName);
// testParser(fileName);
testCompiler(fileName);
}