forked from artagnon/rhine-ml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbindings.c
236 lines (223 loc) · 5.05 KB
/
bindings.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#include <string.h>
#include <stdarg.h>
#include <caml/mlvalues.h>
#include <caml/memory.h>
#include <caml/alloc.h>
struct value_t {
int type_tag;
long int_val;
bool bool_val;
char *string_val;
struct value_t **array_val;
long array_len;
double dbl_val;
struct value_t *(*function_val)(struct value_t *);
char char_val;
};
void print_atom(struct value_t *v) {
int i;
if (!v) {
printf("(nil)");
return;
}
switch(v->type_tag) {
case 1:
printf("(int) %ld", v->int_val);
break;
case 2:
printf("(bool) %s", !v->bool_val ? "false" : "true");
break;
case 3:
printf("(string len %ld) %s", v->array_len, v->string_val);
break;
case 4:
printf("(array len %ld) [", v->array_len);
for (i = 0; i < v->array_len; i++) {
struct value_t *el = (v->array_val)[i];
print_atom(el);
printf(";");
}
printf("]");
break;
case 6:
printf("(double) %f", v->dbl_val);
break;
case 8:
printf("(char) %c", v->char_val);
break;
default:
printf("Don't know how to print type %d", v->type_tag);
}
}
int v_to_atype(struct value_t *v) {
if (!v)
return 6;
switch(v->type_tag) {
case 1:
return 0;
case 2:
return 1;
case 3:
return 2;
case 4:
return 3;
case 6:
return 4;
case 8:
return 5;
default:
printf("Don't know how to print type %d", v->type_tag);
exit(1);
}
}
value mlbox_value(int atype, struct value_t *v) {
value int_block = caml_alloc(1, 0);
value bool_block = caml_alloc(1, 1);
value string_block = caml_alloc(1, 2);
value array_block = caml_alloc(1, 3);
value dbl_block = caml_alloc(1, 4);
value char_block = caml_alloc(1, 5);
value dbl_value = caml_alloc(1, Double_tag);
value array_value = 0;
int i;
if (atype == 3)
array_value = caml_alloc(v->array_len, 0);
switch(atype) {
case 0:
Store_field(int_block, 0, Val_long(v->int_val));
return int_block;
case 1:
Store_field(bool_block, 0, Val_int(!!v->bool_val));
return bool_block;
case 2:
Store_field(string_block, 0, caml_copy_string(v->string_val));
return string_block;
case 3:
for (i = 0; i < v->array_len; i++) {
struct value_t *el = (v->array_val)[i];
value v = mlbox_value(v_to_atype(el), el);
Store_field(array_value, i, v);
}
Store_field(array_block, 0, array_value);
return array_block;
case 4:
Store_double_field(dbl_value, 0, v->dbl_val);
Store_field(dbl_block, 0, dbl_value);
return dbl_block;
case 5:
Store_field(char_block, 0, Val_int(v->char_val));
return char_block;
case 6:
return Val_int(0);
default:
printf("Don't know how to box type: %d", atype);
exit(1);
}
}
value unbox_value(value ptr_value) {
CAMLparam1(ptr_value);
struct value_t *v = (struct value_t *) ptr_value;
CAMLreturn(mlbox_value(v_to_atype(v), v));
}
extern struct value_t *print(int nargs, struct value_t **env, ...) {
struct value_t *ret;
va_list ap;
va_start(ap, env);
struct value_t *v = va_arg(ap, struct value_t *);
print_atom(v);
va_end(ap);
ret = malloc(sizeof(struct value_t));
ret->int_val = 0;
ret->type_tag = 1;
return ret;
}
extern struct value_t *println(int nargs, struct value_t **env, ...) {
struct value_t *ret;
va_list ap;
va_start(ap, env);
struct value_t *v = va_arg(ap, struct value_t *);
print_atom(v);
printf("\n");
va_end(ap);
ret = malloc(sizeof(struct value_t));
ret->int_val = 0;
ret->type_tag = 1;
return ret;
}
struct value_t *save_value(double val, int ret_type) {
struct value_t *ret;
ret = malloc(sizeof(struct value_t));
if (ret_type == 6) {
ret->dbl_val = val;
ret->type_tag = 6;
} else if (ret_type == 2) {
ret->bool_val = (bool) val;
ret->type_tag = 2;
} else {
ret->int_val = (int) val;
ret->type_tag = 1;
}
return ret;
}
extern struct value_t *cequ(int nargs, struct value_t **env, ...) {
struct value_t *ret;
va_list ap;
va_start(ap, env);
struct value_t *v = va_arg(ap, struct value_t *);
struct value_t *v2 = va_arg(ap, struct value_t *);
va_end(ap);
// only makes sense for integers and bools currently
switch(v->type_tag) {
case 1:
if (v->int_val == v2->int_val) {
ret = save_value(1.0, 2);
} else {
ret = save_value(0.0, 2);
}
break;
case 2:
if (!v->bool_val == !v2->bool_val) {
ret = save_value(1.0, 2);
} else {
ret = save_value(0.0, 2);
}
break;
case 4:
if (v->array_len == v2->array_len) {
ret = save_value(1.0, 2);
} else {
ret = save_value(0.0, 2);
}
break;
case 3:
if (v->array_len == v2->array_len && memcmp(v->array_val, v2->array_val, sizeof(char)*v->array_len) == 0) {
ret = save_value(1.0, 2);
} else {
ret = save_value(0.0, 2);
}
break;
}
return ret;
}
extern struct value_t *cstrjoin(int nargs, struct value_t **env, ...) {
struct value_t *ret;
va_list ap;
va_start(ap, env);
struct value_t *v = va_arg(ap, struct value_t *);
va_end(ap);
int i = 0;
ret = malloc(sizeof(struct value_t));
ret->string_val = malloc(sizeof(char) * (v->array_len+1));
ret->type_tag = 3;
ret->array_len = v->array_len;
while (i < v->array_len) {
ret->string_val[i] = (v->array_val)[i]->char_val;
i++;
}
*(ret->string_val+i) = '\0';
return ret;
}