-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse.cc
50 lines (47 loc) · 1.12 KB
/
parse.cc
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
#include <vector>
#include "parse.h"
unique_ptr<ExprAST> Parse(const string& inp) {
vector<BodyAST*> stack;
auto *ast = new BodyAST;
for (char ch : inp) {
switch (ch) {
case '+':
ast->append(new OperationAST(true));
break;
case '-':
ast->append(new OperationAST(false));
break;
case '<':
ast->append(new ShiftAST(false));
break;
case '>':
ast->append(new ShiftAST(true));
break;
case '[':
stack.push_back(ast);
ast = new WhileAST;
break;
case ']':
if (stack.empty()) // error, for now just discard the input
break;
stack.back()->append(ast);
ast = stack.back();
stack.pop_back();
break;
case '.':
ast->append(new PrintAST);
break;
case ',':
ast->append(new GetAST);
break;
default: break; // ignore invalid characters
}
}
// automatically close all open blocks
while (!stack.empty()) {
stack.back()->append(ast);
ast = stack.back();
stack.pop_back();
}
return unique_ptr<ExprAST>(ast);
}