-
Notifications
You must be signed in to change notification settings - Fork 6
/
struct.c
117 lines (101 loc) · 2.87 KB
/
struct.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
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "struct.h"
#include "gc.h"
static void init_tagged(tagged *t, int tag) {
t->type = tag;
}
#if !FIXNUM_ENCODING
tagged* make_num(int n) {
num_val *nv = (num_val *)gc_malloc0(sizeof(num_val));
init_tagged(&nv->t, num_type);
nv->n = n;
return (tagged*)nv;
}
#endif
tagged* make_func(tagged *lam, env *e) {
func_val *fv = (func_val *)gc_malloc2(sizeof(func_val),
&lam,
&e);
init_tagged(&fv->t, func_type);
fv->lam = (lambda_expr*)lam;
fv->e = e;
# if USE_JIT
fv->specialize_counter = SPECIALIZE_AFTER_COUNT + 1;
# endif
return (tagged*)fv;
}
symbol* make_symbol(char *s) {
symbol *sym = (symbol *)gc_malloc0(sizeof(symbol));
init_tagged(&sym->t, sym_type);
sym->s = strdup(s);
return sym;
}
int same_symbol(symbol* a, symbol *b)
{
return !strcmp(a->s, b->s);
}
tagged* make_debruijn(int pos) {
debruijn_expr *db = (debruijn_expr *)gc_malloc0(sizeof(debruijn_expr));
init_tagged(&db->t, debruijn_type);
db->pos = pos;
return (tagged*)db;
}
tagged* make_bin_op(int type, tagged *left, tagged *right) {
bin_op_expr *bin = (bin_op_expr *)gc_malloc2(sizeof(bin_op_expr),
&left,
&right);
init_tagged(&bin->t, type);
bin->left = left;
bin->right = right;
return (tagged*)bin;
}
tagged* make_plus(tagged *left, tagged *right) {
return make_bin_op(plus_type, left, right);
}
tagged* make_minus(tagged *left, tagged *right) {
return make_bin_op(minus_type, left, right);
}
tagged* make_times(tagged *left, tagged *right) {
return make_bin_op(times_type, left, right);
}
tagged* make_app(tagged *left, tagged *right) {
return make_bin_op(app_type, left, right);
}
tagged* make_lambda(symbol *arg_name, tagged *body) {
lambda_expr *lam = (lambda_expr *)gc_malloc2(sizeof(lambda_expr),
&arg_name,
&body);
init_tagged(&lam->t, lambda_type);
lam->arg_name = arg_name;
lam->body = body;
# if USE_JIT
lam->code = NULL;
lam->tail_code = NULL;
# endif
return (tagged*)lam;
}
tagged* make_if0(tagged *tst, tagged *thn, tagged *els) {
if0_expr *if0 = (if0_expr *)gc_malloc3(sizeof(if0_expr),
&tst,
&thn,
&els);
init_tagged(&if0->t, if0_type);
if0->tst = tst;
if0->thn = thn;
if0->els = els;
return (tagged*)if0;
}
env* make_env(symbol *id, tagged *val, env *rest)
{
env *e = (env *)gc_malloc3(sizeof(env),
&id,
&val,
&rest);
e->type = env_type;
e->id = id;
e->val = val;
e->rest = rest;
return e;
}