/
primitives.c
3513 lines (2899 loc) · 114 KB
/
primitives.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
/*******************************************************************
** w o r d s . c
** Forth Inspired Command Language
** ANS Forth CORE word-set written in C
** Author: John Sadler (john_sadler@alum.mit.edu)
** Created: 19 July 1997
** $Id: primitives.c,v 1.4 2010/09/13 18:43:04 asau Exp $
*******************************************************************/
/*
** Copyright (c) 1997-2001 John Sadler (john_sadler@alum.mit.edu)
** All rights reserved.
**
** Get the latest Ficl release at http://ficl.sourceforge.net
**
** I am interested in hearing from anyone who uses Ficl. If you have
** a problem, a success story, a defect, an enhancement request, or
** if you would like to contribute to the Ficl release, please
** contact me by email at the address above.
**
** L I C E N S E and D I S C L A I M E R
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions
** are met:
** 1. Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** 2. Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in the
** documentation and/or other materials provided with the distribution.
**
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
** ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
** SUCH DAMAGE.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "ficl.h"
/*
** Control structure building words use these
** strings' addresses as markers on the stack to
** check for structure completion.
*/
static char doTag[] = "do";
static char colonTag[] = "colon";
static char leaveTag[] = "leave";
static char destTag[] = "target";
static char origTag[] = "origin";
static char caseTag[] = "case";
static char ofTag[] = "of";
static char fallthroughTag[] = "fallthrough";
/*
** C O N T R O L S T R U C T U R E B U I L D E R S
**
** Push current dictionary location for later branch resolution.
** The location may be either a branch target or a patch address...
*/
static void markBranch(ficlDictionary *dictionary, ficlVm *vm, char *tag)
{
ficlStackPushPointer(vm->dataStack, dictionary->here);
ficlStackPushPointer(vm->dataStack, tag);
return;
}
static void markControlTag(ficlVm *vm, char *tag)
{
ficlStackPushPointer(vm->dataStack, tag);
return;
}
static void matchControlTag(ficlVm *vm, char *wantTag)
{
char *tag;
FICL_STACK_CHECK(vm->dataStack, 1, 0);
tag = (char *)ficlStackPopPointer(vm->dataStack);
/*
** Changed the code below to compare the pointers first (by popular demand)
*/
if ( (tag != wantTag) && strcmp(tag, wantTag) )
{
ficlVmThrowError(vm, "Error -- unmatched control structure \"%s\"", wantTag);
}
return;
}
/*
** Expect a branch target address on the param stack,
** FICL_VM_STATE_COMPILE a literal offset from the current dictionary location
** to the target address
*/
static void resolveBackBranch(ficlDictionary *dictionary, ficlVm *vm, char *tag)
{
ficlInteger offset;
ficlCell *patchAddr;
matchControlTag(vm, tag);
FICL_STACK_CHECK(vm->dataStack, 1, 0);
patchAddr = (ficlCell *)ficlStackPopPointer(vm->dataStack);
offset = patchAddr - dictionary->here;
ficlDictionaryAppendCell(dictionary, FICL_LVALUE_TO_CELL(offset));
return;
}
/*
** Expect a branch patch address on the param stack,
** FICL_VM_STATE_COMPILE a literal offset from the patch location
** to the current dictionary location
*/
static void resolveForwardBranch(ficlDictionary *dictionary, ficlVm *vm, char *tag)
{
ficlInteger offset;
ficlCell *patchAddr;
matchControlTag(vm, tag);
FICL_STACK_CHECK(vm->dataStack, 1, 0);
patchAddr = (ficlCell *)ficlStackPopPointer(vm->dataStack);
offset = dictionary->here - patchAddr;
*patchAddr = FICL_LVALUE_TO_CELL(offset);
return;
}
/*
** Match the tag to the top of the stack. If success,
** sopy "here" address into the ficlCell whose address is next
** on the stack. Used by do..leave..loop.
*/
static void resolveAbsBranch(ficlDictionary *dictionary, ficlVm *vm, char *wantTag)
{
ficlCell *patchAddr;
char *tag;
FICL_STACK_CHECK(vm->dataStack, 2, 0);
tag = ficlStackPopPointer(vm->dataStack);
/*
** Changed the comparison below to compare the pointers first (by popular demand)
*/
if ((tag != wantTag) && strcmp(tag, wantTag))
{
ficlVmTextOut(vm, "Warning -- Unmatched control word: ");
ficlVmTextOut(vm, wantTag);
ficlVmTextOut(vm, "\n");
}
patchAddr = (ficlCell *)ficlStackPopPointer(vm->dataStack);
*patchAddr = FICL_LVALUE_TO_CELL(dictionary->here);
return;
}
/**************************************************************************
c o l o n d e f i n i t i o n s
** Code to begin compiling a colon definition
** This function sets the state to FICL_VM_STATE_COMPILE, then creates a
** new word whose name is the next word in the input stream
** and whose code is colonParen.
**************************************************************************/
static void ficlPrimitiveColon(ficlVm *vm)
{
ficlDictionary *dictionary = ficlVmGetDictionary(vm);
ficlString name = ficlVmGetWord(vm);
vm->state = FICL_VM_STATE_COMPILE;
markControlTag(vm, colonTag);
ficlDictionaryAppendWord(dictionary, name, (ficlPrimitive)ficlInstructionColonParen, FICL_WORD_DEFAULT | FICL_WORD_SMUDGED);
#if FICL_WANT_LOCALS
vm->callback.system->localsCount = 0;
#endif
return;
}
static void ficlPrimitiveSemicolonCoIm(ficlVm *vm)
{
ficlDictionary *dictionary = ficlVmGetDictionary(vm);
matchControlTag(vm, colonTag);
#if FICL_WANT_LOCALS
if (vm->callback.system->localsCount > 0)
{
ficlDictionary *locals = ficlSystemGetLocals(vm->callback.system);
ficlDictionaryEmpty(locals, locals->forthWordlist->size);
ficlDictionaryAppendUnsigned(dictionary, ficlInstructionUnlinkParen);
}
vm->callback.system->localsCount = 0;
#endif
ficlDictionaryAppendUnsigned(dictionary, ficlInstructionSemiParen);
vm->state = FICL_VM_STATE_INTERPRET;
ficlDictionaryUnsmudge(dictionary);
return;
}
/**************************************************************************
e x i t
** CORE
** This function simply pops the previous instruction
** pointer and returns to the "next" loop. Used for exiting from within
** a definition. Note that exitParen is identical to semiParen - they
** are in two different functions so that "see" can correctly identify
** the end of a colon definition, even if it uses "exit".
**************************************************************************/
static void ficlPrimitiveExitCoIm(ficlVm *vm)
{
ficlDictionary *dictionary = ficlVmGetDictionary(vm);
FICL_IGNORE(vm);
#if FICL_WANT_LOCALS
if (vm->callback.system->localsCount > 0)
{
ficlDictionaryAppendUnsigned(dictionary, ficlInstructionUnlinkParen);
}
#endif
ficlDictionaryAppendUnsigned(dictionary, ficlInstructionExitParen);
return;
}
/**************************************************************************
c o n s t a n t
** IMMEDIATE
** Compiles a constant into the dictionary. Constants return their
** value when invoked. Expects a value on top of the parm stack.
**************************************************************************/
static void ficlPrimitiveConstant(ficlVm *vm)
{
ficlDictionary *dictionary = ficlVmGetDictionary(vm);
ficlString name = ficlVmGetWord(vm);
FICL_STACK_CHECK(vm->dataStack, 1, 0);
ficlDictionaryAppendConstantInstruction(dictionary, name, ficlInstructionConstantParen, ficlStackPopInteger(vm->dataStack));
return;
}
static void ficlPrimitive2Constant(ficlVm *vm)
{
ficlDictionary *dictionary = ficlVmGetDictionary(vm);
ficlString name = ficlVmGetWord(vm);
FICL_STACK_CHECK(vm->dataStack, 2, 0);
ficlDictionaryAppend2ConstantInstruction(dictionary, name, ficlInstruction2ConstantParen, ficlStackPop2Integer(vm->dataStack));
return;
}
/**************************************************************************
d i s p l a y C e l l
** Drop and print the contents of the ficlCell at the top of the param
** stack
**************************************************************************/
static void ficlPrimitiveDot(ficlVm *vm)
{
ficlCell c;
FICL_STACK_CHECK(vm->dataStack, 1, 0);
c = ficlStackPop(vm->dataStack);
ficlLtoa((c).i, vm->pad, vm->base);
strcat(vm->pad, " ");
ficlVmTextOut(vm, vm->pad);
return;
}
static void ficlPrimitiveUDot(ficlVm *vm)
{
ficlUnsigned u;
FICL_STACK_CHECK(vm->dataStack, 1, 0);
u = ficlStackPopUnsigned(vm->dataStack);
ficlUltoa(u, vm->pad, vm->base);
strcat(vm->pad, " ");
ficlVmTextOut(vm, vm->pad);
return;
}
static void ficlPrimitiveHexDot(ficlVm *vm)
{
ficlUnsigned u;
FICL_STACK_CHECK(vm->dataStack, 1, 0);
u = ficlStackPopUnsigned(vm->dataStack);
ficlUltoa(u, vm->pad, 16);
strcat(vm->pad, " ");
ficlVmTextOut(vm, vm->pad);
return;
}
/**************************************************************************
s t r l e n
** Ficl ( c-string -- length )
**
** Returns the length of a C-style (zero-terminated) string.
**
** --lch
**/
static void ficlPrimitiveStrlen(ficlVm *vm)
{
char *address = (char *)ficlStackPopPointer(vm->dataStack);
ficlStackPushInteger(vm->dataStack, strlen(address));
}
/**************************************************************************
s p r i n t f
** Ficl ( i*x c-addr-fmt u-fmt c-addr-buffer u-buffer -- c-addr-buffer u-written success-flag )
** Similar to the C sprintf() function. It formats into a buffer based on
** a "format" string. Each character in the format string is copied verbatim
** to the output buffer, until SPRINTF encounters a percent sign ("%").
** SPRINTF then skips the percent sign, and examines the next character
** (the "format character"). Here are the valid format characters:
** s - read a C-ADDR U-LENGTH string from the stack and copy it to
** the buffer
** d - read a ficlCell from the stack, format it as a string (base-10,
** signed), and copy it to the buffer
** x - same as d, except in base-16
** u - same as d, but unsigned
** % - output a literal percent-sign to the buffer
** SPRINTF returns the c-addr-buffer argument unchanged, the number of bytes
** written, and a flag indicating whether or not it ran out of space while
** writing to the output buffer (FICL_TRUE if it ran out of space).
**
** If SPRINTF runs out of space in the buffer to store the formatted string,
** it still continues parsing, in an effort to preserve your stack (otherwise
** it might leave uneaten arguments behind).
**
** --lch
**************************************************************************/
static void ficlPrimitiveSprintf(ficlVm *vm) /* */
{
int bufferLength = ficlStackPopInteger(vm->dataStack);
char *buffer = (char *)ficlStackPopPointer(vm->dataStack);
char *bufferStart = buffer;
int formatLength = ficlStackPopInteger(vm->dataStack);
char *format = (char *)ficlStackPopPointer(vm->dataStack);
char *formatStop = format + formatLength;
int base = 10;
int unsignedInteger = 0; /* false */
int append = 1; /* true */
while (format < formatStop)
{
char scratch[64];
char *source;
int actualLength;
int desiredLength;
int leadingZeroes;
if (*format != '%')
{
source = format;
actualLength = desiredLength = 1;
leadingZeroes = 0;
}
else
{
format++;
if (format == formatStop)
break;
leadingZeroes = (*format == '0');
if (leadingZeroes)
{
format++;
if (format == formatStop)
break;
}
desiredLength = isdigit((unsigned char)*format);
if (desiredLength)
{
desiredLength = strtoul(format, &format, 10);
if (format == formatStop)
break;
}
else if (*format == '*')
{
desiredLength = ficlStackPopInteger(vm->dataStack);
format++;
if (format == formatStop)
break;
}
switch (*format)
{
case 's':
case 'S':
{
actualLength = ficlStackPopInteger(vm->dataStack);
source = (char *)ficlStackPopPointer(vm->dataStack);
break;
}
case 'x':
case 'X':
base = 16;
case 'u':
case 'U':
unsignedInteger = 1; /* true */
case 'd':
case 'D':
{
int integer = ficlStackPopInteger(vm->dataStack);
if (unsignedInteger)
ficlUltoa(integer, scratch, base);
else
ficlLtoa(integer, scratch, base);
base = 10;
unsignedInteger = 0; /* false */
source = scratch;
actualLength = strlen(scratch);
break;
}
case '%':
source = format;
actualLength = 1;
default:
continue;
}
}
if (append)
{
if (!desiredLength)
desiredLength = actualLength;
if (desiredLength > bufferLength)
{
append = 0; /* false */
desiredLength = bufferLength;
}
while (desiredLength > actualLength)
{
*buffer++ = (char)((leadingZeroes) ? '0' : ' ');
bufferLength--;
desiredLength--;
}
memcpy(buffer, source, actualLength);
buffer += actualLength;
bufferLength -= actualLength;
}
format++;
}
ficlStackPushPointer(vm->dataStack, bufferStart);
ficlStackPushInteger(vm->dataStack, buffer - bufferStart);
ficlStackPushInteger(vm->dataStack, append && FICL_TRUE);
}
/**************************************************************************
d u p & f r i e n d s
**
**************************************************************************/
static void ficlPrimitiveDepth(ficlVm *vm)
{
int i;
FICL_STACK_CHECK(vm->dataStack, 0, 1);
i = ficlStackDepth(vm->dataStack);
ficlStackPushInteger(vm->dataStack, i);
return;
}
/**************************************************************************
e m i t & f r i e n d s
**
**************************************************************************/
static void ficlPrimitiveEmit(ficlVm *vm)
{
char *buffer = vm->pad;
int i;
FICL_STACK_CHECK(vm->dataStack, 1, 0);
i = ficlStackPopInteger(vm->dataStack);
buffer[0] = (char)i;
buffer[1] = '\0';
ficlVmTextOut(vm, buffer);
return;
}
static void ficlPrimitiveCR(ficlVm *vm)
{
ficlVmTextOut(vm, "\n");
return;
}
static void ficlPrimitiveBackslash(ficlVm *vm)
{
char *trace = ficlVmGetInBuf(vm);
char *stop = ficlVmGetInBufEnd(vm);
char c = *trace;
while ((trace != stop) && (c != '\r') && (c != '\n'))
{
c = *++trace;
}
/*
** Cope with DOS or UNIX-style EOLs -
** Check for /r, /n, /r/n, or /n/r end-of-line sequences,
** and point trace to next char. If EOL is \0, we're done.
*/
if (trace != stop)
{
trace++;
if ( (trace != stop) && (c != *trace)
&& ((*trace == '\r') || (*trace == '\n')) )
trace++;
}
ficlVmUpdateTib(vm, trace);
return;
}
/*
** paren CORE
** Compilation: Perform the execution semantics given below.
** Execution: ( "ccc<paren>" -- )
** Parse ccc delimited by ) (right parenthesis). ( is an immediate word.
** The number of characters in ccc may be zero to the number of characters
** in the parse area.
**
*/
static void ficlPrimitiveParenthesis(ficlVm *vm)
{
ficlVmParseStringEx(vm, ')', 0);
return;
}
/**************************************************************************
F E T C H & S T O R E
**
**************************************************************************/
/**************************************************************************
i f C o I m
** IMMEDIATE
** Compiles code for a conditional branch into the dictionary
** and pushes the branch patch address on the stack for later
** patching by ELSE or THEN/ENDIF.
**************************************************************************/
static void ficlPrimitiveIfCoIm(ficlVm *vm)
{
ficlDictionary *dictionary = ficlVmGetDictionary(vm);
ficlDictionaryAppendUnsigned(dictionary, ficlInstructionBranch0ParenWithCheck);
markBranch(dictionary, vm, origTag);
ficlDictionaryAppendUnsigned(dictionary, 1);
return;
}
/**************************************************************************
e l s e C o I m
**
** IMMEDIATE -- compiles an "else"...
** 1) FICL_VM_STATE_COMPILE a branch and a patch address; the address gets patched
** by "endif" to point past the "else" code.
** 2) Pop the the "if" patch address
** 3) Patch the "if" branch to point to the current FICL_VM_STATE_COMPILE address.
** 4) Push the "else" patch address. ("endif" patches this to jump past
** the "else" code.
**************************************************************************/
static void ficlPrimitiveElseCoIm(ficlVm *vm)
{
ficlCell *patchAddr;
ficlInteger offset;
ficlDictionary *dictionary = ficlVmGetDictionary(vm);
/* (1) FICL_VM_STATE_COMPILE branch runtime */
ficlDictionaryAppendUnsigned(dictionary, ficlInstructionBranchParenWithCheck);
matchControlTag(vm, origTag);
patchAddr =
(ficlCell *)ficlStackPopPointer(vm->dataStack); /* (2) pop "if" patch addr */
markBranch(dictionary, vm, origTag); /* (4) push "else" patch addr */
ficlDictionaryAppendUnsigned(dictionary, 1); /* (1) FICL_VM_STATE_COMPILE patch placeholder */
offset = dictionary->here - patchAddr;
*patchAddr = FICL_LVALUE_TO_CELL(offset); /* (3) Patch "if" */
return;
}
/**************************************************************************
e n d i f C o I m
**
**************************************************************************/
static void ficlPrimitiveEndifCoIm(ficlVm *vm)
{
ficlDictionary *dictionary = ficlVmGetDictionary(vm);
resolveForwardBranch(dictionary, vm, origTag);
return;
}
/**************************************************************************
c a s e C o I m
** IMMEDIATE FICL_VM_STATE_COMPILE-ONLY
**
**
** At FICL_VM_STATE_COMPILE-time, a CASE-SYS (see DPANS94 6.2.0873) looks like this:
** i*addr i caseTag
** and an OF-SYS (see DPANS94 6.2.1950) looks like this:
** i*addr i caseTag addr ofTag
** The integer under caseTag is the count of fixup addresses that branch
** to ENDCASE.
**************************************************************************/
static void ficlPrimitiveCaseCoIm(ficlVm *vm)
{
FICL_STACK_CHECK(vm->dataStack, 0, 2);
ficlStackPushUnsigned(vm->dataStack, 0);
markControlTag(vm, caseTag);
return;
}
/**************************************************************************
e n d c a s eC o I m
** IMMEDIATE FICL_VM_STATE_COMPILE-ONLY
**************************************************************************/
static void ficlPrimitiveEndcaseCoIm(ficlVm *vm)
{
ficlUnsigned fixupCount;
ficlDictionary *dictionary;
ficlCell *patchAddr;
ficlInteger offset;
/*
** if the last OF ended with FALLTHROUGH,
** just add the FALLTHROUGH fixup to the
** ENDOF fixups
*/
if (ficlStackGetTop(vm->dataStack).p == fallthroughTag)
{
matchControlTag(vm, fallthroughTag);
patchAddr = ficlStackPopPointer(vm->dataStack);
matchControlTag(vm, caseTag);
fixupCount = ficlStackPopUnsigned(vm->dataStack);
ficlStackPushPointer(vm->dataStack, patchAddr);
ficlStackPushUnsigned(vm->dataStack, fixupCount + 1);
markControlTag(vm, caseTag);
}
matchControlTag(vm, caseTag);
FICL_STACK_CHECK(vm->dataStack, 1, 0);
fixupCount = ficlStackPopUnsigned(vm->dataStack);
FICL_STACK_CHECK(vm->dataStack, fixupCount, 0);
dictionary = ficlVmGetDictionary(vm);
ficlDictionaryAppendUnsigned(dictionary, ficlInstructionDrop);
while (fixupCount--)
{
patchAddr = (ficlCell *)ficlStackPopPointer(vm->dataStack);
offset = dictionary->here - patchAddr;
*patchAddr = FICL_LVALUE_TO_CELL(offset);
}
return;
}
/**************************************************************************
o f C o I m
** IMMEDIATE FICL_VM_STATE_COMPILE-ONLY
**************************************************************************/
static void ficlPrimitiveOfCoIm(ficlVm *vm)
{
ficlDictionary *dictionary = ficlVmGetDictionary(vm);
ficlCell *fallthroughFixup = NULL;
FICL_STACK_CHECK(vm->dataStack, 1, 3);
if (ficlStackGetTop(vm->dataStack).p == fallthroughTag)
{
matchControlTag(vm, fallthroughTag);
fallthroughFixup = ficlStackPopPointer(vm->dataStack);
}
matchControlTag(vm, caseTag);
markControlTag(vm, caseTag);
ficlDictionaryAppendUnsigned(dictionary, ficlInstructionOfParen);
markBranch(dictionary, vm, ofTag);
ficlDictionaryAppendUnsigned(dictionary, 2);
if (fallthroughFixup != NULL)
{
ficlInteger offset = dictionary->here - fallthroughFixup;
*fallthroughFixup = FICL_LVALUE_TO_CELL(offset);
}
return;
}
/**************************************************************************
e n d o f C o I m
** IMMEDIATE FICL_VM_STATE_COMPILE-ONLY
**************************************************************************/
static void ficlPrimitiveEndofCoIm(ficlVm *vm)
{
ficlCell *patchAddr;
ficlUnsigned fixupCount;
ficlInteger offset;
ficlDictionary *dictionary = ficlVmGetDictionary(vm);
FICL_STACK_CHECK(vm->dataStack, 4, 3);
/* ensure we're in an OF, */
matchControlTag(vm, ofTag);
/* grab the address of the branch location after the OF */
patchAddr = (ficlCell *)ficlStackPopPointer(vm->dataStack);
/* ensure we're also in a "case" */
matchControlTag(vm, caseTag);
/* grab the current number of ENDOF fixups */
fixupCount = ficlStackPopUnsigned(vm->dataStack);
/* FICL_VM_STATE_COMPILE branch runtime */
ficlDictionaryAppendUnsigned(dictionary, ficlInstructionBranchParenWithCheck);
/* push a new ENDOF fixup, the updated count of ENDOF fixups, and the caseTag */
ficlStackPushPointer(vm->dataStack, dictionary->here);
ficlStackPushUnsigned(vm->dataStack, fixupCount + 1);
markControlTag(vm, caseTag);
/* reserve space for the ENDOF fixup */
ficlDictionaryAppendUnsigned(dictionary, 2);
/* and patch the original OF */
offset = dictionary->here - patchAddr;
*patchAddr = FICL_LVALUE_TO_CELL(offset);
}
/**************************************************************************
f a l l t h r o u g h C o I m
** IMMEDIATE FICL_VM_STATE_COMPILE-ONLY
**************************************************************************/
static void ficlPrimitiveFallthroughCoIm(ficlVm *vm)
{
ficlCell *patchAddr;
ficlInteger offset;
ficlDictionary *dictionary = ficlVmGetDictionary(vm);
FICL_STACK_CHECK(vm->dataStack, 4, 3);
/* ensure we're in an OF, */
matchControlTag(vm, ofTag);
/* grab the address of the branch location after the OF */
patchAddr = (ficlCell *)ficlStackPopPointer(vm->dataStack);
/* ensure we're also in a "case" */
matchControlTag(vm, caseTag);
/* okay, here we go. put the case tag back. */
markControlTag(vm, caseTag);
/* FICL_VM_STATE_COMPILE branch runtime */
ficlDictionaryAppendUnsigned(dictionary, ficlInstructionBranchParenWithCheck);
/* push a new FALLTHROUGH fixup and the fallthroughTag */
ficlStackPushPointer(vm->dataStack, dictionary->here);
markControlTag(vm, fallthroughTag);
/* reserve space for the FALLTHROUGH fixup */
ficlDictionaryAppendUnsigned(dictionary, 2);
/* and patch the original OF */
offset = dictionary->here - patchAddr;
*patchAddr = FICL_LVALUE_TO_CELL(offset);
}
/**************************************************************************
h a s h
** hash ( c-addr u -- code)
** calculates hashcode of specified string and leaves it on the stack
**************************************************************************/
static void ficlPrimitiveHash(ficlVm *vm)
{
ficlString s;
FICL_STRING_SET_LENGTH(s, ficlStackPopUnsigned(vm->dataStack));
FICL_STRING_SET_POINTER(s, ficlStackPopPointer(vm->dataStack));
ficlStackPushUnsigned(vm->dataStack, ficlHashCode(s));
return;
}
/**************************************************************************
i n t e r p r e t
** This is the "user interface" of a Forth. It does the following:
** while there are words in the VM's Text Input Buffer
** Copy next word into the pad (ficlVmGetWord)
** Attempt to find the word in the dictionary (ficlDictionaryLookup)
** If successful, execute the word.
** Otherwise, attempt to convert the word to a number (isNumber)
** If successful, push the number onto the parameter stack.
** Otherwise, print an error message and exit loop...
** End Loop
**
** From the standard, section 3.4
** Text interpretation (see 6.1.1360 EVALUATE and 6.1.2050 QUIT) shall
** repeat the following steps until either the parse area is empty or an
** ambiguous condition exists:
** a) Skip leading spaces and parse a name (see 3.4.1);
**************************************************************************/
static void ficlPrimitiveInterpret(ficlVm *vm)
{
ficlString s;
int i;
ficlSystem *system;
FICL_VM_ASSERT(vm, vm);
system = vm->callback.system;
s = ficlVmGetWord0(vm);
/*
** Get next word...if out of text, we're done.
*/
if (s.length == 0)
{
ficlVmThrow(vm, FICL_VM_STATUS_OUT_OF_TEXT);
}
/*
** Run the parse chain against the incoming token until somebody eats it.
** Otherwise emit an error message and give up.
*/
for (i=0; i < FICL_MAX_PARSE_STEPS; i++)
{
ficlWord *word = system->parseList[i];
if (word == NULL)
break;
if (word->code == ficlPrimitiveParseStepParen)
{
ficlParseStep pStep;
pStep = (ficlParseStep)(word->param->fn);
if ((*pStep)(vm, s))
return;
}
else
{
ficlStackPushPointer(vm->dataStack, FICL_STRING_GET_POINTER(s));
ficlStackPushUnsigned(vm->dataStack, FICL_STRING_GET_LENGTH(s));
ficlVmExecuteXT(vm, word);
if (ficlStackPopInteger(vm->dataStack))
return;
}
}
ficlVmThrowError(vm, "%.*s not found", FICL_STRING_GET_LENGTH(s), FICL_STRING_GET_POINTER(s));
return; /* back to inner interpreter */
}
/*
** Surrogate precompiled parse step for ficlParseWord (this step is hard coded in
** FICL_VM_STATE_INTERPRET)
*/
static void ficlPrimitiveLookup(ficlVm *vm)
{
ficlString name;
FICL_STRING_SET_LENGTH(name, ficlStackPopUnsigned(vm->dataStack));
FICL_STRING_SET_POINTER(name, ficlStackPopPointer(vm->dataStack));
ficlStackPushInteger(vm->dataStack, ficlVmParseWord(vm, name));
return;
}
/**************************************************************************
p a r e n P a r s e S t e p
** (parse-step) ( c-addr u -- flag )
** runtime for a precompiled parse step - pop a counted string off the
** stack, run the parse step against it, and push the result flag (FICL_TRUE
** if success, FICL_FALSE otherwise).
**************************************************************************/
void ficlPrimitiveParseStepParen(ficlVm *vm)
{
ficlString s;
ficlWord *word = vm->runningWord;
ficlParseStep pStep = (ficlParseStep)(word->param->fn);
FICL_STRING_SET_LENGTH(s, ficlStackPopInteger(vm->dataStack));
FICL_STRING_SET_POINTER(s, ficlStackPopPointer(vm->dataStack));
ficlStackPushInteger(vm->dataStack, (*pStep)(vm, s));
return;
}
static void ficlPrimitiveAddParseStep(ficlVm *vm)
{
ficlWord *pStep;
ficlDictionary *dictionary = ficlVmGetDictionary(vm);
FICL_STACK_CHECK(vm->dataStack, 1, 0);
pStep = (ficlWord *)(ficlStackPop(vm->dataStack).p);
if ((pStep != NULL) && ficlDictionaryIsAWord(dictionary, pStep))
ficlSystemAddParseStep(vm->callback.system, pStep);
return;
}
/**************************************************************************
l i t e r a l I m
**
** IMMEDIATE code for "literal". This function gets a value from the stack
** and compiles it into the dictionary preceded by the code for "(literal)".
** IMMEDIATE
**************************************************************************/
void ficlPrimitiveLiteralIm(ficlVm *vm)
{
ficlDictionary *dictionary = ficlVmGetDictionary(vm);
ficlInteger value;
value = ficlStackPopInteger(vm->dataStack);
switch (value)
{
case 1:
case 2:
case 3: