-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalue.c
More file actions
1329 lines (1192 loc) · 43.2 KB
/
value.c
File metadata and controls
1329 lines (1192 loc) · 43.2 KB
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "common.h"
#include "value.h"
/* http://www.cse.yorku.ca/~oz/hash.html djb2 hash algorithm */
static unsigned long
hash(char *str)
{
unsigned long hash = 5381;
char c;
while ((c = *str++)) {
hash = hash * 33 + c;
}
return hash;
}
static void
brd_value_list_init_with_capacity(struct brd_value_list *list, size_t capacity)
{
list->capacity = capacity;
list->length = 0;
list->items = malloc(sizeof(struct brd_value) * list->capacity);
}
void
brd_value_list_init(struct brd_value_list *list)
{
brd_value_list_init_with_capacity(list, 4);
}
void
brd_value_list_push(struct brd_value_list *list, struct brd_value *value)
{
if (list->capacity == list->length) {
list->capacity *= 1.5;
list->items = realloc(
list->items,
sizeof(struct brd_value) * list->capacity
);
}
list->items[list->length++] = *value;
}
void
brd_value_list_set(struct brd_value_list *list, size_t idx, struct brd_value *value)
{
if (list->length == 0) {
brd_value_list_push(list, value);
} else {
idx = idx % list->length;
list->items[idx] = *value;
}
}
char *
brd_value_list_to_string(struct brd_value_list *list) {
size_t length;
char *string;
struct brd_value *list_strings = malloc(sizeof(*list_strings) * list->length);
int *new_string = malloc(sizeof(int) * list->length);
length = 4; // "[ ]" and null byte
for (size_t i = 0; i < list->length; i++) {
list_strings[i] = list->items[i];
new_string[i] = brd_value_coerce_string(&list_strings[i]);
length += 2 + AS_STRING(list_strings[i])->length;
}
string = malloc(length);
length = 0;
string[length++] = '[';
string[length++] = ' ';
for (size_t i = 0; i < list->length; i++) {
strcpy(string + length, AS_STRING(list_strings[i])->s);
length += AS_STRING(list_strings[i])->length;
if (i < list->length - 1) {
string[length++] = ',';
}
string[length++] = ' ';
if (new_string[i]) {
brd_heap_destroy(list_strings[i].as.heap);
}
}
string[length++] = ']';
string[length] = '\0';
free(list_strings);
free(new_string);
return string;
}
int
brd_value_list_equals(struct brd_value_list *a, struct brd_value_list *b)
{
struct brd_comparison cmp;
if (a->length != b->length) {
return false;
}
for (size_t i = 0; i < a->length; i++) {
cmp = brd_value_compare(&a->items[i], &b->items[i]);
if (!brd_comparison_eq(cmp)) {
return false;
}
}
return true;
}
void
brd_value_string_init(struct brd_value_string *string, char *s)
{
string->s = s;
string->length = strlen(s);
}
struct brd_value_string *
brd_value_string_new(char *s)
{
struct brd_value_string *string = malloc(sizeof(*string));
brd_value_string_init(string, s);
return string;
}
struct brd_heap_entry *
brd_heap_new(enum brd_heap_type htype) {
struct brd_heap_entry *heap = malloc(sizeof(*heap));
heap->htype = htype;
switch (htype) {
case BRD_HEAP_STRING:
heap->as.string = malloc(sizeof(struct brd_value_string));
break;
case BRD_HEAP_LIST:
heap->as.list = malloc(sizeof(struct brd_value_list));
break;
case BRD_HEAP_CLOSURE:
heap->as.closure = malloc(sizeof(struct brd_value_closure));
break;
case BRD_HEAP_CLASS:
heap->as.class = malloc(sizeof(struct brd_value_class));
break;
case BRD_HEAP_OBJECT:
heap->as.object = malloc(sizeof(struct brd_value_object));
break;
case BRD_HEAP_DICT:
heap->as.dict = malloc(sizeof(struct brd_value_dict));
break;
}
return heap;
}
void
brd_heap_destroy(struct brd_heap_entry *entry)
{
switch(entry->htype) {
case BRD_HEAP_STRING:
free(entry->as.string->s);
free(entry->as.string);
break;
case BRD_HEAP_LIST:
free(entry->as.list->items);
free(entry->as.list);
break;
case BRD_HEAP_CLOSURE:
brd_value_closure_destroy(entry->as.closure);
free(entry->as.closure);
break;
case BRD_HEAP_CLASS:
brd_value_class_destroy(entry->as.class);
free(entry->as.class);
break;
case BRD_HEAP_OBJECT:
if (!entry->as.object->is_super) {
brd_value_object_destroy(entry->as.object);
}
free(entry->as.object);
break;
case BRD_HEAP_DICT:
brd_value_dict_destroy(entry->as.dict);
free(entry->as.dict);
break;
}
free(entry);
}
void
brd_value_gc_mark(struct brd_value *value)
{
struct brd_value v;
if (value->vtype == BRD_VAL_HEAP) {
struct brd_heap_entry *entry = value->as.heap;
if (entry->marked) {
return;
}
entry->marked = true;
switch (entry->htype) {
case BRD_HEAP_STRING:
break;
case BRD_HEAP_LIST:
for (size_t i = 0; i < entry->as.list->length; i++) {
brd_value_gc_mark(&entry->as.list->items[i]);
}
break;
case BRD_HEAP_CLOSURE:
brd_value_map_mark(&entry->as.closure->env);
break;
case BRD_HEAP_CLASS:
/* @Object is marked before GCing, so this is fine */
v = brd_heap_value(closure, entry->as.class->constructor);
brd_value_gc_mark(&v);
v = brd_heap_value(class, entry->as.class->super);
brd_value_gc_mark(&v);
brd_value_map_mark(&entry->as.class->methods);
break;
case BRD_HEAP_OBJECT:
v = brd_heap_value(class, entry->as.object->class);
brd_value_gc_mark(&v);
brd_value_map_mark(&entry->as.object->fields);
break;
case BRD_HEAP_DICT:
for (size_t i = 0; i < entry->as.dict->keys.length; i++) {
brd_value_gc_mark(&entry->as.dict->keys.items[i]);
}
brd_value_map_mark(&entry->as.dict->map);
break;
}
} else if (value->vtype == BRD_VAL_METHOD) {
v = brd_heap_value(object, value->as.method.this);
brd_value_gc_mark(&v);
v = brd_heap_value(closure, value->as.method.fn);
brd_value_gc_mark(&v);
}
}
void
brd_value_map_init(struct brd_value_map *map)
{
map->bucket = malloc(sizeof(struct brd_value_map_list) * BUCKET_SIZE);
for (int i = 0; i < BUCKET_SIZE; i++) {
map->bucket[i].key = "";
map->bucket[i].val.vtype = BRD_VAL_UNIT;
map->bucket[i].next = NULL;
}
}
void
brd_value_map_destroy(struct brd_value_map *map)
{
/* At this point in time I'm not going to free the strings */
for (int i = 0; i < BUCKET_SIZE; i++) {
struct brd_value_map_list *list = map->bucket[i].next;
while (list != NULL) {
struct brd_value_map_list *next = list->next;
free(list);
list = next;
}
}
free(map->bucket);
}
void
brd_value_map_set(struct brd_value_map *map, char *key, struct brd_value *val)
{
/* return true if key already in dict */
unsigned long h = hash(key) % BUCKET_SIZE;
struct brd_value_map_list *list = &map->bucket[h];
/*
* Always at least 1 iteration since each bucket entry
* has an initial dummy value
*/
for (;;) {
if (strcmp(list->key, key) == 0) {
list->val = *val;
return;
} else if (list->next == NULL) {
break;
} else {
list = list->next;
}
}
/* key not in list */
list->next = malloc(sizeof(struct brd_value_map_list));
list->next->key = key;
list->next->val = *val;
list->next->next = NULL;
return;
}
struct brd_value *
brd_value_map_get(struct brd_value_map *map, char *key)
{
unsigned long h = hash(key) % BUCKET_SIZE;
struct brd_value_map_list *list = &map->bucket[h];
do {
if (strcmp(list->key, key) == 0) {
return &list->val;
}
} while ((list = list->next) != NULL);
return NULL;
}
void
brd_value_map_copy(struct brd_value_map *dest, struct brd_value_map *src)
{
for (int i = 0; i < BUCKET_SIZE; i++) {
struct brd_value_map_list *entry = src->bucket[i].next;
while (entry != NULL) {
brd_value_map_set(dest, entry->key, &entry->val);
entry = entry->next;
}
}
}
void
brd_value_map_mark(struct brd_value_map *map)
{
for (int i = 0; i < BUCKET_SIZE; i++) {
struct brd_value_map_list *p = &map->bucket[i];
while (p != NULL) {
brd_value_gc_mark(&p->val);
p = p->next;
}
}
}
void
brd_value_closure_init(struct brd_value_closure *closure, char **args, size_t num_args, size_t pc)
{
struct brd_value val;
brd_value_map_init(&closure->env);
closure->args = args;
closure->num_args = num_args;
closure->pc = pc;
/*
* technically this isn't necessary, but theoretically
* with a large env this will make accessing function arguments
* more efficient
*/
val.vtype = BRD_VAL_UNIT;
for (size_t i = 0; i < num_args; i++) {
brd_value_map_set(&closure->env, args[i], &val);
}
}
void
brd_value_closure_destroy(struct brd_value_closure *closure)
{
brd_value_map_destroy(&closure->env);
free(closure->args);
}
void
brd_value_class_init(struct brd_value_class *class)
{
brd_value_map_init(&class->methods);
}
void
brd_value_class_subclass(
struct brd_value_class *sub,
struct brd_value_class **super,
struct brd_value_closure **constructor)
{
sub->super = super;
sub->constructor = constructor;
brd_value_class_init(sub);
brd_value_map_copy(&sub->methods, &(**super).methods);
}
void
brd_value_class_destroy(struct brd_value_class *class)
{
brd_value_map_destroy(&class->methods);
}
void
brd_value_object_init(struct brd_value_object *object, struct brd_value_class **class)
{
object->class = class;
brd_value_map_init(&object->fields);
object->is_super = false;
}
void
brd_value_object_super(struct brd_value_object *this, struct brd_value_object *super)
{
super->class = (**this->class).super;
super->fields = this->fields;
super->is_super = true;
}
void
brd_value_object_destroy(struct brd_value_object *object)
{
brd_value_map_destroy(&object->fields);
}
void
brd_value_dict_init(struct brd_value_dict *dict)
{
brd_value_list_init(&dict->keys);
brd_value_map_init(&dict->map);
dict->size = 0;
}
void
brd_value_dict_destroy(struct brd_value_dict *dict)
{
brd_value_map_destroy(&dict->map);
free(dict->keys.items);
}
struct brd_value *
brd_value_dict_get(struct brd_value_dict *dict, struct brd_value *key)
{
if (IS_STRING(*key)) {
char *k = AS_STRING(*key)->s;
return brd_value_map_get(&dict->map, k);
} else {
BARF("dict key must be a string");
return NULL;
}
}
void
brd_value_dict_set(
struct brd_value_dict *dict,
struct brd_value *key,
struct brd_value *value)
{
char *k;
struct brd_value *vp;
if (!IS_STRING(*key)) {
BARF("dict key must be a string");
}
k = AS_STRING(*key)->s;
vp = brd_value_map_get(&dict->map, k);
if (vp == NULL) {
if (!IS_VAL(*value, BRD_VAL_UNIT)) {
dict->size++;
}
if (IS_VAL(*key, BRD_VAL_HEAP)) {
brd_value_list_push(&dict->keys, key);
}
brd_value_map_set(&dict->map, k, value);
} else {
if (IS_VAL(*vp, BRD_VAL_UNIT) && !IS_VAL(*value, BRD_VAL_UNIT)) {
dict->size++;
} else if (!IS_VAL(*vp, BRD_VAL_UNIT) && IS_VAL(*value, BRD_VAL_UNIT)) {
dict->size--;
}
*vp = *value;
}
}
char *
brd_value_dict_to_string(struct brd_value_dict *dict)
{
char *s, **strings = malloc(sizeof(char *) * dict->size);
int *lengths = malloc(sizeof(int) * dict->size);
struct brd_value value;
int new, idx = 0, total_length;
total_length = 4 + 2 * dict->size;
for (int i = 0; i < BUCKET_SIZE; i++) {
struct brd_value_map_list *list = dict->map.bucket[i].next;
while (list != NULL) {
if (IS_VAL(list->val, BRD_VAL_UNIT)) {
list = list->next;
continue;
}
value = list->val;
new = brd_value_coerce_string(&value);
s = malloc(strlen(list->key) + AS_STRING(value)->length + 6);
lengths[idx] = sprintf(
s, "\"%s\" : %s",
list->key, AS_STRING(value)->s
);
strings[idx] = s;
total_length += lengths[idx];
idx++;
if (new) {
brd_heap_destroy(value.as.heap);
}
list = list->next;
}
}
idx = 0;
s = malloc(sizeof(char) * total_length);
s[idx++] = '{';
s[idx++] = ' ';
for (size_t i = 0; i < dict->size; i++) {
strcpy(s + idx, strings[i]);
idx += lengths[i];
if (i < dict->size - 1) {
s[idx++] = ',';
}
s[idx++] = ' ';
free(strings[i]);
}
s[idx++] = '}';
s[idx] = '\0';
free(strings);
free(lengths);
return s;
}
int
brd_comparison_eq(struct brd_comparison cmp)
{
return cmp.is_ord ? cmp.cmp == 0 : cmp.cmp;
}
void
brd_value_debug(struct brd_value *value)
{
switch (value->vtype) {
case BRD_VAL_NUM:
printf("%.10Lg", value->as.num);
break;
case BRD_VAL_STRING:
printf("\"%s\"", value->as.string->s);
break;
case BRD_VAL_BOOL:
printf("%s", value->as.boolean ? "true" : "false");
break;
case BRD_VAL_UNIT:
printf("unit");
break;
case BRD_VAL_BUILTIN:
printf("<< builtin: %s >>", builtin_name[value->as.builtin]);
break;
case BRD_VAL_METHOD:
printf("<< method >>");
break;
case BRD_VAL_HEAP:
switch (value->as.heap->htype) {
case BRD_HEAP_STRING:
printf("\"%s\"", value->as.heap->as.string->s);
break;
case BRD_HEAP_LIST:
printf("[ ");
for (size_t i = 0; i < value->as.heap->as.list->length; i++) {
brd_value_debug(
&value->as.heap->as.list->items[i]
);
if (i < value->as.heap->as.list->length - 1) {
printf(",");
}
printf(" ");
}
printf("]");
break;
case BRD_HEAP_CLOSURE:
printf("<< closure >>");
break;
case BRD_HEAP_CLASS:
printf("<< class >>");
break;
case BRD_HEAP_OBJECT:
printf("<< object >>");
break;
case BRD_HEAP_DICT:
printf("<< dict >>");
break;
}
}
}
int brd_value_is_string(struct brd_value *value)
{
return IS_VAL(*value, BRD_VAL_STRING) || IS_HEAP(*value, BRD_HEAP_STRING);
}
void
brd_value_coerce_num(struct brd_value *value)
{
switch (value->vtype) {
case BRD_VAL_NUM:
break;
case BRD_VAL_STRING:
value->as.num = strtold(value->as.string->s, NULL);
break;
case BRD_VAL_BOOL:
value->as.num = value->as.boolean ? 1 : 0;
break;
case BRD_VAL_UNIT:
value->as.num = 0;
break;
case BRD_VAL_BUILTIN:
BARF("builtin functions can't be coerced to a number");
break;
case BRD_VAL_METHOD:
BARF("can't coerce a method into a number");
break;
case BRD_VAL_HEAP:
switch (value->as.heap->htype) {
case BRD_HEAP_STRING:
value->as.num = strtold(value->as.heap->as.string->s, NULL);
break;
case BRD_HEAP_LIST:
BARF("can't coerce a list to a number");
break;
case BRD_HEAP_CLOSURE:
BARF("can't coerce a closure to a number");
break;
case BRD_HEAP_CLASS:
BARF("can't coerce a class into a number");
break;
case BRD_HEAP_OBJECT:
BARF("can't coerce an object into a number");
break;
case BRD_HEAP_DICT:
BARF("can't coerce a dict into a number");
}
break;
}
value->vtype = BRD_VAL_NUM;
}
int
brd_value_coerce_string(struct brd_value *value)
{
/* return true if new allocation was made */
char *string = "";
switch (value->vtype) {
case BRD_VAL_NUM:
value->vtype = BRD_VAL_HEAP;
string = malloc(sizeof(char) * 50); /* that's enough, right? */
sprintf(string, "%Lg", value->as.num);
value->as.heap = brd_heap_new(BRD_HEAP_STRING);
brd_value_string_init(value->as.heap->as.string, string);
return true;
case BRD_VAL_STRING:
return false;
case BRD_VAL_BOOL:
value->vtype = BRD_VAL_STRING;
value->as.string = value->as.boolean ? &true_string : &false_string;
return false;
case BRD_VAL_UNIT:
value->vtype = BRD_VAL_STRING;
value->as.string = &unit_string;
return false;
case BRD_VAL_BUILTIN:
value->vtype = BRD_VAL_STRING;
value->as.string = &builtin_string[value->as.builtin];
return false;
case BRD_VAL_METHOD:
value->vtype = BRD_VAL_STRING;
value->as.string = &method_string;
return false;
case BRD_VAL_HEAP:
switch (value->as.heap->htype) {
case BRD_HEAP_STRING:
return false;
case BRD_HEAP_LIST:
string = brd_value_list_to_string(value->as.heap->as.list);
value->vtype = BRD_VAL_HEAP;
value->as.heap = brd_heap_new(BRD_HEAP_STRING);
brd_value_string_init(value->as.heap->as.string, string);
return true;
case BRD_HEAP_CLOSURE:
value->vtype = BRD_VAL_STRING;
value->as.string = &closure_string;
return false;
case BRD_HEAP_CLASS:
value->vtype = BRD_VAL_STRING;
value->as.string = &class_string;
return false;
case BRD_HEAP_OBJECT:
value->vtype = BRD_VAL_STRING;
value->as.string = &object_string;
return false;
case BRD_HEAP_DICT:
string = brd_value_dict_to_string(value->as.heap->as.dict);
value->vtype = BRD_VAL_HEAP;
value->as.heap = brd_heap_new(BRD_HEAP_STRING);
brd_value_string_init(value->as.heap->as.string, string);
return true;
}
break;
}
BARF("what?");
return -1;
}
static intmax_t
brd_value_index_clamp(intmax_t idx, size_t length)
{
if (length == 0) {
return idx;
} else if (idx >= 0) {
return idx % length;
} else {
return length - (-idx) % length;
}
}
int
brd_value_index(struct brd_value *value, intmax_t idx)
{
if IS_STRING(*value) {
struct brd_value_string *string = AS_STRING(*value);
char *c;
idx = brd_value_index_clamp(idx, string->length);
if (string->length == 0) {
value->vtype = BRD_VAL_UNIT;
return false;
}
c = malloc(2);
c[0] = string->s[idx];
c[1] = '\0';
value->vtype = BRD_VAL_HEAP;
value->as.heap = brd_heap_new(BRD_HEAP_STRING);
brd_value_string_init(value->as.heap->as.string, c);
return true;
} else if (IS_HEAP(*value, BRD_HEAP_LIST)) {
struct brd_value_list *list = value->as.heap->as.list;
idx = brd_value_index_clamp(idx, list->length);
if (list->length == 0) {
value->vtype = BRD_VAL_UNIT;
return false;
} else {
*value = list->items[idx];
return false;
}
} else {
BARF("attempted to index a non-indexable");
return -1;
}
}
int
brd_value_truthify(struct brd_value *value)
{
switch (value->vtype) {
case BRD_VAL_NUM:
return value->as.num != 0;
case BRD_VAL_STRING:
return value->as.string->length > 0;
case BRD_VAL_BOOL:
return value->as.boolean;
case BRD_VAL_UNIT:
return false;
case BRD_VAL_BUILTIN:
return true;
case BRD_VAL_METHOD:
return true;
case BRD_VAL_HEAP:
switch(value->as.heap->htype) {
case BRD_HEAP_STRING:
return value->as.heap->as.string->length > 0;
case BRD_HEAP_LIST:
return value->as.heap->as.list->length > 0;
case BRD_HEAP_CLOSURE:
return true;
case BRD_HEAP_CLASS:
return true;
case BRD_HEAP_OBJECT:
return true;
case BRD_HEAP_DICT:
// TODO: truthy iff not empty
return true;
}
}
BARF("what?");
return false;
}
// https://stackoverflow.com/questions/1903954/is-there-a-standard-sign-function-signum-sgn-in-c-c
#define signum(a) ((0 < (a)) - ((a) < 0)) //why isn't this in math.h?
struct brd_comparison
brd_value_compare(struct brd_value *a, struct brd_value *b)
{
struct brd_comparison result;
if (IS_STRING(*a)) {
char *sa = AS_STRING(*a)->s;
if (IS_STRING(*b)) {
result.cmp = signum(strcmp(sa, AS_STRING(*b)->s));
result.is_ord = true;
} else if (IS_VAL(*b, BRD_VAL_NUM)) {
long double da = strtold(sa, NULL);
result.cmp = signum(da - b->as.num);
result.is_ord = true;
} else if (IS_VAL(*b, BRD_VAL_BOOL)) {
result.cmp = strcmp(sa, b->as.boolean ? "true" : "false");
result.is_ord = true;
} else if (IS_VAL(*b, BRD_VAL_UNIT)) {
result.cmp = signum(strcmp(sa, "unit"));
result.is_ord = true;
} else {
result.cmp = false;
result.is_ord = false;
}
} else if (IS_VAL(*a, BRD_VAL_NUM)) {
long double da = a->as.num;
if (IS_STRING(*b)) {
long double db = strtold(AS_STRING(*b)->s, NULL);
result.cmp = signum(da - db);
result.is_ord = true;
} else if (IS_VAL(*b, BRD_VAL_NUM)) {
result.cmp = signum(da - b->as.num);
result.is_ord = true;
} else if (IS_VAL(*b, BRD_VAL_BOOL)) {
result.cmp = signum(da - b->as.boolean);
result.is_ord = true;
} else if (IS_VAL(*b, BRD_VAL_UNIT)) {
result.cmp = signum(da);
result.is_ord = true;
} else {
result.cmp = false;
result.is_ord = false;
}
} else if (IS_VAL(*a, BRD_VAL_BOOL)) {
int ba = a->as.boolean;
if (IS_STRING(*b)) {
result.cmp = strcmp(ba ? "true" : "false", AS_STRING(*b)->s);
result.is_ord = true;
} else if (IS_VAL(*b, BRD_VAL_NUM)) {
result.cmp = signum(ba - b->as.num);
result.is_ord = true;
} else if (IS_VAL(*b, BRD_VAL_BOOL)) {
result.cmp = ba == b->as.boolean;
result.is_ord = false;
} else if (IS_VAL(*b, BRD_VAL_UNIT)) {
result.cmp = ba;
result.is_ord = false;
} else {
result.cmp = false;
result.is_ord = false;
}
} else if (IS_VAL(*a, BRD_VAL_UNIT)) {
if (IS_STRING(*b)) {
result.cmp = strcmp("unit", AS_STRING(*b)->s);
result.is_ord = true;
} else if (IS_VAL(*b, BRD_VAL_NUM)) {
result.cmp = signum(-b->as.num);
result.is_ord = true;
} else if (IS_VAL(*b, BRD_VAL_BOOL)) {
result.cmp = b->as.boolean;
result.is_ord = false;
} else if (IS_VAL(*b, BRD_VAL_UNIT)) {
result.cmp = 0;
result.is_ord = true;
} else {
result.cmp = false;
result.is_ord = false;
}
} else if (IS_VAL(*a, BRD_VAL_METHOD)) {
struct brd_value_method ma = a->as.method;
if (IS_VAL(*b, BRD_VAL_METHOD)) {
struct brd_value_method mb = b->as.method;
result.cmp = ma.this == mb.this && ma.fn == mb.fn;
result.is_ord = false;
} else {
result.cmp = false;
result.is_ord = false;
}
} else if (IS_HEAP(*a, BRD_HEAP_LIST)) {
struct brd_value_list *la = a->as.heap->as.list;
if (IS_HEAP(*b, BRD_HEAP_LIST)) {
struct brd_value_list *lb = b->as.heap->as.list;
result.cmp = brd_value_list_equals(la, lb);
result.is_ord = false;
} else {
result.cmp = false;
result.is_ord = false;
}
} else if (IS_VAL(*a, BRD_VAL_HEAP)) {
if (IS_VAL(*b, BRD_VAL_HEAP)) {
result.cmp = a->as.heap == b->as.heap;
result.is_ord = false;
} else {
result.cmp = false;
result.is_ord = false;
}
} else {
BARF("what?");
}
return result;
}
#undef signum
void
brd_value_concat(struct brd_value *a, struct brd_value *b)
{
struct brd_heap_entry *new;
if (IS_HEAP(*a, BRD_HEAP_LIST)) {
struct brd_value_list *list_a = a->as.heap->as.list;
new = brd_heap_new(BRD_HEAP_LIST);
if (IS_HEAP(*b, BRD_HEAP_LIST)) {
struct brd_value_list *list_b = b->as.heap->as.list;
brd_value_list_init_with_capacity(
new->as.list, list_a->length + list_b->length
);
new->as.list->length = list_a->length + list_b->length;
for (size_t i = 0; i < list_a->length; i++) {
new->as.list->items[i] = list_a->items[i];
}
for (size_t i = 0; i < list_b->length; i++) {
new->as.list->items[i + list_a->length]
= list_b->items[i];
}
} else {
brd_value_list_init_with_capacity(
new->as.list, list_a->length + 1
);
new->as.list->length = list_a->length + 1;
for (size_t i = 0; i < list_a->length; i++) {
new->as.list->items[i] = list_a->items[i];
}
new->as.list->items[list_a->length] = *b;
}
} else {
int free_a, free_b;
char *new_string;
free_a = brd_value_coerce_string(a);
free_b = brd_value_coerce_string(b);
new = brd_heap_new(BRD_HEAP_STRING);
new_string = malloc(AS_STRING(*a)->length + AS_STRING(*b)->length + 1);
strcpy(new_string, AS_STRING(*a)->s);
strcpy(new_string + AS_STRING(*a)->length, AS_STRING(*b)->s);
brd_value_string_init(new->as.string, new_string);
if (free_a) {
brd_heap_destroy(a->as.heap);
}
if (free_b) {
brd_heap_destroy(b->as.heap);
}
}
a->vtype = BRD_VAL_HEAP;
a->as.heap = new;
}
static int
_builtin_write(struct brd_value *args, size_t num_args, struct brd_value *out)
{
if (out != NULL) {
out->vtype = BRD_VAL_UNIT;
}
for (size_t i = 0; i < num_args; i++) {
int new = brd_value_coerce_string(&args[i]);
printf("%s", AS_STRING(args[i])->s);
if (new) {
brd_heap_destroy(args[i].as.heap);
}
}
return false;
}
static int
_builtin_writeln(struct brd_value *args, size_t num_args, struct brd_value *out)
{
_builtin_write(args, num_args, out);
printf("\n");
return false;
}
static int
_builtin_readln(struct brd_value *args, size_t num_args, struct brd_value *out)
{