aboutsummaryrefslogtreecommitdiff
path: root/tree.c
blob: 11c08d44b9df07b4cb9ce49e923247fa8cf2f613 (plain)
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
207
208
209
210
211
212
213
214
215
216
217
218
219
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>

#include "node.h"
#include "tree.h"
#include "y.tab.h"
#include "pc.h"

/* parse tree funcs */
ptree* mktree(type, l, r)
int type;
ptree *l, *r;
{
	ptree *t = (ptree*)malloc(sizeof(ptree));
	assert(t);

	t->type = type;
	t->l = l;
	t->r = r;

	return t;
}

ptree* mkid(n)
node *n;
{
	ptree *p = mktree(ID, NULL, NULL);
	p->attr.nval = n;
	return p;
}

ptree* mkinum(n)
int n;
{
	ptree *p = mktree(INUM, NULL, NULL);
	p->attr.ival = n;
	return p;
}

ptree* mkrnum(n)
float n;
{
	ptree *p = mktree(RNUM, NULL, NULL);
	p->attr.rval = n;
	return p;
}

ptree* mkop(type, sub, l, r)
int type, sub;
ptree *l, *r;
{
	ptree *p = mktree(type, l, r);
	p->attr.opval = sub;
	return p;
}

void update_type_info(list, type)
int type;
ptree *list;
{
	assert(list);
	if (list->type == ID) {
		list->attr.nval->var_type = type;
		return;
	}

	while (list->r && list->r->type == ID) {
		/*Set type of right child through list*/
		list->r->attr.nval->var_type = type;

		if (list->l) {
			if (list->l->type == LIST) {
				list = list->l;
				continue; /*Continue down list*/
			} else if (list->l->type == ID)
				/*Set type of first declared ID
				    (only left node in LIST)*/
				list->l->attr.nval->var_type = type;
		}
		return; /*At _end_ of list (did not continue)*/
	}
}

void set_ret_type(t)
ptree *t;
{
	if (!t){
		return;
	}

	switch (t->type) {
	
		case ADDOP:
			fprintf(stderr, "[ADDOP]");
			break;
		case MULOP:
			fprintf(stderr, "[MULOP]");
			break;
		case RELOP:
			fprintf(stderr, "[RELOP]");
			break;
		case NOT:
			fprintf(stderr, "[NOT]");
			break;
		case ARRAY_ACCESS:
			fprintf(stderr, "[ARRAY ACCESS]");
			break;
		case LIST:
			fprintf(stderr, "[LIST]");
			break;
		case ID:
			if (t->r && t->r->attr.nval)
				fprintf(stderr, "[ID: %s %s]",
					t->r->attr.nval->name,
					pretty_type(
						t->attr.nval->var_type));
			else
				fprintf(stderr, "[ID: %s %s]",
					t->attr.nval->name,
					pretty_type(
						t->attr.nval->var_type));
			break;
		case INUM:
			fprintf(stderr, "[INUM: %d]", t->attr.ival);
			break;
		case RNUM:
			fprintf(stderr, "[RNUM: %f]", t->attr.rval);
			break;
		case ASSIGNOP:
			fprintf(stderr, "[ASSIGN]");
			break;
		default:
			fprintf(stderr, "\t%d", t->type);
			yyerror("Error in tree_print");
	}
	set_ret_type(t->l);
	set_ret_type(t->r);
	t->ret_type = check_ret_type(t);

	return;
}


/*PRINT FUNCS*/

void print_tree(t)
ptree *t;
{
	fprintf(stderr, "\n\nTREE\n"
	"==========================================================\n");
	aux_tree_print(t, 0);
	fprintf(stderr,
	"**********************************************************\n");
	return;
}

void aux_tree_print(t, spaces)
ptree* t;
int spaces;
{
	int i;
	if ( t ) {
		for (i = 0; i < spaces; i++)
			fprintf(stderr," ");
		switch (t->type) {
		
			case ADDOP:
				fprintf(stderr, "[ADDOP]");
				break;
			case MULOP:
				fprintf(stderr, "[MULOP]");
				break;
			case RELOP:
				fprintf(stderr, "[RELOP]");
				break;
			case NOT:
				fprintf(stderr, "[NOT]");
				break;
			case ARRAY_ACCESS:
				fprintf(stderr, "[ARRAY ACCESS]");
				break;
			case LIST:
				fprintf(stderr, "[LIST]");
				break;
			case ID:
				if (t->r && t->r->attr.nval)
					fprintf(stderr, "[ID: %s %s]",
						t->r->attr.nval->name,
						pretty_type(
							t->attr.nval->var_type));
				else
					fprintf(stderr, "[ID: %s %s]",
						t->attr.nval->name,
						pretty_type(
							t->attr.nval->var_type));
				break;
			case INUM:
				fprintf(stderr, "[INUM: %d]", t->attr.ival);
				break;
			case RNUM:
				fprintf(stderr, "[RNUM: %f]", t->attr.rval);
				break;
			case ASSIGNOP:
				fprintf(stderr, "[ASSIGN]");
				break;
			default:
				fprintf(stderr, "\t%d", t->type);
				yyerror("Error in tree_print");
		}
		fprintf(stderr," %d\n", t->ret_type);
		aux_tree_print(t->l, spaces + 2);
		fprintf(stderr,"\n");
		aux_tree_print(t->r, spaces + 2);
		fprintf(stderr,"\n");
	}

}