-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrint_Visitor.cpp
More file actions
190 lines (161 loc) · 4.47 KB
/
Print_Visitor.cpp
File metadata and controls
190 lines (161 loc) · 4.47 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 "Print_Visitor.hpp"
#include "taxicality_config.hpp"
#include "taxicality.h"
#include <iostream>
using std::cout;
using std::endl;
static string get_operator(int token)
{
if(token < 128)
return string{static_cast<char>(token)};
else
switch(token)
{
case TRUNCATE: return "\\\\";
case LT_EQUALS: return "<=";
case GT_EQUALS: return ">=";
case EQUALS: return "==";
case NOT_EQUALS: return "!=";
case OR: return "or";
case AND: return "and";
case NOT: return "not";
case UNINITDEC: return ":=";
case INITIALIZE: return "<-";
}
return "(unimplemented operator)";
}
static int indent_level;
static void indent()
{
for(int i=0; i<indent_level*4; i++)
cout << ' ';
}
static void print_if_triple(const If_Triple& triple, Print_Visitor& visitor)
{
indent(); cout << "Triple Condition:" << endl;
if(triple.condition)
{
indent_level++;
triple.condition->visit_with(visitor);
indent_level--;
}
else
{
indent(); cout << "NULL";
}
indent(); cout << "Triple Assertion:" << endl;
if(triple.assertion)
{
indent_level++;
triple.assertion->visit_with(visitor);
indent_level--;
}
else
{
indent(); cout << "NULL";
}
indent(); cout << "Triple Value:" << endl;
if(triple.value)
{
indent_level++;
triple.value->visit_with(visitor);
indent_level--;
}
else
{
indent(); cout << "NULL";
}
}
void Print_Visitor::visit(const list<Declaration>& root)
{
for(const auto& decl : root)
{
visit(decl);
}
}
void Print_Visitor::visit(const Declaration& decl)
{
cout << decl.identifier;
switch(decl.init_status)
{
case Declaration::UNINITIALIZED:
cout << ": ";
cout << get_type(decl.rhs.type);
break;
case Declaration::INITIALIZED:
cout << " = ";
decl.rhs.expression->visit_with(*this);
break;
case Declaration::POST_INITIALIZED:
cout << " <- ";
decl.rhs.expression->visit_with(*this);
break;
}
cout << endl;
}
void Print_Visitor::visit(If_Expression& if_expression)
{
indent_level++;
indent(); cout << "If Expression:" << endl;
indent_level++;
indent(); cout << "If Branch:" << endl;
print_if_triple(if_expression.primary_branch,*this);
for(auto& elif_triple : if_expression.elif_branch_list)
{
indent(); cout << "Elif branch:" << endl;
print_if_triple(elif_triple,*this);
}
indent(); cout << "Else Branch:" << endl;
print_if_triple(If_Triple{NULL,
if_expression.else_branch_assert,
if_expression.else_branch_value},*this);
indent_level-=2;
}
void Print_Visitor::visit(Arithmetic_Expression& ae)
{
indent_level++;
indent(); cout << "Arithmetic Expression:" << endl;
indent_level++;
indent(); cout << "Left Expression:" << endl;
ae.left_expression->visit_with(*this);
indent(); cout << "Operator: " << get_operator(ae.token) << endl;
if(ae.right_expression)
{
indent(); cout << "Right Expression:" << endl;
ae.right_expression->visit_with(*this);
}
if(ae.ternary_lt_token)
{
indent(); cout << "Ternary LT Token: " << get_operator(ae.ternary_lt_token) << endl;
indent(); cout << "Ternary LT Expression:" << endl;
ae.ternary_lt_expression->visit_with(*this);
}
indent_level-=2;
}
void Print_Visitor::visit(Identifier_Expression& ie)
{
indent_level++;
indent(); cout << "IDENTIFIER: " << ie.identifier << endl;
indent_level--;
}
void Print_Visitor::visit(Value_Expression& ve)
{
indent_level++;
indent(); cout << "VALUE: " << get_value(ve.value) << endl;
indent_level--;
}
void Print_Visitor::visit(Array_Expression& ae)
{
indent_level++;
indent(); cout << "Array Expression:" << endl;
indent_level++;
indent(); cout << "Array Base:" << endl;
ae.base->visit_with(*this);
indent(); cout << "Array Index:" << endl;
ae.index->visit_with(*this);
indent(); cout << "Second Array Index:";
if(!ae.index2)
cout << " NULL" << endl;
else
ae.index2->visit_with(*this);
}