-
Notifications
You must be signed in to change notification settings - Fork 0
/
boing.h
9225 lines (7625 loc) · 264 KB
/
boing.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
/*
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
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 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.
For more information, please refer to <http://unlicense.org/>
*/
#ifndef BOING_H__
#define BOING_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <setjmp.h>
#include <ctype.h>
#include <limits.h>
#include <stdarg.h>
#include <math.h>
/*
syntax:
if not enclosed in () after an operation, the next operations,
depending on the amount by default it requests, will be passed
example: `operation`(`default number of arguments`)
p"im a literal"
p("multiple ""things, "s'100')
notes:
- everything is passed by reference. It is possible to copy explicitly
-
- MUST wrap operations not wanted divided into implicit arguments with ()
value evolutions:
- associative arrays (tables):
[[key][value]...]
- stacks:
[above_stack_reference identifier_table return_buf]
- programs:
[ast root_identifier_table]
non-operation syntax:
# - parser ignores everything until the next \n
"" - string literal. Follows C style escapes.
'' - number literal. Can do 0x prefix, -,+, and decimal.
0-9 - number literal.
A-Z_ - signals to the parser that this is a multi-char identifier
() - this just evaluates the operations inside immediately and returns. This
- runs in the same scope as the thing it surrounds.
{} - prevents the operations inside from being executed. Just returns the AST.
[] - sets the following expressions to be the values of an array. Examples:
-
- ["h""e""l""l""o"['23''0.23']]
-
- Note: operations will be evaluated at runtime, but anything enclosed in
- {} list brackets will not. Can be evaluated at any time later
-
- p*["hello,"r('0')]" "
-
- outputs "hello, whatever was input until newline"
-
- ["test"{}]
operations (#_of_implicit_args,explicit&implicit(e) OR implicit ONLY(i)):
p(1e)- print value passed to stdout. Note, arrays are handled as strings
- and printed as an array of chars and individial values are numbers. If
- it is necessary to print a double, use the following operation
s(1e)- turns values into an ascii string array, usually formatted how they would
- appear as literals. If the second argument is provided, and a 1 or 0, the
- output is either minified or a more readable form is created.
r(0e)- returns a value read from stdin, single character if no value passed.
- if '0', then it will return everything until an EOF. >0 just blocks
- until the input amount reaches the limit passed. If an array is passed,
- it is tested as a string and will use the first character to read until
+(1e)- adds values or makes positive. If 1 double, it just makes it positive.
- If 1 array, all values inside will be added together and their sum returned.
- If 2+ doubles, its normal addition. If its any mix of
- arrays and doubles, it will just concatenate them all into a single array
-(1e)- subtracts values or makes negative if its 1 double. TBD if 1 array
- If 2+ doubles its normal subtraction. Will error if
- an array is passed
*(1e)- multiplies values. 1 arg is array op. If 2+ doubles, its normal multiplication. If its
- under 2 and an array is passed with an array or double, then, on a single level,
- each value in the first array will be concatenated to the next with whatever
- follows between. Exampe:
-
- p*(["hello""world"]" ")
-
- outputs "hello world"
/(1e)- divides values. 1 arg is array op. If 2+ doubles, its normal division. Behaves similar to
- multiplication, but tokenizes by the string of arguments after the first
- array
%(2e)- modulo of 2+ numbers
^(2e)- power of 2+ numbers
c(1e)- copies the value recursively and returns the copy
i(2e)- indexes arrays. The 1st arg is (only arrays and operations). The 2ng arg, depending on if there
- are only 2 total arguments, it just returns the value at the position in the array.
- If there are 3 arguments provided, the last 2 arguments are used as a range to return.
- The range will return an array of the array values within that range. a -1 in either
- arg1 or arg2 will just return a range of values from, if the arg1 is -1, will read till arg2.
- The inverse is true if arg3 is -1.
- example:
-
- i[1 2 3 4]2
-
- output: 3
-
- # range example:
-
- i([1 2 3 4 5] 1 3)
-
- output: [2 3 4]
-
- # negative argument read till begin/end example:
-
- i([1 2 3 4 5] 2 -1)
-
- output: [3 4 5]
t(2e)- searches a table (associative array) for the first instance of the value passed.
- If 2 arguments passed, it will use the 1st as the source for the associative array
- and the 2nd as the cmp. If 3 arguments are passed, it follows the same setup
- as if 2 were passed, but it will set (or create) the key by the value compared
- in the associative array. Notes below:
-
- associative array format (will return '0' if not found (get only) or improper array)
- also, _anything_ can be used as the cmp value or a key. It is recursively compared.
-
- NOTE: to delete a row, set the value to 0. Because tables return 0 by default if
- no row exists, if a script sets a row to 0 without the intention of deleting,
- it will appear to still function.
-
- internal format:
- [[key_value data_value]...]
-
- examples:
-
-
- t[["test""hey"]]"test"
- returns: "hey"
-
- t(same_value_as_prev "test")
- returns: "hey"
-
- t([] "test" "hey")
- returns: 0 (only because it didnt exist before. If it had, it would return 1)
- the array inside the ast, because all values are passed by reference will
- remember this the next time it is run.
z(1e)- returns the size of the array.
y(1e)- returns the type as a number (BOING_TYPE_VALUE_...). Can be compared against
- NUMBER, ARRAY, OPERATION, EXTERNAL. If more than 1 argument passed, arg0 is
- used as the castee and arg1 is used as the type to cast it to.
- A NOTE ABOUT CONVERSION:
-
- array -> number: [1 3 4 5 6] -> 23456
- this is because the first number is what is used to store the sign of the
- original number and also to store the fractional part.
=(2e)- tests for equality between values recursively.
<(2e)- less than. Tests if arg1 is less than arg2. TBD if more than 2
>(2e)- greater than. Follows same style as the one above.
f(2i)- if statement. Uses the previous argument always and tests the first arg if
- nonzero. The 2nd argument, which _has_ to be a block, will get executed if
- the test argument is nonzero.
- examples:
-
- f(...){...} #note, the (...) isnt necessary, but its probably a good idea
-
- 0f(=3'0'){p"impossible"}f(=3'3'){p"this ran"}
- minified version: f=3'0{p"impossible"}f=3'3{p"this ran"}
l(2i)- loops. if just 2 arguments, it acts like a while loop in C.
- examples:
-
- l(=ITER5){... } #or alternatively,
- l=ITER5{... }
w(2e)- sets arg1 to arg2 (NOTE: value is _replicated_. Not the same as copying, but
- it does mean that if it's being set to an array, it will share all array members)
e(2e)- eval. This evaluates arg1. Arg2 is what "_" or "ARGS" is set to.
- If more than 2 arguments are passed and there's a 3rd argument, it takes a
- stack external type. If a 4th argument is passed, it expects a number. If that
- number is nonzero, a new stack will be pushed. Otherwise, the stack passed in arg3
- is the stack used.
m(1e)- parse/import. Parses files into an ast that can be evaluated by the above operation
- Arg1 is the file to parse and eval. If a second argument is provided, and it contains
- a string, it will use arg0 as a string to parse rather than as a filepath. Also, arg1
- becomes the "filename". Useful for tracking errors.
- Usually used to import othre files like:
-
- m"file.bg"
-
- Parsing a string instead:
-
- em("p\"hello\"" "testscript")] # evals a string that prints hello. The "]" could be replaced
- with a 0 or anything else, but its just for convenience.
o(1e)- file handling. Just a filepath reads the entire file to an array and returns that.
- If 2 arguments, it is a write to the file and will overwrite the contents of the
- file with the stringified contents of the array in arg2. If 3 arguments and it
- follows o("path" '0' '-1'), it will read the contents from arg2-arg3. If arg3 is
- negative, it will read the entire file.
- Examples:
-
- po"examples/hello_world.bg" # prints the hello world script to console
-
- o("file.txt""hello, I am a text file")
-
- em(o"examples/hello_world.bg" 1)] #a more verbose way of doing just m"whatever.bg"
&(2e)- logical & (&&). Will compare all arguments if == 1. Allows 2 or more args.
|(2e)- logical | (||). Will compare all arguments if == 1. Allows 2 or more args.
!(1i)- logical ! (!). Negates arg0.
n(1i)- increment numeric value. Returns the value incremented. Equivalent to postfix inc.
- If 2 arguments supplied, it will increment by that number.
d(1i)- decrement numeric value. Returns the value decremented. Equivalent to postfix dec
- If 2 arguments supplied, it will decrement by that number.
x(2e)- external call. This function takes 2(default) arguments, arg0 being the external typed value
- (must be the function subtype), and arg1 is the argument array. Follows the same
- convention as the 'e' eval operation. If more than 2 arguments are passed and there's
- a 3rd argument, it takes a stack external type.
h(1e)- hash (FNV-1a). THis recursively hashes the value passed. Only takes a single
- argument.
k(1e)- scope stack manual control. depending on the argument, it can be used to pass
- the root stack or n level stacks up. If arg0 == 0, it returns the
- root stack. >0 is n level stacks up. If arg0 == -1, it creates a
- whole new stack.
- NOTE: circular references are very easy to make. If you set something
- to your current stack, it will leak. Manually setting that variable to
- some other type will fix this but it means it has to be manually managed
- in any case where you do use your own stack.
- Example:
-
- wEXT "initial"
- wFUNC{wEXT "test"}
- e(FUNC [] k-2)
- pEXT # prints "initial" because FUNC didnt have access to EXT
-
- e(FUNC [] k-1)
- pEXT # prints "test" because FUNC had access to EXT
a(2e)- search. arg0 is an array and arg1 is anything to be searched inside. Note:
- this has a bias toward strings so a("hey?hey" 63) and a("hey?hey" [63]) are
- functionally the same
g(0e)- random number. Generates a random number with no arguments, but if a single argument
- is passed, that number will be what limits the output. It can be negative and the output
- will be limited to a negative range. If 2 numeric arguments are supplied, the number
- fall in that range.
q(1e)- quicksort. If a single array argument is supplied, the return value will be the sorted array.
- If more than one argument, all of the arguments will be sorted and an array will be returned
u(2e)- array initialize/setup. if a single argument and a number, the array resilting array of
- length arg0 will be initialized to numeric 0. If 2 aguments, the array will contain
- arg0 elements but arg1 is evaluated for every index. This also passes the current index
- as index 0 of the ARGS/_ identifiers.
- Example:
-
- wARRAY_VAR u(6)
-
- produces [0 0 0 0 0 0]
-
- wARRAY_VAR u6 {-i_0}
-
- produces [-0 -1 -2 -3 -4 -5]
j(2i)- wrap number. Wraps any positive or negative number within [0, arg1) or (arg1, 0]
automatic default identifiers
ARGS
- arguments to operation block. Created when evaluated by something
_ (note: "_")
- same as ARGS
NUMBER
- equal to BOING_TYPE_VALUE_NUMBER
ARRAY
- equal to BOING_TYPE_VALUE_ARRAY
OPERATION
- equal to BOING_TYPE_VALUE_OPERATION
EXTERNAL
- equal to BOING_TYPE_VALUE_EXTERNAL
TODO: add all of the stdlib identifiers and boing controls
*/
/* this may be a bit out of place, but because
various struct definitions exist below, this is necessary */
#ifndef BOING_ENABLE_LINE_NUM
#define BOING_ENABLE_LINE_NUM 1
#endif
enum
{
BOING_FLAG_NONE = 0ull,
BOING_FLAG_INITIALIZED = 1ull,
BOING_FLAG_NO_PRINT_NEWLINE = (1ull<<2),
};
enum
{
BOING_VALUE_STRING_MINIFIED,
BOING_VALUE_STRING_READABLE,
};
enum
{
BOING_OPERATION_IMPLICIT,
BOING_OPERATION_EXPLICIT
};
enum
{
BOING_OPERATION_PASS_ARGS,
BOING_OPERATION_EVAL_ARGS
};
enum
{
BOING_EXTERNAL_POINTER,
BOING_EXTERNAL_FUNCTION
};
enum
{
BOING_TYPE_VALUE_ARRAY = 1,
BOING_TYPE_VALUE_NUMBER = (1<<1),
BOING_TYPE_VALUE_OPERATION = (1<<2),
BOING_TYPE_VALUE_EXTERNAL = (1<<3)
};
enum
{
BOING_COMPARISON_NOT_EQUAL,
BOING_COMPARISON_EQUAL,
BOING_COMPARISON_LESS_THAN,
BOING_COMPARISON_GREATER_THAN
};
typedef struct boing_value_t boing_value_t;
typedef struct boing_t boing_t;
typedef struct boing_pool_t boing_pool_t;
typedef struct boing_module_t boing_module_t;
/* pool types */
typedef struct boing_pool_container_t
{
uint8_t taken;
size_t size; /* DO NOT USE IF VALUE */
struct boing_pool_container_t *next, *prev;
void *data;
} boing_pool_container_t;
typedef size_t (*boing_pool_container_destroy_cb_t)(boing_pool_t *pool, boing_pool_container_t *container);
typedef size_t (*boing_pool_container_size_cb_t)(boing_pool_t *pool, boing_pool_container_t *container);
struct boing_pool_t
{
boing_pool_container_t *head, *end;
size_t amount, free, max_free, block_alloc; /* if max_free is 0, then it is unlimited */
boing_pool_container_destroy_cb_t container_cleanup;
boing_pool_container_size_cb_t get_size;
};
/* boing types */
/* only used to report runtime errors if enabled */
typedef struct
{
char *script, *name;
} boing_script_t;
typedef struct
{
size_t references;
void *ptr, *extra;
int (*destroy_cb)(boing_t *boing, boing_value_t *value);
} boing_managed_ptr_t;
typedef boing_value_t *(*boing_external_function_t)(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
/* operation data for the boing boing */
typedef struct
{
char op;
size_t arg_amount;
uint8_t can_explicit, eval_args;
boing_external_function_t callback;
} boing_operation_data_t;
typedef struct
{
char op;
boing_value_t *args_value; /* always an array */
} boing_operation_t;
typedef struct
{
uint8_t type;
union
{
void *ptr;
boing_external_function_t fun;
};
int (*destroy_cb)(boing_t *boing, boing_value_t *value);
/* because pointers sometimes need to be handled independently of where they originated,
its necessary to provide a way to handle when they actually gain/lose references like
when external values get copied and set or when they get released.
If used this way, turning destroy into reference decrement works. */
int (*reference_inc_cb)(boing_t *boing, boing_value_t *value);
} boing_external_t;
struct boing_module_t
{
int (*module_init)(boing_t *boing, boing_module_t *module);
int (*module_destroy)(boing_t *boing, boing_module_t *module);
int (*module_stack_add)(boing_t *boing, boing_value_t *stack, boing_module_t *module);
void *handle, *user_data;
};
struct boing_value_t
{
#if BOING_ENABLE_LINE_NUM
uint32_t line;
boing_value_t *script;
#endif
boing_pool_container_t *container;
uint32_t length, allocated, references;
uint8_t type;
union
{
double number;
boing_value_t **array;
boing_operation_t operation;
boing_external_t external;
};
};
struct boing_t
{
void *user_data;
size_t flags;
/* program value. This is stored as
[AST base_stack] */
boing_value_t *program;
boing_operation_data_t *operations;
uint8_t operation_amount;
/* the modules table stores all native modules and their functions */
boing_value_t *modules;
/* pools */
struct
{
boing_pool_t value,
array_internal,
string;
} pool;
struct
{
uint8_t *(*boing_read_file_cb)(boing_t *boing, char *path, size_t *size_read, size_t offset, size_t amount);
size_t (*boing_write_file_cb)(boing_t *boing, char *path, void *data, size_t write_size);
void (*boing_print_cb)(boing_t *boing, char *message);
uint8_t *(*boing_read_cb)(boing_t *boing, size_t *read_size, size_t read_limit, uint8_t read_until_char);
uint8_t *(*boing_import_cb)(boing_t *boing, boing_value_t *stack, char *path);
void (*boing_error_cb)(boing_t *boing, char *message, int fatal);
int (*boing_root_stack_init_cb)(boing_t *boing, boing_value_t *stack);
int (*boing_module_cleanup_cb)(boing_t *boing, boing_module_t *module);
} callback;
};
/* macros */
#define BOING_FLAG_READ(subject, flag) (subject & flag)
#define BOING_FLAG_SET(subject, flag) do{ subject |= flag; }while(0);
#define BOING_TO_STR_BEFORE(number) #number
#define BOING_TO_STR(number) BOING_TO_STR_BEFORE(number)
#define BOING_ADD_GLOBAL(identifier, value) do{ \
boing_value_t *ident = NULL; \
boing_value_t *value_ptr = NULL; \
value_ptr = value; \
if(!value_ptr){ \
boing_error(boing, 0, "value is NULL for " identifier); \
return 1; \
} \
if(!(ident = boing_value_from_str(boing, identifier))){ \
boing_error(boing, 0, "could not create identifier " identifier); \
return 1; \
} \
if(boing_value_stack_add_set(boing, stack, ident, value_ptr)){ \
boing_error(boing, 0, "could not add identifier " identifier " to stack"); \
return 1; \
} \
/* need to refdec both */ \
if(boing_value_reference_dec(boing, ident)){ \
boing_error(boing, 0, "could not refdec identifier value for " identifier); \
return 1; \
} \
if(boing_value_reference_dec(boing, value_ptr)){ \
boing_error(boing, 0, "could not refdec value for " identifier); \
return 1; \
} \
} while(0)
/* global definitions */
#define BOING_VERSION_MAJOR 0
#define BOING_VERSION_MINOR 1
#define BOING_VERSION_REVISION 4
#define BOING_VERSION_STRING "Boing v."BOING_TO_STR(BOING_VERSION_MAJOR)"."BOING_TO_STR(BOING_VERSION_MINOR)"."BOING_TO_STR(BOING_VERSION_REVISION)", compiled "__DATE__" "__TIME__
/* function prototypes */
void boing_value_debug_print(boing_value_t *v, int level, FILE *stream);
uint8_t *boing_read_file(boing_t *boing, char *path, size_t *size_read, size_t offset, size_t amount);
size_t boing_write_file(boing_t *boing, char *path, void *data, size_t write_size);
void boing_print(boing_t *boing, char *message);
uint8_t *boing_read_input(boing_t *boing, size_t *read_size, size_t read_limit, uint8_t read_until_char);
uint8_t *boing_import_file(boing_t *boing, boing_value_t *stack, char *path);
void boing_error(boing_t *boing, int fatal, char *fmt, ...);
void boing_error_script_print(boing_t *boing, boing_value_t *value);
int boing_module_add(boing_t *boing, char *name, int (*module_init)(boing_t *boing, boing_module_t *module), int (*module_destroy)(boing_t *boing, boing_module_t *module), int (*module_stack_add)(boing_t *boing, boing_value_t *stack, boing_module_t *module), void *handle);
int boing_module_cleanup_handle(boing_t *boing, boing_module_t *module);
int boing_module_stack_add(boing_t *boing, boing_value_t *stack, char *name);
int boing_root_stack_init(boing_t *boing, boing_value_t *stack);
size_t boing_sort_partition(boing_t *boing, boing_value_t *array, size_t start, size_t end);
void boing_sort_continue(boing_t *boing, boing_value_t *array, size_t start, size_t end);
boing_value_t *boing_sort_array(boing_t *boing, boing_value_t *array);
boing_value_t *boing_std_sin(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_std_asin(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_std_sinh(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_std_cos(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_std_acos(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_std_cosh(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_std_tan(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_std_atan(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_std_tanh(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_std_exp(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_std_log(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_std_log10(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_std_ceil(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_std_fabs(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_std_floor(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_std_atan2(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_std_relative(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_std_pool_string_get_block_alloc(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_std_pool_string_set_block_alloc(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_std_pool_string_get_amount(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_std_pool_string_get_free(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_std_pool_string_get_max_free(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_std_pool_string_set_max_free(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_std_pool_array_internal_get_block_alloc(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_std_pool_array_internal_set_block_alloc(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_std_pool_array_internal_get_amount(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_std_pool_array_internal_get_free(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_std_pool_array_internal_get_max_free(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_std_pool_array_internal_set_max_free(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_std_pool_value_get_block_alloc(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_std_pool_value_set_block_alloc(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_std_pool_value_get_amount(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_std_pool_value_get_free(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_std_pool_value_get_max_free(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_std_pool_value_set_max_free(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
int boing_pool_init(boing_t *boing, boing_pool_t *pool, boing_pool_container_destroy_cb_t cleanup_callback, boing_pool_container_size_cb_t size_callback, size_t max_free);
int boing_pool_destroy(boing_t *boing, boing_pool_t *pool);
int boing_pool_list_remove(boing_t *boing, boing_pool_t *pool, boing_pool_container_t *remove);
int boing_pool_list_inject(boing_t *boing, boing_pool_t *pool, boing_pool_container_t *at, boing_pool_container_t *container);
int boing_pool_list_inject_after(boing_t *boing, boing_pool_t *pool, boing_pool_container_t *at, boing_pool_container_t *container);
void *boing_pool_request(boing_t *boing, boing_pool_t *pool, size_t min_size);
int boing_pool_add(boing_t *boing, boing_pool_t *pool, void *data, uint8_t taken, size_t size, boing_pool_container_t **data_container);
int boing_pool_shrink(boing_t *boing, boing_pool_t *pool);
boing_pool_container_t *boing_pool_data_container(boing_t *boing, boing_pool_t *pool, void *data);
int boing_pool_data_release(boing_t *boing, boing_pool_t *pool, void *data);
int boing_pool_data_release_container(boing_t *boing, boing_pool_t *pool, boing_pool_container_t *container);
char *boing_str_request(boing_t *boing, size_t size);
int boing_str_create_free(boing_t *boing, size_t size);
int boing_str_release(boing_t *boing, char *ptr);
size_t boing_str_alloc_size(boing_t *boing, char *ptr);
char *boing_str_resize(boing_t *boing, char *ptr, size_t size);
char *boing_str_sprintf(boing_t *boing, char *fmt, ...);
int boing_str_ncat(boing_t *boing, char **dest, char *src, size_t length);
char *boing_str_ndup(boing_t *boing, char *str, size_t length);
int boing_str_replace(boing_t *boing, char **source, char *replacee, char *replacement);
boing_value_t **boing_array_internl_request(boing_t *boing, size_t size);
int boing_array_internl_create_free(boing_t *boing, size_t size);
int boing_array_internl_release(boing_t *boing, void *ptr);
size_t boing_array_internl_alloc_size(boing_t *boing, void *ptr);
boing_value_t **boing_array_internl_resize(boing_t *boing, boing_value_t **ptr, size_t size);
boing_value_t *boing_value_pool_request(boing_t *boing, size_t size);
int boing_value_pool_create_free(boing_t *boing, size_t size);
int boing_value_pool_release(boing_t *boing, boing_value_t *ptr);
boing_value_t *boing_value_stack_create(boing_t *boing, boing_value_t *program);
boing_value_t *boing_value_stack_push(boing_t *boing, boing_value_t *parent);
boing_value_t *boing_value_stack_pop(boing_t *boing, boing_value_t *stack);
int boing_value_stack_is_root(boing_t *boing, boing_value_t *stack);
boing_value_t *boing_value_stack_search(boing_t *boing, boing_value_t *stack, boing_value_t *identifier);
int boing_value_stack_add_set(boing_t *boing, boing_value_t *stack, boing_value_t *identifier, boing_value_t *value);
boing_value_t *boing_value_stack_get_root(boing_t *boing, boing_value_t *stack);
int boing_value_table_add_set(boing_t *boing, boing_value_t *table, boing_value_t *key, boing_value_t *value, int add_not_found);
int boing_value_table_remove(boing_t *boing, boing_value_t *table, boing_value_t *key);
boing_value_t *boing_value_table_get(boing_t *boing, boing_value_t *table, boing_value_t *key);
boing_value_t *boing_value_table_get_str(boing_t *boing, boing_value_t *table, char *key);
boing_value_t *boing_operation_print(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_operation_string(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_operation_read(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_operation_plus(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_operation_minus(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_operation_multiply(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_operation_divide(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_operation_modulo(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_operation_power(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_operation_logical_and(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_operation_logical_or(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_operation_logical_not(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_operation_write(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_operation_array(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_operation_block_runtime(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_operation_block_pass(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_operation_identifier(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_operation_eval(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_operation_external(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_operation_index(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_operation_if(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_operation_equality(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_operation_lessthan(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_operation_greaterthan(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_operation_loop(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_operation_sizeof(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_operation_type(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_operation_copy(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_operation_inc(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_operation_dec(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_operation_table(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_operation_hash(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_operation_stack(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_operation_file(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_operation_import(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_operation_search(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_operation_random(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_operation_sort(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_operation_array_setup(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args);
boing_value_t *boing_value_request(boing_t *boing, uint8_t type, uint32_t alloc_length);
int boing_value_release(boing_t *boing, boing_value_t *value);
int boing_value_array_append_insert(boing_t *boing, boing_value_t *array, size_t position, boing_value_t *value);
int boing_value_array_remove(boing_t *boing, boing_value_t *array, size_t position);
boing_value_t *boing_value_array_index(boing_t *boing, boing_value_t *array, size_t position);
int boing_value_set(boing_t *boing, boing_value_t *target, boing_value_t *value);
boing_value_t *boing_value_copy(boing_t *boing, boing_value_t *value);
boing_value_t *boing_value_cast(boing_t *boing, boing_value_t *value, uint8_t cast);
void boing_value_reference_inc(boing_t *boing, boing_value_t *value);
int boing_value_reference_dec(boing_t *boing, boing_value_t *value);
boing_value_t *boing_value_from_ptr(boing_t *boing, void *ptr, uint8_t type, int (*destroy_cb)(boing_t *boing, boing_value_t *value));
boing_value_t *boing_value_from_ptr_managed(boing_t *boing, void *ptr, int (*destroy_cb)(boing_t *boing, boing_value_t *value));
boing_value_t *boing_value_from_args(boing_t *boing, int argc, char **argv);
boing_value_t *boing_value_from_double(boing_t *boing, double value);
boing_value_t *boing_value_from_strn(boing_t *boing, char *str, size_t length);
boing_value_t *boing_value_from_str(boing_t *boing, char *str);
boing_value_t *boing_value_from_table_fmt(boing_t *boing, char *fmt, ...);
char *boing_str_from_value_array_dest(boing_t *boing, boing_value_t *value, char *dest);
char *boing_str_from_value_array(boing_t *boing, boing_value_t *value);
char *boing_str_from_value_readable(boing_t *boing, boing_value_t *value, uint8_t *previous_type, uint8_t style, uint32_t level);
double boing_number_from_value_array(boing_t *boing, boing_value_t *value);
int boing_external_decrement_value_cleanup(boing_t *boing, boing_value_t *value);
int boing_external_increment_value(boing_t *boing, boing_value_t *value);
int boing_external_decrement_managed_cleanup(boing_t *boing, boing_value_t *value);
int boing_external_increment_managed(boing_t *boing, boing_value_t *value);
int boing_value_compare(boing_t *boing, boing_value_t *lhs, boing_value_t *rhs);
int boing_value_array_search(boing_t *boing, boing_value_t *array, boing_value_t *needle, size_t offset, size_t *pos);
size_t boing_value_hash_continue(boing_t *boing, boing_value_t *value, size_t start);
size_t boing_value_hash(boing_t *boing, boing_value_t *value);
int boing_is_operation(boing_t *boing, char op);
boing_operation_data_t *boing_operation_data(boing_t *boing, char op);
int boing_operation_has_explicit(boing_t *boing, char op);
size_t boing_operation_arg_amount(boing_t *boing, char op);
void boing_consume_whitespace(boing_t *boing, char **str);
boing_value_t *boing_parse_number(boing_t *boing, char **str);
boing_value_t *boing_parse_string(boing_t *boing, char **str);
boing_value_t *boing_parse_identifier(boing_t *boing, char **str);
boing_value_t *boing_parse_from_string(boing_t *boing, boing_value_t *parent, boing_value_t *current, boing_value_t *script, char **str, size_t *line);
boing_value_t *boing_program_call_global(boing_t *boing, boing_value_t *program, char *identifier, boing_value_t *args);
boing_value_t *boing_program_get_global(boing_t *boing, boing_value_t *program, char *identifier);
boing_value_t *boing_eval_value(boing_t *boing, boing_value_t *program, boing_value_t *value, boing_value_t *stack, boing_value_t *previous);
boing_value_t *boing_eval(boing_t *boing, char *str, boing_value_t *args, char *script_name);
boing_value_t *boing_eval_file(boing_t *boing, char *path, boing_value_t *args);
int boing_init(boing_t *boing);
int boing_destroy(boing_t *boing, int error_print_limit);
#ifdef BOING_IMPLEMENTATION
/* config */
#ifndef BOING_DEFAULT_SIZE_STRING
#define BOING_DEFAULT_SIZE_STRING 40 /* minimum of 0 */
#endif
#ifndef BOING_DEFAULT_FREE_STRING
#define BOING_DEFAULT_FREE_STRING 20 /* minimum of 2 */
#endif
#ifndef BOING_DEFAULT_FREE_STRING_MAX
#define BOING_DEFAULT_FREE_STRING_MAX 50 /* minimum of 0 */
#endif
/* just the parts of the internal array buffer */
#ifndef BOING_DEFAULT_SIZE_VALUE_ARRAY_INTERNAL
#define BOING_DEFAULT_SIZE_VALUE_ARRAY_INTERNAL 40 /* minimum of 0 */
#endif
#ifndef BOING_DEFAULT_FREE_VALUE_ARRAY_INTERNAL
#define BOING_DEFAULT_FREE_VALUE_ARRAY_INTERNAL 50 /* minimum of 2 */
#endif
#ifndef BOING_DEFAULT_FREE_VALUE_ARRAY_INTERNAL_MAX
#define BOING_DEFAULT_FREE_VALUE_ARRAY_INTERNAL_MAX 80 /* minimum of 0 */
#endif
/* the base value struct pool */
#ifndef BOING_DEFAULT_FREE_VALUE_BASE
#define BOING_DEFAULT_FREE_VALUE_BASE 80 /* minimum of 2 */
#endif
#ifndef BOING_DEFAULT_FREE_VALUE_BASE_MAX
#define BOING_DEFAULT_FREE_VALUE_BASE_MAX 150 /* minimum of 0 */
#endif
#ifdef BOING_HASH_64
#define BOING_FNV_OFFSET 0xcbf29ce484222325LL
#define BOING_FNV_PRIME 0x100000001b3LL
#else /* just default to a 32 bit version */
#define BOING_FNV_OFFSET 0x811c9dc5L
#define BOING_FNV_PRIME 0x01000193L
#endif
#ifndef BOING_VSNPRINTF
#define BOING_VSNPRINTF vsnprintf
#endif
#ifndef BOING_PATH_SEPARATE
#define BOING_PATH_SEPARATE '/'
#endif
/* ========================= standard operations ========================= */
static int boing_script_data_cleanup(boing_t *boing, boing_value_t *value)
{
/* free the strings inside first */
if(boing_str_release(boing, ((boing_script_t *)value->external.ptr)->script))
{
boing_error(boing, 0, "could not release script text data in script %s", ((boing_script_t *)value->external.ptr)->name);
return 1;
}
if(boing_str_release(boing, ((boing_script_t *)value->external.ptr)->name))
{
/* well, couldnt release it so might as well read it */
boing_error(boing, 0, "could not release script name string in script %s", ((boing_script_t *)value->external.ptr)->name);
return 1;
}
/* free the container */
if(boing_str_release(boing, value->external.ptr))
{
boing_error(boing, 0, "could not release script container");
return 1;
}
return 0;
}
boing_value_t *boing_operation_print(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args)
{
/* convert array to a bunch of strings */
char *message = NULL, *temp = NULL;
size_t i;
if(!(message = boing_str_request(boing, 1)))
{
boing_error(boing, 0, "could not request empty string for message in print operation");
return NULL;
}
message[0] = 0;
for(i = 0; i < args->length; ++i)
{
switch(args->array[i]->type)
{
case BOING_TYPE_VALUE_ARRAY:
if(!(temp = boing_str_from_value_array(boing, args->array[i])))
{
boing_error(boing, 0, "could not create string from value array in print operation");
/* TODO throw error */
return NULL;
}
if(boing_str_ncat(boing, &message, temp, strlen(temp)))
{
boing_error(boing, 0, "could not concatenate string in print operation");
/* TODO throw error */
return NULL;
}
if(boing_str_release(boing, temp))
{
boing_error(boing, 0, "could not release temporary string in print operation");
/* TODO throw error */
return NULL;
}
break;
case BOING_TYPE_VALUE_NUMBER:
if(!(temp = boing_str_sprintf(boing, "%g", args->array[i]->number)))
{
boing_error(boing, 0, "could not convert number to string in print operation");
/* TODO throw error */
return NULL;
}
if(boing_str_ncat(boing, &message, temp, strlen(temp)))
{
boing_error(boing, 0, "could not concatenate string in print operation");
/* TODO throw error */
return NULL;
}
if(boing_str_release(boing, temp))
{
boing_error(boing, 0, "could not release temporary string in print operation");
/* TODO throw error */
return NULL;
}
break;
}
}
boing_print(boing, message);
if(boing_str_release(boing, message))
{
boing_error(boing, 0, "could not release message string after printing");
/* TODO throw error */
return NULL;
}
return boing_value_from_double(boing, 0);
}
boing_value_t *boing_operation_string(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args)
{
boing_value_t *ret = NULL;
char *temp = NULL;
uint8_t previous_type = -1, style = BOING_VALUE_STRING_MINIFIED;
if(args->length < 1 || args->length > 2)
{
boing_error(boing, 0, "'s' operation expects just 1 or 2 arguments");
/* TODO throw error */
return NULL;
}
if(args->length == 2 && args->array[1]->type == BOING_TYPE_VALUE_NUMBER && args->array[1]->number)
style = BOING_VALUE_STRING_READABLE;
else if(args->length == 2 && args->array[1]->type == BOING_TYPE_VALUE_NUMBER && !args->array[1]->number)
style = BOING_VALUE_STRING_MINIFIED;
else if(args->length == 2)
{
boing_error(boing, 0, "expected numeric type in arg1 in 's' operation");
/* TODO throw error */
return NULL;
}
if(!(temp = boing_str_from_value_readable(boing, args->array[0], &previous_type, style, 0)))
{
boing_error(boing, 0, "could not turn value into string in 's' operation");
/* TODO throw error */
return NULL;
}
if(!(ret = boing_value_from_str(boing, temp)))
{
boing_error(boing, 0, "could not turn string into value array in 's' operation");
/* TODO throw error */
return NULL;
}
if(boing_str_release(boing, temp))
{
boing_error(boing, 0, "could not release string in 's' operation");
/* TODO throw error */
return NULL;
}
return ret;
}
boing_value_t *boing_operation_read(boing_t *boing, boing_value_t *program, boing_value_t *stack, boing_value_t *previous, boing_value_t *args)
{
boing_value_t *ret = NULL;
uint8_t *input = NULL;
size_t size = 0;
if(!args->length)
{
/* read single character */
if(!(input = boing_read_input(boing, &size, 1, 0)))
{
boing_error(boing, 0, "could not read from input in the read operation 'r'");
/* TODO throw error */
return NULL;
}
/* turn into single number */
if(!(ret = boing_value_from_double(boing, (double)input[0])))
{
boing_error(boing, 0, "could not request value from character in the read operation 'r'");
/* TODO throw error */
return NULL;
}
if(boing_str_release(boing, input))
{
boing_error(boing, 0, "could not release input str in the read operation 'r'");
/* TODO throw error */
return NULL;
}
}
else if(args->length == 1 && (args->array[0]->type == BOING_TYPE_VALUE_NUMBER || args->array[0]->type == BOING_TYPE_VALUE_ARRAY))
{
/* if 0, read till EOF, if >0 read until amount of chars satisfied */
if(args->array[0]->type == BOING_TYPE_VALUE_NUMBER && args->array[0]->number)
{
/* read until amount set */
if(!(input = boing_read_input(boing, &size, args->array[0]->number, 0)))
{
boing_error(boing, 0, "could not read from input in the read operation 'r'");
/* TODO throw error */
return NULL;
}
}
else if(args->array[0]->type == BOING_TYPE_VALUE_ARRAY && args->array[0]->length && args->array[0]->array[0]->type == BOING_TYPE_VALUE_NUMBER)
{
/* read until character inside string */
if(!(input = boing_read_input(boing, &size, 0, (char)args->array[0]->array[0]->number)))
{
boing_error(boing, 0, "could not read from input in the read operation 'r'");
/* TODO throw error */
return NULL;
}
}
else
{
/* read until EOF */