summaryrefslogtreecommitdiff
path: root/util.c
blob: dfe01ad1d0d09fbca7417566c467cd5f196b9b79 (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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/* Oort
 * Copyright 2007, Soren Sandmann Pedersen
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "ast.h"
#include <glib.h>
#include <glib/gprintf.h>
#include <string.h>

struct set_t
{
    GPtrArray *elements;
};

set_t *
set_new_full (void)
{
    set_t *set = g_new0 (set_t, 1);

    set->elements = NULL;

    return set;
}

set_t *
set_new (void)
{
    set_t *set = g_new0 (set_t, 1);

    set->elements = g_ptr_array_new ();

    return set;
}

#define IS_FULL(set)	(set->elements == NULL)

set_t *
set_copy (set_t *orig)
{
    if (IS_FULL (orig))
    {
	return set_new_full ();
    }
    else
    {
	set_t *copy = set_new ();
	int i;

	for (i = 0; i < orig->elements->len; ++i)
	    g_ptr_array_add (copy->elements, orig->elements->pdata[i]);

	return copy;
    }
}


void
set_free (set_t *s)
{
    if (!IS_FULL (s))
	g_ptr_array_free (s->elements, TRUE);

    g_free (s);
}

gboolean
set_contains (set_t *set, gpointer element)
{
    if (IS_FULL (set))
    {
	return TRUE;
    }
    else
    {
	int i;

	for (i = 0; i < set->elements->len; ++i)
	{
	    if (set->elements->pdata[i] == element)
		return TRUE;
	}
    }

    return FALSE;
}

void
set_intersect (set_t *s1, set_t *s2)
{
    if (IS_FULL (s1))
    {
	set_t *tmp = set_copy (s2);

	s1->elements = tmp->elements;

	g_free (tmp);
    }
    else
    {
	int i;
	GPtrArray *new_elts = g_ptr_array_new ();

	for (i = 0; i < s1->elements->len; ++i)
	{
	    gpointer element = s1->elements->pdata[i];

	    if (set_contains (s2, element))
		g_ptr_array_add (new_elts, element);
	}

	g_ptr_array_free (s1->elements, TRUE);
	s1->elements = new_elts;
    }
}

gboolean
set_subset_eq (set_t *s1, set_t *s2)
{
    int i;

    if (IS_FULL (s1))
    {
	return IS_FULL (s2);
    }
    else
    {
	for (i = 0; i < s1->elements->len; ++i)
	{
	    if (!set_contains (s2, s1->elements->pdata[i]))
		return FALSE;
	}
    }

    return TRUE;
}

gboolean
set_equal (set_t *s1,
	   set_t *s2)
{
    return set_subset_eq (s1, s2) && set_subset_eq (s2, s1);
}

void
set_add (set_t *s1,
	 gpointer element)
{
    if (IS_FULL (s1))
	return;

    g_ptr_array_add (s1->elements, element);
}

/* Make up an internal name that contain a $ so it can't conflict
 * with a user name
 */
char *
unique_name (const char *prefix)
{
    static int count = 0;

    return g_strdup_printf ("%s$%d", prefix, count++);
}

/* Count the number of elements in a null terminated
 * array of pointers
 */
int
array_length (gpointer *arr)
{
    int i;

    for (i = 0; arr[i] != NULL; ++i)
	;

    return i;
}

/* Evaluate expressions
 */
void
evaluate_binary_operator (ast_binary_operator_t  op,
			  const ast_type_spec_t	*left_type,
			  const value_t		*left,
			  const ast_type_spec_t	*right_type,
			  const value_t		*right,
			  const ast_type_spec_t	*result_type,
			  value_t		*result)
{
#define INT32_OP(op)							\
    result->int32_val = left->int32_val op right->int32_val;

#define TYPED_BINOP(op)							\
    if (is_int32)							\
	result->int32_val = left->int32_val op right->int32_val;	\
    else								\
	result->double_val = left->double_val op right->double_val;

#define TYPED_COMPOUND(op)						\
    interprete_expression (expr.left, env, &left);			\
    interprete_expression (expr.right, env, result);			\
    if (is_int32)							\
	result->int32_val = left->int32_val op right->int32_val;	\
    else								\
	result->double_val = left->double_val op right->double_val;	\
    location = get_address (expr.left, env);				\
    write_value (location, expr.common.type_spec, result);

#define INT32_COMPOUND(op)						\
    interprete_expression (expr.left, env, &left);			\
    interprete_expression (expr.right, env, result);			\
    result->int32_val = left->int32_val op right->int32_val;		\
    location = get_address (expr.left, env);				\
    write_value (location, expr.common.type_spec, result);

#define RELATIONAL(op)							\
    if (left_type->common.type == AST_INT32_TYPE)			\
	result->bool_val = left->int32_val op right->int32_val;		\
    else								\
	result->bool_val = left->double_val op right->double_val;

#define EQUALITY(op)							\
    if (left_type->common.type == AST_INT32_TYPE)			\
	result->bool_val = left->int32_val op right->int32_val;		\
    else if (left_type->common.type == AST_DOUBLE_TYPE)			\
	result->bool_val = left->double_val op right->double_val;	\
    else if (left_type->common.type == AST_BOOL_TYPE)			\
	result->bool_val = left->bool_val op right->bool_val;		\
    else if (left_type->common.type == AST_FUNCTION_TYPE)		\
	result->bool_val = left->closure_val.function op right->closure_val.function; \
    else if (left_type->common.type == AST_OBJECT_TYPE)			\
	result->bool_val = left->pointer_val op right->pointer_val;	\
    else if (left_type->common.type == AST_ARRAY_TYPE)			\
	result->bool_val = left->pointer_val op right->pointer_val;	\
    else								\
    {									\
	g_print ("bad type: %d\n", left_type->common.type);		\
	g_assert_not_reached();						\
    }

    gboolean is_int32 = result_type->common.type == AST_INT32_TYPE;

    switch (op)
    {
    case AST_PLUS:
	TYPED_BINOP(+);
	break;

    case AST_MINUS:
	TYPED_BINOP(-);
	break;

    case AST_TIMES:
	TYPED_BINOP(*);
	break;

    case AST_DIVIDE:
	/* FIXME: divide by zero */
	TYPED_BINOP(/);
	break;

    case AST_MOD:
	/* FIXME: divide by zero */
	INT32_OP(%);
	break;

    case AST_LSHIFT:
	INT32_OP(<<);
	break;

    case AST_RSHIFT:
	INT32_OP(>>);
	break;

    case AST_LT:
	RELATIONAL(<);
	break;

    case AST_GT:
	RELATIONAL(>);
	break;

    case AST_LTE:
	RELATIONAL(<=);
	break;

    case AST_GTE:
	RELATIONAL(>=);
	break;

    case AST_EQUAL:
	EQUALITY(==);
	break;

    case AST_NOT_EQUAL:
	EQUALITY(!=);
	break;

    case AST_BOR:
	result->int32_val = left->int32_val | right->int32_val;
	break;

    case AST_BXOR:
	result->int32_val = left->int32_val ^ right->int32_val;
	break;

    case AST_BAND:
	result->int32_val = left->int32_val & right->int32_val;
	break;

    case AST_ASSIGN:
	*result = *right;
	break;

    case AST_OR:
	result->bool_val = left->bool_val || right->bool_val;
	break;

    case AST_AND:
	result->bool_val = left->bool_val && right->bool_val;
	break;
    }
}

void
evaluate_unary_operator (ast_unary_operator_t   op,
			 const ast_type_spec_t *child_type,
			 const value_t         *child,
			 const ast_type_spec_t *result_type,
			 value_t               *result)
{
    switch (op)
    {
    case AST_UNARY_NOT:
	result->bool_val = !child->bool_val;
	break;

    case AST_UNARY_MINUS:
	if (result_type->common.type == AST_INT32_TYPE)
	    result->int32_val = -child->int32_val;
	else
	    result->double_val = -child->double_val;
	break;

    case AST_POSTFIX_DEC:
    case AST_POSTFIX_INC:
    case AST_UNARY_PLUS:
	*result = *child;
	break;

    case AST_UNARY_BNEG:
	result->int32_val = ~child->int32_val;
	break;

    case AST_PREFIX_INC:
	result->int32_val = child->int32_val + 1;
	break;

    case AST_PREFIX_DEC:
	result->int32_val = child->int32_val - 1;
	break;
    }
}

gchar *
value_to_string (value_t *val, ast_type_spec_t *type)
{
    switch (type->common.type)
    {
    case AST_INT32_TYPE:
	return g_strdup_printf  ("%d", val->int32_val);
	break;

    case AST_LABEL_TYPE:
	return g_strdup_printf ("<label %p>\n", val->label_val.label);
	break;

    case AST_ARRAY_TYPE:
	return g_strdup_printf ("<array %p>", val->pointer_val);
	break;

    case AST_DOUBLE_TYPE:
	return g_strdup_printf ("%f", val->double_val);
	break;

    case AST_STRING_TYPE:
	return g_strdup_printf ("%s", (char *)val->pointer_val);
	break;

    case AST_BOOL_TYPE:
	return g_strdup_printf ("%s", val->bool_val ? "true" : "false");
	break;

    case AST_VOID_TYPE:
	return g_strdup_printf  ("<void>");
	break;

    case AST_OBJECT_TYPE:
	return g_strdup_printf  ("<object %p of type %s>",
				 val->pointer_val, type->object.class->name);
	break;

    case AST_NULL_TYPE:
	return g_strdup_printf  ("<null: %p>", val->pointer_val);
	break;

    case AST_FUNCTION_TYPE:
	return g_strdup_printf  (
	    "<function %s>", val->closure_val.function->name);
	break;

    case AST_IDENTIFIER_TYPE:
    case AST_INFERRED_TYPE:
	/* These shouldn't happen, so just fall through to the default return */
	break;
    }

    return g_strdup ("BUG: value has unknown type");
}

void
print_value (value_t *val, ast_type_spec_t *type_spec)
{
    char *s = value_to_string (val, type_spec);

    g_print ("%s\n", s);

    g_free (s);
}

gboolean
report_error (const char *fmt, ...)
{
    va_list ap;

    va_start (ap, fmt);

    g_vprintf (fmt, ap);

    va_end (ap);

    return FALSE;
}

void
program_breadth_first (ast_program_t   *program,
		       node_callback_t  callback,
		       gpointer	        data)
{
    int i;

    node_breadth_first (program->enter, callback, data);
    for (i = 0; program->functions[i]; ++i)
	node_breadth_first (program->functions[i]->enter, callback, data);
}