-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexpressions.c
executable file
·206 lines (191 loc) · 5.62 KB
/
expressions.c
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
/* expressions.c by William Ho */
#include "expressions.h"
#include "globals.h"
#include "declarations.h"
#include "symtable.h"
#include "y.tab.h"
#include <stdlib.h>
#include <stdio.h>
struct expr_node *new_string_node(char *ptr) {
struct string_node *node = malloc(sizeof(struct string_node));
node->nodetype = STRING;
node->str = ptr;
return (struct expr_node *) node;
}
struct expr_node *new_sym_node(struct symbol *sym) {
struct sym_node *node = malloc(sizeof(struct sym_node));
node->nodetype = IDENT;
node->sym = sym;
return (struct expr_node *) node;
}
// for PLUSEQ, MINUSEQ, TIMESEQ, etc
struct expr_node *new_asgn(int type, struct expr_node *lval,
struct expr_node *rval)
{
struct expr_node *node = new_binary_node(type,lval,rval);
return new_asgn_node(lval,node);
}
struct expr_node *new_func_call_node(
struct expr_node *func, struct expr_node *arg)
{
struct func_call_node *node = malloc(sizeof(struct func_call_node));
node->nodetype = E_FUNC_CALL;
node->func = func;
node->first_arg = (struct func_arg_node *)arg;
return (struct expr_node *) node;
}
struct expr_node *new_func_arg_list(struct expr_node *first_arg) {
struct func_arg_node *node = malloc(sizeof(struct func_arg_node));
node->nodetype = E_FUNC_ARG;
node->val = first_arg;
node->next = 0;
node->last = node;
return (struct expr_node *) node;
}
struct expr_node *add_func_arg(struct expr_node *to, struct expr_node *arg) {
struct func_arg_node *node = malloc(sizeof(struct func_arg_node));
struct func_arg_node *to_casted = (struct func_arg_node *) to;
node->nodetype = E_FUNC_ARG;
node->val = arg;
node->next = 0;
to_casted->last->next = node;
to_casted->last = node;
return (struct expr_node *) node;
}
struct expr_node *new_const_node(int val) {
struct const_node *node = malloc(sizeof(struct const_node));
node->nodetype = NUMBER;
node->val = val;
return (struct expr_node *) node;
}
struct expr_node *new_unary_node(int type, struct expr_node *n) {
struct unary_node *node = malloc(sizeof(struct unary_node));
node->nodetype = E_UNARY;
node->type = type;
node->child = n;
return (struct expr_node *) node;
}
struct expr_node *new_binary_node(int type, struct expr_node *l,
struct expr_node *r)
{
struct binary_node *node = malloc(sizeof(struct binary_node));
node->nodetype = E_BINARY;
node->type = type;
node->left = l;
node->right = r;
return (struct expr_node *) node;
}
struct expr_node *new_asgn_node(struct expr_node *lval,
struct expr_node *rval)
{
struct asgn_node *node = malloc(sizeof(struct asgn_node));
node->nodetype = E_ASGN;
node->lval = lval;
node->rval = rval;
return (struct expr_node *) node;
}
struct expr_node *new_array_access_node(struct expr_node *array, struct expr_node *offset) {
struct array_access_node *node = malloc(sizeof(struct array_access_node));
node->nodetype = E_ARRAY_ACCESS;
node->array = array;
node->offset = offset;
return (struct expr_node *) node;
}
#define EXPR_SPACING(d) for(i=0;i<d;i++) putchar(' ')
void print_expr(struct expr_node *node, int depth) {
struct unary_node *unode = (struct unary_node *)node;
struct binary_node *bnode = (struct binary_node *)node;
struct asgn_node *anode = (struct asgn_node *)node;
struct array_access_node *arrnode = (struct array_access_node *)node;
struct string_node *strnode = (struct string_node *)node;
struct const_node *cnode = (struct const_node *)node;
struct sym_node *symnode = (struct sym_node *)node;
struct func_call_node *fnode = (struct func_call_node *)node;
struct func_arg_node *fanode = (struct func_arg_node *)node;
int i,j = 0;
char c;
EXPR_SPACING(depth);
depth++;
switch(node->nodetype) {
case E_ASGN:
printf("ASSIGNMENT\n");
print_expr(anode->lval,depth);
print_expr(anode->rval,depth);
break;
case E_UNARY:
printf("UNARY OP ",unode->type,unode->type);
switch(unode->type) {
case E_POSTINC: printf("POSTINC"); break;
case E_PREINC: printf("PREINC"); break;
case E_POSTDEC: printf("POSTDEC"); break;
case E_PREDEC: printf("PREDEC"); break;
case '*': printf("DEREFERENCE"); break;
default: printf("%c",unode->type); break;
}
putchar('\n');
print_expr(unode->child,depth);
break;
case E_BINARY:
printf("BINARY OP ",bnode->type,bnode->type);
switch(bnode->type) {
case SHL: printf("<<"); break;
case SHR: printf(">>"); break;
case GTEQ: printf(">="); break;
case LTEQ: printf("<="); break;
case EQEQ: printf("=="); break;
case NOTEQ: printf("!="); break;
case LOGAND: printf("&&"); break;
case LOGOR: printf("||"); break;
default: printf("%c",bnode->type); break;
}
putchar('\n');
print_expr(bnode->left,depth);
print_expr(bnode->right,depth);
break;
case STRING:
printf("STRING \"");
print_escaped_string(strnode->str);
printf("\"\n");
break;
case NUMBER:
printf("CONSTANT %d\n",cnode->val);
break;
case IDENT:
if (!symnode->sym) {
yyerror("invalid symbol\n");
return;
}
printf("SYMBOL %s (declared @%s:%d)\n",symnode->sym->id,
symnode->sym->file,symnode->sym->line);
break;
case E_ARRAY_ACCESS:
printf("ARRAY ACCESS\n");
EXPR_SPACING(depth);
printf("array\n");
print_expr(arrnode->array,depth+1);
EXPR_SPACING(depth);
printf("offset\n");
print_expr(arrnode->offset,depth+1);
break;
case E_FUNC_CALL:
printf("FUNCTION CALL\n");
EXPR_SPACING(depth+1);
printf("function\n");
print_expr(fnode->func,depth+2);
EXPR_SPACING(depth+1);
if (fnode->first_arg) {
printf("arguments\n");
print_expr((struct expr_node *)fnode->first_arg,depth+2);
}
break;
case E_FUNC_ARG:
do {
printf("FUNCTION ARGUMENT\n");
print_expr(fanode->val,depth);
if (fanode->next)
EXPR_SPACING(depth-1);
}
while (fanode = fanode->next);
break;
}
}