-
Notifications
You must be signed in to change notification settings - Fork 30
/
snek.h
1098 lines (842 loc) · 20.9 KB
/
snek.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
/*
* Copyright © 2018 Keith Packard <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*/
#pragma once
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdarg.h>
#include <string.h>
#include <math.h>
#include "snek-builtin.h"
// #define DEBUG_MEMORY /* Debug meory allocation */
// #define DEBUG_COMPILE /* Dump code at compile time */
// #define DEBUG_EXEC /* Dump code and values at run time */
#ifdef DEBUG_MEMORY
#define debug_memory(fmt, args...) printf(fmt, ## args)
#else
#define debug_memory(fmt, args...)
#endif
#ifndef SNEK_POOL
#define SNEK_POOL (32 * 1024)
#endif
#define SNEK_ALLOC_SHIFT 2
#define SNEK_ALLOC_ROUND (1 << SNEK_ALLOC_SHIFT)
/*
* Offsets are encoded in floats as NaN values with the sign bit
* set. That means the top 9 bits of the value are all one. There is
* one other value with that value, -inf, which is encoded as negative
* sign, all exponent bits one and a zero mantissa.
*
* This leaves us 23 bits for offsets. The bottom two bits of offsets
* are used as tags; allocations within the heap are rounded to a
* multiple of four bytes so that offsets will always have the bottom
* two bits clear.
*/
#define SNEK_OFFSET_MASK 0x007ffffcu
#define SNEK_EXPONENT_MASK 0xff800000u
#define SNEK_NINF 0xff800000u
#if SNEK_POOL <= 65536
typedef uint16_t snek_offset_t;
typedef int16_t snek_soffset_t;
#define SNEK_OFFSET_NONE 0xfffcu
#define SNEK_SOFFSET_NONE 0x7ffc
#else
typedef uint32_t snek_offset_t;
typedef int32_t snek_soffset_t;
#define SNEK_OFFSET_NONE 0xfffffffcu
#define SNEK_SOFFSET_NONE 0x7ffffffc
#endif
typedef snek_offset_t snek_id_t;
#define SNEK_ID_NONE 0
typedef union {
uint32_t u;
float f;
} snek_poly_t;
typedef enum {
snek_op_eq = 0,
snek_op_ne = 1,
snek_op_gt = 2,
snek_op_lt = 3,
snek_op_ge = 4,
snek_op_le = 5,
snek_op_chain_eq = 6,
snek_op_chain_ne = 7,
snek_op_chain_gt = 8,
snek_op_chain_lt = 9,
snek_op_chain_ge = 10,
snek_op_chain_le = 11,
snek_op_is = 12,
snek_op_is_not = 13,
snek_op_in = 14,
snek_op_not_in = 15,
snek_op_array = 16,
snek_op_plus = 17,
snek_op_minus = 18,
snek_op_times = 19,
snek_op_divide = 20,
snek_op_div = 21,
snek_op_mod = 22,
snek_op_pow = 23,
snek_op_land = 24,
snek_op_lor = 25,
snek_op_lxor = 26,
snek_op_lshift = 27,
snek_op_rshift = 28,
snek_op_assign_plus = 29,
snek_op_assign_minus = 30,
snek_op_assign_times = 31,
snek_op_assign_divide = 32,
snek_op_assign_div = 33,
snek_op_assign_mod = 34,
snek_op_assign_pow = 35,
snek_op_assign_land = 36,
snek_op_assign_lor = 37,
snek_op_assign_lxor = 38,
snek_op_assign_lshift = 39,
snek_op_assign_rshift = 40,
snek_op_assign = 41,
snek_op_assign_named = 42,
snek_op_num,
snek_op_int,
snek_op_string,
snek_op_list,
snek_op_tuple,
#ifndef SNEK_NO_DICT
snek_op_dict,
#endif
snek_op_id,
snek_op_not,
snek_op_uminus,
snek_op_lnot,
snek_op_call,
snek_op_slice,
snek_op_global,
snek_op_del,
snek_op_assert,
snek_op_branch,
snek_op_branch_true,
snek_op_branch_false,
snek_op_forward,
snek_op_range_start,
snek_op_range_step,
snek_op_in_step,
snek_op_return,
snek_op_line,
snek_op_null,
snek_op_nop,
snek_op_push = 0x80,
} __attribute__((packed)) snek_op_t;
typedef enum {
snek_forward_break,
snek_forward_continue,
snek_forward_if,
snek_forward_cmp,
} __attribute__((packed)) snek_forward_t;
typedef enum {
snek_builtin = 0,
snek_list = 1,
snek_string = 2,
snek_func = 3,
snek_float = 4,
} __attribute__((packed)) snek_type_t;
typedef struct snek_mem {
snek_offset_t (*size)(void *addr);
void (*mark)(void *addr);
void (*move)(void *addr);
#ifdef SNEK_MEM_INCLUDE_NAME
char name[16];
#endif
} snek_mem_t;
#ifndef SNEK_MEM_DECLARE
#define SNEK_MEM_DECLARE(n) n
#define SNEK_MEM_SIZE(m) ((m)->size)
#define SNEK_MEM_MARK(m) ((m)->mark)
#define SNEK_MEM_MOVE(m) ((m)->move)
#endif
#ifndef SNEK_MEMS_DECLARE
#define SNEK_MEMS_DECLARE(n) n
#endif
#ifdef SNEK_MEM_INCLUDE_NAME
#define SNEK_MEM_DECLARE_NAME(_name) .name = _name,
#else
#define SNEK_MEM_DECLARE_NAME(_name)
#endif
typedef enum {
snek_list_list,
snek_list_tuple,
#ifndef SNEK_NO_DICT
snek_list_dict
#endif
} __attribute__((packed)) snek_list_type_t;
typedef struct snek_list {
snek_offset_t size;
snek_offset_t alloc;
snek_offset_t note_next_and_type;
snek_offset_t data;
} snek_list_t;
typedef struct snek_code {
snek_offset_t size;
uint8_t code[0];
} snek_code_t;
typedef struct snek_range {
snek_offset_t prev;
snek_id_t id;
float current;
float limit;
float step;
} snek_range_t;
typedef struct snek_in {
snek_offset_t prev;
snek_id_t id;
snek_poly_t array;
snek_offset_t i;
} snek_in_t;
typedef struct snek_func {
uint8_t nformal;
uint8_t nrequired;
snek_offset_t code;
snek_id_t formals[0];
} snek_func_t;
#define SNEK_FUNC_VARARGS SNEK_SOFFSET_NONE
typedef struct snek_name {
snek_offset_t next;
char name[0];
} snek_name_t;
typedef struct snek_variable {
snek_poly_t value;
snek_id_t id;
} snek_variable_t;
typedef struct snek_frame {
snek_offset_t prev;
snek_offset_t code;
snek_offset_t ip;
snek_offset_t nvariables;
snek_variable_t variables[0];
} snek_frame_t;
typedef struct snek_slice {
/* number of outputs */
snek_offset_t count;
/* position of current object */
snek_offset_t pos;
/* stride between inputs */
snek_soffset_t stride;
/* slice is identity */
bool identity;
} snek_slice_t;
#define SNEK_SLICE_DEFAULT SNEK_SOFFSET_NONE /* empty value provided [1:] */
typedef struct snek_builtin {
union {
struct {
int8_t nformal;
union {
snek_poly_t (*funcv)(uint8_t nposition, uint8_t nnamed,
snek_poly_t *args);
snek_poly_t (*func0)(void);
snek_poly_t (*func1)(snek_poly_t a0);
snek_poly_t (*func2)(snek_poly_t a0, snek_poly_t a1);
snek_poly_t (*func3)(snek_poly_t a0, snek_poly_t a1, snek_poly_t a2);
snek_poly_t (*func4)(snek_poly_t a0, snek_poly_t a1, snek_poly_t a2, snek_poly_t a3);
};
};
snek_poly_t value;
};
} snek_builtin_t;
typedef struct snek_buf {
int (*put_c)(int c, void *closure);
int (*put_s)(const char *s, void *closure);
void *closure;
} snek_buf_t;
extern const snek_builtin_t snek_builtins[];
#define SNEK_BUILTIN_FLOAT -2
#define SNEK_BUILTIN_VARARGS -1
#define SNEK_NAN_U 0x7fffffffu
#define SNEK_NAN ((snek_poly_t) { .u = SNEK_NAN_U })
#define SNEK_NULL_U 0xfffffffcu
#define SNEK_NULL ((snek_poly_t) { .u = SNEK_NULL_U })
#define SNEK_GLOBAL_U 0xfffffff8u
#define SNEK_GLOBAL ((snek_poly_t) { .u = SNEK_GLOBAL_U })
#define SNEK_INVALID_U 0xfffffff4u
#define SNEK_INVALID ((snek_poly_t) { .u = SNEK_INVALID_U })
#define SNEK_ZERO ((snek_poly_t) { .f = 0.0f })
#define SNEK_ONE ((snek_poly_t) { .f = 1.0f })
#ifndef SNEK_STACK
#define SNEK_STACK 256
#endif
extern snek_poly_t snek_stack[SNEK_STACK];
extern snek_offset_t snek_stackp;
extern snek_poly_t snek_a;
extern snek_code_t *snek_code;
bool
snek_is_nan(snek_poly_t p);
static inline bool
snek_is_null(snek_poly_t p)
{
return p.u == SNEK_NULL_U;
}
static inline bool
snek_is_invalid(snek_poly_t p)
{
return p.u == SNEK_INVALID_U;
}
static inline bool
snek_is_global(snek_poly_t p)
{
return p.u == SNEK_GLOBAL_U;
}
#ifdef SNEK_DYNAMIC
extern uint8_t *snek_pool __attribute__((aligned(SNEK_ALLOC_ROUND)));
extern uint32_t snek_pool_size;
#else
extern uint8_t snek_pool[SNEK_POOL] __attribute__((aligned(SNEK_ALLOC_ROUND)));
#endif
#ifndef SNEK_CODE_HOOK_START
#define SNEK_CODE_HOOK_START
#endif
#ifndef SNEK_CODE_HOOK_STOP
#define SNEK_CODE_HOOK_STOP
#endif
#include "snek-gram.h"
typedef union {
bool bools;
int16_t _ints;
uint8_t indent;
snek_offset_t line;
snek_op_t op;
snek_offset_t offset;
snek_soffset_t soffset;
snek_id_t id;
float number;
char *string;
} snek_token_val_t;
extern snek_token_val_t snek_token_val;
/* snek-builtin.c */
#define SNEK_BUILTIN_DECLS
#include "snek-builtin.h"
/* snek-code.c */
extern uint8_t *snek_compile;
extern snek_offset_t snek_compile_size;
extern snek_offset_t snek_compile_prev, snek_compile_prev_prev;
#define SNEK_OP_SLICE_START 1
#define SNEK_OP_SLICE_END 2
#define SNEK_OP_SLICE_STRIDE 4
/*
* Construct a temporary variable name to use in 'for' loops. These
* ids are taken from the very top of the id space to avoid
* conflicting with any actual names
*/
static inline snek_id_t
snek_for_tmp(uint8_t for_depth, uint8_t i)
{
return SNEK_OFFSET_NONE - 1 - (for_depth * 2 + i);
}
void
snek_code_delete_prev(void);
void
snek_code_add_op(snek_op_t op);
void
snek_code_add_op_array(snek_op_t op);
void
snek_code_add_number(float number);
void
snek_code_add_string(char *string);
void
snek_code_add_op_offset(snek_op_t op, snek_offset_t offset);
void
snek_code_add_op_uint8(snek_op_t op, uint8_t u8);
static inline void
snek_code_add_op_id(snek_op_t op, snek_id_t id)
{
snek_code_add_op_offset(op, id);
}
static inline void
snek_code_add_forward_op(snek_forward_t forward, snek_op_t op)
{
snek_code_add_op_offset(snek_op_forward,
(snek_offset_t) forward |
((snek_offset_t) op << 8));
}
static inline void
snek_code_add_forward(snek_forward_t forward)
{
snek_code_add_forward_op(forward, snek_op_branch);
}
void
snek_code_add_op_branch(snek_op_t op);
void
snek_code_add_forward(snek_forward_t forward);
void
snek_code_patch_forward(snek_offset_t start, snek_offset_t stop, snek_forward_t forward, snek_offset_t target);
static inline void
snek_code_add_slice(uint8_t param)
{
snek_code_add_op_uint8(snek_op_slice, param);
}
void
snek_code_add_in_range(snek_id_t id, uint8_t nactual, uint8_t for_depth);
void
snek_code_add_in_enum(snek_id_t id, uint8_t for_depth);
void
snek_code_add_line(void);
static inline void
snek_code_patch_branch(snek_offset_t branch, snek_offset_t target)
{
memcpy(snek_compile + branch + 1, &target, sizeof (snek_offset_t));
}
void
snek_code_reset(void);
snek_code_t *
snek_code_finish(void);
snek_offset_t
snek_code_line(snek_code_t *code);
#if defined(DEBUG_COMPILE) || defined(DEBUG_EXEC)
snek_offset_t
snek_code_dump_instruction(snek_code_t *code, snek_offset_t ip);
#endif
extern const snek_mem_t snek_code_mem;
extern const snek_mem_t snek_compile_mem;
/* snek-exec.c */
snek_soffset_t
snek_stack_pop_soffset(void);
float
snek_stack_pop_float(void);
void
snek_stack_push(snek_poly_t p);
snek_poly_t
snek_stack_pop(void);
snek_poly_t
snek_stack_pick(snek_offset_t off);
void
snek_stack_drop(snek_offset_t off);
snek_poly_t
snek_exec(snek_code_t *code);
/* snek-error.c */
#ifndef snek_error_name
#define snek_error_name snek_error
#define snek_error_0_name snek_error_0
#endif
snek_poly_t
snek_error_name(const char *format, ...);
snek_poly_t
snek_error_0_name(const char *string);
snek_poly_t
snek_error_step(void);
snek_poly_t
snek_error_value(snek_poly_t p);
snek_poly_t
snek_error_type_2(snek_poly_t a, snek_poly_t b);
snek_poly_t
snek_error_type_1(snek_poly_t a);
snek_poly_t
snek_error_args(snek_soffset_t want, snek_soffset_t got);
snek_poly_t
snek_error_arg(snek_id_t bad);
snek_poly_t
snek_error_syntax(char *where);
#if SNEK_DEBUG || defined(DEBUG_MEMORY)
void
snek_panic(const char *message);
#endif
#ifndef sprintf_const
#define sprintf_const sprintf
#endif
#ifndef strfromf_const
#define strfromf_const strfromf
#endif
extern bool snek_abort;
/* snek-frame.c */
extern snek_frame_t *snek_globals;
extern snek_frame_t *snek_frame;
void
snek_frame_mark_global(snek_offset_t name);
bool
snek_frame_push(snek_offset_t ip, snek_offset_t nformal);
snek_offset_t
snek_frame_pop(void);
snek_poly_t *
snek_id_ref(snek_id_t id, bool insert);
bool
snek_id_store(snek_id_t id, snek_poly_t value);
bool
snek_id_is_local(snek_id_t id);
bool
snek_id_del(snek_id_t id);
extern const snek_mem_t snek_frame_mem;
/* snek-func.c */
extern snek_code_t *snek_stash_code;
snek_func_t *
snek_func_alloc(snek_code_t *code);
bool
snek_func_push(uint8_t nposition, uint8_t nnamed, snek_offset_t ip);
snek_code_t *
snek_func_pop(snek_offset_t *ip);
snek_offset_t
snek_func_size(void *addr);
void
snek_func_mark(void *addr);
void
snek_func_move(void *addr);
/* snek-lex.c */
extern snek_offset_t snek_line;
extern snek_offset_t snek_lex_line;
#ifndef SNEK_NO_FILE
extern char *snek_file;
#endif
extern uint8_t snek_current_indent;
extern uint8_t snek_lex_indent;
extern uint8_t snek_ignore_nl;
extern bool snek_lex_midline;
extern bool snek_lex_exdent;
extern char snek_lex_text[];
token_t
snek_lex(void);
/* snek-list.c */
extern snek_list_t *snek_empty_tuple;
snek_list_t *
snek_list_resize(snek_list_t *list, snek_offset_t size);
snek_list_t *
snek_list_make(snek_offset_t size, snek_list_type_t type);
snek_list_t *
snek_list_append(snek_list_t *list, snek_list_t *append);
snek_list_t *
snek_list_plus(snek_list_t *a, snek_list_t *b);
snek_list_t *
snek_list_times(snek_list_t *a, snek_soffset_t count);
snek_poly_t *
snek_list_ref(snek_list_t *list, snek_poly_t p, bool report_error);
snek_poly_t
snek_list_get(snek_list_t *list, snek_poly_t p, bool report_error);
snek_poly_t *
snek_list_data(snek_list_t *list);
void
snek_list_del(snek_poly_t lp, snek_poly_t p);
int8_t
snek_list_cmp(snek_list_t *a, snek_list_t *b);
snek_poly_t
snek_list_imm(snek_offset_t size, snek_list_type_t type);
snek_list_t *
snek_list_slice(snek_list_t *list, snek_slice_t *slice);
snek_offset_t
snek_list_size(void *addr);
void
snek_list_mark(void *addr);
void
snek_list_move(void *addr);
#ifdef SNEK_LIST_BUILD
snek_poly_t
snek_list_build(snek_list_type_t type, snek_offset_t size, ...);
#endif
/* snek-memory.c */
#define SNEK_COLLECT_FULL 0
#define SNEK_COLLECT_INCREMENTAL 1
bool
snek_is_pool_addr(const void *addr);
bool
snek_poly_mark(snek_poly_t p);
bool
snek_poly_mark_ref(snek_poly_t *p);
snek_offset_t
snek_collect(uint8_t style);
bool
snek_mark_blob(void *addr, snek_offset_t size);
bool
snek_mark_block_addr(const struct snek_mem *type, void *addr);
bool
snek_mark_addr(const struct snek_mem *type, void *addr);
bool
snek_mark_offset(const struct snek_mem *type, snek_offset_t offset);
bool
snek_move_block_offset(void *ref);
bool
snek_move_block_addr(void **ref);
bool
snek_poly_move(snek_poly_t *ref);
bool
snek_move_addr(const struct snek_mem *type, void **ref);
bool
snek_move_offset(const struct snek_mem *type, snek_offset_t *ref);
void *
snek_alloc(snek_offset_t size);
void
snek_stack_push_string(const char *s);
char *
snek_stack_pop_string(const char *s);
void
snek_stack_push_list(snek_list_t *list);
snek_list_t *
snek_stack_pop_list(void);
void *
snek_pool_addr(snek_offset_t offset);
snek_offset_t
snek_pool_offset(const void *addr);
extern const struct snek_mem SNEK_MEMS_DECLARE(_snek_mems)[];
static inline const struct snek_mem *snek_mems(snek_type_t type)
{
/* Oddly, the compiler complains about this particular array
* operation. However, this lets us declare _snek_mems with
* one fewer entry, without causing any increase in code size
*/
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Warray-bounds"
return &((&_snek_mems[-1])[type]);
#pragma GCC diagnostic pop
}
/* snek-name.c */
snek_id_t
snek_name_id(char *name, bool *keyword);
const char *
snek_name_string(snek_id_t id);
extern const snek_mem_t snek_name_mem;
extern snek_name_t *snek_names;
/* snek-parse.c */
extern bool snek_parse_middle;
extern bool snek_interactive;
#define SNEK_MAX_FORMALS 10
extern uint8_t snek_parse_nformal;
extern uint8_t snek_parse_nnamed;
extern snek_id_t snek_parse_formals[SNEK_MAX_FORMALS];
typedef enum {
snek_parse_success,
snek_parse_error,
} __attribute__((packed)) snek_parse_ret_t;
snek_parse_ret_t
snek_parse(void);
/* snek-poly.c */
void *
snek_ref(snek_poly_t poly);
snek_poly_t
snek_poly_offset(snek_offset_t offset, snek_type_t type);
snek_poly_t
snek_poly(const void *addr, snek_type_t type);
snek_poly_t
snek_float_to_poly(float f);
snek_poly_t
snek_soffset_to_poly(snek_soffset_t s);
snek_type_t
snek_poly_type(snek_poly_t v);
void
snek_poly_print(FILE *file, snek_poly_t poly, char format);
int8_t
snek_poly_cmp(snek_poly_t a, snek_poly_t b, bool is);
bool
snek_poly_true(snek_poly_t a);
snek_offset_t
snek_poly_len(snek_poly_t a);
float
snek_poly_get_float(snek_poly_t a);
snek_soffset_t
snek_poly_get_soffset(snek_poly_t a);
/* snek-print.c */
void
snek_poly_format(snek_buf_t *buf, snek_poly_t a, char format);
void
snek_print(snek_buf_t *buf, snek_poly_t poly);
/* snek-string.c */
snek_poly_t
snek_string_make(char c);
#ifdef SNEK_STRING_BUILD
snek_poly_t
snek_string_build(const char *s);
#endif
snek_poly_t
snek_string_get(char *string, snek_poly_t p, bool report_error);
snek_poly_t
snek_string_cat(char *a, char *b);
snek_poly_t
snek_string_times(char *a, snek_soffset_t b);
char *
snek_string_slice(char *a, snek_slice_t *slice);
snek_poly_t
snek_string_interpolate(char *a, snek_poly_t poly);
snek_offset_t
snek_string_size(void *addr);
void
snek_string_mark_move(void *addr);
/* inlines */
static inline bool
snek_slice_test(snek_slice_t *slice)
{
return slice->count != 0;
}
static inline void
snek_slice_step(snek_slice_t *slice)
{
slice->count--;
slice->pos += slice->stride;
}
static inline snek_poly_t
snek_offset_to_poly(snek_offset_t offset, snek_type_t type)
{
return (snek_poly_t) { .u = SNEK_EXPONENT_MASK | offset | type };
}
static inline snek_offset_t
snek_poly_to_offset(snek_poly_t v)
{
return v.u & SNEK_OFFSET_MASK;
}
static inline float
snek_poly_to_float(snek_poly_t v)
{
return v.f;
}
static inline snek_poly_t
snek_list_to_poly(snek_list_t *list)
{
return snek_poly(list, snek_list);
}
static inline snek_list_t *
snek_poly_to_list(snek_poly_t poly)
{
return snek_ref(poly);
}
static inline snek_poly_t
snek_string_to_poly(char *string)
{
return snek_poly(string, snek_string);
}
static inline char *
snek_poly_to_string(snek_poly_t poly)
{
return snek_ref(poly);
}
static inline snek_func_t *
snek_poly_to_func(snek_poly_t poly)
{
return snek_ref(poly);
}
static inline snek_poly_t
snek_func_to_poly(snek_func_t *func)
{
return snek_poly(func, snek_func);
}
static inline snek_poly_t
snek_builtin_id_to_poly(snek_id_t id)
{
if (id < SNEK_BUILTIN_MAX_FUNC)
return snek_offset_to_poly(id << SNEK_ALLOC_SHIFT, snek_builtin);
return SNEK_BUILTIN_VALUE(&snek_builtins[id-1]);
}
static inline snek_id_t
snek_poly_to_builtin_id(snek_poly_t a)
{
return snek_poly_to_offset(a) >> SNEK_ALLOC_SHIFT;
}
static inline const snek_builtin_t *
snek_poly_to_builtin(snek_poly_t a)
{
return snek_builtins + (snek_poly_to_builtin_id(a) - 1);
}
static inline bool
snek_offset_is_none(snek_offset_t offset)
{
return offset == SNEK_OFFSET_NONE;
}
snek_poly_t
snek_bool_to_poly(bool b);
static inline snek_offset_t
snek_offset_value(snek_offset_t offset)
{
offset = offset & ~3;
return offset;
}
static inline bool
snek_offset_flag_0(snek_offset_t offset)
{
return offset & 1;
}
static inline bool
snek_offset_flag_1(snek_offset_t offset)