-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime.c
More file actions
1408 lines (1208 loc) · 38.3 KB
/
runtime.c
File metadata and controls
1408 lines (1208 loc) · 38.3 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 "runtime.h"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char *name;
Value value;
} VariableSlot;
typedef struct {
VariableSlot *slots;
int capacity;
int count;
} Scope;
typedef struct {
char *name;
const struct Statement *function_statement;
} FunctionSlot;
typedef struct {
char *key;
Value value;
} RecordSlot;
struct List {
Value *items;
int count;
};
struct Record {
RecordSlot *slots;
int capacity;
int count;
};
struct Runtime {
Scope *scopes;
int scope_count;
FunctionSlot *functions;
int function_capacity;
int function_count;
};
typedef struct {
char *text;
size_t length;
size_t capacity;
} StringBuilder;
static bool value_copy_into(const Value *source, Value *out_value);
static void list_destroy(List *list);
static bool list_copy(const List *source, List **out_list);
static void record_destroy(Record *record);
static bool record_copy(const Record *source, Record **out_record);
static unsigned long hash_text_case_sensitive(const char *text) {
unsigned long hash = 5381;
while (*text != '\0') {
hash = ((hash << 5) + hash) + (unsigned char) *text;
text += 1;
}
return hash;
}
static unsigned long hash_text_case_insensitive(const char *text) {
unsigned long hash = 5381;
while (*text != '\0') {
hash = ((hash << 5) + hash) + (unsigned char) tolower((unsigned char) *text);
text += 1;
}
return hash;
}
static bool equals_ignore_case(const char *left, const char *right) {
size_t index = 0;
while (left[index] != '\0' && right[index] != '\0') {
if (tolower((unsigned char) left[index]) != tolower((unsigned char) right[index])) {
return false;
}
index += 1;
}
return left[index] == '\0' && right[index] == '\0';
}
static char *copy_text(const char *text) {
size_t length = strlen(text);
char *copy = (char *) malloc(length + 1);
if (copy == NULL) {
return NULL;
}
(void) memcpy(copy, text, length + 1);
return copy;
}
static char *normalize_name(const char *name) {
size_t length = strlen(name);
size_t index;
char *normalized = (char *) malloc(length + 1);
if (normalized == NULL) {
return NULL;
}
for (index = 0; index < length; index += 1) {
normalized[index] = (char) tolower((unsigned char) name[index]);
}
normalized[length] = '\0';
return normalized;
}
static bool builder_ensure_capacity(StringBuilder *builder, size_t extra) {
size_t needed = builder->length + extra + 1;
size_t new_capacity;
char *new_text;
if (needed <= builder->capacity) {
return true;
}
new_capacity = builder->capacity == 0 ? 32 : builder->capacity;
while (new_capacity < needed) {
new_capacity *= 2;
}
new_text = (char *) realloc(builder->text, new_capacity);
if (new_text == NULL) {
return false;
}
builder->text = new_text;
builder->capacity = new_capacity;
return true;
}
static bool builder_append_text(StringBuilder *builder, const char *text) {
size_t length = strlen(text);
if (!builder_ensure_capacity(builder, length)) {
return false;
}
(void) memcpy(builder->text + builder->length, text, length + 1);
builder->length += length;
return true;
}
static bool builder_append_char(StringBuilder *builder, char ch) {
if (!builder_ensure_capacity(builder, 1)) {
return false;
}
builder->text[builder->length] = ch;
builder->length += 1;
builder->text[builder->length] = '\0';
return true;
}
static bool append_value_text(StringBuilder *builder, const Value *value);
static List *list_create(void) {
return (List *) calloc(1, sizeof(List));
}
static bool list_append_owned(List *list, Value item) {
Value *new_items = (Value *) realloc(list->items, (size_t) (list->count + 1) * sizeof(Value));
if (new_items == NULL) {
return false;
}
list->items = new_items;
list->items[list->count] = item;
list->count += 1;
return true;
}
static bool list_copy(const List *source, List **out_list) {
int index;
List *copy = list_create();
if (copy == NULL) {
return false;
}
for (index = 0; index < source->count; index += 1) {
Value item_copy = value_none();
if (!value_copy_into(&source->items[index], &item_copy)) {
list_destroy(copy);
return false;
}
if (!list_append_owned(copy, item_copy)) {
value_destroy(&item_copy);
list_destroy(copy);
return false;
}
}
*out_list = copy;
return true;
}
static void list_destroy(List *list) {
int index;
if (list == NULL) {
return;
}
for (index = 0; index < list->count; index += 1) {
value_destroy(&list->items[index]);
}
free(list->items);
free(list);
}
static Record *record_create(void) {
Record *record = (Record *) calloc(1, sizeof(Record));
if (record == NULL) {
return NULL;
}
record->capacity = 8;
record->slots = (RecordSlot *) calloc((size_t) record->capacity, sizeof(RecordSlot));
if (record->slots == NULL) {
free(record);
return NULL;
}
return record;
}
static bool record_ensure_capacity(Record *record) {
int old_capacity;
RecordSlot *old_slots;
int index;
if ((record->count + 1) * 10 < record->capacity * 7) {
return true;
}
old_capacity = record->capacity;
old_slots = record->slots;
record->capacity *= 2;
record->slots = (RecordSlot *) calloc((size_t) record->capacity, sizeof(RecordSlot));
if (record->slots == NULL) {
record->capacity = old_capacity;
record->slots = old_slots;
return false;
}
record->count = 0;
for (index = 0; index < old_capacity; index += 1) {
if (old_slots[index].key != NULL) {
size_t slot = hash_text_case_sensitive(old_slots[index].key) % (unsigned long) record->capacity;
while (record->slots[slot].key != NULL) {
slot = (slot + 1U) % (unsigned long) record->capacity;
}
record->slots[slot] = old_slots[index];
record->count += 1;
}
}
free(old_slots);
return true;
}
static RecordSlot *record_find_slot(Record *record, const char *key) {
size_t slot = hash_text_case_sensitive(key) % (unsigned long) record->capacity;
while (record->slots[slot].key != NULL) {
if (strcmp(record->slots[slot].key, key) == 0) {
return &record->slots[slot];
}
slot = (slot + 1U) % (unsigned long) record->capacity;
}
return &record->slots[slot];
}
static bool record_set_owned(Record *record, const char *key, Value value) {
RecordSlot *slot;
if (!record_ensure_capacity(record)) {
return false;
}
slot = record_find_slot(record, key);
if (slot->key != NULL) {
value_destroy(&slot->value);
slot->value = value;
return true;
}
slot->key = copy_text(key);
if (slot->key == NULL) {
return false;
}
slot->value = value;
record->count += 1;
return true;
}
static bool record_copy(const Record *source, Record **out_record) {
int index;
Record *copy = record_create();
if (copy == NULL) {
return false;
}
for (index = 0; index < source->capacity; index += 1) {
if (source->slots[index].key != NULL) {
Value value_copy = value_none();
if (!value_copy_into(&source->slots[index].value, &value_copy)) {
record_destroy(copy);
return false;
}
if (!record_set_owned(copy, source->slots[index].key, value_copy)) {
value_destroy(&value_copy);
record_destroy(copy);
return false;
}
}
}
*out_record = copy;
return true;
}
static void record_destroy(Record *record) {
int index;
if (record == NULL) {
return;
}
for (index = 0; index < record->capacity; index += 1) {
if (record->slots[index].key != NULL) {
free(record->slots[index].key);
value_destroy(&record->slots[index].value);
}
}
free(record->slots);
free(record);
}
static bool append_value_text(StringBuilder *builder, const Value *value) {
switch (value->type) {
case VALUE_NONE:
return builder_append_text(builder, "nothing");
case VALUE_NUMBER: {
char buffer[64];
int written = snprintf(buffer, sizeof(buffer), "%.15g", value->number);
if (written < 0) {
return false;
}
return builder_append_text(builder, buffer);
}
case VALUE_STRING:
return builder_append_text(builder, value->string != NULL ? value->string : "");
case VALUE_BOOLEAN:
return builder_append_text(builder, value->boolean ? "true" : "false");
case VALUE_LIST: {
int index;
if (!builder_append_char(builder, '[')) {
return false;
}
for (index = 0; index < value->list->count; index += 1) {
if (index > 0 && !builder_append_text(builder, ", ")) {
return false;
}
if (!append_value_text(builder, &value->list->items[index])) {
return false;
}
}
return builder_append_char(builder, ']');
}
case VALUE_RECORD: {
int index;
bool first = true;
if (!builder_append_char(builder, '{')) {
return false;
}
for (index = 0; index < value->record->capacity; index += 1) {
if (value->record->slots[index].key == NULL) {
continue;
}
if (!first && !builder_append_text(builder, ", ")) {
return false;
}
if (!builder_append_text(builder, value->record->slots[index].key) ||
!builder_append_text(builder, ": ")) {
return false;
}
if (!append_value_text(builder, &value->record->slots[index].value)) {
return false;
}
first = false;
}
return builder_append_char(builder, '}');
}
}
return false;
}
static bool variable_table_ensure_capacity(VariableSlot **slots_ptr, int *capacity_ptr, int count) {
VariableSlot *old_slots = *slots_ptr;
int old_capacity = *capacity_ptr;
int new_capacity;
int index;
VariableSlot *new_slots;
if ((count + 1) * 10 < (*capacity_ptr) * 7) {
return true;
}
new_capacity = *capacity_ptr == 0 ? 8 : (*capacity_ptr) * 2;
new_slots = (VariableSlot *) calloc((size_t) new_capacity, sizeof(VariableSlot));
if (new_slots == NULL) {
return false;
}
for (index = 0; index < old_capacity; index += 1) {
if (old_slots[index].name != NULL) {
size_t slot = hash_text_case_insensitive(old_slots[index].name) % (unsigned long) new_capacity;
while (new_slots[slot].name != NULL) {
slot = (slot + 1U) % (unsigned long) new_capacity;
}
new_slots[slot] = old_slots[index];
}
}
free(old_slots);
*slots_ptr = new_slots;
*capacity_ptr = new_capacity;
return true;
}
static VariableSlot *scope_find_slot(Scope *scope, const char *normalized_name) {
size_t slot;
if (scope->capacity == 0) {
return NULL;
}
slot = hash_text_case_insensitive(normalized_name) % (unsigned long) scope->capacity;
while (scope->slots[slot].name != NULL) {
if (equals_ignore_case(scope->slots[slot].name, normalized_name)) {
return &scope->slots[slot];
}
slot = (slot + 1U) % (unsigned long) scope->capacity;
}
return &scope->slots[slot];
}
static bool function_table_ensure_capacity(Runtime *runtime) {
FunctionSlot *old_slots = runtime->functions;
int old_capacity = runtime->function_capacity;
int index;
if ((runtime->function_count + 1) * 10 < runtime->function_capacity * 7) {
return true;
}
runtime->function_capacity = runtime->function_capacity == 0 ? 8 : runtime->function_capacity * 2;
runtime->functions = (FunctionSlot *) calloc((size_t) runtime->function_capacity, sizeof(FunctionSlot));
if (runtime->functions == NULL) {
runtime->functions = old_slots;
runtime->function_capacity = old_capacity;
return false;
}
for (index = 0; index < old_capacity; index += 1) {
if (old_slots[index].name != NULL) {
size_t slot = hash_text_case_insensitive(old_slots[index].name) %
(unsigned long) runtime->function_capacity;
while (runtime->functions[slot].name != NULL) {
slot = (slot + 1U) % (unsigned long) runtime->function_capacity;
}
runtime->functions[slot] = old_slots[index];
}
}
free(old_slots);
return true;
}
static FunctionSlot *function_find_slot(Runtime *runtime, const char *normalized_name) {
size_t slot;
if (runtime->function_capacity == 0) {
return NULL;
}
slot = hash_text_case_insensitive(normalized_name) % (unsigned long) runtime->function_capacity;
while (runtime->functions[slot].name != NULL) {
if (equals_ignore_case(runtime->functions[slot].name, normalized_name)) {
return &runtime->functions[slot];
}
slot = (slot + 1U) % (unsigned long) runtime->function_capacity;
}
return &runtime->functions[slot];
}
Runtime *runtime_create(void) {
Runtime *runtime = (Runtime *) calloc(1, sizeof(Runtime));
if (runtime == NULL) {
return NULL;
}
if (!runtime_push_scope(runtime)) {
runtime_destroy(runtime);
return NULL;
}
return runtime;
}
bool runtime_push_scope(Runtime *runtime) {
Scope *new_scopes = (Scope *) realloc(runtime->scopes, (size_t) (runtime->scope_count + 1) * sizeof(Scope));
if (new_scopes == NULL) {
return false;
}
runtime->scopes = new_scopes;
runtime->scopes[runtime->scope_count].slots = NULL;
runtime->scopes[runtime->scope_count].capacity = 0;
runtime->scopes[runtime->scope_count].count = 0;
runtime->scope_count += 1;
return true;
}
bool runtime_pop_scope(Runtime *runtime) {
Scope *scope;
int index;
if (runtime->scope_count <= 1) {
return false;
}
scope = &runtime->scopes[runtime->scope_count - 1];
for (index = 0; index < scope->capacity; index += 1) {
if (scope->slots[index].name != NULL) {
free(scope->slots[index].name);
value_destroy(&scope->slots[index].value);
}
}
free(scope->slots);
runtime->scope_count -= 1;
return true;
}
Value value_none(void) {
Value value;
value.type = VALUE_NONE;
value.number = 0.0;
value.boolean = false;
value.string = NULL;
value.list = NULL;
value.record = NULL;
return value;
}
Value value_number(double number) {
Value value = value_none();
value.type = VALUE_NUMBER;
value.number = number;
return value;
}
Value value_boolean(bool boolean) {
Value value = value_none();
value.type = VALUE_BOOLEAN;
value.boolean = boolean;
return value;
}
Value value_string_owned(char *text) {
Value value = value_none();
value.type = VALUE_STRING;
value.string = text;
return value;
}
bool value_make_string_copy(const char *text, Value *out_value) {
char *copy = copy_text(text);
if (copy == NULL) {
return false;
}
*out_value = value_string_owned(copy);
return true;
}
bool value_make_empty_list(Value *out_value) {
List *list = list_create();
if (list == NULL) {
return false;
}
*out_value = value_none();
out_value->type = VALUE_LIST;
out_value->list = list;
return true;
}
bool value_make_empty_record(Value *out_value) {
Record *record = record_create();
if (record == NULL) {
return false;
}
*out_value = value_none();
out_value->type = VALUE_RECORD;
out_value->record = record;
return true;
}
static bool value_copy_into(const Value *source, Value *out_value) {
switch (source->type) {
case VALUE_NONE:
*out_value = value_none();
return true;
case VALUE_NUMBER:
*out_value = value_number(source->number);
return true;
case VALUE_STRING:
return value_make_string_copy(source->string != NULL ? source->string : "", out_value);
case VALUE_BOOLEAN:
*out_value = value_boolean(source->boolean);
return true;
case VALUE_LIST: {
List *copy = NULL;
if (!list_copy(source->list, ©)) {
return false;
}
*out_value = value_none();
out_value->type = VALUE_LIST;
out_value->list = copy;
return true;
}
case VALUE_RECORD: {
Record *copy = NULL;
if (!record_copy(source->record, ©)) {
return false;
}
*out_value = value_none();
out_value->type = VALUE_RECORD;
out_value->record = copy;
return true;
}
}
return false;
}
bool value_copy(const Value *source, Value *out_value) {
return value_copy_into(source, out_value);
}
void value_destroy(Value *value) {
if (value == NULL) {
return;
}
if (value->type == VALUE_STRING) {
free(value->string);
} else if (value->type == VALUE_LIST) {
list_destroy(value->list);
} else if (value->type == VALUE_RECORD) {
record_destroy(value->record);
}
*value = value_none();
}
void value_fprint(FILE *stream, const Value *value) {
char *text = NULL;
if (!value_to_text_copy(value, &text)) {
(void) fputs("<value>", stream);
return;
}
(void) fputs(text, stream);
free(text);
}
bool value_to_text_copy(const Value *value, char **out_text) {
StringBuilder builder;
builder.text = NULL;
builder.length = 0;
builder.capacity = 0;
if (!append_value_text(&builder, value)) {
free(builder.text);
return false;
}
if (builder.text == NULL) {
builder.text = copy_text("");
if (builder.text == NULL) {
return false;
}
}
*out_text = builder.text;
return true;
}
bool value_try_parse_number(const char *text, double *out_number) {
char *end = NULL;
while (*text != '\0' && isspace((unsigned char) *text)) {
text += 1;
}
if (*text == '\0') {
return false;
}
*out_number = strtod(text, &end);
if (end == text) {
return false;
}
while (*end != '\0' && isspace((unsigned char) *end)) {
end += 1;
}
return *end == '\0';
}
bool value_try_parse_boolean(const char *text, bool *out_boolean) {
while (*text != '\0' && isspace((unsigned char) *text)) {
text += 1;
}
if (equals_ignore_case(text, "true")) {
*out_boolean = true;
return true;
}
if (equals_ignore_case(text, "false")) {
*out_boolean = false;
return true;
}
return false;
}
bool value_to_number(const Value *value, double *out_number) {
switch (value->type) {
case VALUE_NUMBER:
*out_number = value->number;
return true;
case VALUE_BOOLEAN:
*out_number = value->boolean ? 1.0 : 0.0;
return true;
case VALUE_STRING:
return value_try_parse_number(value->string != NULL ? value->string : "", out_number);
case VALUE_NONE:
case VALUE_LIST:
case VALUE_RECORD:
return false;
}
return false;
}
bool value_is_truthy(const Value *value) {
switch (value->type) {
case VALUE_NONE:
return false;
case VALUE_NUMBER:
return value->number != 0.0;
case VALUE_STRING:
return value->string != NULL && value->string[0] != '\0';
case VALUE_BOOLEAN:
return value->boolean;
case VALUE_LIST:
return value->list != NULL && value->list->count > 0;
case VALUE_RECORD:
return value->record != NULL && value->record->count > 0;
}
return false;
}
bool value_equals(const Value *left, const Value *right, bool *out_equal) {
int index;
if (left->type != right->type) {
if ((left->type == VALUE_NUMBER || left->type == VALUE_BOOLEAN || left->type == VALUE_STRING) &&
(right->type == VALUE_NUMBER || right->type == VALUE_BOOLEAN || right->type == VALUE_STRING)) {
char *left_text = NULL;
char *right_text = NULL;
if (!value_to_text_copy(left, &left_text)) {
return false;
}
if (!value_to_text_copy(right, &right_text)) {
free(left_text);
return false;
}
*out_equal = strcmp(left_text, right_text) == 0;
free(left_text);
free(right_text);
return true;
}
*out_equal = false;
return true;
}
switch (left->type) {
case VALUE_NONE:
*out_equal = true;
return true;
case VALUE_NUMBER:
*out_equal = left->number == right->number;
return true;
case VALUE_STRING:
*out_equal = strcmp(left->string != NULL ? left->string : "",
right->string != NULL ? right->string : "") == 0;
return true;
case VALUE_BOOLEAN:
*out_equal = left->boolean == right->boolean;
return true;
case VALUE_LIST:
if (left->list->count != right->list->count) {
*out_equal = false;
return true;
}
for (index = 0; index < left->list->count; index += 1) {
bool item_equal = false;
if (!value_equals(&left->list->items[index], &right->list->items[index], &item_equal)) {
return false;
}
if (!item_equal) {
*out_equal = false;
return true;
}
}
*out_equal = true;
return true;
case VALUE_RECORD:
if (left->record->count != right->record->count) {
*out_equal = false;
return true;
}
for (index = 0; index < left->record->capacity; index += 1) {
if (left->record->slots[index].key != NULL) {
bool field_equal = false;
Value right_value = value_none();
if (!value_record_get(right, left->record->slots[index].key, &right_value)) {
*out_equal = false;
return true;
}
if (!value_equals(&left->record->slots[index].value, &right_value, &field_equal)) {
value_destroy(&right_value);
return false;
}
value_destroy(&right_value);
if (!field_equal) {
*out_equal = false;
return true;
}
}
}
*out_equal = true;
return true;
}
return false;
}
const char *value_type_name(const Value *value) {
switch (value->type) {
case VALUE_NONE:
return "nothing";
case VALUE_NUMBER:
return "number";
case VALUE_STRING:
return "text";
case VALUE_BOOLEAN:
return "boolean";
case VALUE_LIST:
return "list";
case VALUE_RECORD:
return "record";
}
return "unknown";
}
int value_list_length(const Value *list_value) {
if (list_value->type != VALUE_LIST || list_value->list == NULL) {
return -1;
}
return list_value->list->count;
}
bool value_list_get(const Value *list_value, int one_based_index, Value *out_value) {
int zero_based_index = one_based_index - 1;
if (list_value->type != VALUE_LIST || list_value->list == NULL) {
return false;
}
if (zero_based_index < 0 || zero_based_index >= list_value->list->count) {
return false;
}
return value_copy_into(&list_value->list->items[zero_based_index], out_value);
}
bool value_list_append_copy(const Value *list_value, const Value *item, Value *out_value) {
List *copy;
Value item_copy = value_none();
if (list_value->type != VALUE_LIST || list_value->list == NULL) {
return false;
}
if (!list_copy(list_value->list, ©)) {
return false;
}
if (!value_copy_into(item, &item_copy)) {
list_destroy(copy);
return false;
}
if (!list_append_owned(copy, item_copy)) {
value_destroy(&item_copy);
list_destroy(copy);
return false;
}
*out_value = value_none();
out_value->type = VALUE_LIST;
out_value->list = copy;
return true;
}
bool value_list_set_copy(const Value *list_value, int one_based_index, const Value *item, Value *out_value) {
int zero_based_index = one_based_index - 1;
List *copy;
if (list_value->type != VALUE_LIST || list_value->list == NULL) {
return false;
}
if (zero_based_index < 0 || zero_based_index >= list_value->list->count) {
return false;
}
if (!list_copy(list_value->list, ©)) {
return false;
}
value_destroy(©->items[zero_based_index]);
if (!value_copy_into(item, ©->items[zero_based_index])) {
list_destroy(copy);
return false;
}
*out_value = value_none();
out_value->type = VALUE_LIST;
out_value->list = copy;
return true;
}
bool value_list_insert_copy(const Value *list_value, int one_based_index, const Value *item, Value *out_value) {
int zero_based_index = one_based_index - 1;
List *copy;
Value item_copy = value_none();
Value *new_items;
int index;
if (list_value->type != VALUE_LIST || list_value->list == NULL) {
return false;
}
if (zero_based_index < 0 || zero_based_index > list_value->list->count) {
return false;
}
if (!list_copy(list_value->list, ©)) {
return false;
}
if (!value_copy_into(item, &item_copy)) {
list_destroy(copy);
return false;
}
new_items = (Value *) realloc(copy->items, (size_t) (copy->count + 1) * sizeof(Value));
if (new_items == NULL) {
value_destroy(&item_copy);
list_destroy(copy);
return false;