-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsepl.h
More file actions
1122 lines (942 loc) · 28.2 KB
/
sepl.h
File metadata and controls
1122 lines (942 loc) · 28.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* zlib License
*
* (C) 2025 G.Nithesh (SteelSocket)
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
#ifndef SEPL_LIBRARY
#define SEPL_LIBRARY
#ifdef __cplusplus
extern "C" {
#endif
#define sepl__static_assert(cond, name) \
typedef char sepl__assert_##name[(cond) ? 1 : -1]
#define sepl__is_unsigned(type) ((type)(-1) > (type)(0))
#ifndef SEPL_DEF_SIZE
typedef unsigned long sepl_size;
#else
typedef SEPL_DEF_SIZE sepl_size;
sepl__static_assert(sepl__is_unsigned(sepl_size), is_unsigned);
#endif
#ifndef SEPL_NULL
#ifdef __cplusplus
#define SEPL_NULL nullptr
#else
#define SEPL_NULL ((void *)0)
#endif
#endif
#ifndef SEPL_LIB
#define SEPL_LIB extern
#endif
#ifndef SEPL_API
#define SEPL_API static
#endif
typedef enum {
SEPL_TOK_ERROR,
SEPL_TOK_SEMICOLON,
SEPL_TOK_COMMA,
SEPL_TOK_NUM,
SEPL_TOK_VAR,
SEPL_TOK_FUNC,
SEPL_TOK_NONE,
SEPL_TOK_STRING,
SEPL_TOK_ASSIGN,
SEPL_TOK_ADD,
SEPL_TOK_SUB,
SEPL_TOK_MUL,
SEPL_TOK_DIV,
SEPL_TOK_NOT,
SEPL_TOK_AND,
SEPL_TOK_OR,
SEPL_TOK_LT,
SEPL_TOK_LTE,
SEPL_TOK_GT,
SEPL_TOK_GTE,
SEPL_TOK_EQ,
SEPL_TOK_NEQ,
SEPL_TOK_LPAREN,
SEPL_TOK_RPAREN,
SEPL_TOK_LCURLY,
SEPL_TOK_RCURLY,
SEPL_TOK_IDENTIFIER,
SEPL_TOK_RETURN,
SEPL_TOK_IF,
SEPL_TOK_ELSE,
SEPL_TOK_WHILE,
SEPL_TOK_EOF
} SeplTokenT;
typedef struct {
SeplTokenT type;
const char *start;
const char *end;
sepl_size line;
} SeplToken;
typedef struct {
const char *source;
sepl_size line;
SeplToken current;
} SeplLexer;
SEPL_LIB char sepl_is_digit(char c);
SEPL_LIB char sepl_is_alpha(char c);
SEPL_LIB char sepl_is_delim(char c);
SEPL_LIB char sepl_is_identifier(char c);
SEPL_LIB int sepl_to_digit(char c);
SEPL_LIB char sepl_to_special(char c);
SEPL_LIB SeplLexer sepl_lex_init(const char *source);
SEPL_LIB SeplToken sepl_lex_next(SeplLexer *lex);
SEPL_LIB SeplToken sepl_lex_peek(SeplLexer lex);
SEPL_LIB double sepl_lex_num(SeplToken tok);
typedef enum {
SEPL_ERR_OK,
/* Value buffer errors */
SEPL_ERR_VOVERFLOW,
SEPL_ERR_VUNDERFLOW,
/* Bytecode buffer errors */
SEPL_ERR_BOVERFLOW,
/* Compile errors */
SEPL_ERR_UPTOK, /* unexpected token */
SEPL_ERR_SYN, /* generic syntax error */
SEPL_ERR_EEXPR, /* expected expression */
SEPL_ERR_IDEN_NDEF, /* identifier not defined */
SEPL_ERR_IDEN_RDEF, /* identifier redefined */
SEPL_ERR_PREDEF, /* set predefined variable */
SEPL_ERR_CLOSURE, /* closure not supported */
SEPL_ERR_FUNC_UPV, /* set function to upvalue */
/* Runtime Errors */
SEPL_ERR_BC, /* undefined bytecode */
SEPL_ERR_FUNC_RET, /* returning a sepl function */
SEPL_ERR_FUNC_CALL, /* calling a non function */
SEPL_ERR_REFMOVE, /* moving a reference to another variable */
SEPL_ERR_OPER /* invalid operations on value */
} SeplErrorCode;
typedef struct {
#ifndef NDEBUG
sepl_size src_line;
#endif
sepl_size line;
SeplErrorCode code;
union {
/* SEPL_ERR_UPTOK, SEPL_ERR_SYN */
struct {
SeplToken up;
SeplTokenT exp;
} tokd;
/* SEPL_ERR_IDEN_NDEF, SEPL_ERR_IDEN_RDEF */
SeplToken iden;
/* SEPL_ERR_BC */
char bc;
} info;
} SeplError;
#ifndef NDEBUG
#define sepl_err_new(e, ecode) ((e)->code = ecode, (e)->src_line = __LINE__)
#else
#define sepl_err_new(e, ecode) ((e)->code = ecode)
#endif
#define sepl_err_uptok(e, utok, etok) \
(sepl_err_new(e, SEPL_ERR_UPTOK), (e)->info.tokd.up = utok, \
(e)->info.tokd.exp = etok)
#define sepl_err_iden(e, code, i) (sepl_err_new(e, code), (e)->info.iden = i)
typedef struct SeplValue SeplValue;
typedef enum {
SEPL_VAL_NONE,
SEPL_VAL_SCOPE,
SEPL_VAL_NUM,
SEPL_VAL_STR,
SEPL_VAL_FUNC,
SEPL_VAL_CFUNC,
SEPL_VAL_REF,
SEPL_VAL_OBJ
} SeplValueType;
typedef struct {
SeplValue *values;
sepl_size size;
} SeplArgs;
typedef SeplValue (*sepl_c_func)(SeplArgs, SeplError *);
typedef void (*sepl_free_func)(SeplValue);
struct SeplValue {
sepl_size type;
union {
/* SEPL_VAL_SCOPE, SEPL_VAL_FUNC */
sepl_size pos;
/* SEPL_VAL_NUM */
double num;
/* SEPL_VAL_CFUNC */
sepl_c_func cfunc;
/* SEPL_VAL_STR, SEPL_VAL_REF, SEPL_VAL_OBJ */
void *obj;
} as;
};
extern const SeplValue SEPL_NONE;
#define sepl_val_isnone(val) (val.type == SEPL_VAL_NONE)
#define sepl_val_isscp(val) (val.type == SEPL_VAL_SCOPE)
#define sepl_val_isnum(val) (val.type == SEPL_VAL_NUM)
#define sepl_val_isstr(val) (val.type == SEPL_VAL_STR)
#define sepl_val_isfun(val) (val.type == SEPL_VAL_FUNC)
#define sepl_val_iscfun(val) (val.type == SEPL_VAL_CFUNC)
#define sepl_val_isref(val) (val.type == SEPL_VAL_REF)
#define sepl_val_isobj(val) (val.type >= SEPL_VAL_OBJ)
SEPL_LIB SeplValue sepl_val_asref(void *v);
SEPL_LIB SeplValue sepl_val_scope(sepl_size pos);
SEPL_LIB SeplValue sepl_val_number(double vnum);
SEPL_LIB SeplValue sepl_val_str(char *str);
SEPL_LIB SeplValue sepl_val_func(sepl_size pos);
SEPL_LIB SeplValue sepl_val_cfunc(sepl_c_func cfunc);
SEPL_LIB SeplValue sepl_val_object(void *vobj);
SEPL_LIB SeplValue sepl_val_type(void *vobj, sepl_size custom_id);
typedef struct {
const char *key;
SeplValue value;
} SeplValuePair;
typedef struct {
sepl_free_func free;
SeplValuePair *predef;
sepl_size predef_len;
} SeplEnv;
typedef enum {
SEPL_BC_RETURN,
SEPL_BC_JUMPIF,
SEPL_BC_JUMP,
SEPL_BC_CALL,
SEPL_BC_POP,
SEPL_BC_NONE,
SEPL_BC_CONST,
SEPL_BC_STR,
SEPL_BC_SCOPE,
SEPL_BC_FUNC,
SEPL_BC_GET,
SEPL_BC_SET,
SEPL_BC_GET_UP,
SEPL_BC_SET_UP,
SEPL_BC_NEG,
SEPL_BC_ADD,
SEPL_BC_SUB,
SEPL_BC_MUL,
SEPL_BC_DIV,
SEPL_BC_NOT,
SEPL_BC_AND,
SEPL_BC_OR,
SEPL_BC_LT,
SEPL_BC_LTE,
SEPL_BC_GT,
SEPL_BC_GTE,
SEPL_BC_EQ,
SEPL_BC_NEQ
} SeplBC;
typedef struct {
unsigned char *bytes;
sepl_size bpos;
sepl_size bsize;
SeplValue *values;
sepl_size vpos;
sepl_size vsize;
const char **exports;
sepl_size esize;
sepl_size pc;
} SeplModule;
SEPL_LIB SeplModule sepl_mod_new(unsigned char bytes[], sepl_size bsize,
SeplValue values[], sepl_size vsize);
SEPL_LIB sepl_size sepl_mod_bc(SeplModule *mod, SeplBC bc, SeplError *e);
SEPL_LIB sepl_size sepl_mod_bcnum(SeplModule *mod, double n, SeplError *e);
SEPL_LIB sepl_size sepl_mod_bcsize(SeplModule *mod, sepl_size s, SeplError *e);
SEPL_LIB sepl_size sepl_mod_val(SeplModule *mod, SeplValue v, SeplError *e);
SEPL_LIB void sepl_mod_init(SeplModule *mod, SeplError *e, SeplEnv env);
SEPL_LIB void sepl_mod_cleanup(SeplModule *mod, SeplEnv env);
SEPL_LIB SeplValue sepl_mod_step(SeplModule *mod, SeplError *e, SeplEnv env);
SEPL_LIB SeplValue sepl_mod_exec(SeplModule *mod, SeplError *e, SeplEnv env);
SEPL_LIB void sepl_mod_initfunc(SeplModule *mod, SeplError *e, SeplValue func,
SeplArgs args);
SEPL_LIB SeplValue sepl_mod_getexport(SeplModule *mod, SeplEnv env,
const char *key);
#ifdef __cplusplus
}
#endif
#ifdef SEPL_IMPLEMENTATION
SEPL_LIB char sepl_is_digit(char c) { return c >= '0' && c <= '9'; }
SEPL_LIB char sepl_is_alpha(char c) {
return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
}
SEPL_LIB char sepl_is_delim(char c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t';
}
SEPL_LIB char sepl_is_identifier(char c) {
return sepl_is_digit(c) || sepl_is_alpha(c) || c == '_';
}
SEPL_LIB int sepl_to_digit(char c) { return c - '0'; }
SEPL_LIB char sepl_to_special(char c) {
switch (c) {
case 'n':
return '\n';
case 't':
return '\t';
case 'r':
return '\r';
case 'b':
return '\b';
case 'f':
return '\f';
case 'v':
return '\v';
case 'a':
return '\a';
case '\\':
return '\\';
case '\'':
return '\'';
case '\"':
return '\"';
case '?':
return '\?';
case '0':
return '\0';
}
return c;
}
SEPL_API SeplToken sepl__make_tok(SeplLexer *lex, SeplTokenT type) {
SeplToken tok;
tok.type = type;
tok.start = lex->source - 1;
tok.end = lex->source;
return tok;
}
SEPL_API SeplToken sepl__make_number(SeplLexer *lex) {
SeplToken tok;
char dot_found = 0;
tok.type = SEPL_TOK_NUM;
tok.line = lex->line;
tok.start = lex->source - 1;
tok.end = lex->source;
while (1) {
char c = *lex->source++;
if (sepl_is_digit(c)) {
++tok.end;
continue;
}
if (c == '.') {
if (dot_found)
goto invalid_num;
dot_found = 1;
++tok.end;
continue;
}
if (sepl_is_alpha(c))
goto invalid_num;
--lex->source;
break;
}
return tok;
invalid_num:
tok.type = SEPL_TOK_ERROR;
tok.start = "Invalid number";
tok.line = lex->line;
return tok;
}
SEPL_API SeplToken sepl__make_string(SeplLexer *lex) {
SeplToken tok;
tok.type = SEPL_TOK_STRING;
tok.start = lex->source - 1;
tok.end = lex->source;
while (*tok.end != '"' && *tok.end != '\0') {
if (*tok.end++ == '\\') {
tok.end++;
}
}
if (*tok.end == '\0') {
tok.type = SEPL_TOK_ERROR;
tok.start = "Unexpected EOF while parsing string";
tok.line = lex->line;
return tok;
}
lex->source = ++tok.end;
return tok;
}
SEPL_API SeplToken sepl__make_identifier(SeplLexer *lex) {
SeplToken tok;
tok.type = SEPL_TOK_IDENTIFIER;
tok.start = lex->source - 1;
tok.end = lex->source;
while (sepl_is_identifier(*tok.end++));
lex->source = --tok.end;
return tok;
}
SEPL_API SeplToken sepl__make_keyword(SeplLexer *lex, const char *keyword,
SeplTokenT type) {
SeplToken iden = sepl__make_identifier(lex);
const char *start = iden.start;
while (start != iden.end) {
if (*start++ != *keyword++)
return iden;
}
if (*keyword != '\0')
return iden;
iden.type = type;
return iden;
}
SEPL_API SeplToken next_token(SeplLexer *lex) {
char c = *lex->source++;
switch (c) {
case '\n':
++lex->line;
return next_token(lex);
case ' ':
case '\r':
case '\t':
return next_token(lex);
case ';':
return sepl__make_tok(lex, SEPL_TOK_SEMICOLON);
case ',':
return sepl__make_tok(lex, SEPL_TOK_COMMA);
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
return sepl__make_number(lex);
case '=':
if (*lex->source == '=') {
lex->source++;
return sepl__make_tok(lex, SEPL_TOK_EQ);
}
return sepl__make_tok(lex, SEPL_TOK_ASSIGN);
case '+':
return sepl__make_tok(lex, SEPL_TOK_ADD);
case '-':
return sepl__make_tok(lex, SEPL_TOK_SUB);
case '*':
return sepl__make_tok(lex, SEPL_TOK_MUL);
case '/':
return sepl__make_tok(lex, SEPL_TOK_DIV);
case '!':
if (*lex->source == '=') {
lex->source++;
return sepl__make_tok(lex, SEPL_TOK_NEQ);
}
return sepl__make_tok(lex, SEPL_TOK_NOT);
case '&':
if (*lex->source == '&') {
lex->source++;
return sepl__make_tok(lex, SEPL_TOK_AND);
}
break;
case '|':
if (*lex->source == '|') {
lex->source++;
return sepl__make_tok(lex, SEPL_TOK_OR);
}
break;
case '>':
if (*lex->source == '=') {
lex->source++;
return sepl__make_tok(lex, SEPL_TOK_GTE);
}
return sepl__make_tok(lex, SEPL_TOK_GT);
case '<':
if (*lex->source == '=') {
lex->source++;
return sepl__make_tok(lex, SEPL_TOK_LTE);
}
return sepl__make_tok(lex, SEPL_TOK_LT);
case '(':
return sepl__make_tok(lex, SEPL_TOK_LPAREN);
case ')':
return sepl__make_tok(lex, SEPL_TOK_RPAREN);
case '{':
return sepl__make_tok(lex, SEPL_TOK_LCURLY);
case '}':
return sepl__make_tok(lex, SEPL_TOK_RCURLY);
case '@':
return sepl__make_tok(lex, SEPL_TOK_VAR);
case '$':
return sepl__make_tok(lex, SEPL_TOK_FUNC);
case '"':
return sepl__make_string(lex);
case 'e':
return sepl__make_keyword(lex, "else", SEPL_TOK_ELSE);
case 'i':
return sepl__make_keyword(lex, "if", SEPL_TOK_IF);
case 'N':
return sepl__make_keyword(lex, "NONE", SEPL_TOK_NONE);
case 'r':
return sepl__make_keyword(lex, "return", SEPL_TOK_RETURN);
case 'w':
return sepl__make_keyword(lex, "while", SEPL_TOK_WHILE);
case '_':
return sepl__make_identifier(lex);
case '\0': {
lex->source--; /* Ensure lexer always returns EOF after first EOF*/
return sepl__make_tok(lex, SEPL_TOK_EOF);
}
default:
break;
}
if (sepl_is_alpha(c)) {
return sepl__make_identifier(lex);
}
return sepl__make_tok(lex, SEPL_TOK_ERROR);
}
SEPL_LIB SeplLexer sepl_lex_init(const char *source) {
SeplLexer lex = {0};
lex.source = source;
return lex;
}
SEPL_LIB SeplToken sepl_lex_next(SeplLexer *lex) {
SeplToken tok = next_token(lex);
lex->current = tok;
return tok;
}
SEPL_LIB SeplToken sepl_lex_peek(SeplLexer lex) { return next_token(&lex); }
SEPL_LIB double sepl_lex_num(SeplToken tok) {
double result = 0.0;
sepl_size factor = 10;
for (; tok.start != tok.end;) {
char c = *tok.start++;
if (c == '.') {
break;
}
result = result * 10 + sepl_to_digit(c);
}
for (; tok.start != tok.end;) {
char c = *tok.start++;
result = result + ((double)sepl_to_digit(c) / factor);
factor *= 10;
}
return result;
}
const SeplValue SEPL_NONE = {0};
SEPL_LIB SeplValue sepl_val_asref(void *v) {
SeplValue r;
r.type = SEPL_VAL_REF;
r.as.obj = v;
return r;
}
SEPL_LIB SeplValue sepl_val_scope(sepl_size pos) {
SeplValue r;
r.type = SEPL_VAL_SCOPE;
r.as.pos = pos;
return r;
}
SEPL_LIB SeplValue sepl_val_number(double vnum) {
SeplValue r;
r.type = SEPL_VAL_NUM;
r.as.num = vnum;
return r;
}
SEPL_LIB SeplValue sepl_val_str(char *str) {
SeplValue r;
r.type = SEPL_VAL_STR;
r.as.obj = str;
return r;
}
SEPL_LIB SeplValue sepl_val_func(sepl_size pos) {
SeplValue r;
r.type = SEPL_VAL_FUNC;
r.as.pos = pos;
return r;
}
SEPL_LIB SeplValue sepl_val_cfunc(sepl_c_func cfunc) {
SeplValue r;
r.type = SEPL_VAL_CFUNC;
r.as.cfunc = cfunc;
return r;
}
SEPL_LIB SeplValue sepl_val_object(void *vobj) {
return sepl_val_type(vobj, 0);
}
SEPL_LIB SeplValue sepl_val_type(void *vobj, sepl_size custom_id) {
SeplValue r;
r.type = SEPL_VAL_OBJ + custom_id;
r.as.obj = vobj;
return r;
}
SEPL_LIB SeplModule sepl_mod_new(unsigned char bytes[], sepl_size bsize,
SeplValue values[], sepl_size vsize) {
SeplModule mod = {0};
mod.bytes = bytes;
mod.bsize = bsize;
mod.values = values;
mod.vsize = vsize;
return mod;
}
SEPL_LIB sepl_size sepl_mod_bc(SeplModule *mod, SeplBC bc, SeplError *e) {
if (mod->bpos + 1 > mod->bsize) {
sepl_err_new(e, SEPL_ERR_BOVERFLOW);
return 0;
}
mod->bytes[mod->bpos] = bc;
return mod->bpos++;
}
SEPL_LIB sepl_size sepl_mod_bcnum(SeplModule *mod, double n, SeplError *e) {
if (mod->bpos + sizeof(n) > mod->bsize) {
sepl_err_new(e, SEPL_ERR_BOVERFLOW);
return 0;
}
*(double *)(mod->bytes + mod->bpos) = n;
mod->bpos += sizeof(n);
return mod->bpos - sizeof(n);
}
SEPL_LIB sepl_size sepl_mod_bcsize(SeplModule *mod, sepl_size s, SeplError *e) {
if (mod->bpos + sizeof(s) > mod->bsize) {
sepl_err_new(e, SEPL_ERR_BOVERFLOW);
return 0;
}
*(sepl_size *)(mod->bytes + mod->bpos) = s;
mod->bpos += sizeof(s);
return mod->bpos - sizeof(s);
}
SEPL_LIB sepl_size sepl_mod_val(SeplModule *mod, SeplValue v, SeplError *e) {
if (mod->vpos >= mod->vsize) {
sepl_err_new(e, SEPL_ERR_VOVERFLOW);
return 0;
}
mod->values[mod->vpos] = v;
return mod->vpos++;
}
SEPL_API void sepl__free(SeplValue _) { (void)_; }
SEPL_LIB void sepl_mod_init(SeplModule *mod, SeplError *e, SeplEnv env) {
sepl_size i;
mod->vpos = 0;
e->code = SEPL_ERR_OK;
if (env.free == SEPL_NULL) {
env.free = sepl__free;
}
for (i = 0; i < env.predef_len; i++) {
sepl_mod_val(mod, env.predef[i].value, e);
}
for (i = 0; i < mod->esize; i++) {
sepl_mod_val(mod, SEPL_NONE, e);
}
}
SEPL_LIB void sepl_mod_cleanup(SeplModule *mod, SeplEnv env) {
sepl_size i;
for (i = mod->vpos; i-- > env.predef_len;) {
SeplValue v = mod->values[i];
if (sepl_val_isobj(v))
env.free(v);
}
}
SEPL_API double sepl__todbl(SeplError *err, SeplValue v) {
if (sepl_val_isnum(v)) {
return v.as.num;
} else if (sepl_val_isref(v)) {
return sepl__todbl(err, *(SeplValue *)v.as.obj);
}
sepl_err_new(err, SEPL_ERR_OPER);
return 0.0;
}
SEPL_LIB SeplValue sepl_mod_step(SeplModule *mod, SeplError *e, SeplEnv env) {
#define sepl__rddbl() \
(mod->pc += sizeof(double), \
*(double *)(mod->bytes + mod->pc - sizeof(double)))
#define sepl__rdsz() \
(mod->pc += sizeof(sepl_size), \
*(sepl_size *)(mod->bytes + mod->pc - sizeof(sepl_size)))
#define sepl__pushv(val) (sepl_mod_val(mod, val, e))
#define sepl__popv() \
(mod->vpos > env.predef_len \
? mod->values[--mod->vpos] \
: (sepl_err_new(e, SEPL_ERR_VUNDERFLOW), SEPL_NONE))
#define sepl__peekv(offset) (mod->values[mod->vpos - offset - 1])
#define sepl__popd() \
(sepl_val_isobj(sepl__peekv(0)) \
? (env.free(sepl__popv()), sepl__peekv(-1)) \
: sepl__popv())
#define sepl__unaryop(op) \
do { \
SeplValue v = sepl__popv(); \
double d = sepl__todbl(e, v); \
if (e->code) \
break; \
sepl__pushv(sepl_val_number(op d)); \
} while (0)
#define sepl__binaryop(op) \
do { \
SeplValue v2 = sepl__popv(), v1 = sepl__popv(); \
double d1 = sepl__todbl(e, v1), d2 = sepl__todbl(e, v2); \
if (e->code) \
break; \
sepl__pushv(sepl_val_number(d1 op d2)); \
} while (0)
SeplBC bc = (SeplBC)mod->bytes[mod->pc++];
switch (bc) {
case SEPL_BC_RETURN: {
SeplValue retv, v;
e->code = SEPL_ERR_OK;
if (mod->vpos <= env.predef_len)
return SEPL_NONE;
retv = sepl__popv();
if (sepl_val_isfun(retv)) {
sepl_err_new(e, SEPL_ERR_FUNC_RET);
return SEPL_NONE;
}
/* Pop all values until scope block end */
while (1) {
v = sepl__popv();
/* Prevent the return ref object from being freed */
if (sepl_val_isref(retv) &&
retv.as.obj == (mod->values + mod->vpos)) {
retv = v; /* Deference */
continue;
} else if (sepl_val_isobj(v)) {
env.free(v);
} else if (sepl_val_isscp(v)) {
break;
}
}
if (v.as.pos >= mod->bpos) {
mod->pc = mod->bpos;
return retv;
}
sepl__pushv(retv);
mod->pc = v.as.pos;
break;
}
case SEPL_BC_JUMP: {
sepl_size jump = sepl__rdsz();
mod->pc = jump;
break;
}
case SEPL_BC_JUMPIF: {
sepl_size jump = sepl__rdsz();
SeplValue cond = sepl__peekv(0);
if (!cond.as.num) {
mod->pc = jump;
}
sepl__popd();
break;
}
case SEPL_BC_CALL: {
sepl_size offset = sepl__rdsz();
SeplValue v = mod->values[mod->vpos - offset - 1];
if (sepl_val_iscfun(v)) {
SeplArgs args;
SeplValue result;
args.values = mod->values + mod->vpos - offset;
args.size = offset;
result = v.as.cfunc(args, e);
/* Pop arguments */
while (offset--) sepl__popd();
mod->vpos--; /* Pop function variable */
sepl__pushv(result);
} else if (sepl_val_isfun(v)) {
sepl_size param_c;
mod->values[mod->vpos - offset - 1] = sepl_val_scope(mod->pc);
mod->pc = v.as.pos;
param_c = sepl__rdsz();
if (param_c < offset) {
while (param_c++ != offset) sepl__popd();
} else if (param_c > offset) {
while (param_c-- != offset) sepl__pushv(SEPL_NONE);
}
} else {
sepl_err_new(e, SEPL_ERR_FUNC_CALL);
return SEPL_NONE;
}
break;
}
case SEPL_BC_POP: {
sepl__popd();
break;
}
case SEPL_BC_SET: {
sepl_size i = sepl__rdsz();
SeplValue peek = sepl__peekv(0);
sepl_size offset = mod->vpos - i;
/* Reference is assign to another variable */
if (sepl_val_isref(peek)) {
sepl_err_new(e, SEPL_ERR_REFMOVE);
break;
}
if (sepl_val_isobj(mod->values[offset]))
env.free(mod->values[offset]);
mod->values[offset] = sepl__popv();
break;
}
case SEPL_BC_GET: {
sepl_size i = sepl__rdsz();
sepl_size offset = mod->vpos - i;
SeplValue v = mod->values[offset];
if (sepl_val_isobj(v)) {
sepl__pushv(sepl_val_asref(&mod->values[offset]));
break;
}
sepl__pushv(v);
break;
}
case SEPL_BC_SET_UP: {
sepl_size offset = sepl__rdsz();
SeplValue peek = sepl__peekv(0);
/* Reference is assign to another variable */
if (sepl_val_isref(peek)) {
sepl_err_new(e, SEPL_ERR_REFMOVE);
break;
}
if (sepl_val_isobj(mod->values[offset]))
env.free(mod->values[offset]);
mod->values[offset] = sepl__popv();
break;
}
case SEPL_BC_GET_UP: {
sepl_size offset = sepl__rdsz();
SeplValue v = mod->values[offset];
if (sepl_val_isobj(v)) {
sepl__pushv(sepl_val_asref(&mod->values[offset]));
break;
}
sepl__pushv(v);
break;
}
case SEPL_BC_NONE: {
sepl__pushv(SEPL_NONE);
break;
}
case SEPL_BC_CONST: {
double v = sepl__rddbl();
sepl__pushv(sepl_val_number(v));
break;
}
case SEPL_BC_STR: {
sepl_size len = sepl__rdsz();
sepl__pushv(sepl_val_str((char *)(mod->bytes + mod->pc)));
mod->pc += len + 1;
break;
}
case SEPL_BC_SCOPE: {
sepl_size end_pos = sepl__rdsz();
sepl__pushv(sepl_val_scope(end_pos));
break;
}
case SEPL_BC_FUNC: {
sepl_size skip_pos = sepl__rdsz();
sepl__pushv(sepl_val_func(mod->pc));
mod->pc = skip_pos;
break;
}
case SEPL_BC_NEG: {