-
Notifications
You must be signed in to change notification settings - Fork 0
/
mal_04.c
4298 lines (4086 loc) · 117 KB
/
mal_04.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
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
/* MIT License
Copyright (c) 2022 Bezdek Miroslav
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <math.h>
#include <ctype.h>
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#define DEBUG 0
#define GC_ON 1
#define VAR_NIL 0
typedef enum {false, true} bool;
struct gc_s;
typedef struct gc_s gc_t, *gc_p, **gc_pp;
struct text_s;
typedef struct text_s text_t, *text_p, **text_pp;
struct token_s;
typedef struct token_s token_t, *token_p;
struct function_s;
typedef struct function_s function_t, *function_p;
struct closure_s;
typedef struct closure_s closure_t, *closure_p;
struct list_s;
typedef struct list_s list_t, *list_p;
struct vector_s;
typedef struct vector_s vector_t, *vector_p;
struct hashmap_s;
typedef struct hashmap_s hashmap_t, *hashmap_p;
struct env_s;
typedef struct env_s env_t, *env_p;
struct error_s;
typedef struct error_s error_t, *error_p;
struct comment_s;
typedef struct comment_s comment_t, *comment_p;
struct mal_s;
typedef struct mal_s mal_t, *mal_p, **mal_pp;
struct lvm_s;
typedef struct lvm_s lvm_t, *lvm_p, **lvm_pp;
typedef enum {
GC_TEXT, GC_TOKEN, GC_LIST, GC_VECTOR, GC_ENV, GC_HASHMAP, GC_MAL, GC_COMMENT,
GC_FUNCTION, GC_CLOSURE, GC_ERROR
} gc_type;
typedef enum {
TOKEN_CURRENT, TOKEN_NEXT
} token_position;
typedef enum {
ERROR_NONE, ERROR_READER, ERROR_RUNTIME, ERROR_PRINTER
} error_type;
struct gc_s {
gc_type type;
int mark;
gc_p next;
};
struct text_s {
gc_t gc;
char *data;
size_t count;
size_t capacity;
};
struct function_s {
gc_t gc;
mal_p (*definition)(lvm_p this, mal_p params);
text_p name;
};
struct closure_s {
gc_t gc;
env_p env;
mal_p parameters;
mal_p more;
mal_p definition;
};
struct list_s {
gc_t gc;
mal_pp data;
size_t count;
size_t capacity;
};
struct vector_s {
gc_t gc;
mal_pp data;
size_t count;
size_t capacity;
};
struct hashmap_s {
gc_t gc;
mal_pp data;
size_t count;
size_t capacity;
};
struct env_s {
gc_t gc;
env_p outer;
vector_p hashmap;
};
struct error_s {
gc_t gc;
text_pp data;
error_type *type;
size_t count;
size_t capacity;
};
struct comment_s {
gc_t gc;
text_pp data;
size_t count;
size_t capacity;
};
typedef enum {
TOKEN_EOI, TOKEN_NIL, TOKEN_COMMENT, TOKEN_BOOLEAN, TOKEN_QUOTE, TOKEN_COLON,
TOKEN_LPAREN, TOKEN_RPAREN, TOKEN_AT, TOKEN_LBRACKET, TOKEN_BACKSLASH,
TOKEN_RBRACKET, TOKEN_CARET, TOKEN_BACKTICK, TOKEN_LBRACE, TOKEN_RBRACE,
TOKEN_TILDE_AT, TOKEN_TILDE, TOKEN_SYMBOL, TOKEN_INTEGER, TOKEN_DECIMAL,
TOKEN_KEYWORD, TOKEN_STRING
} token_type;
typedef union {
text_p eoi;
text_p nil;
text_p error;
text_p comment;
text_p boolean;
text_p special;
text_p symbol;
text_p keyword;
text_p string;
text_p function;
text_p closure;
text_p list;
text_p vector;
text_p hashmap;
text_p number;
} token_value;
struct token_s {
gc_t gc;
token_type type;
token_value as;
size_t length;
size_t line;
size_t column;
};
typedef enum {
MAL_EOI, MAL_ERROR, MAL_BOOLEAN, MAL_SYMBOL, MAL_KEYWORD, MAL_STRING,
MAL_NIL, MAL_LIST, MAL_VECTOR, MAL_HASHMAP, MAL_INTEGER, MAL_DECIMAL,
MAL_ENV, MAL_FUNCTION, MAL_CLOSURE
} mal_type;
typedef union {
text_p eoi;
text_p error;
bool boolean;
text_p symbol;
text_p keyword;
text_p string;
text_p nil;
function_p function;
closure_p closure;
list_p list;
vector_p vector;
hashmap_p hashmap;
env_p env;
long integer;
double decimal;
} mal_value;
struct mal_s {
gc_t gc;
mal_type type;
mal_value as;
token_p token;
text_p signature;
text_p identity;
size_t hash;
};
struct lvm_s {
struct {
gc_p first;
size_t count;
size_t total;
int mark;
} gc;
struct {
char *str;
size_t pos;
size_t line;
size_t column;
token_p token[2];
bool valid[2];
} reader;
env_p env;
error_p error;
comment_p comment;
};
text_p text_make(lvm_p this, char *str);
text_p text_append(lvm_p this, text_p text, char item);
text_p text_concat(lvm_p this, text_p text, char *item);
text_p text_concat_text(lvm_p this, text_p text, text_p item);
text_p text_offset(lvm_p this, text_p text, size_t offset);
text_p text_escape(lvm_p this, text_p text);
text_p text_unescape(lvm_p this, text_p text);
text_p text_make_integer(lvm_p this, long item);
text_p text_make_decimal(lvm_p this, double item);
long text_to_integer(lvm_p this, text_p p);
double text_to_decimal(lvm_p this, text_p p);
int text_cmp(lvm_p this, text_p text, char *item);
int text_cmp_text(lvm_p this, text_p text, text_p item);
size_t text_hash_fnv_1a(lvm_p this, text_p p);
size_t text_hash_jenkins(lvm_p this, text_p p);
text_p text_display_position(lvm_p this, token_p token, char *text);
char *text_str(lvm_p this, text_p text);
void text_free(lvm_p this, gc_p text);
bool error_make(lvm_p this);
text_p error_append(lvm_p this, error_type type, text_p text);
text_p error_collapse(lvm_p this);
void error_free(lvm_p this, gc_p gc);
bool comment_make(lvm_p this);
text_p comment_append(lvm_p this, text_p text);
text_p comment_collapse(lvm_p this);
void comment_free(lvm_p this, gc_p gc);
function_p function_make(lvm_p this,
mal_p (*definition)(lvm_p this, mal_p params), text_p name);
void function_free(lvm_p this, gc_p gc);
closure_p closure_make(lvm_p this, env_p env, mal_p parameters,
mal_p definition, mal_p more);
text_p closure_text(lvm_p this, closure_p closure);
mal_p closure_parameters(lvm_p this, mal_pp params, mal_pp more);
void closure_free(lvm_p this, gc_p gc);
list_p list_make(lvm_p this, size_t init);
bool list_append(lvm_p this, list_p list, mal_p mal);
text_p list_text(lvm_p this, list_p list);
text_p list_more_text(lvm_p this, list_p list, mal_p more);
list_p list_offset(lvm_p this, list_p original, size_t offset);
list_p list_params(lvm_p this, list_p original);
mal_p list_find(lvm_p this, list_p list, mal_p symbol);
void list_free(lvm_p this, gc_p list);
vector_p vector_make(lvm_p this, size_t init);
bool vector_append(lvm_p this, vector_p vector, mal_p mal);
void vector_free(lvm_p this, gc_p vector);
hashmap_p hashmap_make(lvm_p this, size_t init);
bool hashmap_set(lvm_p this, hashmap_p hashmap, mal_p key, mal_p value);
bool hashmap_get(lvm_p this, hashmap_p hashmap, mal_p key, mal_pp value);
bool hashmap_get_by_text(lvm_p this, hashmap_p hashmap, text_p key,
mal_pp value);
bool hashmap_append(lvm_p this, hashmap_p hashmap, mal_p mal);
text_p hashmap_text(lvm_p this, hashmap_p hashmap);
text_p hashmap_text(lvm_p this, hashmap_p hashmap);
void hashmap_free(lvm_p this, gc_p hashmap);
env_p env_make(lvm_p this, env_p outer, list_p symbols, list_p exprs,
mal_p more, size_t init);
bool env_set(lvm_p this, env_p env, mal_p key, mal_p value);
bool env_get(lvm_p this, env_p env, mal_p key, mal_pp value);
bool env_get_by_text(lvm_p this, env_p env, text_p key, mal_pp value);
text_p env_text(lvm_p this, env_p env);
void env_free(lvm_p this, gc_p env);
#if __STDC__
#ifndef __STDC_VERSION__
/* C89 */
char *strdup(char *str);
char *strndup(char *str, size_t n);
#else
/* C90[+] */
#endif
#endif
char *readline(lvm_p this, char *prompt);
char tokenizer_peek(lvm_p this);
char tokenizer_peek_next(lvm_p this);
char tokenizer_next(lvm_p this);
token_p tokenizer_scan(lvm_p this);
token_p token_make(lvm_p this);
token_p token_eoi(lvm_p this);
token_p token_comment(lvm_p this);
token_p token_special(lvm_p this);
token_p token_number(lvm_p this);
token_p token_symbol(lvm_p this);
token_p token_keyword(lvm_p this);
token_p token_string(lvm_p this);
void token_free(lvm_p this, gc_p gc);
token_p reader_peek(lvm_p this);
token_p reader_peek_next(lvm_p this);
token_p reader_next(lvm_p this);
mal_p read_str(lvm_p this);
mal_p read_form(lvm_p this);
mal_p read_list(lvm_p this);
mal_p read_parenthesis(lvm_p this);
mal_p read_vector(lvm_p this);
mal_p read_brackets(lvm_p this);
mal_p read_hashmap(lvm_p this);
mal_p read_braces(lvm_p this);
mal_p read_symbol_list(lvm_p this, char *name);
mal_p read_atom(lvm_p this);
mal_p mal_make(lvm_p this, mal_type type);
mal_p mal_eoi(lvm_p this);
mal_p mal_nil(lvm_p this);
mal_p mal_error(lvm_p this, error_type type, text_p text);
mal_p mal_boolean(lvm_p this, bool boolean);
mal_p mal_symbol(lvm_p this, text_p symbol);
mal_p mal_keyword(lvm_p this, text_p keyword);
mal_p mal_string(lvm_p this, text_p string);
mal_p mal_list(lvm_p this, list_p list);
mal_p mal_vector(lvm_p this, vector_p vector);
mal_p mal_hashmap(lvm_p this, hashmap_p hashmap);
mal_p mal_integer(lvm_p this, long integer);
mal_p mal_decimal(lvm_p this, double decimal);
mal_p mal_as_str(lvm_p this, mal_p args, bool readable, char *separator);
text_p mal_print(lvm_p this, mal_p mal, bool readable);
void mal_free(lvm_p this, gc_p mal);
bool is_eoi(mal_p mal);
bool is_nil(mal_p mal);
bool is_true(mal_p mal);
bool is_false(mal_p mal);
bool is_error(mal_p mal);
bool is_integer(mal_p mal);
bool is_decimal(mal_p mal);
bool is_number(mal_p mal);
bool is_symbol(mal_p mal);
bool is_keyword(mal_p mal);
bool is_string(mal_p mal);
bool is_self_evaluating(mal_p mal);
bool is_list(mal_p mal);
bool is_vector(mal_p mal);
bool is_hashmap(mal_p mal);
bool is_sequential(mal_p mal);
bool is_function(mal_p mal);
bool is_callable(mal_p mal);
lvm_p lvm_make();
void lvm_gc(lvm_p this);
void lvm_gc_free(lvm_p this);
void lvm_free(lvm_pp this);
mal_p eval_ast(lvm_p this, mal_p ast, env_p env);
mal_p eval_list(lvm_p this, list_p original, env_p env);
mal_p eval_vector(lvm_p this, vector_p original, env_p env);
mal_p eval_hashmap(lvm_p this, hashmap_p original, env_p env);
mal_p eval_def_bang(lvm_p this, mal_p ast, env_p env);
mal_p eval_let_star(lvm_p this, mal_p ast, env_p outer);
mal_p eval_if(lvm_p this, mal_p ast, env_p env);
mal_p eval_fn_star(lvm_p this, mal_p ast, env_p env);
mal_p eval_do(lvm_p this, mal_p ast, env_p env);
mal_p core_add(lvm_p this, mal_p args);
mal_p core_sub(lvm_p this, mal_p args);
mal_p core_mul(lvm_p this, mal_p args);
mal_p core_div(lvm_p this, mal_p args);
mal_p core_list(lvm_p this, mal_p args);
mal_p core_vector(lvm_p this, mal_p args);
mal_p core_hashmap(lvm_p this, mal_p args);
mal_p core_zip(lvm_p this, mal_p args);
mal_p core_pr_str(lvm_p this, mal_p args);
mal_p core_str(lvm_p this, mal_p args);
mal_p core_prn(lvm_p this, mal_p args);
mal_p core_println(lvm_p this, mal_p args);
mal_p lvm_read(lvm_p this, char *str);
mal_p lvm_eval(lvm_p this, mal_p ast, env_p env);
char *lvm_print(lvm_p this, mal_p value);
char *lvm_rep(lvm_p this, char *str);
text_p text_make(lvm_p this, char *str)
{
size_t size = strlen(str);
text_p text = (text_p)calloc(1, sizeof(text_t));
text->count = size;
text->capacity = ((0 != (size % 32)) + (size / 32)) * 32;
text->data = (char *)calloc((text->capacity + 1), sizeof(char));
strncpy(text->data, str, size);
text->data[size] = 0x00;
text->gc.type = GC_TEXT;
#if GC_ON
text->gc.mark = !this->gc.mark;
#else
text->gc.mark = this->gc.mark;
#endif
text->gc.next = this->gc.first;
this->gc.first = (gc_p)text;
this->gc.count++;
return text;
}
text_p text_append(lvm_p this, text_p text, char item)
{
(void)this;
if (text->count + 1 >= text->capacity) {
text->capacity = text->capacity + 32;
text->data = (char *)realloc(text->data, (text->capacity + 1) * sizeof (char));
}
text->data[text->count] = item;
if (item) {
text->data[++text->count] = 0x00;
}
return text;
}
text_p text_concat(lvm_p this, text_p text, char *item)
{
size_t size = strlen(item);
(void)this;
if (text->count + size + 1 >= text->capacity) {
text->capacity = ((0 != ((text->count + size) % 32))
+ ((text->count + size) / 32)) * 32;
text->data = (char *)realloc(text->data,
(text->capacity + 1) * sizeof(char));
}
if (size > 0) {
strncpy(text->data + text->count, item, size);
text->count = text->count + size;
text->data[text->count] = 0x00;
}
return text;
}
text_p text_concat_text(lvm_p this, text_p text, text_p item)
{
size_t size = item->count;
(void)this;
if (text->count + size + 1 >= text->capacity) {
text->capacity = ((0 != ((text->count + size) % 32))
+ ((text->count + size) / 32)) * 32;
text->data = (char *)realloc(text->data,
(text->capacity + 1) * sizeof(char));
}
if (size > 0) {
strncpy(text->data + text->count, item->data, size);
text->count = text->count + size;
text->data[text->count] = 0x00;
}
return text;
}
text_p text_offset(lvm_p this, text_p text, size_t offset)
{
text_p offseted = text_make(this, "");
size_t at;
for (at = offset; at < text->count; at++) {
text_append(this, offseted, text->data[at]);
}
return offseted;
}
text_p text_escape(lvm_p this, text_p text)
{
text_p escaped = text_make(this, "");
char *string = text->data;
text_append(this, escaped, '"');
while (0x00 != *string) {
switch (*string) {
case 0x09:
text_append(this, escaped, 0x5C);
text_append(this, escaped, 't');
break;
case 0x0A:
text_append(this, escaped, 0x5C);
text_append(this, escaped, 'n');
break;
case 0x0D:
text_append(this, escaped, 0x5C);
text_append(this, escaped, 'r');
break;
case '"':
text_append(this, escaped, 0x5C);
text_append(this, escaped, '"');
break;
case 0x5C:
text_append(this, escaped, 0x5C);
text_append(this, escaped, 0x5C);
break;
default:
text_append(this, escaped, *string);
break;
}
string++;
}
text_append(this, escaped, '"');
return escaped;
}
text_p text_unescape(lvm_p this, text_p text)
{
text_p unescaped = text_make(this, "");
char *string = text->data;
size_t index = 1;
for (; index < text->count; index++) {
if (0x5C == string[index]) {
switch (string[index + 1]) {
case '"':
text_append(this, unescaped, '"');
index++;
break;
case 0x5C:
text_append(this, unescaped, 0x5C);
index++;
break;
case 'n':
text_append(this, unescaped, 0x0A);
index++;
break;
case 'r':
text_append(this, unescaped, 0x0D);
index++;
break;
case 't':
text_append(this, unescaped, 0x09);
index++;
break;
default:
text_append(this, unescaped, string[index]);
break;
}
} else if (index != text->count && '"' != string[index]) {
text_append(this, unescaped, string[index]);
}
}
return unescaped;
}
text_p text_make_integer(lvm_p this, long item)
{
text_p result = text_make(this, "");
(void)this;
if (!item) {
text_append(this, result, '0');
} else {
size_t from = result->count;
size_t to, tmp;
result->count = 0;
while (item) {
text_append(this, result, (item % 10) + '0');
item /= 10;
}
to = result->count - 1;
while (from < to) {
tmp = result->data[from];
result->data[from++] = result->data[to];
result->data[to--] = tmp;
}
}
result->data[result->count] = 0x00;
return result;
}
text_p text_make_decimal(lvm_p this, double item)
{
text_p result = text_make(this, "");
long integer = (long)item;
double fractional = item - integer;
int digits = 0;
(void)this;
if (0 == integer) {
text_append(this, result, '0');
} else {
int from;
int to, tmp;
if (0 > integer) {
text_append(this, result, '-');
integer *= -1;
}
from = result->count;
while (integer && digits < 16) {
text_append(this, result, (integer % 10) + '0');
integer /= 10;
digits++;
}
while (integer && digits >= 16) {
text_append(this, result, '0');
integer /= 10;
}
to = result->count - 1;
while (from < to) {
tmp = result->data[from];
result->data[from++] = result->data[to];
result->data[to--] = tmp;
}
result->data[result->count] = 0x00;
}
if (!fractional) {
text_append(this, result, '.');
text_append(this, result, '0');
} else {
text_append(this, result, '.');
integer = fractional *= 10;
while (fractional > 0 && digits < 16) {
text_append(this, result, (integer % 10) + '0');
integer = fractional *= 10;
fractional -= integer;
digits++;
}
while (fractional > 0 && digits++ >= 16) {
integer = fractional *= 10;
fractional -= integer;
}
}
return result;
}
long text_to_integer(lvm_p this, text_p text)
{
long value = 0x00;
size_t at = 0x00;
(void)this;
for (at = 0x00; at < text->count; at++) {
char ch = text->data[at];
if ('0' <= ch && '9' >= ch) {
value = value * 0x0A + (long)(ch - '0');
} else {
return value;
}
}
return value;
}
double text_to_decimal(lvm_p this, text_p text)
{
double value = 0x00;
double multiplier;
int decimal = 0;
int sign = 1;
size_t at = 0x00;
(void)this;
switch (text->data[at]) {
case '+':
at++;
break;
case '-':
at++;
sign *= -1;
break;
default:
break;
}
for (at = 0x00; at < text->count; at++) {
char ch = text->data[at];
if ('0' <= ch && '9' >= ch && !decimal) {
value = value * 0x0A + (long)(ch - '0');
} else if ('0' <= ch && '9' >= ch && decimal) {
multiplier = pow(0.1, decimal);
value += (multiplier) * (double)(ch - '0');
decimal++;
} else if ('.' == ch) {
decimal = 1;
}
}
return value;
}
int text_cmp(lvm_p this, text_p text, char *item)
{
size_t size = strlen(item);
(void)this;
return strncmp(text->data, item, size);
}
int text_cmp_text(lvm_p this, text_p text, text_p item)
{
(void)this;
return strncmp(text->data, item->data, text->count);
}
size_t text_hash_fnv_1a(lvm_p this, text_p text)
{
size_t hash = 2166136261U;
unsigned int i = 0;
(void)this;
for (; i < text->count; i++) {
hash ^= (unsigned char)text->data[i];
hash *= 16777619;
}
return hash;
}
size_t text_hash_jenkins(lvm_p this, text_p text)
{
size_t hash = 0;
unsigned char *str = (unsigned char *)text->data;
(void)this;
for (; *str; str++) {
hash += *str;
hash += (hash << 10);
hash ^= (hash >> 6);
}
hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);
return hash;
}
text_p text_display_position(lvm_p this, token_p token, char *text)
{
return text_append(this, text_concat_text(this, text_concat_text(this,
text_make(this, "L:"), text_make_integer(this, token->line)),
text_concat_text(this, text_append(this, text_concat_text(this,
text_make(this, " C:"), text_make_integer(this, token->column)), ' '),
text_make(this, text))), '\n');
}
char *text_str(lvm_p this, text_p text)
{
(void)this;
return strdup(text->data);
}
char text_get(lvm_p this, text_p text, size_t offset)
{
(void)this;
if (offset < text->count) {
return text->data[offset];
} else {
return 0x00;
}
}
void text_free(lvm_p this, gc_p text)
{
(void)this;
free((void *)((text_p)text)->data);
free((void *)((text_p)text));
}
bool error_make(lvm_p this)
{
this->error = (error_p)calloc(1, sizeof(error_t));
this->error->count = 0;
this->error->capacity = 32;
this->error->data = (text_pp)calloc((this->error->capacity), sizeof (text_p));
this->error->type = (error_type *)calloc((this->error->capacity),
sizeof (error_type));
this->error->gc.type = GC_ERROR;
#if GC_ON
this->error->gc.mark = !this->gc.mark;
#else
this->error->gc.mark = this->gc.mark;
#endif
this->error->gc.next = this->gc.first;
this->gc.first = (gc_p)this->error;
this->gc.count++;
return true;
}
text_p error_append(lvm_p this, error_type type, text_p text)
{
if (!this->error) {
this->error = (error_p)calloc(1, sizeof(error_t));
this->error->count = 0;
this->error->capacity = 0;
this->error->gc.type = GC_ERROR;
#if GC_ON
this->error->gc.mark = !this->gc.mark;
#else
this->error->gc.mark = this->gc.mark;
#endif
this->error->gc.next = this->gc.first;
this->gc.first = (gc_p)this->error;
this->gc.count++;
}
if (this->error->count + 1 >= this->error->capacity) {
this->error->capacity += 32;
this->error->data = (text_pp)realloc(this->error->data,
(this->error->capacity) * sizeof (text_p));
this->error->type = (error_type *)realloc(this->error->type,
(this->error->capacity) * sizeof (error_type));
}
this->error->type[this->error->count] = type;
this->error->data[this->error->count++] = text;
return text;
}
text_p error_collapse(lvm_p this)
{
text_p text;
size_t i;
text = text_make(this, "([ERROR]\n");
if (this->error) {
for (i = 0; i < this->error->count; i++) {
switch (this->error->type[i]) {
case ERROR_NONE:
text_concat(this, text, " [OK] ");
break;
case ERROR_READER:
text_concat(this, text, " [READER] ");
break;
case ERROR_RUNTIME:
text_concat(this, text, " [RUNTIME] ");
break;
case ERROR_PRINTER:
text_concat(this, text, " [PRINTER] ");
break;
default:
text_concat(this, text, " [UNKNOWN] ");
}
text_concat_text(this, text, this->error->data[i]);
}
}
text_concat(this, text, ")\n");
return text;
}
bool error_empty(lvm_p this)
{
if (!this->error) {
return true;
}
return 0 == this->error->count;
}
void error_free(lvm_p this, gc_p gc)
{
(void)this;
free((void *)((error_p)gc)->data);
free((void *)((error_p)gc)->type);
free((void *)((error_p)gc));
}
bool comment_make(lvm_p this)
{
this->comment = (comment_p)calloc(1, sizeof(comment_t));
this->comment->count = 0;
this->comment->capacity = 32;
this->comment->data = (text_pp)realloc(this->comment->data,
(this->comment->capacity) * sizeof (text_p));
this->comment->gc.type = GC_COMMENT;
#if GC_ON
this->comment->gc.mark = !this->gc.mark;
#else
this->comment->gc.mark = this->gc.mark;
#endif
this->comment->gc.next = this->gc.first;
this->gc.first = (gc_p)this->comment;
this->gc.count++;
return true;
}
text_p comment_append(lvm_p this, text_p text)
{
if (!this->comment) {
this->comment = (comment_p)calloc(1, sizeof(comment_t));
this->comment->count = 0;
this->comment->capacity = 0;
}
if (this->comment->count + 1 >= this->comment->capacity) {
this->comment->capacity += 32;
this->comment->data = (text_pp)realloc(this->comment->data,
(this->comment->capacity) * sizeof (text_p));
}
this->comment->data[this->comment->count++] = text;
this->comment->data[this->comment->count] = NULL;
return text;
}
text_p comment_collapse(lvm_p this)
{
text_p text;
size_t i;
text = text_make(this, "([comment]\n");
if (this->comment) {
for (i = 0; i < this->comment->count; i++) {
text_concat_text(this, text, this->comment->data[i]);
}
}
text_concat(this, text, ")\n");
return text;
}
void comment_free(lvm_p this, gc_p gc)
{
(void)this;
free((void *)((comment_p)gc)->data);
free((void *)gc);
}
function_p function_make(lvm_p this,
mal_p (*definition)(lvm_p this, mal_p params), text_p name)
{
function_p function = (function_p)calloc(1, sizeof(function_t));
function->definition = definition;
function->name = name;
function->gc.type = GC_FUNCTION;
#if GC_ON
function->gc.mark = !this->gc.mark;
#else
function->gc.mark = this->gc.mark;
#endif
function->gc.next = this->gc.first;
this->gc.first = (gc_p)function;
this->gc.count++;
return function;
}
void function_free(lvm_p this, gc_p gc)
{
(void)this;
free((void *)gc);
}
closure_p closure_make(lvm_p this, env_p env, mal_p parameters,
mal_p definition, mal_p more)
{
closure_p closure = (closure_p)calloc(1, sizeof(closure_t));
closure->env = env;
closure->parameters = parameters;
closure->definition = definition;
closure->more = more;
closure->gc.type = GC_CLOSURE;
#if GC_ON
closure->gc.mark = !this->gc.mark;
#else
closure->gc.mark = this->gc.mark;
#endif
closure->gc.next = this->gc.first;
this->gc.first = (gc_p)closure;
this->gc.count++;
return closure;
}
text_p closure_text(lvm_p this, closure_p closure)
{
text_p mal = text_make(this, "(fn* ");
text_concat_text(this, mal, list_more_text(this, closure->parameters->as.list,
closure->more));
text_append(this, mal, ' ');
text_concat_text(this, mal, mal_print(this, closure->definition, false));
return text_append(this, mal, ')');
}
mal_p closure_parameters(lvm_p this, mal_pp params, mal_pp more)
{
mal_p nil;
list_p list = (*params)->as.list;
list_p args = list_make(this, list->count - 1);
size_t at;
env_get_by_text(this, this->env, text_make(this, "nil: nil"), &nil);
(*more) = nil;
if (!is_list(*params) && is_symbol(*params)) {
args = list_make(this, 1);
list_append(this, args, (*params));
list_append(this, args, nil);
(*params) = mal_list(this, args);
return nil;
} else if (!is_list(*params) && !is_symbol(*params)) {
return mal_error(this, ERROR_RUNTIME, text_concat(this,
text_concat_text(this, text_make(this,
"'fn*' non-symbol in argument list '"),
mal_print(this, *params, false)), "'\n"));
}
for (at = 0; at < list->count - 1; at++) {
mal_p mal = list->data[at];
if (!is_symbol(mal)) {
return mal_error(this, ERROR_RUNTIME, text_concat(this,
text_concat_text(this, text_make(this,
"'fn*' non-symbol in argument list '"), mal_print(this, mal, false)),
"'\n"));
}
if ('&' == mal->as.symbol->data[0]) {
if (0x00 == mal->as.symbol->data[1]) {
if (is_nil(list->data[at + 1])) {
return mal_error(this, ERROR_RUNTIME, text_make(this,
"'fn*' missing symbol after '&' in argument list\n"));
} else if (is_symbol(list->data[at + 1]) &&
is_nil(list->data[at + 2]) && (at + 3) == list->count) {
if (NULL != list_find(this, args, mal)) {
return mal_error(this, ERROR_RUNTIME, text_concat(this,
text_concat_text(this, text_make(this,
"'fn*' more symbol is duplicate symbol in argument list '"),
mal_print(this, mal, false)), "'\n"));
} else {