-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast-build.cc
207 lines (169 loc) · 6.41 KB
/
ast-build.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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*********************************************************************************************
cfglp : A CFG Language Processor
--------------------------------
About:
Implemented by Uday Khedker (http://www.cse.iitb.ac.in/~uday) for
the courses cs302+cs306: Language Processors (theory and lab) at
IIT Bombay. Release date Jan 6, 2013. Copyrights reserved by Uday
Khedker. This implemenation has been made available purely for
academic purposes without any warranty of any kind.
Please see the README file for some details. A doxygen generated
documentation can be found at http://www.cse.iitb.ac.in/~uday/cfglp
***********************************************************************************************/
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <string>
#include <vector>
using namespace std;
#include "common-classes.hh"
#include "evaluate.hh"
#include "reg-alloc.hh"
#include "symtab.hh"
#include "ast.hh"
#include "support.hh"
/*
Please see the documentation in file ast.hh for a description of the
classes. Here we provide an implementation of the class methods.
*/
/************* Methods for class asgn_Ast ******************/
asgn_Ast::asgn_Ast(ast_Ptr l, ast_Ptr r, int line)
{
t_op = asgn;
node_arity = binary;
CHECK_INVARIANT(l != NULL, "Left child of an assignment tree node cannot be NULL")
CHECK_INVARIANT(l->get_Tree_Op() != num_Leaf, "Left child of an assignment tree node cannot be a number")
CHECK_INVARIANT(r != NULL, "Right child of an assignment tree node cannot be NULL")
CHECK_INVARIANT(l->get_Tree_Op() != asgn, "Left child cannot be an assignment node")
CHECK_INVARIANT(r->get_Tree_Op() != asgn, "Right child cannot be an assignment node")
left = l;
right = r;
lineno = line;
}
asgn_Ast& asgn_Ast::operator=(const asgn_Ast& rhs)
{
t_op = rhs.t_op;
node_arity = rhs.node_arity;
left = rhs.left;
right = rhs.right;
lineno = rhs.lineno;
return *this;
}
/************* Methods for class name_Ast ******************/
name_Ast::name_Ast(string n)
{
t_op = name_Leaf;
name = n;
node_arity = zero_Arity;
sym_entry = symtab_in_scope_P->get_Sym_Entry(n);
}
sym_Entry_Ptr name_Ast::get_Sym_Entry()
{
return sym_entry;
}
/************* Methods for class int_num_Ast ******************/
int_num_Ast::int_num_Ast(int n)
{
t_op = num_Leaf;
num = n;
node_arity = zero_Arity;
}
/************* Methods for class ret_Ast ******************/
ret_Ast::ret_Ast()
{
t_op = ret;
node_arity = zero_Arity;
}
exp_var_Ast::exp_var_Ast(string n)
{
t_op = exp_var_Leaf;
name = n;
node_arity = zero_Arity;
sym_entry = symtab_in_scope_P->get_Sym_Entry(n);
}
exp_var_Ast::exp_var_Ast(string n, sym_Entry_Ptr s)
{
t_op = arti_var_Leaf;
name = n;
node_arity = zero_Arity;
sym_entry = s;
}
sym_Entry_Ptr exp_var_Ast::get_Sym_Entry()
{
return sym_entry;
}
/* methods for class float_num_Ast */
float_num_Ast::float_num_Ast(double n)
{
t_op = num_Leaf;
num = n;
node_arity = zero_Arity;
}
/************* Methods for class mult_Ast ******************/
mult_Ast::mult_Ast(ast_Ptr l, ast_Ptr r)
{
t_op = mult_tree;
node_arity = binary;
CHECK_INVARIANT(l != NULL, "Left child of an multiply tree node cannot be NULL")
//CHECK_INVARIANT(l->get_Tree_Op() != num_Leaf, "Left child of an assignment tree node cannot be a number")
CHECK_INVARIANT(r != NULL, "Right child of an multiply tree node cannot be NULL")
CHECK_INVARIANT(l->get_Tree_Op() != asgn, "Left child cannot be an assignment node")
CHECK_INVARIANT(r->get_Tree_Op() != asgn, "Right child cannot be an assignment node")
left = l;
right = r;
data_type = left->get_Val_Type();
}
/************* Methods for class plus_Ast ******************/
plus_Ast::plus_Ast(ast_Ptr l, ast_Ptr r)
{
t_op = plus_tree;
node_arity = binary;
CHECK_INVARIANT(l != NULL, "Left child of an plus tree node cannot be NULL")
//CHECK_INVARIANT(l->get_Tree_Op() != num_Leaf, "Left child of an assignment tree node cannot be a number")
CHECK_INVARIANT(r != NULL, "Right child of an plus tree node cannot be NULL")
CHECK_INVARIANT(l->get_Tree_Op() != asgn, "Left child cannot be an assignment node")
CHECK_INVARIANT(r->get_Tree_Op() != asgn, "Right child cannot be an assignment node")
left = l;
right = r;
data_type = left->get_Val_Type();
}
/************* Methods for class minus_Ast ******************/
minus_Ast::minus_Ast(ast_Ptr l, ast_Ptr r)
{
t_op = minus_tree;
node_arity = binary;
CHECK_INVARIANT(l != NULL, "Left child of an minus tree node cannot be NULL")
//CHECK_INVARIANT(l->get_Tree_Op() != num_Leaf, "Left child of an assignment tree node cannot be a number")
CHECK_INVARIANT(r != NULL, "Right child of an minus tree node cannot be NULL")
CHECK_INVARIANT(l->get_Tree_Op() != asgn, "Left child cannot be an assignment node")
CHECK_INVARIANT(r->get_Tree_Op() != asgn, "Right child cannot be an assignment node")
left = l;
right = r;
data_type = left->get_Val_Type();
}
/************* Methods for class div_Ast ******************/
div_Ast::div_Ast(ast_Ptr l, ast_Ptr r)
{
t_op = divide_tree;
node_arity = binary;
CHECK_INVARIANT(l != NULL, "Left child of an div tree node cannot be NULL")
//CHECK_INVARIANT(l->get_Tree_Op() != num_Leaf, "Left child of an assignment tree node cannot be a number")
CHECK_INVARIANT(r != NULL, "Right child of an div tree node cannot be NULL")
CHECK_INVARIANT(l->get_Tree_Op() != asgn, "Left child cannot be an assignment node")
CHECK_INVARIANT(r->get_Tree_Op() != asgn, "Right child cannot be an assignment node")
left = l;
right = r;
data_type = left->get_Val_Type();
}
/************* Methods for class uminus_Ast ******************/
uminus_Ast::uminus_Ast(ast_Ptr p)
{
t_op = uminus;
node_arity = unary;
CHECK_INVARIANT(p != NULL, "Child of an uminus tree node cannot be NULL")
//CHECK_INVARIANT(l->get_Tree_Op() != num_Leaf, "Left child of an assignment tree node cannot be a number")
CHECK_INVARIANT(p->get_Tree_Op() != asgn, "Child cannot be an assignment node")
left = p;
right = NULL;
data_type = left->get_Val_Type();
}