-
Notifications
You must be signed in to change notification settings - Fork 0
/
expr.c
213 lines (197 loc) · 5.81 KB
/
expr.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
206
207
208
209
210
211
212
213
#include <stdlib.h>
#include "expr.h"
#include "display.h"
static Expression **cache = NULL;
static uint64_t cache_pointer = 0, cache_capacity = 0;
static void cache_inc(uint64_t by){
cache_capacity += by;
cache = (Expression **)realloc(cache, sizeof(Expression *) * cache_capacity);
}
void expr_init(){
cache_inc(10);
}
static Expression* expr_new2(){
Expression* expr = (Expression *)malloc(sizeof(Expression));
if(!expr){
err("Unable to allocate memory for syntax tree!");
abort();
}
if(cache_pointer >= cache_capacity){
cache_inc(2 * cache_capacity);
}
cache[cache_pointer] = expr;
cache_pointer++;
return expr;
}
Expression* expr_new(ExpressionType type){
Expression* e = expr_new2();
e->hash = 0;
e->token = nullToken;
e->type = type;
e->valueType = 0;
e->expectedType = -1;
return e;
}
/*
void expr_dispose2(Expression *exp){
switch(exp->type){
case EXPR_VARIABLE:
case EXPR_CONSTANT:
free(exp);
break;
case EXPR_BINARY:
expr_dispose2(exp->binex.left);
expr_dispose2(exp->binex.right);
free(exp);
break;
case EXPR_UNARY:
expr_dispose2(exp->unex.right);
free(exp);
break;
case EXPR_CALL:
for(uint64_t i = 0;i < exp->calex.arity;i++)
expr_dispose2(exp->calex.args[i]);
free(exp);
break;
case EXPR_REFERENCE:
expr_dispose2(exp->refex.refer);
free(exp);
break;
}
}*/
void expr_dispose(){
for(uint64_t i = 0;i < cache_pointer;i++){
// if(cache[i] != NULL)
if(cache[i]->type == EXPR_CALL || cache[i]->type == EXPR_DEFINE){
free(cache[i]->calex.args);
}
else if(cache[i]->type == EXPR_CONSTANT && cache[i]->token.type == TOKEN_string){
free(cache[i]->consex.sval);
}
free(cache[i]);
}
free(cache);
}
#ifdef DEBUG
#include <stdio.h>
#include <inttypes.h>
static const char *exprString[] = {
"ConstantExpression",
"VariableExpression",
"BinaryExpression",
"UnaryExpression",
"CallExpression",
"DefineExpression",
"ReferenceExpression"
} ;
//"|-- ReferenceExpression"
//" |-- VariableExpression
//" |--
static uint64_t printSpace = 0;
void expr_print_space(uint64_t sp){
for(uint64_t i = 0;i < sp;i++)
printf("%s", i%5 == 0 ? "|" : " ");
}
static void expr_print2(Expression *expr){
printf("\n");
if(printSpace > 0){
print_space();
if(expr == NULL){
printf("|-- BadExpression!");
return;
}
else
printf("|-- %s", exprString[expr->type]);
if(expr->valueType != 0)
printf(" (Type %d)", expr->valueType);
if(expr->expectedType != -1)
printf(" (Expected %d)", expr->expectedType);
}
uint64_t bak = printSpace;
switch(expr->type){
case EXPR_CONSTANT:
printf("\n");
printSpace += 5;
print_space();
printf("|-- ");
switch(expr->token.type){
case TOKEN_number : printf("NumericConstant : %g", expr->consex.dval);
break;
case TOKEN_integer: printf("IntegerConstant : %" PRId64, expr->consex.ival);
break;
case TOKEN_string : printf("StringConstant : ");
lexer_print_token(expr->token, 0);
break;
case TOKEN_True: printf("BooleanConstant : True");
break;
case TOKEN_False: printf("BooleanConstant : False");
break;
case TOKEN_Null: printf("NullConstant : Null");
break;
default: break;
}
break;
case EXPR_VARIABLE:
printf(" : ");
lexer_print_token(expr->token, 0);
break;
case EXPR_UNARY:
printf(" : ");
lexer_print_token(expr->token, 0);
printSpace += 5;
expr_print2(expr->unex.right);
break;
case EXPR_BINARY:
printf(" : ");
lexer_print_token(expr->token, 0);
printSpace += 5;
printf("\n");
print_space();
printf("|-- LeftOperand");
printSpace += 5;
expr_print2(expr->binex.left);
printSpace -= 5;
printf("\n");
print_space();
printf("|-- RightOperand");
printSpace += 5;
expr_print2(expr->binex.right);
break;
case EXPR_DEFINE:
case EXPR_CALL:
printf("\n");
printSpace += 5;
print_space();
printf("|-- Callee : ");
printSpace += 5;
expr_print2(expr->calex.callee);
printSpace -= 5;
printf("\n");
print_space();
printf("|-- Arguments");
printSpace += 5;
for(uint64_t i = 0;i < expr->calex.arity;i++)
expr_print2(expr->calex.args[i]);
break;
case EXPR_REFERENCE:
printf("\n");
printSpace += 5;
print_space();
printf("|-- Parent");
printSpace += 5;
expr_print2(expr->refex.parent);
printSpace -= 5;
printf("\n");
print_space();
printf("|-- Reference");
printSpace += 5;
expr_print2(expr->refex.refer);
break;
}
printSpace = bak;
}
void expr_print(Expression *exp, uint64_t printS){
printSpace = printS;
expr_print2(exp);
}
#endif