summaryrefslogtreecommitdiff
path: root/eval.c
blob: 7fbb37c5fc632eed3723c5a33aa0d6e314b8f51a (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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
typedef enum {
			  VAL_VOID,
			  VAL_INT,
			  VAL_FLOAT,
			  VAL_PTR,
			  VAL_FN,
			  VAL_BOOL
} ValueKind;

#define VAL_FLAG_FN_NULL 0x01 /* is this function null? */

typedef double FloatVal;

typedef struct Value {
	ValueKind kind;
	union {
		Integer intv;
	    FloatVal floatv;
		struct Value *points_to;
		FnExpr *fn;
		bool boolv;
	};
} Value;

static bool eval_truthiness(Value *v) {
	switch (v->kind) {
	case VAL_VOID:
		assert(0);
		return false;
	case VAL_INT:
		return v->intv != 0;
	case VAL_FLOAT:
		return v->floatv != 0;
	case VAL_PTR:
		return v->points_to != NULL;
	case VAL_FN:
		return v->fn != NULL;
	case VAL_BOOL:
		return v->boolv;
	}
	assert(0);
	return false;
}

/* NOTE: expr must be typed before it can be evaluated */
static bool eval_expr(Expression *e, Value *v) {
	switch (e->kind) {
	case EXPR_LITERAL_FLOAT:
		v->kind = VAL_FLOAT;
		v->floatv = (FloatVal)e->floatl;
		return true;
	case EXPR_LITERAL_INT:
		v->kind = VAL_INT;
		if (e->intl > (UInteger)INTEGER_MAX) { /* TODO: FIXME */
			err_print(e->where, "Overflow when evaluating integer.");
			return false;
		}
		v->intv = (Integer)e->intl;
		return true;
	case EXPR_LITERAL_BOOL:
		v->kind = VAL_BOOL;
		v->boolv = e->booll;
		return true;
	case EXPR_UNARY_OP: {
		Expression *of_expr = e->unary.of;
		switch (e->unary.op) {
		case UNARY_MINUS: {
			Value of;
			if (!eval_expr(of_expr, &of)) return false;
		    assert(e->type.kind != TYPE_BUILTIN);
			v->kind = of.kind;
			if (v->kind == VAL_INT) {
				v->intv = -of.intv;
			} else if (v->kind == VAL_FLOAT) {
				v->floatv = -of.floatv;
			} else {
			    assert(0);
				return false;
			}
			return true;
		}
		case UNARY_NOT:
			v->kind = VAL_BOOL;
			v->boolv = !eval_truthiness(v);
		    return true;
		case UNARY_ADDRESS:
			v->kind = VAL_PTR;
			v->points_to = err_malloc(sizeof *v->points_to); /* OPTIM */
			return eval_expr(e->unary.of, v->points_to);
		case UNARY_DEREF: {
			Value ptr;
			if (!eval_expr(of_expr, &ptr)) return false;
			*v = *ptr.points_to;
			return true;
		} break;
		case UNARY_DEL:	/* TODO */
			assert(0);
			break;
		}
	} break;
	case EXPR_BINARY_OP: {
		Value lhs, rhs;
		/* NOTE: this will need to change for short-circuiting */
		if (!eval_expr(e->binary.lhs, &lhs)) return false;
		if (!eval_expr(e->binary.rhs, &rhs)) return false;
		if (e->type.kind != TYPE_BUILTIN) {
			err_print(e->where, "Operators can only be applied to builtin types.");
			return false;
		}
		bool is_int = type_builtin_is_integer(e->type.builtin);
		bool is_float = type_builtin_is_floating(e->type.builtin);
		bool is_bool = e->type.builtin == BUILTIN_BOOL;
		switch (e->binary.op) {
		case BINARY_PLUS:
			v->kind = lhs.kind;
			if (is_int) {
				v->intv = lhs.intv + rhs.intv;
			} else if (is_float) {
				v->floatv = lhs.floatv + rhs.floatv;
			} else assert(0);
			return true;
		case BINARY_MINUS:
			v->kind = lhs.kind;
			if (is_int) {
				v->intv = lhs.intv - rhs.intv;
			} else if (is_float) {
				v->floatv = lhs.floatv - rhs.floatv;
			} else assert(0);
			return true;
		case BINARY_MUL:
			v->kind = lhs.kind;
			if (is_int) {
				v->intv = lhs.intv * rhs.intv;
			} else if (is_float) {
				v->floatv = lhs.floatv * rhs.floatv;
			} else assert(0);
			return true;
		case BINARY_DIV:
			v->kind = lhs.kind;
			/* TODO(eventually): check div by 0 */
			if (is_int) {
				v->intv = lhs.intv / rhs.intv;
			} else if (is_float) {
				v->floatv = lhs.floatv / rhs.floatv;
			} else assert(0);
			return true;
		case BINARY_EQ:
			v->kind = VAL_BOOL;
			if (is_int) {
				v->boolv = lhs.intv == rhs.intv;
			} else if (is_float) {
				v->boolv = lhs.floatv == rhs.floatv;
			} else if (is_bool) {
				v->boolv = lhs.boolv == rhs.boolv;
			} else assert(0);
			return true;
		case BINARY_NE:
			v->kind = VAL_BOOL;
			if (is_int) {
				v->boolv = lhs.intv != rhs.intv;
			} else if (is_float) {
				v->boolv = lhs.floatv != rhs.floatv;
			} else if (is_bool) {
				v->boolv = lhs.boolv != rhs.boolv;
			} else assert(0);
			return true;
		case BINARY_GT:
			v->kind = VAL_BOOL;
			if (is_int) {
				v->boolv = lhs.intv > rhs.intv;
			} else if (is_float) {
				v->boolv = lhs.floatv > rhs.floatv;
			} else assert(0);
			return true;
		case BINARY_GE:
			v->kind = VAL_BOOL;
			if (is_int) {
				v->boolv = lhs.intv >= rhs.intv;
			} else if (is_float) {
				v->boolv = lhs.floatv >= rhs.floatv;
			} else assert(0);
			return true;
		case BINARY_LT:
			v->kind = VAL_BOOL;
			if (is_int) {
				v->boolv = lhs.intv < rhs.intv;
			} else if (is_float) {
				v->boolv = lhs.floatv < rhs.floatv;
			} else assert(0);
			return true;
		case BINARY_LE:
			v->kind = VAL_BOOL;
			if (is_int) {
				v->boolv = lhs.intv <= rhs.intv;
			} else if (is_float) {
				v->boolv = lhs.floatv <= rhs.floatv;
			} else assert(0);
			return true;
	    case BINARY_SET:
			v->kind = VAL_VOID;
			return true;
		case BINARY_COMMA:
			err_print(e->where, "tuples not supported at compile time yet.");
			return false;
		case BINARY_AT_INDEX:
			err_print(e->where, "Cannot get index of array at compile time yet.");
			return false;
		}
	} break;
	case EXPR_IDENT: {
		Identifier id = e->ident;
		IdentDecl *id_decl = ident_decl(id);
		if (!id_decl) {
			char *id_str = ident_to_str(id);
			err_print(e->where, "Undeclared identifier: %s", id_str);
			free(id_str);
			return false;
		}
		Declaration *d = id_decl->decl;
		if (location_after(d->where, e->where)) {
			err_print(e->where, "Use of constant before its declaration.");
			info_print(d->where, "Declaration will be here.");
			return false;
		}
		if (!(d->flags & DECL_FLAG_CONST)) {
			err_print(e->where, "Use of non-constant identifier in a constant expression.");
			info_print(d->where, "Declaration was here.");
			return false;
		}
		if (!d->val) {
			d->val = err_malloc(sizeof *d->val); /* OPTIM */
			if (!eval_expr(&d->expr, d->val))
				return false;
		}
		*v = *d->val;
		return true;
	} break;
	case EXPR_FN:
		v->kind = VAL_FN;
		v->fn = &e->fn;
		return true;
	case EXPR_CAST:
	case EXPR_IF:
	case EXPR_WHILE:
	case EXPR_CALL:
	case EXPR_BLOCK:
	case EXPR_LITERAL_STR:
	case EXPR_LITERAL_CHAR:
	case EXPR_NEW:{
		err_print(e->where, "operation not supported at compile time yet."); /* TODO */
	} break;
	case EXPR_DIRECT:
		switch (e->direct.which) {
		case DIRECT_C:
			err_print(e->where, "Can't run C code at compile time.");
			return false;
		case DIRECT_COUNT: assert(0); break;
		}
		break;
	}
	err_print(e->where, "Not implemented yet");
	return false;
}