forked from HuoLanguage/huo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore_functions.c
342 lines (323 loc) · 9.57 KB
/
core_functions.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
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
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include "structures.h"
#include "constants.h"
#include "base_util.h"
#include "core_functions.h"
void print(struct Value a){
if(a.type == 's'){
printf("\"%s\"", a.data.str.body);
}
else if(a.type == 'k'){
printf("%s", a.data.str.body);
}
else if(a.type == 'l') {
printf("%ld", a.data.ln);
}
else if(a.type == 'f') {
printf("%f", a.data.fl);
}
else if(a.type == 'b') {
if(a.data.bl == bool_true){
printf("True");
}
else if(a.data.bl == bool_false){
printf("False");
}
else if(a.data.bl == bool_undefined){
printf("Unknown");
}
}
else if(a.type == 'a') {
printf("[ ");
for(int i = 0; i < a.data.array->size; i++){
print(*a.data.array->values[i]);
if(i < a.data.array->size-1){
printf(", ");
}
}
printf(" ]");
}
else if(a.type == 'u') {
printf("undefined");
}
}
struct Value add(struct Value a, struct Value b){
if(a.type == 'l' && b.type == 'l'){
a.data.ln = a.data.ln + b.data.ln;
}
else if(a.type == 'f' && b.type == 'f'){
a.data.fl = a.data.fl + b.data.fl;
}
else if(a.type == 'f' && b.type == 'l'){
a.data.fl = a.data.fl + (float)b.data.ln;
}
else if(a.type == 'l' && b.type == 'f'){
a.data.fl = (float)a.data.ln + b.data.fl;
a.type = 'f';
}
else if(a.type == 'a' && b.type == 'a'){
return array_add(a, b);
}
return a;
}
struct Value array_add(struct Value a, struct Value b){
if(a.data.array->size != b.data.array->size){
ERROR("Tried to add arrays of different sizes: %i != %i", a.data.array->size, b.data.array->size);
} else {
for(int i = 0; i < a.data.array->size; i++){
struct Value result = add(*a.data.array->values[i], *b.data.array->values[i]);
copy_value_to(a.data.array->values[i], &result);
}
}
return a;
}
struct Value mul(struct Value a, struct Value b){
if(a.type == 'l' && b.type == 'l'){
a.data.ln = a.data.ln * b.data.ln;
}
else if(a.type == 'f' && b.type == 'f'){
a.data.fl = a.data.fl * b.data.fl;
}
else if(a.type == 'f' && b.type == 'l'){
a.data.fl = a.data.fl * (float)b.data.ln;
}
else if(a.type == 'l' && b.type == 'f'){
a.data.fl = (float)a.data.ln * b.data.fl;
a.type = 'f';
}
return a;
}
struct Value sub(struct Value a, struct Value b){
if(a.type == 'l' && b.type == 'l'){
a.data.ln = a.data.ln - b.data.ln;
}
else if(a.type == 'f' && b.type == 'f'){
a.data.fl = a.data.fl - b.data.fl;
}
else if(a.type == 'f' && b.type == 'l'){
a.data.fl = a.data.fl - (float)b.data.ln;
}
else if(a.type == 'l' && b.type == 'f'){
a.data.fl = (float)a.data.ln - b.data.fl;
a.type = 'f';
}
return a;
}
struct Value divide(struct Value a, struct Value b){
if(a.type == 'l' && b.type == 'l'){
a.data.fl = (float)a.data.ln/(float)b.data.ln;
a.type = 'f';
}
else if(a.type == 'f' && b.type == 'f'){
a.data.fl = a.data.fl/b.data.fl;
}
else if(a.type == 'f' && b.type == 'l'){
a.data.fl = a.data.fl/(float)b.data.ln;
}
else if(a.type == 'l' && b.type == 'f'){
a.data.fl = (float)a.data.ln/b.data.fl;
a.type = 'f';
}
a.type = 'f';
return a;
}
struct Value concat(struct Value a, struct Value b){
if (a.type != 's' || b.type != 's') {
ERROR("Tried to concat %c and %c", a.type, b.type);
}
string_concat_to(&a.data.str, &b.data.str);
return a;
}
struct Value not(struct Value a, struct Value b){
if(a.type == 'f' && b.type == 'f'){
a.type = 'b';
if(a.data.fl == b.data.fl){
a.data.bl = bool_false;
} else {
a.data.bl = bool_true;
}
}
else if(a.type == 'l' && b.type == 'l'){
a.type = 'b';
if(a.data.ln == b.data.ln){
a.data.bl = bool_false;
} else {
a.data.bl = bool_true;
}
}
else if(a.type == 's' && b.type == 's'){
a.type = 'b';
if(string_matches(&a.data.str, &b.data.str)){
a.data.bl = bool_false;
} else {
a.data.bl = bool_true;
}
} else {
ERROR("Mismatched types: %c != %c", a.type, b.type);
}
return a;
}
struct Value equals(struct Value a, struct Value b){
if(a.type == 'f' && b.type == 'f'){
a.type = 'b';
if(a.data.fl == b.data.fl){
a.data.bl = bool_true;
} else {
a.data.bl = bool_false;
}
}
else if(a.type == 'l' && b.type == 'l'){
a.type = 'b';
if(a.data.ln == b.data.ln){
a.data.bl = bool_true;
} else {
a.data.bl = bool_false;
}
}
else if(a.type == 's' && b.type == 's'){
a.type = 'b';
if(string_matches(&a.data.str, &b.data.str)){
a.data.bl = bool_true;
} else {
a.data.bl = bool_false;
}
} else {
ERROR("Mismatched types: %c != %c", a.type, b.type);
}
return a;
}
struct Value greater_than(struct Value a, struct Value b){
if(a.type == 'f' && b.type == 'f'){
a.type = 'b';
if(a.data.fl > b.data.fl){
a.data.bl = bool_true;
} else {
a.data.bl = bool_false;
}
}
else if(a.type == 'l' && b.type == 'l'){
a.type = 'b';
if(a.data.ln > b.data.ln){
a.data.bl = bool_true;
} else {
a.data.bl = bool_false;
}
}
else {
ERROR("Mismatched types: %c != %c", a.type, b.type);
}
return a;
}
struct Value length(struct Value a){
if (a.type == 's') {
assert(string_is_sane(&a.data.str));
}
if(a.type != 'a' && a.type != 's'){
ERROR("Type error: value has no length property");
/* a.type = 'u';
return a; */
} else {
struct Value length = {
.type = 'l',
.data = {
.ln= a.type=='a'?(long)a.data.array->size:a.data.str.length
}
};
return length;
}
}
struct Value array_index(struct Value a, struct Value list){
if(list.type == 'a'){
return *list.data.array->values[a.data.ln];
}
else if(list.type == 's'){
assert(string_is_sane(&list.data.str));
struct Value result = {
.type='s',
.data={
.str={
.length=1
}
}
};
result.data.str.body = malloc(sizeof(char) + 1);
if (result.data.str.body == NULL) {
ERROR("Malloc failure");
}
assert(string_is_sane(&result.data.str));
result.data.str.body[0] = list.data.str.body[a.data.ln];
result.data.str.body[1] = 0;
return result;
} else {
ERROR("Index takes a number and a string or array, but got ('%c' != 'l', '%c' != ['a'|'s']).", a.type, list.type);
}
}
struct Value array_set(struct Value index, struct Value item, struct Value array){
int idx = (int) index.data.ln;
if(idx > array.data.array->size-1){
array.data.array->size = idx+1;
}
array.data.array->values[idx] = copy_value_heap(&item);
return array;
}
struct Value array_push(struct Value a, struct Value arr){
arr.data.array->values[arr.data.array->size] = copy_value_heap(&a);
arr.data.array->size++;
return arr;
}
struct Value substring(int start, int end, struct Value str){
assert(string_is_sane(&str.data.str));
struct Value result;
result.type = 's';
if(start < 0 || start > str.data.str.length) {
ERROR("String start index out of range for substring: should be 0 <= %i < %i", start, str.data.str.length);
}
else if (end < 0 || end > str.data.str.length){
ERROR("String end index out of range for substring: should be 0 <= %i < %i", end, str.data.str.length);
} else {
if (end > start) {
result.data.str.length = (end - start);
result.data.str.body = malloc(sizeof(char) * (result.data.str.length + 1));
if (result.data.str.body == NULL) {
ERROR("Malloc failure");
}
for(int i = 0; i < result.data.str.length; i++){
result.data.str.body[i] = str.data.str.body[i + start];
}
result.data.str.body[result.data.str.length] = 0;
} else {
result.data.str.length = 0;
free(result.data.str.body);
result.data.str.body = NULL;
}
assert(string_is_sane(&result.data.str));
return result;
}
}
struct Value split_string(struct Value a, struct Value str){
assert(string_is_sane(&a.data.str));
assert(string_is_sane(&str.data.str));
int indexes[1000];
int counter = 0;
for(int i = 0; i < str.data.str.length; i++){
if(a.data.str.body[0] == str.data.str.body[i]){
indexes[counter] = i;
counter++;
}
}
struct Value result;
result.type = 'a';
struct Value_array * array = malloc(sizeof(struct Value_array));
array->size = 0;
for(int l = 0; l <= counter; l++){
int start = !l ? l : indexes[l-1] + 1;
int end = (l < counter) ? indexes[l] : str.data.str.length;
struct Value item = substring(start, end, str);
array->values[array->size] = copy_value_heap(&item);
array->size++;
}
result.data.array = array;
return result;
}