-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_tree.cpp
More file actions
68 lines (65 loc) · 1.29 KB
/
code_tree.cpp
File metadata and controls
68 lines (65 loc) · 1.29 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
#include "code_tree.hpp"
using std::endl;
using std::ostream;
using std::string;
#include <iomanip>
using std::setw;
ostream& operator<<(ostream& o, Code_Tree ct) {
static int depth = 0;
for (int i = 0; i < depth; i++) o << " ";
if (ct.t)
o << ct.t->text << "\n";
else
o << ct.name << "\n";
depth++;
for (auto& e : ct.sub_tokens) o << e;
depth--;
return o;
}
string TOKEN_name(TOKEN t) {
#define echocase(x) \
case x: \
return #x
switch (t) {
echocase(EOF_T);
echocase(UNKNOWN);
echocase(ERROR);
echocase(PLUS);
echocase(MINUS);
echocase(MULT);
echocase(DIV);
echocase(EXP);
echocase(LESS);
echocase(LESS_EQ);
echocase(GREATER);
echocase(GREATER_EQ);
echocase(EQUAL);
echocase(NOT_EQUAL);
echocase(ASSIGN);
echocase(NOT);
echocase(LPAREN);
echocase(RPAREN);
echocase(LBRACE);
echocase(RBRACE);
echocase(LBRACKET);
echocase(RBRACKET);
echocase(AND);
echocase(OR);
echocase(DOT);
echocase(AT);
echocase(COLON);
echocase(SEMICOLON);
echocase(COMMA);
echocase(IDENT);
echocase(INT);
echocase(STRING);
echocase(REAL);
#undef echocase
default:
return "bad token";
}
}
ostream& operator<<(ostream& o, Token t) {
return o << "|" << setw(11) << TOKEN_name(t.id) << " | '" << t.text << "' at "
<< t.line << ":" << t.col;
}