-
Notifications
You must be signed in to change notification settings - Fork 118
/
my_basic.c
executable file
·19464 lines (16044 loc) · 515 KB
/
my_basic.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
/*
** This source file is part of MY-BASIC
**
** For the latest info, see https://github.com/paladin-t/my_basic/
**
** Copyright (C) 2011 - 2024 Tony Wang
**
** 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.
*/
#ifdef _MSC_VER
# ifndef _CRT_SECURE_NO_WARNINGS
# define _CRT_SECURE_NO_WARNINGS
# endif /* _CRT_SECURE_NO_WARNINGS */
#endif /* _MSC_VER */
#include "my_basic.h"
#if defined ARDUINO && !defined MB_CP_ARDUINO
# define MB_CP_ARDUINO
#endif /* ARDUINO && !MB_CP_ARDUINO */
#ifdef MB_CP_ARDUINO
# ifndef MB_DISABLE_LOAD_FILE
# define MB_DISABLE_LOAD_FILE
# endif /* MB_DISABLE_LOAD_FILE */
# ifndef MB_MANUAL_REAL_FORMATTING
# define MB_MANUAL_REAL_FORMATTING
# endif /* MB_MANUAL_REAL_FORMATTING */
#endif /* MB_CP_ARDUINO */
#ifdef MB_CP_VC
# include <conio.h>
# include <malloc.h>
# include <Windows.h>
#else /* MB_CP_VC */
# include <stdint.h>
#endif /* MB_CP_VC */
#ifndef MB_CP_ARDUINO
# include <memory.h>
#endif /* MB_CP_ARDUINO */
#include <assert.h>
#include <ctype.h>
#include <float.h>
#include <limits.h>
#include <math.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
#ifdef MB_CP_VC
# pragma warning(push)
# pragma warning(disable : 4127)
# pragma warning(disable : 4214)
# pragma warning(disable : 4305)
# pragma warning(disable : 4309)
# pragma warning(disable : 4805)
# pragma warning(disable : 4996)
#endif /* MB_CP_VC */
#ifdef MB_CP_CLANG
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wunused-function"
# pragma clang diagnostic ignored "-Wunused-variable"
#endif /* MB_CP_CLANG */
#ifdef MB_CP_BORLANDC
# pragma warn -8004
# pragma warn -8008
# pragma warn -8012
#endif /* MB_CP_BORLANDC */
#ifdef MB_COMPACT_MODE
# pragma pack(1)
#endif /* MB_COMPACT_MODE */
/*
** {========================================================
** Data type declarations
*/
/** Macros */
/* Version information */
#define MB_VER_MAJOR 1
#define MB_VER_MINOR 2
#define MB_VER_REVISION 2
#define MB_VER_SUFFIX
#define MB_VERSION ((MB_VER_MAJOR * 0x01000000) + (MB_VER_MINOR * 0x00010000) + (MB_VER_REVISION))
#define MB_MAKE_STRINGIZE(A) #A
#define MB_STRINGIZE(A) MB_MAKE_STRINGIZE(A)
#if MB_VER_REVISION == 0
# define MB_VERSION_STRING MB_STRINGIZE(MB_VER_MAJOR.MB_VER_MINOR MB_VER_SUFFIX)
#else /* MB_VER_REVISION == 0 */
# define MB_VERSION_STRING MB_STRINGIZE(MB_VER_MAJOR.MB_VER_MINOR.MB_VER_REVISION MB_VER_SUFFIX)
#endif /* MB_VER_REVISION == 0 */
/* Define as 1 to create hash table nodes lazily, 0 obligingly */
#ifndef _LAZY_HASH_TABLE
# define _LAZY_HASH_TABLE 1
#endif /* _LAZY_HASH_TABLE */
/* Define as 1 to treat warning as error, 0 just leave it */
#ifndef _WARNING_AS_ERROR
# define _WARNING_AS_ERROR 0
#endif /* _WARNING_AS_ERROR */
/* Define as 1 to automatically raise error during popping argument, 0 just return an error result */
#ifndef _SIMPLE_ARG_ERROR
# define _SIMPLE_ARG_ERROR 0
#endif /* _SIMPLE_ARG_ERROR */
/* Define as 1 to use a comma to PRINT a new line, 0 to use a semicolon */
#ifndef _COMMA_AS_NEWLINE
# define _COMMA_AS_NEWLINE 0
#endif /* _COMMA_AS_NEWLINE */
/* Define as 1 to enable multiline statement */
#ifndef _MULTILINE_STATEMENT
# define _MULTILINE_STATEMENT 1
#endif /* _MULTILINE_STATEMENT */
/* Hash table size */
#ifndef _HT_ARRAY_SIZE_DEFAULT
# define _HT_ARRAY_SIZE_TINY 1
# define _HT_ARRAY_SIZE_SMALL 193
# define _HT_ARRAY_SIZE_MID 1543
# define _HT_ARRAY_SIZE_BIG 12289
# define _HT_ARRAY_SIZE_DEFAULT _HT_ARRAY_SIZE_SMALL
#endif /* _HT_ARRAY_SIZE_DEFAULT */
/* Max length of a single symbol */
#ifndef _SINGLE_SYMBOL_MAX_LENGTH
# define _SINGLE_SYMBOL_MAX_LENGTH 128
#endif /* _SINGLE_SYMBOL_MAX_LENGTH */
/* Buffer length of some string operations */
#ifndef _INPUT_MAX_LENGTH
# define _INPUT_MAX_LENGTH 256
#endif /* _INPUT_MAX_LENGTH */
#ifndef _TEMP_FORMAT_MAX_LENGTH
# define _TEMP_FORMAT_MAX_LENGTH 32
#endif /* _TEMP_FORMAT_MAX_LENGTH */
#ifndef _LAMBDA_NAME_MAX_LENGTH
# define _LAMBDA_NAME_MAX_LENGTH 32
#endif /* _LAMBDA_NAME_MAX_LENGTH */
/* Helper */
#ifdef MB_COMPACT_MODE
# define _PACK1 : 1
# define _PACK2 : 2
# define _PACK8 : 8
#else /* MB_COMPACT_MODE */
# define _PACK1
# define _PACK2
# define _PACK8
#endif /* MB_COMPACT_MODE */
#ifndef _UNALIGNED_ARG
# if defined MB_CP_VC && defined MB_OS_WIN64
# ifdef MB_COMPACT_MODE
# define _UNALIGNED_ARG __unaligned
# else /* MB_COMPACT_MODE */
# define _UNALIGNED_ARG
# endif /* MB_COMPACT_MODE */
# else
# define _UNALIGNED_ARG
# endif
#endif /* _UNALIGNED_ARG */
#ifndef sgn
# define sgn(__v) ((__v) ? ((__v) > 0 ? 1 : -1) : 0)
#endif /* sgn */
#ifndef islower
# define islower(__c) ((__c) >= 'a' && (__c) <= 'z')
#endif /* islower */
#ifndef toupper
# define toupper(__c) (islower(__c) ? ((__c) - 'a' + 'A') : (__c))
#endif /* toupper */
#ifndef countof
# define countof(__a) (sizeof(__a) / sizeof(*(__a)))
#endif /* countof */
#ifndef _mb_check_exit
# define _mb_check_exit(__expr, __exit) do { if((__expr) != MB_FUNC_OK) goto __exit; } while(0)
#endif /* _mb_check_exit */
#ifndef _mb_check_mark_exit
# define _mb_check_mark_exit(__expr, __result, __exit) do { __result = (__expr); if(__result != MB_FUNC_OK) goto __exit; } while(0)
#endif /* _mb_check_mark_exit */
/** Collections */
/* Collection functors */
#define _OP_RESULT_NORMAL 0
#define _OP_RESULT_DEL_NODE -1
typedef int (* _common_compare_t)(void*, void*);
typedef int (* _common_operation_t)(void*, void*);
/* List */
typedef _common_compare_t _ls_compare_t;
typedef _common_operation_t _ls_operation_t;
typedef struct _ls_node_t {
void* data;
struct _ls_node_t* prev;
struct _ls_node_t* next;
void* extra;
} _ls_node_t;
/* Dictionary */
typedef unsigned (* _ht_hash_t)(void*, void*);
typedef _common_compare_t _ht_compare_t;
typedef _common_operation_t _ht_operation_t;
typedef struct _ht_node_t {
_ls_operation_t free_extra;
_ht_compare_t compare;
_ht_hash_t hash;
unsigned array_size;
unsigned count;
_ls_node_t** array;
} _ht_node_t;
/** Normal enum/struct/union/const, etc. */
#ifdef MB_ENABLE_FULL_ERROR
/* Error description text */
MBCONST static const char* const _ERR_DESC[] = {
"No error",
/** Common */
"Function already exists",
"Function does not exist",
"Not supported",
/** Parsing */
"Failed to open file",
"Symbol too long",
"Invalid character",
"Invalid module",
"Duplicate IMPORT",
/** Running */
"Empty program",
"Program too long",
"Syntax error",
"Out of memory",
"Overflow",
"Unexpected type",
"Invalid string",
"Integer expected",
"Number expected",
"String expected",
"Variable expected",
"Index out of bound",
"Cannot find with the specific index",
"Too many dimensions",
"Rank out of bound",
"Invalid identifier usage",
"Cannot assign to reserved word",
"Duplicate identifier",
"Incomplete structure",
"Label does not exist",
"No return point",
"Colon expected",
"Comma expected",
"Comma or semicolon expected",
"Open bracket expected",
"Close bracket expected",
"Too many nested",
"Failed to operation",
"Operator expected",
"Assign operator expected",
"THEN statement expected",
"ELSE statement expected",
"ENDIF statement expected",
"TO statement expected",
"NEXT statement expected",
"UNTIL statement expected",
"Loop variable expected",
"Jump label expected",
"Calculation error",
"Invalid expression",
"Divide by zero",
"Reached to wrong function",
"Cannot suspend in a routine",
"Cannot mix instructional and structured sub routines",
"Invalid routine",
"Routine expected",
"Duplicate routine",
"Invalid class",
"Class expected",
"Duplicate class",
"HASH and COMPARE must be provided together",
"Invalid lambda",
"Empty collection",
"List expected",
"Invalid iterator",
"Iterable expected",
"Collection expected",
"Collection or iterator expected",
"Referenced type expected",
/** Extended abort */
"Extended abort"
};
mb_static_assert(countof(_ERR_DESC) == SE_COUNT);
#endif /* MB_ENABLE_FULL_ERROR */
/* Data type */
typedef enum _data_e {
_DT_INVALID = -1,
_DT_NIL = 0,
_DT_UNKNOWN,
_DT_INT,
_DT_REAL,
_DT_STRING,
_DT_TYPE,
_DT_USERTYPE,
#ifdef MB_ENABLE_USERTYPE_REF
_DT_USERTYPE_REF,
#endif /* MB_ENABLE_USERTYPE_REF */
_DT_FUNC,
_DT_VAR,
_DT_ARRAY,
#ifdef MB_ENABLE_COLLECTION_LIB
_DT_LIST,
_DT_LIST_IT,
_DT_DICT,
_DT_DICT_IT,
#endif /* MB_ENABLE_COLLECTION_LIB */
_DT_LABEL, /* Label type, used for GOTO, GOSUB statement */
#ifdef MB_ENABLE_CLASS
_DT_CLASS, /* Object instance */
#endif /* MB_ENABLE_CLASS */
_DT_ROUTINE, /* User defined sub routine in script */
#ifdef MB_ENABLE_LAMBDA
_DT_OUTER_SCOPE,
#endif /* MB_ENABLE_LAMBDA */
_DT_SEP, /* Separator */
#ifdef MB_ENABLE_SOURCE_TRACE
_DT_PREV_IMPORT,
_DT_POST_IMPORT,
#endif /* MB_ENABLE_SOURCE_TRACE */
_DT_EOS /* End of statement */
} _data_e;
#ifdef MB_ENABLE_COLLECTION_LIB
# define _HAS_REF_OBJ_LOCK
#endif /* MB_ENABLE_COLLECTION_LIB */
#ifdef _HAS_REF_OBJ_LOCK
typedef short _lock_t;
#endif /* _HAS_REF_OBJ_LOCK */
struct _ref_t;
typedef void (* _unref_func_t)(struct _ref_t*, void*);
#define _NONE_REF 1
#ifndef _ref_count_t
typedef unsigned _ref_count_t;
#endif /* _ref_count_t */
/* The reference structure should be always at the head of an object */
typedef struct _ref_t {
_ref_count_t* count;
_ref_count_t* weak_count;
_unref_func_t on_unref;
_data_e type _PACK8;
struct mb_interpreter_t* s;
} _ref_t;
typedef struct _gc_t {
_ht_node_t* table;
_ht_node_t* recursive_table;
_ht_node_t* collected_table;
_ht_node_t* valid_table;
unsigned char collecting;
bool_t disabled _PACK1;
} _gc_t;
#ifdef MB_ENABLE_USERTYPE_REF
typedef struct _calculation_operator_info_t {
mb_meta_operator_t is;
mb_meta_operator_t add;
mb_meta_operator_t sub;
mb_meta_operator_t mul;
mb_meta_operator_t div;
mb_meta_operator_t neg;
} _calculation_operator_info_t;
typedef struct _usertype_ref_t {
_ref_t ref;
void* usertype;
mb_dtor_func_t dtor;
mb_clone_func_t clone;
mb_hash_func_t hash;
mb_cmp_func_t cmp;
mb_fmt_func_t fmt;
#ifdef MB_ENABLE_ALIVE_CHECKING_ON_USERTYPE_REF
mb_alive_value_checker_t alive_checker;
#endif /* MB_ENABLE_ALIVE_CHECKING_ON_USERTYPE_REF */
_calculation_operator_info_t* calc_operators;
mb_meta_func_t coll_func;
mb_meta_func_t generic_func;
} _usertype_ref_t;
#endif /* MB_ENABLE_USERTYPE_REF */
typedef struct _func_t {
char* name;
mb_func_t pointer;
} _func_t;
#define _PATHING_NONE 0
#define _PATHING_NORMAL 1
#define _PATHING_UNKNOWN_FOR_NOT_FOUND 2
#define _PATHING_UPVALUE 3
#define _PN(__b) ((!!(__b)) ? (_PATHING_NORMAL) : (_PATHING_NONE))
#define _PU(__b) ((!!(__b)) ? (_PATHING_UNKNOWN_FOR_NOT_FOUND) : (_PATHING_NONE))
typedef struct _var_t {
char* name;
struct _object_t* data;
#ifdef MB_ENABLE_CLASS
unsigned char pathing _PACK2;
bool_t is_me _PACK1;
#endif /* MB_ENABLE_CLASS */
} _var_t;
typedef struct _array_t {
#ifdef MB_ENABLE_ARRAY_REF
_ref_t ref;
#endif /* MB_ENABLE_ARRAY_REF */
char* name;
_data_e type;
#ifndef MB_SIMPLE_ARRAY
_data_e* types;
#endif /* MB_SIMPLE_ARRAY */
void* raw;
unsigned count;
unsigned char dimension_count;
unsigned dimensions[MB_MAX_DIMENSION_COUNT];
} _array_t;
#ifdef MB_ENABLE_COLLECTION_LIB
typedef struct _array_helper_t {
struct mb_interpreter_t* s;
_array_t* array;
int index;
} _array_helper_t;
#endif /* MB_ENABLE_COLLECTION_LIB */
#ifdef MB_ENABLE_COLLECTION_LIB
typedef struct _list_t {
_ref_t ref;
_lock_t lock;
_ls_node_t* list;
_ls_node_t* cached_node;
int cached_index;
int_t count;
int_t* range_begin;
} _list_t;
typedef struct _list_it_t {
_ref_t weak_ref;
_list_t* list;
bool_t locking _PACK1;
union {
_ls_node_t* node;
int_t ranging;
} curr;
} _list_it_t;
typedef struct _dict_t {
_ref_t ref;
_lock_t lock;
_ht_node_t* dict;
} _dict_t;
#define _INVALID_DICT_IT ((_ls_node_t*)(intptr_t)~0)
typedef struct _dict_it_t {
_ref_t weak_ref;
_dict_t* dict;
bool_t locking _PACK1;
unsigned curr_bucket;
_ls_node_t* curr_node;
} _dict_it_t;
typedef struct _keys_helper_t {
mb_value_t* keys;
int size;
int index;
} _keys_helper_t;
#endif /* MB_ENABLE_COLLECTION_LIB */
typedef struct _label_t {
char* name;
_ls_node_t* node;
} _label_t;
#ifdef MB_ENABLE_CLASS
#define _META_LIST_MAX_DEPTH UINT_MAX
#define _CLASS_ME "ME"
#define _CLASS_HASH_FUNC "HASH"
#define _CLASS_COMPARE_FUNC "COMPARE"
#define _CLASS_TO_STRING_FUNC "TO_STRING"
#define _CLASS_OVERRIDE_FMT "_%s"
typedef struct _class_t {
_ref_t ref;
char* name;
struct _class_t* created_from;
_ls_node_t* meta_list;
struct _running_context_t* scope;
struct _routine_t* hash;
struct _routine_t* compare;
void* userdata;
} _class_t;
#endif /* MB_ENABLE_CLASS */
#ifdef MB_ENABLE_LAMBDA
typedef struct _running_context_ref_t {
_ref_t ref;
struct _running_context_ref_t* prev;
struct _running_context_t* scope;
} _running_context_ref_t;
typedef struct _upvalue_scope_tuple_t {
struct mb_interpreter_t* s;
#ifdef MB_ENABLE_CLASS
_class_t* instance;
#endif /* MB_ENABLE_CLASS */
struct _running_context_t* scope;
_running_context_ref_t* outer_scope;
struct _lambda_t* lambda;
_ht_node_t* filled;
} _upvalue_scope_tuple_t;
typedef struct _lambda_t {
_ref_t ref;
struct _running_context_t* scope;
_ls_node_t* parameters;
_running_context_ref_t* outer_scope;
struct _running_context_t* overlapped;
_ht_node_t* upvalues;
_ls_node_t* entry;
_ls_node_t* end;
} _lambda_t;
#endif /* MB_ENABLE_LAMBDA */
typedef struct _routine_t {
union {
struct {
struct _running_context_t* scope;
_ls_node_t* parameters;
_ls_node_t* entry;
} basic;
#ifdef MB_ENABLE_LAMBDA
_lambda_t lambda;
#endif /* MB_ENABLE_LAMBDA */
struct {
mb_routine_func_t entry;
} native;
} func;
char* name;
#ifdef MB_ENABLE_SOURCE_TRACE
char* source_file;
#endif /* MB_ENABLE_SOURCE_TRACE */
#ifdef MB_ENABLE_CLASS
_class_t* instance;
#endif /* MB_ENABLE_CLASS */
bool_t is_cloned _PACK1;
mb_routine_type_e type;
} _routine_t;
#ifdef MB_ENABLE_SOURCE_TRACE
typedef struct _import_info_t {
char* source_file;
} _import_info_t;
#endif /* MB_ENABLE_SOURCE_TRACE */
typedef union _raw_u { mb_data_e e; char c; int_t i; real_t r; void* p; mb_val_bytes_t b; } _raw_u;
typedef unsigned char _raw_t[sizeof(_raw_u)];
typedef struct _object_t {
_data_e type;
union {
int_t integer;
real_t float_point;
char* string;
mb_data_e type;
void* usertype;
#ifdef MB_ENABLE_USERTYPE_REF
_usertype_ref_t* usertype_ref;
#endif /* MB_ENABLE_USERTYPE_REF */
_func_t* func;
_var_t* variable;
_array_t* array;
#ifdef MB_ENABLE_COLLECTION_LIB
_list_t* list;
_list_it_t* list_it;
_dict_t* dict;
_dict_it_t* dict_it;
#endif /* MB_ENABLE_COLLECTION_LIB */
_label_t* label;
#ifdef MB_ENABLE_CLASS
_class_t* instance;
#endif /* MB_ENABLE_CLASS */
_routine_t* routine;
char separator;
#ifdef MB_ENABLE_SOURCE_TRACE
_import_info_t* import_info;
#endif /* MB_ENABLE_SOURCE_TRACE */
mb_val_bytes_t bytes;
void* pointer;
_raw_t raw;
} data;
bool_t is_ref _PACK1;
#ifdef MB_PREFER_SPEED
bool_t is_const _PACK1;
#endif /* MB_PREFER_SPEED */
#ifdef MB_ENABLE_SOURCE_TRACE
int source_pos;
unsigned short source_row;
unsigned short source_col;
#else /* MB_ENABLE_SOURCE_TRACE */
char source_pos _PACK1;
#endif /* MB_ENABLE_SOURCE_TRACE */
} _object_t;
#ifdef MB_ENABLE_MODULE
typedef struct _module_func_t {
char* module;
mb_func_t func;
} _module_func_t;
#endif /* MB_ENABLE_MODULE */
typedef struct _dynamic_buffer_t {
char bytes[_TEMP_FORMAT_MAX_LENGTH];
union {
char* charp;
#if defined MB_CP_VC && defined MB_ENABLE_UNICODE
wchar_t* wcharp;
#endif /* MB_CP_VC && MB_ENABLE_UNICODE */
} pointer;
size_t size;
} _dynamic_buffer_t;
#define _MB_MEM_TAG_SIZE (sizeof(mb_mem_tag_t))
MBAPI size_t MB_SIZEOF_4BYTES = 4;
MBAPI size_t MB_SIZEOF_8BYTES = 8;
MBAPI size_t MB_SIZEOF_32BYTES = 32;
MBAPI size_t MB_SIZEOF_64BYTES = 64;
MBAPI size_t MB_SIZEOF_128BYTES = 128;
MBAPI size_t MB_SIZEOF_256BYTES = 256;
MBAPI size_t MB_SIZEOF_512BYTES = 512;
#ifdef MB_ENABLE_ALLOC_STAT
MBAPI size_t MB_SIZEOF_INT = _MB_MEM_TAG_SIZE + sizeof(int);
MBAPI size_t MB_SIZEOF_PTR = _MB_MEM_TAG_SIZE + sizeof(intptr_t);
MBAPI size_t MB_SIZEOF_LSN = _MB_MEM_TAG_SIZE + sizeof(_ls_node_t);
MBAPI size_t MB_SIZEOF_HTN = _MB_MEM_TAG_SIZE + sizeof(_ht_node_t);
MBAPI size_t MB_SIZEOF_HTA = _MB_MEM_TAG_SIZE + sizeof(_ht_node_t*) * _HT_ARRAY_SIZE_DEFAULT;
MBAPI size_t MB_SIZEOF_OBJ = _MB_MEM_TAG_SIZE + sizeof(_object_t);
#ifdef MB_ENABLE_USERTYPE_REF
MBAPI size_t MB_SIZEOF_UTR = _MB_MEM_TAG_SIZE + sizeof(_usertype_ref_t);
#endif /* MB_ENABLE_USERTYPE_REF */
MBAPI size_t MB_SIZEOF_FUN = _MB_MEM_TAG_SIZE + sizeof(_func_t);
MBAPI size_t MB_SIZEOF_VAR = _MB_MEM_TAG_SIZE + sizeof(_var_t);
MBAPI size_t MB_SIZEOF_ARR = _MB_MEM_TAG_SIZE + sizeof(_array_t);
#ifdef MB_ENABLE_COLLECTION_LIB
MBAPI size_t MB_SIZEOF_LST = _MB_MEM_TAG_SIZE + sizeof(_list_t);
MBAPI size_t MB_SIZEOF_DCT = _MB_MEM_TAG_SIZE + sizeof(_dict_t);
#endif /* MB_ENABLE_COLLECTION_LIB */
MBAPI size_t MB_SIZEOF_LBL = _MB_MEM_TAG_SIZE + sizeof(_label_t);
#ifdef MB_ENABLE_CLASS
MBAPI size_t MB_SIZEOF_CLS = _MB_MEM_TAG_SIZE + sizeof(_class_t);
#endif /* MB_ENABLE_CLASS */
MBAPI size_t MB_SIZEOF_RTN = _MB_MEM_TAG_SIZE + sizeof(_routine_t);
#else /* MB_ENABLE_ALLOC_STAT */
MBAPI size_t MB_SIZEOF_INT = sizeof(int);
MBAPI size_t MB_SIZEOF_PTR = sizeof(intptr_t);
MBAPI size_t MB_SIZEOF_LSN = sizeof(_ls_node_t);
MBAPI size_t MB_SIZEOF_HTN = sizeof(_ht_node_t);
MBAPI size_t MB_SIZEOF_HTA = sizeof(_ht_node_t*) * _HT_ARRAY_SIZE_DEFAULT;
MBAPI size_t MB_SIZEOF_OBJ = sizeof(_object_t);
#ifdef MB_ENABLE_USERTYPE_REF
MBAPI size_t MB_SIZEOF_UTR = sizeof(_usertype_ref_t);
#endif /* MB_ENABLE_USERTYPE_REF */
MBAPI size_t MB_SIZEOF_FUN = sizeof(_func_t);
MBAPI size_t MB_SIZEOF_VAR = sizeof(_var_t);
MBAPI size_t MB_SIZEOF_ARR = sizeof(_array_t);
#ifdef MB_ENABLE_COLLECTION_LIB
MBAPI size_t MB_SIZEOF_LST = sizeof(_list_t);
MBAPI size_t MB_SIZEOF_DCT = sizeof(_dict_t);
#endif /* MB_ENABLE_COLLECTION_LIB */
MBAPI size_t MB_SIZEOF_LBL = sizeof(_label_t);
#ifdef MB_ENABLE_CLASS
MBAPI size_t MB_SIZEOF_CLS = sizeof(_class_t);
#endif /* MB_ENABLE_CLASS */
MBAPI size_t MB_SIZEOF_RTN = sizeof(_routine_t);
#endif /* MB_ENABLE_ALLOC_STAT */
#ifndef _CONST_PART1
# ifdef MB_PREFER_SPEED
# define _CONST_PART1 false, false,
# else /* MB_PREFER_SPEED */
# define _CONST_PART1 false,
# endif /* MB_PREFER_SPEED */
#endif /* _CONST_PART1 */
#ifndef _CONST_PART2
# ifdef MB_ENABLE_SOURCE_TRACE
# define _CONST_PART2 0, 0, 0
# else /* MB_ENABLE_SOURCE_TRACE */
# define _CONST_PART2 0
# endif /* MB_ENABLE_SOURCE_TRACE */
#endif /* _CONST_PART2 */
#ifndef _CONST_TAIL
# define _CONST_TAIL _CONST_PART1 _CONST_PART2
#endif /* _CONST_TAIL */
MBCONST static const _object_t _OBJ_INT_UNIT = { _DT_INT, (int_t)1, _CONST_TAIL };
MBCONST static const _object_t _OBJ_INT_ZERO = { _DT_INT, (int_t)0, _CONST_TAIL };
#define _MAKE_NIL(__o) do { memset((__o), 0, sizeof(_object_t)); (__o)->type = _DT_NIL; } while(0)
static _object_t* _OBJ_BOOL_TRUE = 0;
static _object_t* _OBJ_BOOL_FALSE = 0;
#ifdef MB_ENABLE_CLASS
MBCONST static const _object_t _OBJ_UNKNOWN = { _DT_UNKNOWN, (int_t)0, _CONST_TAIL };
MBCONST static const _ls_node_t _LS_NODE_UNKNOWN = { (void*)&_OBJ_UNKNOWN, 0, 0, 0 };
#endif /* MB_ENABLE_CLASS */
#define _VAR_ARGS_STR "..."
#define _IS_VAR_ARGS(__v) ((__v) == &_VAR_ARGS)
#ifdef MB_ENABLE_CLASS
MBCONST static const _var_t _VAR_ARGS = { _VAR_ARGS_STR, 0, 0, 0 };
#else /* MB_ENABLE_CLASS */
MBCONST static const _var_t _VAR_ARGS = { _VAR_ARGS_STR, 0 };
#endif /* MB_ENABLE_CLASS */
/* Parsing context */
#define _CLASS_STATE_NONE 0
#define _CLASS_STATE_PROC 1
MBCONST static const char _MULTI_LINE_COMMENT_PREFIX[] = "'[";
MBCONST static const char _MULTI_LINE_COMMENT_POSTFIX[] = "']";
MBCONST static const char _UNICODE_OPEN_QUOTE[] = { 0xe2, 0x80, 0x9c };
MBCONST static const char _UNICODE_CLOSE_QUOTE[] = { 0xe2, 0x80, 0x9d };
typedef enum _parsing_state_e {
_PS_NORMAL,
_PS_STRING,
_PS_COMMENT,
_PS_MULTI_LINE_COMMENT
} _parsing_state_e;
typedef enum _symbol_state_e {
_SS_IDENTIFIER,
_SS_OPERATOR
} _symbol_state_e;
#define _ROUTINE_STATE_NONE 0
#define _ROUTINE_STATE_DEF 1
#define _ROUTINE_STATE_PARAMS 2
typedef struct _parsing_context_t {
_ls_node_t* imported;
char current_char;
char current_symbol[_SINGLE_SYMBOL_MAX_LENGTH + 1];
int current_symbol_nonius;
int current_symbol_contains_accessor;
_object_t* last_symbol;
int multi_line_comment_count;
_parsing_state_e parsing_state;
_symbol_state_e symbol_state;
#ifdef MB_ENABLE_CLASS
unsigned short class_state;
#endif /* MB_ENABLE_CLASS */
unsigned short routine_state;
unsigned short routine_params_state;
int parsing_pos;
unsigned short parsing_row;
unsigned short parsing_col;
} _parsing_context_t;
/* Running context */
#define _SCOPE_META_ROOT (1 << 0)
#define _SCOPE_META_REF (1 << 1)
#define _INFINITY_CALC_DEPTH -1
typedef struct _running_context_t {
struct _running_context_t* prev;
unsigned meta;
_ht_node_t* var_dict;
struct _running_context_t* ref;
_var_t* next_loop_var;
mb_value_t intermediate_value;
int calc_depth;
#ifdef MB_ENABLE_LAMBDA
_ls_node_t* refered_lambdas;
#endif /* MB_ENABLE_LAMBDA */
} _running_context_t;
/* Expression processing utilities */
typedef struct _tuple3_t {
void* e1;
void* e2;
void* e3;
} _tuple3_t;
/* Interpreter tag */
#define _JMP_NIL 0x00
#define _JMP_INS 0x01
#define _JMP_STR 0x02
#define _NO_EAT_COMMA 2
typedef struct mb_interpreter_t {
/** Fundamental */
#ifdef MB_ENABLE_FORK
struct mb_interpreter_t* forked_from;
_running_context_t* forked_context;
_ls_node_t* all_forked;
#endif /* MB_ENABLE_FORK */
bool_t valid _PACK1;
void* userdata;
_ls_node_t* ast;
/** Memory management */
_gc_t gc;
_ls_node_t* edge_destroy_objects;
_ls_node_t* lazy_destroy_objects;
/** Scripting interface and module */
_ht_node_t* local_func_dict;
_ht_node_t* global_func_dict;
#ifdef MB_ENABLE_MODULE
_ht_node_t* module_func_dict;
char* with_module;
_ls_node_t* using_modules;
#endif /* MB_ENABLE_MODULE */
/** Parsing and interpreting */
char* source_file;
_parsing_context_t* parsing_context;
_running_context_t* running_context;
int run_count;
bool_t has_run _PACK1;
_ls_node_t* var_args;
#ifdef MB_ENABLE_USERTYPE_REF
_object_t* usertype_ref_ahead;
#endif /* MB_ENABLE_USERTYPE_REF */
unsigned char jump_set;
#ifdef MB_ENABLE_CLASS
_class_t* last_instance;
bool_t calling _PACK1;
#endif /* MB_ENABLE_CLASS */
_routine_t* last_routine;
_ls_node_t* sub_stack;
_ls_node_t* suspent_point;
int schedule_suspend_tag;
int_t no_eat_comma_mark;
_ls_node_t* skip_to_eoi;
_ls_node_t* in_neg_expr;
#ifdef MB_ENABLE_STACK_TRACE
_ls_node_t* stack_frames;
#endif /* MB_ENABLE_STACK_TRACE */
#if _MULTILINE_STATEMENT
_ls_node_t* multiline_enabled;
#endif /* _MULTILINE_STATEMENT */
/** Error handling */
bool_t handled_error _PACK1;
mb_error_e last_error;
char* last_error_file;
int last_error_pos;
unsigned short last_error_row;
unsigned short last_error_col;
/** Handlers */
mb_alive_checker_t alive_check_handler;
mb_debug_stepped_handler_t debug_prev_stepped_handler;
mb_debug_stepped_handler_t debug_post_stepped_handler;
mb_error_handler_t error_handler;
mb_print_func_t printer;
mb_input_func_t inputer;
mb_import_handler_t import_handler;
} mb_interpreter_t;
/* Operations */
MBCONST static const char _PRECEDE_TABLE[20][20] = { /* Operator priority table */
/* + - * / MOD ^ ( ) = > < >= <= == <> AND OR NOT NEG IS */
{ '>', '>', '<', '<', '<', '<', '<', '>', '>', '>', '>', '>', '>', '>', '>', '>', '>', '>', '>', '>' }, /* + */
{ '>', '>', '<', '<', '<', '<', '<', '>', '>', '>', '>', '>', '>', '>', '>', '>', '>', '>', '>', '>' }, /* - */
{ '>', '>', '>', '>', '>', '<', '<', '>', '>', '>', '>', '>', '>', '>', '>', '>', '>', '>', '>', '>' }, /* * */
{ '>', '>', '>', '>', '>', '<', '<', '>', '>', '>', '>', '>', '>', '>', '>', '>', '>', '>', '>', '>' }, /* / */
{ '>', '>', '<', '<', '>', '<', '<', '>', '>', '>', '>', '>', '>', '>', '>', '>', '>', '>', '>', '>' }, /* MOD */
{ '>', '>', '>', '>', '>', '>', '<', '>', '>', '>', '>', '>', '>', '>', '>', '>', '>', '>', '>', '>' }, /* ^ */
{ '<', '<', '<', '<', '<', '<', '<', '=', ' ', '<', '<', '<', '<', '<', '<', '<', '<', '<', '<', '<' }, /* ( */
{ '>', '>', '>', '>', '>', '>', ' ', '>', '>', '>', '>', '>', '>', '>', '>', '>', '>', '>', '>', '>' }, /* ) */
{ '<', '<', '<', '<', '<', '<', '<', ' ', '=', '<', '<', '<', '<', '<', '<', '<', '<', '<', '<', '<' }, /* = */
{ '<', '<', '<', '<', '<', '<', '<', '>', '>', ' ', ' ', ' ', ' ', ' ', ' ', '>', '>', '>', '>', '>' }, /* > */
{ '<', '<', '<', '<', '<', '<', '<', '>', '>', ' ', ' ', ' ', ' ', ' ', ' ', '>', '>', '>', '>', '>' }, /* < */
{ '<', '<', '<', '<', '<', '<', '<', '>', '>', ' ', ' ', ' ', ' ', ' ', ' ', '>', '>', '>', '>', '>' }, /* >= */
{ '<', '<', '<', '<', '<', '<', '<', '>', '>', ' ', ' ', ' ', ' ', ' ', ' ', '>', '>', '>', '>', '>' }, /* <= */
{ '<', '<', '<', '<', '<', '<', '<', '>', '>', ' ', ' ', ' ', ' ', ' ', ' ', '>', '>', '>', '>', '>' }, /* == */
{ '<', '<', '<', '<', '<', '<', '<', '>', '>', ' ', ' ', ' ', ' ', ' ', ' ', '>', '>', '>', '>', '>' }, /* <> */
{ '<', '<', '<', '<', '<', '<', '<', '>', '>', '<', '<', '<', '<', '<', '<', '>', '>', '<', '>', '>' }, /* AND */
{ '<', '<', '<', '<', '<', '<', '<', '>', '>', '<', '<', '<', '<', '<', '<', '>', '>', '<', '>', '>' }, /* OR */
{ '<', '<', '<', '<', '<', '<', '<', '>', '>', '<', '<', '<', '<', '<', '<', '>', '>', '>', '>', '>' }, /* NOT */
{ '<', '<', '<', '<', '<', '<', '<', '>', '>', '<', '<', '<', '<', '<', '<', '<', '<', '<', '=', '<' }, /* NEG */
{ '<', '<', '<', '<', '<', '<', '<', '>', '>', '<', '<', '<', '<', '<', '<', '>', '>', '<', '>', '>' } /* IS */
};
static _object_t* _exp_assign = 0;
#define _copy_bytes(__l, __r) do { memcpy((__l), (__r), sizeof(mb_val_bytes_t)); } while(0)
#define _set_real_with_hex(__r, __i) \
do { \
if(sizeof(__r) == sizeof(unsigned char)) { \
union { unsigned char i; real_t r; } __u; \
__u.i = __i; \
__r = __u.r; \
} else if(sizeof(__r) == sizeof(unsigned short)) { \
union { unsigned short i; real_t r; } __u; \
__u.i = __i; \
__r = __u.r; \
} else if(sizeof(__r) == sizeof(unsigned)) { \
union { unsigned i; real_t r; } __u; \
__u.i = __i; \
__r = __u.r; \
} else if(sizeof(__r) == sizeof(unsigned long)) { \
union { unsigned long i; real_t r; } __u; \
__u.i = __i; \
__r = __u.r; \
} else if(sizeof(__r) == sizeof(unsigned long long)) { \
union { unsigned long long i; real_t r; } __u; \
__u.i = __i; \
__r = __u.r; \
} else { \
mb_assert(0 && "Invalid real number precision."); \
} \
} while(0)
#if MB_CONVERT_TO_INT_LEVEL == MB_CONVERT_TO_INT_LEVEL_NONE
# define _convert_to_int_if_posible(__o) do { ((void)(__o)); } while(0)
#else /* MB_CONVERT_TO_INT_LEVEL == MB_CONVERT_TO_INT_LEVEL_NONE */
# define _convert_to_int_if_posible(__o) \
do { \
if((__o)->type == _DT_REAL && (real_t)(int_t)(__o)->data.float_point == (__o)->data.float_point) { \
(__o)->type = _DT_INT; \
(__o)->data.integer = (int_t)(__o)->data.float_point; \
} \
} while(0)
#endif /* MB_CONVERT_TO_INT_LEVEL == MB_CONVERT_TO_INT_LEVEL_NONE */
#define _instruct_head(__tuple) \
_object_t opndv1; \
_object_t opndv2; \
_tuple3_t* tpptr = (_tuple3_t*)(*__tuple); \
_object_t* opnd1 = (_object_t*)(tpptr->e1); \
_object_t* opnd2 = (_object_t*)(tpptr->e2); \
_object_t* val = (_object_t*)(tpptr->e3);
#define _instruct_common(__tuple) \
_instruct_head(__tuple) \
opndv1.type = (opnd1->type == _DT_INT || (opnd1->type == _DT_VAR && opnd1->data.variable->data->type == _DT_INT)) ? \
_DT_INT : _DT_REAL; \
opndv1.data = opnd1->type == _DT_VAR ? opnd1->data.variable->data->data : opnd1->data; \
opndv2.type = (opnd2->type == _DT_INT || (opnd2->type == _DT_VAR && opnd2->data.variable->data->type == _DT_INT)) ? \
_DT_INT : _DT_REAL; \
opndv2.data = opnd2->type == _DT_VAR ? opnd2->data.variable->data->data : opnd2->data;
#define _instruct_fun_num_num(__optr, __tuple) \
do { \
_instruct_common(__tuple) \