-
Notifications
You must be signed in to change notification settings - Fork 0
/
gfxboot_prim.c
4991 lines (4218 loc) · 112 KB
/
gfxboot_prim.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
#include <gfxboot/gfxboot.h>
#define WITH_PRIM_NAMES 1
#define WITH_PRIM_HEADERS 1
#include <gfxboot/vocabulary.h>
static obj_id_t prim_array_start_id = 0;
static obj_id_t prim_hash_start_id = 0;
typedef enum {
op_sub, op_mul, op_div, op_mod, op_and, op_or, op_xor, op_min, op_max, op_shl, op_shr,
op_neg, op_not, op_abs
} op_t;
typedef enum {
op_eq, op_ne, op_gt, op_ge, op_lt, op_le, op_cmp
} cmp_op_t;
typedef struct {
obj_id_t id;
obj_t *ptr;
} arg_t;
static arg_t *gfx_arg_1(uint8_t type);
static arg_t *gfx_arg_n(unsigned argc, uint8_t arg_types[]);
static int is_true(obj_id_t id);
static error_id_t do_op(op_t op, int64_t *result, int64_t val1, int64_t val2, unsigned sub_type);
static void binary_op_on_stack(op_t op);
static void unary_op_on_stack(op_t op);
static void binary_cmp_on_stack(cmp_op_t op);
static void gfx_prim_def_at(unsigned where);
static void gfx_prim__add(unsigned direct);
#define IS_NIL 0x80
#define IS_RW 0x40
#define TYPE_MASK 0x3f
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
arg_t *gfx_arg_1(uint8_t type)
{
static arg_t argv[1];
unsigned is_nil = type & IS_NIL;
unsigned is_rw = type & IS_RW;
type &= TYPE_MASK;
GFX_ERROR(err_ok);
array_t *pstack = gfx_obj_array_ptr(gfxboot_data->vm.program.pstack);
if(!pstack || pstack->size < 1) {
GFX_ERROR(err_stack_underflow);
return 0;
}
obj_id_t id = argv[0].id = pstack->ptr[pstack->size - 1];
obj_t *ptr = argv[0].ptr = gfx_obj_ptr(id);
if(
(ptr && (type == ptr->base_type || type == OTYPE_ANY)) ||
(id == 0 && (type == OTYPE_NONE || is_nil))
) {
if(is_rw && ptr && ptr->flags.ro) {
GFX_ERROR(err_readonly);
return 0;
}
return argv;
}
GFX_ERROR(err_invalid_arguments);
return 0;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
arg_t *gfx_arg_n(unsigned argc, uint8_t arg_types[])
{
unsigned i;
static arg_t argv[8];
#if 0
if(argc >= sizeof argv / sizeof *argv) {
GFX_ERROR(err_internal);
return 0;
}
#endif
GFX_ERROR(err_ok);
array_t *pstack = gfx_obj_array_ptr(gfxboot_data->vm.program.pstack);
if(!pstack || pstack->size < argc) {
GFX_ERROR(err_stack_underflow);
return 0;
}
unsigned stack_size = pstack->size;
obj_id_t *stack = pstack->ptr + stack_size - argc;
for(i = 0; i < argc; i++) {
obj_id_t id = argv[i].id = stack[i];
obj_t *ptr = argv[i].ptr = gfx_obj_ptr(id);
uint8_t type = arg_types[i];
unsigned is_nil = type & IS_NIL;
unsigned is_rw = type & IS_RW;
type &= TYPE_MASK;
if(
(ptr && (type == ptr->base_type || type == OTYPE_ANY)) ||
(id == 0 && (type == OTYPE_NONE || is_nil))
) {
if(is_rw && ptr && ptr->flags.ro) {
GFX_ERROR(err_readonly);
return 0;
}
continue;
}
GFX_ERROR(err_invalid_arguments);
return 0;
}
return argv;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// return: 0 = failed, 1 = ok
//
int gfx_setup_dict()
{
unsigned u;
obj_id_t prim_id;
gfx_obj_ref_dec(gfxboot_data->vm.program.dict);
gfxboot_data->vm.program.dict = gfx_obj_hash_new(0);
if(!gfxboot_data->vm.program.dict) return 0;
for(u = 0; u < sizeof prim_names / sizeof *prim_names ; u++) {
gfx_obj_hash_set(
gfxboot_data->vm.program.dict,
gfx_obj_const_mem_nofree_new((const uint8_t *) prim_names[u], gfx_strlen(prim_names[u]), t_ref, 0),
prim_id = gfx_obj_num_new(u, t_prim),
0
);
switch(u) {
case prim_idx_array_start:
prim_array_start_id = prim_id;
break;
case prim_idx_hash_start:
prim_hash_start_id = prim_id;
break;
}
}
if(!prim_array_start_id || !prim_hash_start_id) return 0;
return 1;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
//
error_id_t gfx_run_prim(unsigned prim)
{
if(prim > sizeof gfx_prim_list / sizeof *gfx_prim_list) {
GFX_ERROR(err_invalid_code);
return gfxboot_data->vm.error.id;
}
gfx_prim_list[prim]();
return gfxboot_data->vm.error.id;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// start code block
//
// group: code,array,hash
//
// ( -- code_1 )
//
// Put a reference to the following code block on the stack. The code block
// starts after the opening `{` and extends to (and including) the matching
// closing `}`.
//
// This special word is handled while converting the source code into binary code with `gfxboot-compile`.
// For this reason, this is the only word that cannot be redefined.
//
// example:
//
// { "Hello!" show }
//
void gfx_prim_code_start()
{
// will never be called
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// finish code block
//
// group: code,array,hash
//
// ( -- )
//
// This marks the end of a code block. When the code is executed, the
// interpreter leaves the current execution context and returns to the
// parent context.
//
// example:
//
// /hello { "Hello!" show } def
// hello # print "Hello!"
//
void gfx_prim_code_end()
{
context_t *context = gfx_obj_context_ptr(gfxboot_data->vm.program.context);
if(!context) {
GFX_ERROR(err_invalid_instruction);
return;
}
obj_id_t parent_id = context->parent_id;
switch(context->type) {
case t_ctx_block:
case t_ctx_func:
OBJ_ID_ASSIGN(gfxboot_data->vm.program.context, parent_id);
break;
case t_ctx_loop:
context->ip = 0;
break;
case t_ctx_repeat:
if(--context->index) {
context->ip = 0;
}
else {
OBJ_ID_ASSIGN(gfxboot_data->vm.program.context, parent_id);
}
break;
case t_ctx_for:
context->index += context->inc;
if(
(context->inc > 0 && context->index <= context->max) ||
(context->inc < 0 && context->index >= context->max)
) {
context->ip = 0;
gfx_obj_array_push(gfxboot_data->vm.program.pstack, gfx_obj_num_new(context->index, t_int), 0);
}
else {
OBJ_ID_ASSIGN(gfxboot_data->vm.program.context, parent_id);
}
break;
case t_ctx_forall:
;
obj_id_t val1, val2;
unsigned idx = context->index;
unsigned items = gfx_obj_iterate(context->iterate_id, &idx, &val1, &val2);
context->index = idx;
if(items) {
context->ip = 0;
// note: reference counting for val1 and val2 has been done inside gfx_obj_iterate()
gfx_obj_array_push(gfxboot_data->vm.program.pstack, val1, 0);
if(items > 1) gfx_obj_array_push(gfxboot_data->vm.program.pstack, val2, 0);
}
else {
OBJ_ID_ASSIGN(gfxboot_data->vm.program.context, parent_id);
}
break;
default:
GFX_ERROR(err_invalid_instruction);
break;
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// start array definition
//
// group: array,code,hash
//
// ( -- mark_1 )
//
// mark_1: array start marker
//
// Put array start marker on stack. Array definition is completed with ].
//
// example:
//
// [ 1 2 3 ] # array with 3 elements
//
void gfx_prim_array_start()
{
gfx_obj_array_push(gfxboot_data->vm.program.pstack, prim_array_start_id, 1);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// finish array definition
//
// group: array,code,hash
//
// ( mark_1 any_1 ... any_n -- array_1 )
//
// mark_1: array start marker
// any_1 ... any_n: some elements
// array_1: new array
//
// Search for mark_1 on the stack and put everything between mark_1 and TOS
// into an array.
//
// example:
//
// [ 10 20 "some" "text" ] # array with 4 elements
//
void gfx_prim_array_end()
{
array_t *a = gfx_obj_array_ptr(gfxboot_data->vm.program.pstack);
if(!a) return;
unsigned idx = a->size;
unsigned start_idx = -1u;
while(idx-- > 0) {
if(a->ptr[idx] == prim_array_start_id) {
start_idx = idx;
break;
}
}
if(start_idx == -1u) {
GFX_ERROR(err_no_array_start);
return;
}
obj_id_t array_id = gfx_obj_array_new(a->size - start_idx - 1);
if(!array_id) {
GFX_ERROR(err_no_memory);
return;
}
// no ref counting neded as the elements are just moved
for(idx = start_idx + 1; idx < a->size; idx++) {
gfx_obj_array_push(array_id, a->ptr[idx], 0);
}
a->size = start_idx + 1;
a->ptr[start_idx] = array_id;
gfx_obj_ref_dec(prim_array_start_id);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// start hash definition
//
// group: hash,array,code
//
// ( -- mark_1 )
//
// mark_1: hash start marker
//
// Put hash start marker on stack. Hash definition is completed with ).
//
// example:
//
// ( "foo" 10 "bar" 20 ) # hash with 2 keys "foo" and "bar"
//
void gfx_prim_hash_start()
{
gfx_obj_array_push(gfxboot_data->vm.program.pstack, prim_hash_start_id, 1);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// finish hash definition
//
// group: hash,array,code
//
// ( mark_1 any_1 ... any_n -- hash_1 )
//
// mark_1: array start marker
// any_1 ... any_n: some key - value pairs
// hash_1: new hash
//
// Search for mark_1 on the stack and put everything between mark_1 and TOS
// into a hash. The elements are interpreted alternatingly as key and value.
// If there's an odd number of elements on the stack, the last value is nil.
//
// example:
//
// ( "foo" 10 "bar" 20 ) # hash with 2 keys "foo" and "bar"
//
void gfx_prim_hash_end()
{
array_t *a = gfx_obj_array_ptr(gfxboot_data->vm.program.pstack);
if(!a) return;
unsigned idx = a->size;
unsigned start_idx = -1u;
while(idx-- > 0) {
if(a->ptr[idx] == prim_hash_start_id) {
start_idx = idx;
break;
}
}
if(start_idx == -1u) {
GFX_ERROR(err_no_hash_start);
return;
}
obj_id_t hash_id = gfx_obj_hash_new((a->size - start_idx) / 2);
if(!hash_id) {
GFX_ERROR(err_no_memory);
return;
}
// no ref counting neded as the elements are just moved
for(idx = start_idx + 1; idx < a->size; idx += 2) {
obj_id_t key = a->ptr[idx];
obj_id_t val = idx + 1 < a->size ? a->ptr[idx + 1] : 0;
if(gfx_obj_mem_ptr(key)) {
gfx_obj_hash_set(hash_id, key, val, 0);
}
else {
GFX_ERROR(err_invalid_hash_key);
gfx_obj_ref_dec(hash_id);
return;
}
}
a->size = start_idx + 1;
a->ptr[start_idx] = hash_id;
gfx_obj_ref_dec(prim_hash_start_id);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
//
// where: 0 (existing), 1 (local), 2 (global)
//
void gfx_prim_def_at(unsigned where)
{
array_t *pstack = gfx_obj_array_ptr(gfxboot_data->vm.program.pstack);
if(!pstack || pstack->size < 2) {
GFX_ERROR(err_stack_underflow);
return;
}
obj_id_t id1 = pstack->ptr[pstack->size - 2];
obj_id_t id2 = pstack->ptr[pstack->size - 1];
obj_t *ptr1 = gfx_obj_ptr(id1);
if(!ptr1 || ptr1->sub_type != t_ref) {
GFX_ERROR(err_invalid_instruction);
return;
}
obj_id_t dict_id = 0;
if(where == 0) {
obj_id_pair_t pair = gfx_lookup_dict(OBJ_DATA_FROM_PTR(ptr1));
if(pair.id1) {
dict_id = pair.id1;
}
else {
where = 1;
}
}
if(where) {
context_t *context = gfx_obj_context_ptr(gfxboot_data->vm.program.context);
if(!context) {
GFX_ERROR(err_internal);
return;
}
if(where == 2) {
while(context->parent_id) {
context = gfx_obj_context_ptr(context->parent_id);
if(!context) {
GFX_ERROR(err_internal);
return;
}
}
}
if(!context->dict_id) context->dict_id = gfx_obj_hash_new(0);
dict_id = context->dict_id;
if(!dict_id) {
GFX_ERROR(err_internal);
return;
}
}
gfx_obj_hash_set(dict_id, id1, id2, 1);
gfx_obj_array_pop(gfxboot_data->vm.program.pstack, 1);
gfx_obj_array_pop(gfxboot_data->vm.program.pstack, 1);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// define new word
//
// group: def
//
// ( word_1 any_1 -- )
//
// If word_1 does not exist, define word_1 in the current context.
//
// If word_1 does already exist, redefine word_1 in the context in which it is defined.
//
// example:
// /x 100 def # define x as 100
// /neg { -1 mul } def # define a function that negates its argument
//
void gfx_prim_def()
{
gfx_prim_def_at(0);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// define new local word
//
// group: def
//
// ( word_1 any_1 -- )
//
// Define word_1 in the current local context.
//
// example:
// /foo 200 ldef # define local word foo as 200
//
void gfx_prim_ldef()
{
gfx_prim_def_at(1);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// define new global word
//
// group: def
//
// ( word_1 any_1 -- )
//
// Define word_1 in the global context.
//
// example:
// /foo 300 gdef # define global word foo as 300
//
void gfx_prim_gdef()
{
gfx_prim_def_at(2);
}
#if 0
/* not really needed... */
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void gfx_prim_class()
{
arg_t *argv;
argv = gfx_arg_n(3, (uint8_t [3]) { OTYPE_MEM, OTYPE_HASH, OTYPE_HASH | IS_RW });
if(argv) {
if(argv[0].ptr->sub_type != t_ref) {
GFX_ERROR(err_invalid_arguments);
return;
}
context_t *context = gfx_obj_context_ptr(gfxboot_data->vm.program.context);
if(!context) {
GFX_ERROR(err_internal);
return;
}
while(context->parent_id) {
context = gfx_obj_context_ptr(context->parent_id);
if(!context) {
GFX_ERROR(err_internal);
return;
}
}
if(!context->dict_id) context->dict_id = gfx_obj_hash_new(0);
obj_id_t dict_id = context->dict_id;
gfx_obj_hash_set(dict_id, argv[0].id, argv[2].id, 1);
hash_t *hash = OBJ_HASH_FROM_PTR(argv[2].ptr);
obj_id_t old_id = hash->parent_id;
hash->parent_id = gfx_obj_ref_inc(argv[1].id);
gfx_obj_ref_dec(old_id);
gfx_obj_array_pop_n(3, gfxboot_data->vm.program.pstack, 1);
}
}
#endif
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// conditional execution
//
// group: if
//
// (bool_1 code_1 -- )
// (int_1 code_1 -- )
// (nil code_1 -- )
// (any_1 code_1 -- )
//
// code_1: code block to run if condition evaluates to 'true'
//
// The condition is false for: boolean false, integer 0, or nil. In all other cases it is true.
//
// example:
//
// true { "ok" show } if # "ok"
// 50 { "ok" show } if # "ok"
// nil { "ok" show } if # shows nothing
// "" { "ok" show } if # "ok"
//
void gfx_prim_if()
{
array_t *pstack = gfx_obj_array_ptr(gfxboot_data->vm.program.pstack);
if(!pstack || pstack->size < 2) {
GFX_ERROR(err_stack_underflow);
return;
}
obj_id_t id1 = pstack->ptr[pstack->size - 2];
obj_id_t id2 = pstack->ptr[pstack->size - 1];
if(!gfx_is_code(id2)) {
GFX_ERROR(err_invalid_code);
return;
}
if(is_true(id1)) {
obj_id_t context_id = gfx_obj_context_new(t_ctx_block);
context_t *context = gfx_obj_context_ptr(context_id);
if(!context) {
GFX_ERROR(err_no_memory);
return;
}
context->code_id = gfx_obj_ref_inc(id2);
context->parent_id = gfxboot_data->vm.program.context;
gfxboot_data->vm.program.context = context_id;
}
gfx_obj_array_pop(gfxboot_data->vm.program.pstack, 1);
gfx_obj_array_pop(gfxboot_data->vm.program.pstack, 1);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// conditional execution
//
// group: if
//
// (bool_1 code_1 code_2 -- )
// (int_1 code_1 code_2 -- )
// (nil code_1 code_2 -- )
// (any_1 code_1 code_2 -- )
//
// code_1: code block to run if condition evaluates to 'true'
// code_2: code block to run if condition evaluates to 'false'
//
// The condition is false for: boolean false, integer 0, or nil. In all other cases it is true.
//
// example:
//
// false { "ok" } { "bad" } ifelse show # "bad"
// 20 { "ok" } { "bad" } ifelse show # "ok"
// nil { "ok" } { "bad" } ifelse sho # "bad"
// "" { "ok" } { "bad" } ifelse show # "ok"
//
void gfx_prim_ifelse()
{
array_t *pstack = gfx_obj_array_ptr(gfxboot_data->vm.program.pstack);
if(!pstack || pstack->size < 3) {
GFX_ERROR(err_stack_underflow);
return;
}
obj_id_t id1 = pstack->ptr[pstack->size - 3];
obj_id_t id2 = pstack->ptr[pstack->size - 2];
obj_id_t id3 = pstack->ptr[pstack->size - 1];
if(!gfx_is_code(id2) || !gfx_is_code(id3)) {
GFX_ERROR(err_invalid_code);
return;
}
obj_id_t context_id = gfx_obj_context_new(t_ctx_block);
context_t *context = gfx_obj_context_ptr(context_id);
if(!context) {
GFX_ERROR(err_no_memory);
return;
}
context->code_id = gfx_obj_ref_inc(is_true(id1) ? id2 : id3);
context->parent_id = gfxboot_data->vm.program.context;
gfxboot_data->vm.program.context = context_id;
for(int i = 0; i < 3; i++) gfx_obj_array_pop(gfxboot_data->vm.program.pstack, 1);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// endless loop
//
// group: if,loop
//
// ( code_1 -- )
//
// Repeat code_1 forever until you exit the loop explicitly.
//
// example:
//
// { "Help!" show } loop
//
void gfx_prim_loop()
{
array_t *pstack = gfx_obj_array_ptr(gfxboot_data->vm.program.pstack);
if(!pstack || pstack->size < 1) {
GFX_ERROR(err_stack_underflow);
return;
}
obj_id_t id1 = pstack->ptr[pstack->size - 1];
if(!gfx_is_code(id1)) {
GFX_ERROR(err_invalid_code);
return;
}
obj_id_t context_id = gfx_obj_context_new(t_ctx_loop);
context_t *context = gfx_obj_context_ptr(context_id);
if(!context) {
GFX_ERROR(err_no_memory);
return;
}
context->code_id = gfx_obj_ref_inc(id1);
context->parent_id = gfxboot_data->vm.program.context;
gfxboot_data->vm.program.context = context_id;
gfx_obj_array_pop(gfxboot_data->vm.program.pstack, 1);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// repeat code block
//
// group: if,loop
//
// ( int_1 code_1 -- )
//
// Repeat code_1 int_1 times. If int_1 is less or equal to 0, code_1 is not run.
//
// example:
//
// 3 { "Help!" show } repeat # "Help!Help!Help!"
//
void gfx_prim_repeat()
{
array_t *pstack = gfx_obj_array_ptr(gfxboot_data->vm.program.pstack);
if(!pstack || pstack->size < 2) {
GFX_ERROR(err_stack_underflow);
return;
}
obj_id_t id1 = pstack->ptr[pstack->size - 2];
obj_id_t id2 = pstack->ptr[pstack->size - 1];
int64_t *count = gfx_obj_num_subtype_ptr(id1, t_int);
if(!count || !gfx_is_code(id2)) {
GFX_ERROR(err_invalid_code);
return;
}
if(*count > 0) {
obj_id_t context_id = gfx_obj_context_new(t_ctx_repeat);
context_t *context = gfx_obj_context_ptr(context_id);
if(!context) {
GFX_ERROR(err_no_memory);
return;
}
context->code_id = gfx_obj_ref_inc(id2);
context->index = *count;
context->parent_id = gfxboot_data->vm.program.context;
gfxboot_data->vm.program.context = context_id;
}
gfx_obj_array_pop(gfxboot_data->vm.program.pstack, 1);
gfx_obj_array_pop(gfxboot_data->vm.program.pstack, 1);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// run code block repeatedly, with counter
//
// group: if,loop
//
// ( int_1 int_2 int_3 code_1 -- )
// int_1: start value
// int_2: increment value
// int_3: maximum value (inclusive)
//
// Run code_1 repeatedly and put the current counter value on the stack in every iteration.
//
// The counter starts with int_1 and is incremented by int_2 until it
// reaches int_3. The code block is executed with the start value and then
// as long as the counter is less than or equal to the maximum value.
//
// The increment may be negative. In that case the loop is executed as long as the counter
// is greater than or equal to the maximum value.
//
// If the increment is 0, the loop is not executed.
//
// example:
//
// 0 1 4 { } for # 0 1 2 3 4
// 0 -2 -5 { } for # 0 -2 -4
//
void gfx_prim_for()
{
array_t *pstack = gfx_obj_array_ptr(gfxboot_data->vm.program.pstack);
if(!pstack || pstack->size < 4) {
GFX_ERROR(err_stack_underflow);
return;
}
obj_id_t id1 = pstack->ptr[pstack->size - 4];
obj_id_t id2 = pstack->ptr[pstack->size - 3];
obj_id_t id3 = pstack->ptr[pstack->size - 2];
obj_id_t id4 = pstack->ptr[pstack->size - 1];
int64_t *start = gfx_obj_num_subtype_ptr(id1, t_int);
int64_t *inc = gfx_obj_num_subtype_ptr(id2, t_int);
int64_t *max = gfx_obj_num_subtype_ptr(id3, t_int);
if(!start || !inc || !max || !gfx_is_code(id4)) {
GFX_ERROR(err_invalid_arguments);
return;
}
int pop_count = 4;
if(
(*inc > 0 && *start <= *max) ||
(*inc < 0 && *start >= *max)
) {
obj_id_t context_id = gfx_obj_context_new(t_ctx_for);
context_t *context = gfx_obj_context_ptr(context_id);
if(!context) {
GFX_ERROR(err_no_memory);
return;
}
context->code_id = gfx_obj_ref_inc(id4);
context->index = *start;
context->inc = *inc;
context->max = *max;
context->parent_id = gfxboot_data->vm.program.context;
gfxboot_data->vm.program.context = context_id;
pop_count--;
}
for(int i = 0; i < pop_count; i++) gfx_obj_array_pop(gfxboot_data->vm.program.pstack, 1);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// loop over all elements
//
// group: if,loop
//
// ( array_1 code_1 -- )
// ( hash_1 code_1 -- )
// ( string_1 code_1 -- )
//
// Run code_1 for each element of array_1, hash_1, or string_1.
//
// For array_1 and string_1, each element is put on the stack and code_1 is run.
//
// For hash_1, each key and value pair are put on the stack and code_1 is run.
// The hash keys are iterated in alphanumerical order.
//
// Note that string_1 is interpreted as a sequence of bytes, not UTF8-encoded characters.
//
// example:
//
// [ 10 20 30 ] { } forall # 10 20 30
// ( "foo" 10 "bar" 20 ) { } forall # "bar" 20 "foo" 10
// "ABC" { } forall # 65 66 67
//
void gfx_prim_forall()
{
array_t *pstack = gfx_obj_array_ptr(gfxboot_data->vm.program.pstack);
if(!pstack || pstack->size < 2) {
GFX_ERROR(err_stack_underflow);
return;
}
obj_id_t id1 = pstack->ptr[pstack->size - 2];
obj_id_t id2 = pstack->ptr[pstack->size - 1];
obj_t *ptr1 = gfx_obj_ptr(id1);
if(
!gfx_is_code(id2) ||
!ptr1 ||
(ptr1->base_type != OTYPE_ARRAY && ptr1->base_type != OTYPE_HASH && ptr1->base_type != OTYPE_MEM)
) {
GFX_ERROR(err_invalid_arguments);
return;
}
obj_id_t val1, val2;
unsigned idx = 0;
unsigned items = gfx_obj_iterate(id1, &idx, &val1, &val2);
if(items) {
obj_id_t context_id = gfx_obj_context_new(t_ctx_forall);
context_t *context = gfx_obj_context_ptr(context_id);
if(!context) {
GFX_ERROR(err_no_memory);
return;
}
context->code_id = gfx_obj_ref_inc(id2);
context->index = idx;
context->iterate_id = gfx_obj_ref_inc(id1);
context->parent_id = gfxboot_data->vm.program.context;
gfxboot_data->vm.program.context = context_id;
gfx_obj_array_pop(gfxboot_data->vm.program.pstack, 1);
gfx_obj_array_pop(gfxboot_data->vm.program.pstack, 1);
// note: reference counting for val1 and val2 has been done inside gfx_obj_iterate()
gfx_obj_array_push(gfxboot_data->vm.program.pstack, val1, 0);
if(items > 1) gfx_obj_array_push(gfxboot_data->vm.program.pstack, val2, 0);
}