-
Notifications
You must be signed in to change notification settings - Fork 0
/
d_lang.h
8283 lines (7288 loc) · 184 KB
/
d_lang.h
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
// see demo/*.dt
// TODO: ... to access func args
// TODO: 'this'
// TODO: trait
// TODO: module
// TODO: threading
// TODO: utf8
// TODO: named func
// TODO: expose tokenizer
// TODO: standalone bytecode
// TODO: type
// TODO: string interpolation
// TODO: single ctx handle
// TODO: tail call opti
// TODO: loop return val
// TODO: switch
// TODO: str intern
// TODO: chained comparison
// TODO: iter with index
// TODO: separate debug print / actual print
// TODO: ordered map ?
// TODO: disallow shadowing global var
// TODO: member diconstructing
// TODO: combine GT/LT and EQ
// TODO: assignment in cond
// TODO: global on root scope?
// TODO: windows fs
// TODO: load dyn lib
// TODO: hot reload
// TODO: separate compiler and vm
// TODO: don't rely on emscripten
// TODO: cli arg for log
// TODO: try func table instead of switch
// TODO: require ! postfix for mut funcs
// TODO: pass global list to run()
// TODO: unpack arr / map in creations
#ifndef D_LANG_H
#define D_LANG_H
#define DT_STACK_MAX 2048
#define DT_ARR_INIT_SIZE 4
#define DT_MAP_INIT_SIZE 8
#define DT_MAP_MAX_LOAD 0.75
#define DT_GC_THRESHOLD 1024 * 1024
#define DT_GC_GROW 10
#define DT_PI 3.14
// #define DT_NO_NANBOX
// #define DT_VM_LOG
// #define DT_GC_LOG
// #define DT_GC_STRESS
#define DT_LIB_SYS
#define DT_LIB_DBG
#define DT_LIB_FS
#define DT_LIB_TERM
#define DT_LIB_HTTP
#if defined(__APPLE__)
#include <TargetConditionals.h>
#if TARGET_OS_OSX
#define DT_MACOS
#elif TARGET_OS_IOS
#define DT_IOS
#endif
#elif defined(__EMSCRIPTEN__)
#define DT_WEB
#elif defined(_WIN32) || defined(_WIN64)
#define DT_WINDOWS
#elif defined(__ANDROID__)
#define DT_ANDROID
#elif defined(__linux__) || defined(__unix__)
#define DT_LINUX
#endif
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <math.h>
#include <time.h>
#include <dirent.h>
#include <ctype.h>
#include <unistd.h>
#include <poll.h>
#include <sys/stat.h>
// value types
typedef enum {
DT_VAL_NIL,
DT_VAL_BOOL,
DT_VAL_NUM,
DT_VAL_STR,
DT_VAL_PSTR,
DT_VAL_ARR,
DT_VAL_MAP,
DT_VAL_RANGE,
DT_VAL_FUNC,
DT_VAL_STRUCT,
DT_VAL_BYTES,
DT_VAL_CFUNC,
DT_VAL_CDATA,
DT_VAL_CPTR,
// TODO
DT_VAL_UPVAL,
// TODO
DT_VAL_LOGIC,
} dt_val_ty;
#define DT_HEAPER_STRUCT \
struct dt_heaper *nxt; \
uint8_t type; \
bool marked;
typedef double dt_num;
typedef bool dt_bool;
struct dt_map;
struct dt_arr;
struct dt_str;
struct dt_vm;
struct dt_logic;
struct dt_func;
struct dt_upval;
struct dt_cdata;
// for easy loops and sub sections in lists
typedef struct {
int16_t start;
int16_t end;
} dt_range;
// packed string
typedef struct {
uint8_t len;
char chars[5];
} dt_pstr;
#ifdef DT_NO_NANBOX
struct dt_val;
typedef struct dt_val (dt_cfunc)(struct dt_vm* vm);
typedef struct dt_val {
dt_val_ty type;
union {
dt_num num;
dt_bool boolean;
dt_range range;
dt_pstr pstr;
struct dt_str* str;
struct dt_arr* arr;
struct dt_map* map;
struct dt_func* func;
struct dt_logic* logic;
dt_cfunc* cfunc;
struct dt_cdata* cdata;
struct dt_cptr* cptr;
struct dt_upval* upval;
} data;
} dt_val;
dt_val DT_NIL = (dt_val) {
.type = DT_VAL_NIL,
.data.num = 0,
};
dt_val DT_TRUE = (dt_val) {
.type = DT_VAL_BOOL,
.data.boolean = true,
};
dt_val DT_FALSE = (dt_val) {
.type = DT_VAL_BOOL,
.data.boolean = false,
};
#else
typedef uint64_t dt_val;
// IEEE 754 "double" bit segments
#define DT_SMASK_SIGN 0x8000000000000000
#define DT_SMASK_EXPO 0x7ff0000000000000
#define DT_SMASK_QUIET 0x0008000000000000
#define DT_SMASK_SIG 0xffff000000000000
#define DT_SMASK_PTR 0x0000ffffffffffff
#define DT_SMASK_RANGE 0x00000000ffffffff
#define DT_SMASK_PSTR 0x0000ffffffffffff
// TODO: don't use 2 tmask for bool
// value type
#define DT_TMASK_NAN 0x0000000000000000
#define DT_TMASK_FALSE 0x0001000000000000
#define DT_TMASK_TRUE 0x0002000000000000
#define DT_TMASK_NIL 0x0003000000000000
#define DT_TMASK_RANGE 0x0004000000000000
#define DT_TMASK_PSTR 0x0005000000000000
#define DT_TMASK_HEAP 0x0006000000000000
#define DT_TMASK_STRUCT 0x0007000000000000
#define DT_TMASK_CFUNC 0x8000000000000000
#define DT_TMASK_CDATA 0x8001000000000000
#define DT_TMASK_CPTR 0x8002000000000000
#define DT_TMASK_LOGIC 0x8007000000000000
// value constants
#define DT_NAN (DT_SMASK_EXPO | DT_SMASK_QUIET)
#define DT_FALSE (DT_NAN | DT_TMASK_FALSE)
#define DT_TRUE (DT_NAN | DT_TMASK_TRUE)
#define DT_NIL (DT_NAN | DT_TMASK_NIL)
// value type signature
#define DT_SIG_NAN DT_NAN
#define DT_SIG_FALSE DT_FALSE
#define DT_SIG_TRUE DT_TRUE
#define DT_SIG_NIL DT_NIL
#define DT_SIG_RANGE (DT_NAN | DT_TMASK_RANGE)
#define DT_SIG_PSTR (DT_NAN | DT_TMASK_PSTR)
#define DT_SIG_HEAP (DT_NAN | DT_TMASK_HEAP)
#define DT_SIG_LOGIC (DT_NAN | DT_TMASK_LOGIC)
#define DT_SIG_CFUNC (DT_NAN | DT_TMASK_CFUNC)
#define DT_SIG_CDATA (DT_NAN | DT_TMASK_CDATA)
#define DT_SIG_CPTR (DT_NAN | DT_TMASK_CPTR)
typedef dt_val (dt_cfunc)(struct dt_vm* vm);
#endif
// Arr<Map<Num>>
// type = DT_VAL_ARR
// idx = 0
// nargs = 1
// TODO
typedef struct dt_type {
dt_val_ty type;
// struct / func idx in compiler
uint16_t idx;
} dt_type;
// struct node {
// bool has_child;
// union {
// struct node child;
// char empty;
// } data;
// };
dt_type DT_TYPE_NIL = (dt_type) {
.type = DT_VAL_NIL,
};
dt_type DT_TYPE_BOOL = (dt_type) {
.type = DT_VAL_BOOL,
};
dt_type DT_TYPE_RANGE = (dt_type) {
.type = DT_VAL_RANGE,
};
dt_type DT_TYPE_STR = (dt_type) {
.type = DT_VAL_STR,
};
dt_type DT_TYPE_NUM = (dt_type) {
.type = DT_VAL_NUM,
};
typedef struct {
dt_type type;
} dt_arr_def;
typedef struct {
dt_type type;
} dt_map_def;
// TODO
typedef struct {
char name[16];
int type;
} dt_struct_mem;
typedef struct {
char name[16];
char mem_names[16][16];
dt_type mems[16];
int num_mems;
} dt_struct_def;
// TODO
typedef struct {
char name[16];
dt_type ret;
dt_type args[8];
int num_args;
} dt_func_def;
typedef struct dt_heaper {
DT_HEAPER_STRUCT
} dt_heaper;
typedef struct dt_cdata {
DT_HEAPER_STRUCT
uint32_t size;
uint8_t data[];
} dt_cdata;
// upvalue (runtime)
// - captured: already allocated to the heap
// - not captured: points to a stack slot, must lift when it gets popped
typedef struct dt_upval {
DT_HEAPER_STRUCT
bool captured;
dt_val* val;
} dt_upval;
// string
typedef struct dt_str {
DT_HEAPER_STRUCT
uint32_t len;
uint32_t hash;
char chars[];
} dt_str;
// array
typedef struct dt_arr {
DT_HEAPER_STRUCT
uint32_t len;
uint32_t cap;
dt_val* values;
} dt_arr;
// hashmap entry
typedef struct {
dt_str* key;
dt_val val;
} dt_map_entry;
// hashmap
typedef struct dt_map {
DT_HEAPER_STRUCT
uint32_t cnt;
uint32_t cap;
dt_map_entry* entries;
} dt_map;
typedef struct {
dt_map* map;
uint32_t idx;
} dt_map_iter;
// TODO: store dt_logics on top level instead of consts
// holding bytecodes and constants
typedef struct {
int cnt;
int cap;
uint8_t* code;
int* lines;
dt_arr* consts;
} dt_chunk;
// function prototype
typedef struct dt_logic {
DT_HEAPER_STRUCT
dt_chunk chunk;
uint16_t nargs;
char* name;
} dt_logic;
// function
typedef struct dt_func {
DT_HEAPER_STRUCT
uint8_t num_upvals;
dt_logic* logic;
dt_upval** upvals;
} dt_func;
typedef struct {
int line;
char* msg;
} dt_err;
// the runtime virtual machine
typedef struct dt_vm {
dt_func* func;
uint8_t* ip;
dt_val stack[DT_STACK_MAX];
dt_val* stack_top;
dt_val* stack_bot;
dt_map* globals;
dt_map* libs;
dt_map* strs;
dt_arr* holds;
dt_upval* open_upvals[UINT8_MAX];
int num_upvals;
dt_heaper* heaper;
size_t mem;
size_t gc_next;
dt_func* trying;
bool throwing;
dt_err err;
} dt_vm;
typedef struct {
char* name;
dt_val val;
} dt_reg;
// op codes
typedef enum {
DT_OP_STOP,
DT_OP_THROW,
DT_OP_CONST,
DT_OP_NIL,
DT_OP_TRUE,
DT_OP_FALSE,
DT_OP_ADD,
DT_OP_SUB,
DT_OP_MUL,
DT_OP_DIV,
DT_OP_NEG,
DT_OP_NOT,
DT_OP_EQ,
DT_OP_GT,
DT_OP_GT_EQ,
DT_OP_LT,
DT_OP_LT_EQ,
DT_OP_OR,
DT_OP_NIL_OR,
DT_OP_AND,
DT_OP_LEN,
DT_OP_POP,
DT_OP_GETG,
DT_OP_SETG,
DT_OP_GETL,
DT_OP_SETL,
DT_OP_GETU,
DT_OP_SETU,
DT_OP_GETI,
DT_OP_SETI,
DT_OP_CALL,
DT_OP_TRY_PREP,
DT_OP_TRY,
DT_OP_MKFUNC,
DT_OP_MKARR,
DT_OP_MKMAP,
DT_OP_JMP,
DT_OP_JMP_COND,
DT_OP_REWIND,
DT_OP_CLOSE,
DT_OP_SPREAD,
DT_OP_ITER_PREP,
DT_OP_ITER,
DT_OP_ARGS,
DT_OP_UNPACK,
} dt_op;
// tokens
typedef enum {
// sym
DT_TOKEN_LPAREN, // (
DT_TOKEN_RPAREN, // )
DT_TOKEN_LBRACE, // {
DT_TOKEN_RBRACE, // }
DT_TOKEN_LBRACKET, // [
DT_TOKEN_RBRACKET, // ]
DT_TOKEN_COMMA, // ,
DT_TOKEN_DOT, // .
DT_TOKEN_PLUS, // +
DT_TOKEN_MINUS, // -
DT_TOKEN_STAR, // *
DT_TOKEN_SLASH, // /
DT_TOKEN_CARET, // ^
DT_TOKEN_GT, // >
DT_TOKEN_GT_GT, // >>
DT_TOKEN_LT, // <
DT_TOKEN_LT_LT, // <<
DT_TOKEN_EQ, // =
DT_TOKEN_BANG, // !
DT_TOKEN_HASH, // #
DT_TOKEN_DOLLAR, // $
DT_TOKEN_DOLLAR_LBRACE, // ${
DT_TOKEN_BACKSLASH, //
DT_TOKEN_PERCENT, // %
DT_TOKEN_TILDE, // ~
DT_TOKEN_COLON, // :
DT_TOKEN_COLON_COLON, // ::
DT_TOKEN_COLON_D, // :D
DT_TOKEN_COLON_LPAREN, // :(
DT_TOKEN_COLON_RPAREN, // :)
DT_TOKEN_QUESTION, // ?
DT_TOKEN_QUESTION_Q, // ??
DT_TOKEN_AND, // &
DT_TOKEN_OR, // |
DT_TOKEN_AT, // @
DT_TOKEN_EQ_EQ, // ==
DT_TOKEN_BANG_EQ, // !=
DT_TOKEN_PLUS_EQ, // +=
DT_TOKEN_PLUS_PLUS, // ++
DT_TOKEN_MINUS_EQ, // -=
DT_TOKEN_MINUS_MINUS, // --
DT_TOKEN_STAR_EQ, // *=
DT_TOKEN_SLASH_EQ, // /=
DT_TOKEN_LT_EQ, // <=
DT_TOKEN_GT_EQ, // >=
DT_TOKEN_DOT_DOT, // ..
DT_TOKEN_DOT_DOT_DOT, // ...
DT_TOKEN_AND_AND, // &&
DT_TOKEN_OR_OR, // ||
DT_TOKEN_TILDE_GT, // ~>
DT_TOKEN_AT_GT, // @>
DT_TOKEN_AT_CARET, // @^
DT_TOKEN_PERCENT_GT, // %>
DT_TOKEN_QUOTE, // "
// lit
DT_TOKEN_IDENT,
DT_TOKEN_STR,
DT_TOKEN_NUM,
// key
DT_TOKEN_N,
DT_TOKEN_T,
DT_TOKEN_F,
// util
DT_TOKEN_ERR,
DT_TOKEN_END,
} dt_token_ty;
typedef enum {
DT_PREC_NONE,
DT_PREC_ASSIGN, // =
DT_PREC_LOGIC, // || &&
DT_PREC_EQ, // == !=
DT_PREC_CMP, // < > <= >=
DT_PREC_TERM, // + -
DT_PREC_FACTOR, // * /
DT_PREC_UNARY, // ! - #
DT_PREC_CALL, // () [] .
DT_PREC_PRIMARY
} dt_prec;
typedef struct {
char* start;
int len;
} dt_fstr;
typedef struct {
dt_token_ty type;
int line;
dt_fstr str;
} dt_token;
typedef struct {
dt_token prev;
dt_token cur;
} dt_parser;
typedef struct {
char* start;
char* cur;
int line;
} dt_scanner;
// compile time local variable representation
// TODO: store type
typedef struct {
dt_fstr name;
bool captured;
int depth;
dt_type type;
} dt_c_local;
// compile time upval representation
typedef struct {
int idx;
bool local;
} dt_c_upval;
// block types
typedef enum {
DT_BLOCK_NORMAL,
DT_BLOCK_LOOP,
DT_BLOCK_COND,
} dt_block_ty;
// compile time loop break point bookkeeping
typedef struct {
int pos;
int depth;
bool cont;
} dt_break;
// compile time function representation
// TODO: compact
typedef struct dt_funcenv {
struct dt_funcenv* parent;
dt_chunk chunk;
int cur_depth;
dt_block_ty scopes[UINT8_MAX];
dt_c_local locals[UINT8_MAX];
int num_locals;
dt_c_upval upvals[UINT8_MAX];
int num_upvals;
dt_break breaks[UINT8_MAX];
int num_breaks;
bool currying;
} dt_funcenv;
typedef struct {
dt_scanner scanner;
dt_parser parser;
dt_funcenv base_env;
dt_funcenv* env;
} dt_compiler;
typedef dt_type (*dt_parse_fn)(dt_compiler* compiler);
typedef dt_type (*dt_parse_fn2)(dt_compiler* compiler, dt_type prev_ty);
typedef struct {
dt_parse_fn prefix;
dt_parse_fn2 infix;
dt_prec prec;
} dt_parse_rule;
// vm creation / destruction
dt_vm dt_vm_new (void);
void dt_vm_free (dt_vm* vm);
// running code
dt_val dt_run (dt_vm* vm, char* code, dt_map* lib);
dt_val dt_runfile (dt_vm* vm, char* path, dt_map* lib);
dt_val dt_load (dt_vm* vm, char* src);
dt_val dt_loadfile (dt_vm* vm, char* path);
// throw runtime error
void dt_throw (dt_vm* vm, char* fmt, ...);
// for getting args in cfunc
int dt_nargs (dt_vm* vm);
bool dt_arg_exists (dt_vm* vm, int idx);
dt_val dt_arg (dt_vm* vm, int idx);
dt_val dt_arg_or (dt_vm* vm, int idx, dt_val v);
dt_num dt_arg_num (dt_vm* vm, int idx);
dt_num dt_arg_num_or (dt_vm* vm, int idx, dt_num n);
dt_bool dt_arg_bool (dt_vm* vm, int idx);
dt_bool dt_arg_bool_or (dt_vm* vm, int idx, dt_bool b);
dt_str* dt_arg_str (dt_vm* vm, int idx);
dt_str* dt_arg_str_or (dt_vm* vm, int idx, dt_str* str);
char* dt_arg_cstr (dt_vm* vm, int idx);
char* dt_arg_cstr_dup (dt_vm* vm, int idx);
char* dt_arg_cstr_or (dt_vm* vm, int idx, char* str);
char* dt_arg_cstr_or_dup (dt_vm* vm, int idx, char* str);
dt_map* dt_arg_map (dt_vm* vm, int idx);
dt_arr* dt_arg_arr (dt_vm* vm, int idx);
dt_func* dt_arg_func (dt_vm* vm, int idx);
bool dt_check_args (dt_vm* vm, int nargs, ...);
// calling a dt_func
dt_val dt_call (dt_vm* vm, dt_val func, int nargs, ...);
// value conversions
dt_val dt_to_num (dt_num n);
dt_val dt_to_bool (dt_bool b);
dt_val dt_to_range (dt_range r);
dt_val dt_to_str (dt_str* str);
dt_val dt_to_arr (dt_arr* arr);
dt_val dt_to_map (dt_map* map);
dt_val dt_to_func (dt_func* func);
dt_val dt_to_cfunc (dt_cfunc* func);
dt_num dt_as_num (dt_vm* vm, dt_val v);
dt_num dt_as_num_or (dt_vm* vm, dt_val v, dt_num n);
dt_bool dt_as_bool (dt_vm* vm, dt_val v);
dt_bool dt_as_bool_or (dt_vm* vm, dt_val v, dt_bool b);
dt_range dt_as_range (dt_vm* vm, dt_val v);
dt_str* dt_as_str (dt_vm* vm, dt_val v);
dt_str* dt_as_str_or (dt_vm* vm, dt_val v, dt_str* s);
dt_arr* dt_as_arr (dt_vm* vm, dt_val v);
dt_arr* dt_as_arr_or (dt_vm* vm, dt_val v, dt_arr* s);
dt_map* dt_as_map (dt_vm* vm, dt_val v);
dt_map* dt_as_map_or (dt_vm* vm, dt_val v, dt_map* s);
dt_func* dt_as_func (dt_vm* vm, dt_val v);
dt_cfunc* dt_as_cfunc (dt_vm* vm, dt_val v);
dt_num dt_as_num2 (dt_val v);
dt_bool dt_as_bool2 (dt_val v);
dt_range dt_as_range2 (dt_val v);
dt_str* dt_as_str2 (dt_val v);
dt_arr* dt_as_arr2 (dt_val v);
dt_map* dt_as_map2 (dt_val v);
dt_func* dt_as_func2 (dt_val v);
dt_cfunc* dt_as_cfunc2 (dt_val v);
// type checking
dt_val_ty dt_typeof (dt_val v);
char* dt_typename (dt_val_ty ty);
bool dt_is_nil (dt_val v);
bool dt_is_num (dt_val v);
bool dt_is_bool (dt_val v);
bool dt_is_str (dt_val v);
bool dt_is_map (dt_val v);
bool dt_is_arr (dt_val v);
bool dt_is_func (dt_val v);
bool dt_is_cfunc (dt_val v);
bool dt_typecheck (dt_vm* vm, dt_val v, dt_val_ty ty);
bool dt_typecheck_opt (dt_vm* vm, dt_val v, dt_val_ty ty);
// misc value related
bool dt_val_eq (dt_val a, dt_val b);
bool dt_val_lt (dt_val a, dt_val b);
dt_val dt_val_clone (dt_vm* vm, dt_val v);
bool dt_val_truthy (dt_val v);
void dt_val_print (dt_val v);
void dt_val_println (dt_val v);
// allocate empty str with len (need to manually fill and hash afterwards or the
// content is undefined)
dt_str* dt_str_alloc (dt_vm* vm, int len);
dt_str* dt_str_fmt (dt_vm* vm, char* fmt, ...);
// create str from c str (until '\0')
dt_str* dt_str_new (dt_vm* vm, char* src);
// create str from c str with length
dt_str* dt_str_new_len (dt_vm* vm, char* src, int len);
// free (you shouldn't call this manually if it's managed by gc)
void dt_str_free (dt_vm* vm, dt_str* str);
// generate str hash
void dt_str_hash (dt_str* str);
// create c str (needs manual memory management)
char* dt_str_cstr (dt_str* str);
bool dt_str_eq (dt_str* a, dt_str* b);
dt_str* dt_str_concat (dt_vm* vm, dt_str* a, dt_str* b);
dt_str* dt_str_replace (dt_vm* vm, dt_str* src, dt_str* from, dt_str* to);
dt_arr* dt_str_chars (dt_vm* vm, dt_str* src);
dt_arr* dt_str_split (dt_vm* vm, dt_str* str, dt_str* sep);
dt_str* dt_str_rep (dt_vm* vm, dt_str* str, int times);
dt_str* dt_str_toupper (dt_vm* vm, dt_str* str);
dt_str* dt_str_tolower (dt_vm* vm, dt_str* str);
int dt_str_find (dt_str* str, dt_str* key);
int dt_str_find_at (dt_str* str, dt_str* key, int idx);
bool dt_str_contains (dt_str* str, dt_str* key);
dt_str* dt_str_rev (dt_vm* vm, dt_str* str);
dt_map* dt_str_match (dt_vm* vm, dt_str* str, dt_str* pat);
dt_str* dt_str_trim (dt_vm* vm, dt_str* str);
dt_str* dt_str_trunc (dt_vm* vm, dt_str* str, int len, dt_str* rest);
dt_str* dt_str_sub (dt_vm* vm, dt_str* str, int a, int b);
dt_str* dt_str_get (dt_vm* vm, dt_str* str, int idx);
int dt_str_cmp (dt_str* a, dt_str* b);
dt_str* dt_str_ser (dt_vm* vm, dt_str* str);
void dt_str_print (dt_str* str);
void dt_str_println (dt_str* str);
// create empty array
dt_arr* dt_arr_new (dt_vm* vm);
// create array with capacity
dt_arr* dt_arr_new_len (dt_vm* vm, int len);
void dt_arr_free (dt_vm* vm, dt_arr* arr);
void dt_arr_free_rec (dt_vm* vm, dt_arr* arr);
dt_val dt_arr_get (dt_arr* arr, int idx);
void dt_arr_set (dt_vm* vm, dt_arr* arr, int idx, dt_val val);
void dt_arr_insert (dt_vm* vm, dt_arr* arr, int idx, dt_val val);
void dt_arr_push (dt_vm* vm, dt_arr* arr, dt_val val);
dt_val dt_arr_rm (dt_arr* arr, int idx);
void dt_arr_rm_all (dt_arr* arr, dt_val v);
int dt_arr_find (dt_arr* arr, dt_val v);
dt_arr* dt_arr_map (dt_vm* vm, dt_arr* arr, dt_val f);
dt_arr* dt_arr_sort (dt_vm* vm, dt_arr* arr, dt_val f);
dt_arr* dt_arr_filter (dt_vm* vm, dt_arr* arr, dt_val f);
void dt_arr_each (dt_vm* vm, dt_arr* arr, dt_val f);
dt_val dt_arr_reduce (dt_vm* vm, dt_arr* arr, dt_val init, dt_val f);
bool dt_arr_contains (dt_arr* arr, dt_val v);
dt_str* dt_arr_join (dt_vm* vm, dt_arr* arr, dt_str* s);
dt_arr* dt_arr_dedup (dt_vm* vm, dt_arr* arr);
dt_arr* dt_arr_rev (dt_vm* vm, dt_arr* arr);
dt_val dt_arr_pop (dt_arr* arr);
dt_val dt_arr_pop (dt_arr* arr);
dt_arr* dt_arr_sub (dt_vm* vm, dt_arr* arr, int start, int end);
dt_arr* dt_arr_concat (dt_vm* vm, dt_arr* a1, dt_arr* a2);
dt_val dt_arr_rand (dt_arr* arr);
dt_arr* dt_arr_clone (dt_vm* vm, dt_arr* src);
bool dt_arr_eq (dt_arr* a1, dt_arr* a2);
dt_str* dt_arr_ser (dt_vm* vm, dt_arr* arr);
void dt_arr_print (dt_arr* arr);
void dt_arr_println (dt_arr* arr);
// create empty hashmap
dt_map* dt_map_new (dt_vm* vm);
// create hashmap with capacity
dt_map* dt_map_new_len (dt_vm* vm, int len);
dt_map* dt_map_new_reg (dt_vm* vm, dt_reg* table);
void dt_map_free (dt_vm* vm, dt_map* map);
void dt_map_free_rec (dt_vm* vm, dt_map* map);
dt_val dt_map_get (dt_map* map, dt_str* key);
void dt_map_set (dt_vm* vm, dt_map* map, dt_str* key, dt_val val);
void dt_map_each (dt_vm* vm, dt_map* map, dt_val f);
// get / set from c str
dt_val dt_map_cget (dt_vm* vm, dt_map* map, char* key);
void dt_map_cset (dt_vm* vm, dt_map* map, char* key, dt_val val);
void dt_map_reg (dt_vm* vm, dt_map* map, dt_reg* table);
bool dt_map_has (dt_map* map, dt_str* key);
bool dt_map_chas (dt_vm* vm, dt_map* map, char* key);
dt_val dt_map_rm (dt_map* map, dt_str* key);
// get an array of keys
dt_arr* dt_map_keys (dt_vm* vm, dt_map* map);
// get an array of values
dt_arr* dt_map_vals (dt_vm* vm, dt_map* map);
dt_map* dt_map_clone (dt_vm* vm, dt_map* src);
bool dt_map_eq (dt_map* a1, dt_map* a2);
dt_str* dt_map_ser (dt_vm* vm, dt_map* map);
void dt_map_print (dt_map* map);
void dt_map_println (dt_map* map);
dt_map_iter dt_map_iter_new (dt_map* map);
dt_map_entry* dt_map_iter_nxt (dt_map_iter* iter);
dt_cdata* dt_cdata_new (dt_vm* vm, void* data, size_t size);
void dt_cdata_free (dt_vm* vm, dt_cdata* cdata);
#endif
#ifdef D_IMPL
#define D_LANG_IMPL
#endif
#ifdef D_LANG_IMPL
#ifndef D_LANG_IMPL_ONCE
#define D_LANG_IMPL_ONCE
// unrecoverable error
void dt_fail(char* fmt, ...) {
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
exit(EXIT_FAILURE);
}
// mem ops with total mem bookkeeping
void* dt_malloc(dt_vm* vm, size_t size) {
if (vm) {
vm->mem += size;
}
return malloc(size);
}
void* dt_realloc(dt_vm* vm, void* ptr, size_t old_size, size_t new_size) {
if (vm) {
vm->mem = vm->mem - old_size + new_size;
}
return realloc(ptr, new_size);
}
void dt_free(dt_vm* vm, void* ptr, size_t size) {
if (vm) {
vm->mem -= size;
}
free(ptr);
}
char* dt_token_name(dt_token_ty ty) {
switch (ty) {
case DT_TOKEN_LPAREN: return "LPAREN";
case DT_TOKEN_RPAREN: return "RPAREN";
case DT_TOKEN_LBRACE: return "LBRACE";
case DT_TOKEN_RBRACE: return "RBRACE";
case DT_TOKEN_LBRACKET: return "LBRACKET";
case DT_TOKEN_RBRACKET: return "RBRACKET";
case DT_TOKEN_COMMA: return "COMMA";
case DT_TOKEN_DOT: return "DOT";
case DT_TOKEN_PLUS: return "PLUS";
case DT_TOKEN_MINUS: return "MINUS";
case DT_TOKEN_STAR: return "STAR";
case DT_TOKEN_SLASH: return "SLASH";
case DT_TOKEN_CARET: return "CARET";
case DT_TOKEN_GT: return "GT";
case DT_TOKEN_GT_GT: return "GT_GT";
case DT_TOKEN_LT: return "LT";
case DT_TOKEN_LT_LT: return "LT_LT";
case DT_TOKEN_EQ: return "EQ";
case DT_TOKEN_BANG: return "BANG";
case DT_TOKEN_HASH: return "HASH";
case DT_TOKEN_DOLLAR: return "DOLLAR";
case DT_TOKEN_DOLLAR_LBRACE: return "DOLLAR_LBRACE";
case DT_TOKEN_BACKSLASH: return "BACKSLASH";
case DT_TOKEN_PERCENT: return "PERCENT";
case DT_TOKEN_TILDE: return "TILDE";
case DT_TOKEN_COLON: return "COLON";
case DT_TOKEN_COLON_COLON: return "COLON_COLON";
case DT_TOKEN_COLON_D: return "COLON_D";
case DT_TOKEN_COLON_LPAREN: return "COLON_LPAREN";
case DT_TOKEN_COLON_RPAREN: return "COLON_RPAREN";
case DT_TOKEN_QUESTION: return "QUESTION";
case DT_TOKEN_QUESTION_Q: return "QUESTION_QUESTION";
case DT_TOKEN_QUOTE: return "QUOTE";
case DT_TOKEN_AND: return "AND";
case DT_TOKEN_OR: return "OR";
case DT_TOKEN_AT: return "AT";
case DT_TOKEN_EQ_EQ: return "EQ_EQ";
case DT_TOKEN_BANG_EQ: return "BANG_EQ";
case DT_TOKEN_PLUS_EQ: return "PLUS_EQ";
case DT_TOKEN_PLUS_PLUS: return "PLUS_PLUS";
case DT_TOKEN_MINUS_EQ: return "MINUS_EQ";
case DT_TOKEN_MINUS_MINUS: return "MINUS_MINUS";
case DT_TOKEN_STAR_EQ: return "STAR_EQ";
case DT_TOKEN_SLASH_EQ: return "SLASH_EQ";
case DT_TOKEN_LT_EQ: return "LESS_EQ";
case DT_TOKEN_GT_EQ: return "GT_EQ";
case DT_TOKEN_DOT_DOT: return "DOT_DOT";
case DT_TOKEN_DOT_DOT_DOT: return "DOT_DOT_DOT";
case DT_TOKEN_AND_AND: return "AND_AND";
case DT_TOKEN_OR_OR: return "OR_OR";
case DT_TOKEN_TILDE_GT: return "TILDE_GT";
case DT_TOKEN_AT_GT: return "AT_GT";
case DT_TOKEN_AT_CARET: return "AT_CARET";
case DT_TOKEN_PERCENT_GT: return "PERCENT_GT";
case DT_TOKEN_IDENT: return "IDENT";
case DT_TOKEN_STR: return "STR";
case DT_TOKEN_NUM: return "NUM";
case DT_TOKEN_N: return "N";
case DT_TOKEN_T: return "T";
case DT_TOKEN_F: return "F";
case DT_TOKEN_ERR: return "ERR";
case DT_TOKEN_END: return "END";
}
}
bool dt_token_eq(dt_token* t1, dt_token* t2) {
if (t1->str.len != t2->str.len) {
return false;
}
return memcmp(t1->str.start, t2->str.start, t1->str.len) == 0;
}
dt_val_ty dt_typeof(dt_val v) {
#ifdef DT_NO_NANBOX
return v.type;
#else
if ((~v & DT_SMASK_EXPO) != 0) {
return DT_VAL_NUM;
}
switch (v & DT_SMASK_SIG) {
case DT_SIG_NAN: return DT_VAL_NUM;
case DT_SIG_FALSE:
case DT_SIG_TRUE: return DT_VAL_BOOL;
case DT_SIG_NIL: return DT_VAL_NIL;
case DT_SIG_RANGE: return DT_VAL_RANGE;
case DT_SIG_PSTR: return DT_VAL_PSTR;
case DT_SIG_LOGIC: return DT_VAL_LOGIC;
case DT_SIG_CFUNC: return DT_VAL_CFUNC;
case DT_SIG_HEAP: return ((dt_heaper*)(v & DT_SMASK_PTR))->type;
}
return DT_VAL_NIL;
#endif
}
bool dt_typecheck(dt_vm* vm, dt_val v, dt_val_ty expected) {
dt_val_ty actual = dt_typeof(v);
if (actual != expected) {
dt_throw(
vm,
"expected a '%s', found '%s'",
dt_typename(expected),
dt_typename(actual)
);
return false;
}
return true;
}
bool dt_typecheck_opt(dt_vm* vm, dt_val v, dt_val_ty expected) {
dt_val_ty actual = dt_typeof(v);
if (actual != expected && actual != DT_VAL_NIL) {
dt_throw(
vm,
"expected a '%s', found '%s'",
dt_typename(expected),
dt_typename(actual)
);
return false;
}
return true;
}
char* dt_typename(dt_val_ty ty) {
switch (ty) {
case DT_VAL_NIL: return "nil";
case DT_VAL_BOOL: return "bool";
case DT_VAL_NUM: return "num";
case DT_VAL_STR: return "str";
case DT_VAL_PSTR: return "str";
case DT_VAL_ARR: return "arr";
case DT_VAL_MAP: return "map";
case DT_VAL_RANGE: return "range";
case DT_VAL_STRUCT: return "struct";
case DT_VAL_FUNC: return "func";
case DT_VAL_BYTES: return "bytes";
case DT_VAL_CFUNC: return "cfunc";