-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
173 lines (160 loc) · 4.9 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
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
#include "includes.hpp"
#include "StaticString.hpp"
#include "Lexer.hpp"
#include "Parser.hpp"
#include "ASTWalker.hpp"
using namespace std;
//void ast_traverse(ExprAST *root) {
// if (root) {
// switch (root->id) {
// case ast_binary:
// ast_traverse(((BinaryExprAST *)(root))->LHS);
// ast_traverse(((BinaryExprAST *)(root))->RHS);
// cout << ((BinaryExprAST *)(root))->op << " ";
// break;
// case ast_number:
// cout << ((NumberExprAST *)(root))->num << " ";
// break;
// case ast_identifier:
// ((IdentifierExprAST *)(root))->name.print();
// break;
// case ast_call:
// for (size_t i = 0; i < ((CallExprAST *)root)->num_args; i++) {
// ast_traverse(((CallExprAST *)(root))->args[i]);
// }
// ((CallExprAST *)(root))->identifier.print();
// break;
// default:
// break;
// }
// }
//}
#ifdef ARDUINO
void setup() {
init();
Serial.begin(9600);
}
#endif
int main() {
#ifndef ARDUINO
string str;
cout << "Ready: ";
while (getline(cin, str)) {
if (str == "exit" || str == "quit") {
break;
}
if (str.empty()) {
cout << "Ready: ";
continue;
}
if (str == "clear") {
symbol_table.clear();
str.clear();
cout << "Symbol table cleared" << endl;
cout << "Ready: ";
continue;
}
Lexer::load_input_string(str.c_str());
if (Lexer::current_tok.id == Lexer::tok_end) {
cout << "Ready: ";
continue;
}
ExprAST *ast = Parser::parse_statement(NULL);
ast_post_order_traverse(ast);
if (s.size() != 1) {
cout << "Syntax error" << endl;
} else {
Symbol val = s.pop();
if (val.is_var) {
StaticArray<Symbol, 16>::iterator_prototype iter = symbol_table.find(val);
if (iter == symbol_table.end()) {
cout << "Undefined symbol" << endl;
} else {
cout << iter->value << endl;
}
} else {
cout << val.value << endl;
}
}
s.clear();
delete ast;
cout << "Ready: ";
}
#else
setup();
StaticString<128> str;
char c;
Serial.print("Ready: ");
while (true) {
if (Serial.available() > 0) {
c = Serial.read();
if (c == '\n' || c == '\r') {
Serial.println();
if (str.empty()) {
Serial.print("Ready: ");
continue;
}
if (str == "clear") {
symbol_table.clear();
Serial.println("Symbol table cleared");
str.clear();
Serial.print("Ready: ");
continue;
}
Lexer::load_input_string(str.c_str());
if (Lexer::current_tok.id == Lexer::tok_end) {
str.clear();
Serial.print("Ready: ");
continue;
}
ExprAST *ast = Parser::parse_statement(NULL);
ast_post_order_traverse(ast);
if (s.size() != 1) {
Serial.println("Syntax error");
} else {
Symbol val = s.pop();
if (val.is_var) {
StaticArray<Symbol, 16>::iterator_prototype iter = symbol_table.find(val);
if (iter == symbol_table.end()) {
Serial.println("Undefined symbol");
} else {
Serial.println(iter->value);
}
} else {
Serial.println(val.value);
}
}
// Serial.flush();
s.clear();
str.clear();
delete ast;
Serial.print("Ready: ");
} else if (c == '\b') {
if (str.pop())
Serial.print("\b \b");
} else {
if (str.append(c))
Serial.print(c);
}
}
}
Serial.end();
#endif
// while (Lexer::current_tok.id != Lexer::tok_end) {
// switch (Lexer::current_tok.id) {
// case Lexer::tok_character:
// cout << Lexer::current_tok.c << endl;
// break;
// case Lexer::tok_number:
// cout << Lexer::current_tok.num << endl;
// break;
// case Lexer::tok_identifier:
// Lexer::current_tok.str.print();
// break;
// default:
// break;
// }
// Lexer::get_next_token();
// }
return 0;
}