1 /*******************************************************************
3 ** Forth Inspired Command Language
4 ** ANS Forth CORE word-set written in C
5 ** Author: John Sadler (john_sadler@alum.mit.edu)
6 ** Created: 19 July 1997
8 *******************************************************************/
10 /* $FreeBSD: src/sys/boot/ficl/words.c,v 1.23.2.2 2001/10/10 08:05:43 jkh Exp $ */
24 static void colonParen(FICL_VM *pVM);
25 static void literalIm(FICL_VM *pVM);
26 static void interpWord(FICL_VM *pVM, STRINGINFO si);
29 ** Control structure building words use these
30 ** strings' addresses as markers on the stack to
31 ** check for structure completion.
33 static char doTag[] = "do";
34 static char colonTag[] = "colon";
35 static char leaveTag[] = "leave";
37 static char destTag[] = "target";
38 static char origTag[] = "origin";
41 ** Pointers to various words in the dictionary
42 ** -- initialized by ficlCompileCore, below --
43 ** for use by compiling words. Colon definitions
44 ** in ficl are lists of pointers to words. A bit
47 static FICL_WORD *pBranchParen = NULL;
48 static FICL_WORD *pComma = NULL;
49 static FICL_WORD *pDoParen = NULL;
50 static FICL_WORD *pDoesParen = NULL;
51 static FICL_WORD *pExitParen = NULL;
52 static FICL_WORD *pIfParen = NULL;
53 static FICL_WORD *pInterpret = NULL;
54 static FICL_WORD *pLitParen = NULL;
55 static FICL_WORD *pTwoLitParen = NULL;
56 static FICL_WORD *pLoopParen = NULL;
57 static FICL_WORD *pPLoopParen = NULL;
58 static FICL_WORD *pQDoParen = NULL;
59 static FICL_WORD *pSemiParen = NULL;
60 static FICL_WORD *pStore = NULL;
61 static FICL_WORD *pStringLit = NULL;
62 static FICL_WORD *pType = NULL;
65 static FICL_WORD *pGetLocalParen= NULL;
66 static FICL_WORD *pGet2LocalParen= NULL;
67 static FICL_WORD *pGetLocal0 = NULL;
68 static FICL_WORD *pGetLocal1 = NULL;
69 static FICL_WORD *pToLocalParen = NULL;
70 static FICL_WORD *pTo2LocalParen = NULL;
71 static FICL_WORD *pToLocal0 = NULL;
72 static FICL_WORD *pToLocal1 = NULL;
73 static FICL_WORD *pLinkParen = NULL;
74 static FICL_WORD *pUnLinkParen = NULL;
75 static int nLocals = 0;
76 static CELL *pMarkLocals = NULL;
78 static void doLocalIm(FICL_VM *pVM);
79 static void do2LocalIm(FICL_VM *pVM);
85 ** C O N T R O L S T R U C T U R E B U I L D E R S
87 ** Push current dict location for later branch resolution.
88 ** The location may be either a branch target or a patch address...
90 static void markBranch(FICL_DICT *dp, FICL_VM *pVM, char *tag)
92 stackPushPtr(pVM->pStack, dp->here);
93 stackPushPtr(pVM->pStack, tag);
97 static void markControlTag(FICL_VM *pVM, char *tag)
99 stackPushPtr(pVM->pStack, tag);
103 static void matchControlTag(FICL_VM *pVM, char *tag)
105 char *cp = (char *)stackPopPtr(pVM->pStack);
106 if ( strcmp(cp, tag) )
108 vmThrowErr(pVM, "Error -- unmatched control structure \"%s\"", tag);
115 ** Expect a branch target address on the param stack,
116 ** compile a literal offset from the current dict location
117 ** to the target address
119 static void resolveBackBranch(FICL_DICT *dp, FICL_VM *pVM, char *tag)
124 matchControlTag(pVM, tag);
126 patchAddr = (CELL *)stackPopPtr(pVM->pStack);
127 offset = patchAddr - dp->here;
128 dictAppendCell(dp, LVALUEtoCELL(offset));
135 ** Expect a branch patch address on the param stack,
136 ** compile a literal offset from the patch location
137 ** to the current dict location
139 static void resolveForwardBranch(FICL_DICT *dp, FICL_VM *pVM, char *tag)
144 matchControlTag(pVM, tag);
146 patchAddr = (CELL *)stackPopPtr(pVM->pStack);
147 offset = dp->here - patchAddr;
148 *patchAddr = LVALUEtoCELL(offset);
154 ** Match the tag to the top of the stack. If success,
155 ** sopy "here" address into the cell whose address is next
156 ** on the stack. Used by do..leave..loop.
158 static void resolveAbsBranch(FICL_DICT *dp, FICL_VM *pVM, char *tag)
163 cp = stackPopPtr(pVM->pStack);
166 vmTextOut(pVM, "Warning -- Unmatched control word: ", 0);
167 vmTextOut(pVM, tag, 1);
170 patchAddr = (CELL *)stackPopPtr(pVM->pStack);
171 *patchAddr = LVALUEtoCELL(dp->here);
177 /**************************************************************************
179 ** Attempts to convert the NULL terminated string in the VM's pad to
180 ** a number using the VM's current base. If successful, pushes the number
181 ** onto the param stack and returns TRUE. Otherwise, returns FALSE.
182 **************************************************************************/
184 static int isNumber(FICL_VM *pVM, STRINGINFO si)
188 unsigned base = pVM->base;
189 char *cp = SI_PTR(si);
190 FICL_COUNT count= (FICL_COUNT)SI_COUNT(si);
200 else if ((cp[0] == '0') && (cp[1] == 'x'))
201 { /* detect 0xNNNN format for hex numbers */
210 while (count-- && ((ch = *cp++) != '\0'))
212 if (!(isdigit(ch)||isalpha(ch)))
218 digit = tolower(ch) - 'a' + 10;
223 accum = accum * base + digit;
229 stackPushINT(pVM->pStack, accum);
235 static void ficlIsNum(FICL_VM *pVM)
240 SI_SETLEN(si, stackPopINT(pVM->pStack));
241 SI_SETPTR(si, stackPopPtr(pVM->pStack));
242 ret = isNumber(pVM, si) ? FICL_TRUE : FICL_FALSE;
243 stackPushINT(pVM->pStack, ret);
247 /**************************************************************************
248 a d d & f r i e n d s
250 **************************************************************************/
252 static void add(FICL_VM *pVM)
256 vmCheckStack(pVM, 2, 1);
258 i = stackPopINT(pVM->pStack);
259 i += stackGetTop(pVM->pStack).i;
260 stackSetTop(pVM->pStack, LVALUEtoCELL(i));
264 static void sub(FICL_VM *pVM)
268 vmCheckStack(pVM, 2, 1);
270 i = stackPopINT(pVM->pStack);
271 i = stackGetTop(pVM->pStack).i - i;
272 stackSetTop(pVM->pStack, LVALUEtoCELL(i));
276 static void mul(FICL_VM *pVM)
280 vmCheckStack(pVM, 2, 1);
282 i = stackPopINT(pVM->pStack);
283 i *= stackGetTop(pVM->pStack).i;
284 stackSetTop(pVM->pStack, LVALUEtoCELL(i));
288 static void negate(FICL_VM *pVM)
292 vmCheckStack(pVM, 1, 1);
294 i = -stackPopINT(pVM->pStack);
295 stackPushINT(pVM->pStack, i);
299 static void ficlDiv(FICL_VM *pVM)
303 vmCheckStack(pVM, 2, 1);
305 i = stackPopINT(pVM->pStack);
306 i = stackGetTop(pVM->pStack).i / i;
307 stackSetTop(pVM->pStack, LVALUEtoCELL(i));
312 ** slash-mod CORE ( n1 n2 -- n3 n4 )
313 ** Divide n1 by n2, giving the single-cell remainder n3 and the single-cell
314 ** quotient n4. An ambiguous condition exists if n2 is zero. If n1 and n2
315 ** differ in sign, the implementation-defined result returned will be the
316 ** same as that returned by either the phrase
317 ** >R S>D R> FM/MOD or the phrase >R S>D R> SM/REM .
318 ** NOTE: Ficl complies with the second phrase (symmetric division)
320 static void slashMod(FICL_VM *pVM)
327 vmCheckStack(pVM, 2, 2);
329 n2 = stackPopINT(pVM->pStack);
330 n1.lo = stackPopINT(pVM->pStack);
333 qr = m64SymmetricDivI(n1, n2);
334 stackPushINT(pVM->pStack, qr.rem);
335 stackPushINT(pVM->pStack, qr.quot);
339 static void onePlus(FICL_VM *pVM)
343 vmCheckStack(pVM, 1, 1);
345 i = stackGetTop(pVM->pStack).i;
347 stackSetTop(pVM->pStack, LVALUEtoCELL(i));
351 static void oneMinus(FICL_VM *pVM)
355 vmCheckStack(pVM, 1, 1);
357 i = stackGetTop(pVM->pStack).i;
359 stackSetTop(pVM->pStack, LVALUEtoCELL(i));
363 static void twoMul(FICL_VM *pVM)
367 vmCheckStack(pVM, 1, 1);
369 i = stackGetTop(pVM->pStack).i;
371 stackSetTop(pVM->pStack, LVALUEtoCELL(i));
375 static void twoDiv(FICL_VM *pVM)
379 vmCheckStack(pVM, 1, 1);
381 i = stackGetTop(pVM->pStack).i;
383 stackSetTop(pVM->pStack, LVALUEtoCELL(i));
387 static void mulDiv(FICL_VM *pVM)
392 vmCheckStack(pVM, 3, 1);
394 z = stackPopINT(pVM->pStack);
395 y = stackPopINT(pVM->pStack);
396 x = stackPopINT(pVM->pStack);
399 x = m64SymmetricDivI(prod, z).quot;
401 stackPushINT(pVM->pStack, x);
406 static void mulDivRem(FICL_VM *pVM)
412 vmCheckStack(pVM, 3, 2);
414 z = stackPopINT(pVM->pStack);
415 y = stackPopINT(pVM->pStack);
416 x = stackPopINT(pVM->pStack);
419 qr = m64SymmetricDivI(prod, z);
421 stackPushINT(pVM->pStack, qr.rem);
422 stackPushINT(pVM->pStack, qr.quot);
427 /**************************************************************************
430 ** Signal the system to shut down - this causes ficlExec to return
431 ** VM_USEREXIT. The rest is up to you.
432 **************************************************************************/
434 static void bye(FICL_VM *pVM)
436 vmThrow(pVM, VM_USEREXIT);
441 /**************************************************************************
442 c o l o n d e f i n i t i o n s
443 ** Code to begin compiling a colon definition
444 ** This function sets the state to COMPILE, then creates a
445 ** new word whose name is the next word in the input stream
446 ** and whose code is colonParen.
447 **************************************************************************/
449 static void colon(FICL_VM *pVM)
451 FICL_DICT *dp = ficlGetDict();
452 STRINGINFO si = vmGetWord(pVM);
454 dictCheckThreshold(dp);
456 pVM->state = COMPILE;
457 markControlTag(pVM, colonTag);
458 dictAppendWord2(dp, si, colonParen, FW_DEFAULT | FW_SMUDGE);
466 /**************************************************************************
468 ** This is the code that executes a colon definition. It assumes that the
469 ** virtual machine is running a "next" loop (See the vm.c
470 ** for its implementation of member function vmExecute()). The colon
471 ** code simply copies the address of the first word in the list of words
472 ** to interpret into IP after saving its old value. When we return to the
473 ** "next" loop, the virtual machine will call the code for each word in
476 **************************************************************************/
478 static void colonParen(FICL_VM *pVM)
480 IPTYPE tempIP = (IPTYPE) (pVM->runningWord->param);
481 vmPushIP(pVM, tempIP);
487 /**************************************************************************
488 s e m i c o l o n C o I m
490 ** IMMEDIATE code for ";". This function sets the state to INTERPRET and
491 ** terminates a word under compilation by appending code for "(;)" to
492 ** the definition. TO DO: checks for leftover branch target tags on the
493 ** return stack and complains if any are found.
494 **************************************************************************/
495 static void semiParen(FICL_VM *pVM)
502 static void semicolonCoIm(FICL_VM *pVM)
504 FICL_DICT *dp = ficlGetDict();
507 matchControlTag(pVM, colonTag);
510 assert(pUnLinkParen);
513 FICL_DICT *pLoc = ficlGetLoc();
514 dictEmpty(pLoc, pLoc->pForthWords->size);
515 dictAppendCell(dp, LVALUEtoCELL(pUnLinkParen));
520 dictAppendCell(dp, LVALUEtoCELL(pSemiParen));
521 pVM->state = INTERPRET;
527 /**************************************************************************
530 ** This function simply pops the previous instruction
531 ** pointer and returns to the "next" loop. Used for exiting from within
532 ** a definition. Note that exitParen is identical to semiParen - they
533 ** are in two different functions so that "see" can correctly identify
534 ** the end of a colon definition, even if it uses "exit".
535 **************************************************************************/
536 static void exitParen(FICL_VM *pVM)
542 static void exitCoIm(FICL_VM *pVM)
544 FICL_DICT *dp = ficlGetDict();
551 dictAppendCell(dp, LVALUEtoCELL(pUnLinkParen));
554 dictAppendCell(dp, LVALUEtoCELL(pExitParen));
559 /**************************************************************************
560 c o n s t a n t P a r e n
561 ** This is the run-time code for "constant". It simply returns the
562 ** contents of its word's first data cell.
564 **************************************************************************/
566 void constantParen(FICL_VM *pVM)
568 FICL_WORD *pFW = pVM->runningWord;
570 vmCheckStack(pVM, 0, 1);
572 stackPush(pVM->pStack, pFW->param[0]);
576 void twoConstParen(FICL_VM *pVM)
578 FICL_WORD *pFW = pVM->runningWord;
580 vmCheckStack(pVM, 0, 2);
582 stackPush(pVM->pStack, pFW->param[0]); /* lo */
583 stackPush(pVM->pStack, pFW->param[1]); /* hi */
588 /**************************************************************************
591 ** Compiles a constant into the dictionary. Constants return their
592 ** value when invoked. Expects a value on top of the parm stack.
593 **************************************************************************/
595 static void constant(FICL_VM *pVM)
597 FICL_DICT *dp = ficlGetDict();
598 STRINGINFO si = vmGetWord(pVM);
601 vmCheckStack(pVM, 1, 0);
603 dictAppendWord2(dp, si, constantParen, FW_DEFAULT);
604 dictAppendCell(dp, stackPop(pVM->pStack));
609 static void twoConstant(FICL_VM *pVM)
611 FICL_DICT *dp = ficlGetDict();
612 STRINGINFO si = vmGetWord(pVM);
616 vmCheckStack(pVM, 2, 0);
618 c = stackPop(pVM->pStack);
619 dictAppendWord2(dp, si, twoConstParen, FW_DEFAULT);
620 dictAppendCell(dp, stackPop(pVM->pStack));
621 dictAppendCell(dp, c);
626 /**************************************************************************
627 d i s p l a y C e l l
628 ** Drop and print the contents of the cell at the top of the param
630 **************************************************************************/
632 static void displayCell(FICL_VM *pVM)
636 vmCheckStack(pVM, 1, 0);
638 c = stackPop(pVM->pStack);
639 ltoa((c).i, pVM->pad, pVM->base);
640 strcat(pVM->pad, " ");
641 vmTextOut(pVM, pVM->pad, 0);
645 static void displayCellNoPad(FICL_VM *pVM)
649 vmCheckStack(pVM, 1, 0);
651 c = stackPop(pVM->pStack);
652 ltoa((c).i, pVM->pad, pVM->base);
653 vmTextOut(pVM, pVM->pad, 0);
657 static void uDot(FICL_VM *pVM)
661 vmCheckStack(pVM, 1, 0);
663 u = stackPopUNS(pVM->pStack);
664 ultoa(u, pVM->pad, pVM->base);
665 strcat(pVM->pad, " ");
666 vmTextOut(pVM, pVM->pad, 0);
671 static void hexDot(FICL_VM *pVM)
675 vmCheckStack(pVM, 1, 0);
677 u = stackPopUNS(pVM->pStack);
678 ultoa(u, pVM->pad, 16);
679 strcat(pVM->pad, " ");
680 vmTextOut(pVM, pVM->pad, 0);
685 /**************************************************************************
686 d i s p l a y S t a c k
687 ** Display the parameter stack (code for ".s")
688 **************************************************************************/
690 static void displayStack(FICL_VM *pVM)
692 int d = stackDepth(pVM->pStack);
696 vmCheckStack(pVM, 0, 0);
699 vmTextOut(pVM, "(Stack Empty)", 1);
702 pCell = pVM->pStack->sp;
703 for (i = 0; i < d; i++)
705 vmTextOut(pVM, ltoa((*--pCell).i, pVM->pad, pVM->base), 1);
711 /**************************************************************************
712 d u p & f r i e n d s
714 **************************************************************************/
716 static void depth(FICL_VM *pVM)
720 vmCheckStack(pVM, 0, 1);
722 i = stackDepth(pVM->pStack);
723 stackPushINT(pVM->pStack, i);
728 static void drop(FICL_VM *pVM)
731 vmCheckStack(pVM, 1, 0);
733 stackDrop(pVM->pStack, 1);
738 static void twoDrop(FICL_VM *pVM)
741 vmCheckStack(pVM, 2, 0);
743 stackDrop(pVM->pStack, 2);
748 static void dup(FICL_VM *pVM)
751 vmCheckStack(pVM, 1, 2);
753 stackPick(pVM->pStack, 0);
758 static void twoDup(FICL_VM *pVM)
761 vmCheckStack(pVM, 2, 4);
763 stackPick(pVM->pStack, 1);
764 stackPick(pVM->pStack, 1);
769 static void over(FICL_VM *pVM)
772 vmCheckStack(pVM, 2, 3);
774 stackPick(pVM->pStack, 1);
778 static void twoOver(FICL_VM *pVM)
781 vmCheckStack(pVM, 4, 6);
783 stackPick(pVM->pStack, 3);
784 stackPick(pVM->pStack, 3);
789 static void pick(FICL_VM *pVM)
791 CELL c = stackPop(pVM->pStack);
793 vmCheckStack(pVM, c.i+1, c.i+2);
795 stackPick(pVM->pStack, c.i);
800 static void questionDup(FICL_VM *pVM)
804 vmCheckStack(pVM, 1, 2);
806 c = stackGetTop(pVM->pStack);
809 stackPick(pVM->pStack, 0);
815 static void roll(FICL_VM *pVM)
817 int i = stackPop(pVM->pStack).i;
820 vmCheckStack(pVM, i+1, i+1);
822 stackRoll(pVM->pStack, i);
827 static void minusRoll(FICL_VM *pVM)
829 int i = stackPop(pVM->pStack).i;
832 vmCheckStack(pVM, i+1, i+1);
834 stackRoll(pVM->pStack, -i);
839 static void rot(FICL_VM *pVM)
842 vmCheckStack(pVM, 3, 3);
844 stackRoll(pVM->pStack, 2);
849 static void swap(FICL_VM *pVM)
852 vmCheckStack(pVM, 2, 2);
854 stackRoll(pVM->pStack, 1);
859 static void twoSwap(FICL_VM *pVM)
862 vmCheckStack(pVM, 4, 4);
864 stackRoll(pVM->pStack, 3);
865 stackRoll(pVM->pStack, 3);
870 /**************************************************************************
871 e m i t & f r i e n d s
873 **************************************************************************/
875 static void emit(FICL_VM *pVM)
881 vmCheckStack(pVM, 1, 0);
883 i = stackPopINT(pVM->pStack);
886 vmTextOut(pVM, cp, 0);
891 static void cr(FICL_VM *pVM)
893 vmTextOut(pVM, "", 1);
898 static void commentLine(FICL_VM *pVM)
900 char *cp = vmGetInBuf(pVM);
901 char *pEnd = vmGetInBufEnd(pVM);
904 while ((cp != pEnd) && (ch != '\r') && (ch != '\n'))
910 ** Cope with DOS or UNIX-style EOLs -
911 ** Check for /r, /n, /r/n, or /n/r end-of-line sequences,
912 ** and point cp to next char. If EOL is \0, we're done.
918 if ( (cp != pEnd) && (ch != *cp)
919 && ((*cp == '\r') || (*cp == '\n')) )
923 vmUpdateTib(pVM, cp);
930 ** Compilation: Perform the execution semantics given below.
931 ** Execution: ( "ccc<paren>" -- )
932 ** Parse ccc delimited by ) (right parenthesis). ( is an immediate word.
933 ** The number of characters in ccc may be zero to the number of characters
934 ** in the parse area.
937 static void commentHang(FICL_VM *pVM)
939 vmParseStringEx(pVM, ')', 0);
944 /**************************************************************************
945 F E T C H & S T O R E
947 **************************************************************************/
949 static void fetch(FICL_VM *pVM)
953 vmCheckStack(pVM, 1, 1);
955 pCell = (CELL *)stackPopPtr(pVM->pStack);
956 stackPush(pVM->pStack, *pCell);
961 ** two-fetch CORE ( a-addr -- x1 x2 )
962 ** Fetch the cell pair x1 x2 stored at a-addr. x2 is stored at a-addr and
963 ** x1 at the next consecutive cell. It is equivalent to the sequence
964 ** DUP CELL+ @ SWAP @ .
966 static void twoFetch(FICL_VM *pVM)
970 vmCheckStack(pVM, 1, 2);
972 pCell = (CELL *)stackPopPtr(pVM->pStack);
973 stackPush(pVM->pStack, *pCell++);
974 stackPush(pVM->pStack, *pCell);
980 ** store CORE ( x a-addr -- )
981 ** Store x at a-addr.
983 static void store(FICL_VM *pVM)
987 vmCheckStack(pVM, 2, 0);
989 pCell = (CELL *)stackPopPtr(pVM->pStack);
990 *pCell = stackPop(pVM->pStack);
994 ** two-store CORE ( x1 x2 a-addr -- )
995 ** Store the cell pair x1 x2 at a-addr, with x2 at a-addr and x1 at the
996 ** next consecutive cell. It is equivalent to the sequence
997 ** SWAP OVER ! CELL+ ! .
999 static void twoStore(FICL_VM *pVM)
1003 vmCheckStack(pVM, 3, 0);
1005 pCell = (CELL *)stackPopPtr(pVM->pStack);
1006 *pCell++ = stackPop(pVM->pStack);
1007 *pCell = stackPop(pVM->pStack);
1010 static void plusStore(FICL_VM *pVM)
1014 vmCheckStack(pVM, 2, 0);
1016 pCell = (CELL *)stackPopPtr(pVM->pStack);
1017 pCell->i += stackPop(pVM->pStack).i;
1021 static void iFetch(FICL_VM *pVM)
1025 vmCheckStack(pVM, 1, 1);
1027 pw = (UNS32 *)stackPopPtr(pVM->pStack);
1028 stackPushUNS(pVM->pStack, (FICL_UNS)*pw);
1032 static void iStore(FICL_VM *pVM)
1036 vmCheckStack(pVM, 2, 0);
1038 pw = (UNS32 *)stackPopPtr(pVM->pStack);
1039 *pw = (UNS32)(stackPop(pVM->pStack).u);
1042 static void wFetch(FICL_VM *pVM)
1046 vmCheckStack(pVM, 1, 1);
1048 pw = (UNS16 *)stackPopPtr(pVM->pStack);
1049 stackPushUNS(pVM->pStack, (FICL_UNS)*pw);
1053 static void wStore(FICL_VM *pVM)
1057 vmCheckStack(pVM, 2, 0);
1059 pw = (UNS16 *)stackPopPtr(pVM->pStack);
1060 *pw = (UNS16)(stackPop(pVM->pStack).u);
1063 static void cFetch(FICL_VM *pVM)
1067 vmCheckStack(pVM, 1, 1);
1069 pc = (UNS8 *)stackPopPtr(pVM->pStack);
1070 stackPushUNS(pVM->pStack, (FICL_UNS)*pc);
1074 static void cStore(FICL_VM *pVM)
1078 vmCheckStack(pVM, 2, 0);
1080 pc = (UNS8 *)stackPopPtr(pVM->pStack);
1081 *pc = (UNS8)(stackPop(pVM->pStack).u);
1085 /**************************************************************************
1088 ** Compiles code for a conditional branch into the dictionary
1089 ** and pushes the branch patch address on the stack for later
1090 ** patching by ELSE or THEN/ENDIF.
1091 **************************************************************************/
1093 static void ifCoIm(FICL_VM *pVM)
1095 FICL_DICT *dp = ficlGetDict();
1099 dictAppendCell(dp, LVALUEtoCELL(pIfParen));
1100 markBranch(dp, pVM, origTag);
1101 dictAppendUNS(dp, 1);
1106 /**************************************************************************
1108 ** Runtime code to do "if" or "until": pop a flag from the stack,
1109 ** fall through if true, branch if false. Probably ought to be
1110 ** called (not?branch) since it does "branch if false".
1111 **************************************************************************/
1113 static void ifParen(FICL_VM *pVM)
1118 vmCheckStack(pVM, 1, 0);
1120 flag = stackPopUNS(pVM->pStack);
1123 { /* fall through */
1124 vmBranchRelative(pVM, 1);
1127 { /* take branch (to else/endif/begin) */
1128 vmBranchRelative(pVM, *(int*)(pVM->ip));
1135 /**************************************************************************
1138 ** IMMEDIATE -- compiles an "else"...
1139 ** 1) Compile a branch and a patch address; the address gets patched
1140 ** by "endif" to point past the "else" code.
1141 ** 2) Pop the the "if" patch address
1142 ** 3) Patch the "if" branch to point to the current compile address.
1143 ** 4) Push the "else" patch address. ("endif" patches this to jump past
1145 **************************************************************************/
1147 static void elseCoIm(FICL_VM *pVM)
1151 FICL_DICT *dp = ficlGetDict();
1153 assert(pBranchParen);
1154 /* (1) compile branch runtime */
1155 dictAppendCell(dp, LVALUEtoCELL(pBranchParen));
1156 matchControlTag(pVM, origTag);
1158 (CELL *)stackPopPtr(pVM->pStack); /* (2) pop "if" patch addr */
1159 markBranch(dp, pVM, origTag); /* (4) push "else" patch addr */
1160 dictAppendUNS(dp, 1); /* (1) compile patch placeholder */
1161 offset = dp->here - patchAddr;
1162 *patchAddr = LVALUEtoCELL(offset); /* (3) Patch "if" */
1168 /**************************************************************************
1169 b r a n c h P a r e n
1171 ** Runtime for "(branch)" -- expects a literal offset in the next
1172 ** compilation address, and branches to that location.
1173 **************************************************************************/
1175 static void branchParen(FICL_VM *pVM)
1177 vmBranchRelative(pVM, *(int *)(pVM->ip));
1182 /**************************************************************************
1185 **************************************************************************/
1187 static void endifCoIm(FICL_VM *pVM)
1189 FICL_DICT *dp = ficlGetDict();
1190 resolveForwardBranch(dp, pVM, origTag);
1195 /**************************************************************************
1197 ** hash ( c-addr u -- code)
1198 ** calculates hashcode of specified string and leaves it on the stack
1199 **************************************************************************/
1201 static void hash(FICL_VM *pVM)
1204 SI_SETLEN(si, stackPopUNS(pVM->pStack));
1205 SI_SETPTR(si, stackPopPtr(pVM->pStack));
1206 stackPushUNS(pVM->pStack, hashHashCode(si));
1211 /**************************************************************************
1213 ** This is the "user interface" of a Forth. It does the following:
1214 ** while there are words in the VM's Text Input Buffer
1215 ** Copy next word into the pad (vmGetWord)
1216 ** Attempt to find the word in the dictionary (dictLookup)
1217 ** If successful, execute the word.
1218 ** Otherwise, attempt to convert the word to a number (isNumber)
1219 ** If successful, push the number onto the parameter stack.
1220 ** Otherwise, print an error message and exit loop...
1223 ** From the standard, section 3.4
1224 ** Text interpretation (see 6.1.1360 EVALUATE and 6.1.2050 QUIT) shall
1225 ** repeat the following steps until either the parse area is empty or an
1226 ** ambiguous condition exists:
1227 ** a) Skip leading spaces and parse a name (see 3.4.1);
1228 **************************************************************************/
1230 static void interpret(FICL_VM *pVM)
1232 STRINGINFO si = vmGetWord0(pVM);
1235 vmBranchRelative(pVM, -1);
1238 ** Get next word...if out of text, we're done.
1242 vmThrow(pVM, VM_OUTOFTEXT);
1245 interpWord(pVM, si);
1248 return; /* back to inner interpreter */
1251 /**************************************************************************
1252 ** From the standard, section 3.4
1253 ** b) Search the dictionary name space (see 3.4.2). If a definition name
1254 ** matching the string is found:
1255 ** 1.if interpreting, perform the interpretation semantics of the definition
1256 ** (see 3.4.3.2), and continue at a);
1257 ** 2.if compiling, perform the compilation semantics of the definition
1258 ** (see 3.4.3.3), and continue at a).
1260 ** c) If a definition name matching the string is not found, attempt to
1261 ** convert the string to a number (see 3.4.1.3). If successful:
1262 ** 1.if interpreting, place the number on the data stack, and continue at a);
1263 ** 2.if compiling, compile code that when executed will place the number on
1264 ** the stack (see 6.1.1780 LITERAL), and continue at a);
1266 ** d) If unsuccessful, an ambiguous condition exists (see 3.4.4).
1267 **************************************************************************/
1268 static void interpWord(FICL_VM *pVM, STRINGINFO si)
1270 FICL_DICT *dp = ficlGetDict();
1274 dictCheck(dp, pVM, 0);
1275 vmCheckStack(pVM, 0, 0);
1278 #if FICL_WANT_LOCALS
1281 tempFW = dictLookupLoc(dp, si);
1285 tempFW = dictLookup(dp, si);
1287 if (pVM->state == INTERPRET)
1291 if (wordIsCompileOnly(tempFW))
1293 vmThrowErr(pVM, "Error: Compile only!");
1296 vmExecute(pVM, tempFW);
1299 else if (!isNumber(pVM, si))
1301 int i = SI_COUNT(si);
1302 vmThrowErr(pVM, "%.*s not found", i, SI_PTR(si));
1306 else /* (pVM->state == COMPILE) */
1310 if (wordIsImmediate(tempFW))
1312 vmExecute(pVM, tempFW);
1316 dictAppendCell(dp, LVALUEtoCELL(tempFW));
1319 else if (isNumber(pVM, si))
1325 int i = SI_COUNT(si);
1326 vmThrowErr(pVM, "%.*s not found", i, SI_PTR(si));
1334 /**************************************************************************
1335 l i t e r a l P a r e n
1337 ** This is the runtime for (literal). It assumes that it is part of a colon
1338 ** definition, and that the next CELL contains a value to be pushed on the
1339 ** parameter stack at runtime. This code is compiled by "literal".
1341 **************************************************************************/
1343 static void literalParen(FICL_VM *pVM)
1346 vmCheckStack(pVM, 0, 1);
1348 stackPushINT(pVM->pStack, *(FICL_INT *)(pVM->ip));
1349 vmBranchRelative(pVM, 1);
1353 static void twoLitParen(FICL_VM *pVM)
1356 vmCheckStack(pVM, 0, 2);
1358 stackPushINT(pVM->pStack, *((FICL_INT *)(pVM->ip)+1));
1359 stackPushINT(pVM->pStack, *(FICL_INT *)(pVM->ip));
1360 vmBranchRelative(pVM, 2);
1365 /**************************************************************************
1368 ** IMMEDIATE code for "literal". This function gets a value from the stack
1369 ** and compiles it into the dictionary preceded by the code for "(literal)".
1371 **************************************************************************/
1373 static void literalIm(FICL_VM *pVM)
1375 FICL_DICT *dp = ficlGetDict();
1378 dictAppendCell(dp, LVALUEtoCELL(pLitParen));
1379 dictAppendCell(dp, stackPop(pVM->pStack));
1385 static void twoLiteralIm(FICL_VM *pVM)
1387 FICL_DICT *dp = ficlGetDict();
1388 assert(pTwoLitParen);
1390 dictAppendCell(dp, LVALUEtoCELL(pTwoLitParen));
1391 dictAppendCell(dp, stackPop(pVM->pStack));
1392 dictAppendCell(dp, stackPop(pVM->pStack));
1397 /**************************************************************************
1400 **************************************************************************/
1402 static void listWords(FICL_VM *pVM)
1404 FICL_DICT *dp = ficlGetDict();
1405 FICL_HASH *pHash = dp->pSearch[dp->nLists - 1];
1413 char *pPad = pVM->pad;
1415 for (i = 0; i < pHash->size; i++)
1417 for (wp = pHash->table[i]; wp != NULL; wp = wp->link, nWords++)
1419 if (wp->nName == 0) /* ignore :noname defs */
1423 nChars += sprintf(pPad + nChars, "%s", cp);
1427 pPad[nChars] = '\0';
1432 vmTextOut(pVM, "--- Press Enter to continue ---",0);
1434 vmTextOut(pVM,"\r",0);
1436 vmTextOut(pVM, pPad, 1);
1440 len = nCOLWIDTH - nChars % nCOLWIDTH;
1442 pPad[nChars++] = ' ';
1447 pPad[nChars] = '\0';
1452 vmTextOut(pVM, "--- Press Enter to continue ---",0);
1454 vmTextOut(pVM,"\r",0);
1456 vmTextOut(pVM, pPad, 1);
1463 pPad[nChars] = '\0';
1465 vmTextOut(pVM, pPad, 1);
1468 sprintf(pVM->pad, "Dictionary: %d words, %ld cells used of %u total",
1469 nWords, (long) (dp->here - dp->dict), dp->size);
1470 vmTextOut(pVM, pVM->pad, 1);
1475 static void listEnv(FICL_VM *pVM)
1477 FICL_DICT *dp = ficlGetEnv();
1478 FICL_HASH *pHash = dp->pForthWords;
1483 for (i = 0; i < pHash->size; i++)
1485 for (wp = pHash->table[i]; wp != NULL; wp = wp->link, nWords++)
1487 vmTextOut(pVM, wp->name, 1);
1491 sprintf(pVM->pad, "Environment: %d words, %ld cells used of %u total",
1492 nWords, (long) (dp->here - dp->dict), dp->size);
1493 vmTextOut(pVM, pVM->pad, 1);
1498 /**************************************************************************
1499 l o g i c a n d c o m p a r i s o n s
1501 **************************************************************************/
1503 static void zeroEquals(FICL_VM *pVM)
1507 vmCheckStack(pVM, 1, 1);
1509 c.i = FICL_BOOL(stackPopINT(pVM->pStack) == 0);
1510 stackPush(pVM->pStack, c);
1514 static void zeroLess(FICL_VM *pVM)
1518 vmCheckStack(pVM, 1, 1);
1520 c.i = FICL_BOOL(stackPopINT(pVM->pStack) < 0);
1521 stackPush(pVM->pStack, c);
1525 static void zeroGreater(FICL_VM *pVM)
1529 vmCheckStack(pVM, 1, 1);
1531 c.i = FICL_BOOL(stackPopINT(pVM->pStack) > 0);
1532 stackPush(pVM->pStack, c);
1536 static void isEqual(FICL_VM *pVM)
1541 vmCheckStack(pVM, 2, 1);
1543 x = stackPop(pVM->pStack);
1544 y = stackPop(pVM->pStack);
1545 stackPushINT(pVM->pStack, FICL_BOOL(x.i == y.i));
1549 static void isLess(FICL_VM *pVM)
1553 vmCheckStack(pVM, 2, 1);
1555 y = stackPop(pVM->pStack);
1556 x = stackPop(pVM->pStack);
1557 stackPushINT(pVM->pStack, FICL_BOOL(x.i < y.i));
1561 static void uIsLess(FICL_VM *pVM)
1565 vmCheckStack(pVM, 2, 1);
1567 u2 = stackPopUNS(pVM->pStack);
1568 u1 = stackPopUNS(pVM->pStack);
1569 stackPushINT(pVM->pStack, FICL_BOOL(u1 < u2));
1573 static void isGreater(FICL_VM *pVM)
1577 vmCheckStack(pVM, 2, 1);
1579 y = stackPop(pVM->pStack);
1580 x = stackPop(pVM->pStack);
1581 stackPushINT(pVM->pStack, FICL_BOOL(x.i > y.i));
1585 static void bitwiseAnd(FICL_VM *pVM)
1589 vmCheckStack(pVM, 2, 1);
1591 x = stackPop(pVM->pStack);
1592 y = stackPop(pVM->pStack);
1593 stackPushINT(pVM->pStack, x.i & y.i);
1597 static void bitwiseOr(FICL_VM *pVM)
1601 vmCheckStack(pVM, 2, 1);
1603 x = stackPop(pVM->pStack);
1604 y = stackPop(pVM->pStack);
1605 stackPushINT(pVM->pStack, x.i | y.i);
1609 static void bitwiseXor(FICL_VM *pVM)
1613 vmCheckStack(pVM, 2, 1);
1615 x = stackPop(pVM->pStack);
1616 y = stackPop(pVM->pStack);
1617 stackPushINT(pVM->pStack, x.i ^ y.i);
1621 static void bitwiseNot(FICL_VM *pVM)
1625 vmCheckStack(pVM, 1, 1);
1627 x = stackPop(pVM->pStack);
1628 stackPushINT(pVM->pStack, ~x.i);
1633 /**************************************************************************
1635 ** do -- IMMEDIATE COMPILE ONLY
1636 ** Compiles code to initialize a loop: compile (do),
1637 ** allot space to hold the "leave" address, push a branch
1638 ** target address for the loop.
1639 ** (do) -- runtime for "do"
1640 ** pops index and limit from the p stack and moves them
1641 ** to the r stack, then skips to the loop body.
1642 ** loop -- IMMEDIATE COMPILE ONLY
1644 ** Compiles code for the test part of a loop:
1645 ** compile (loop), resolve forward branch from "do", and
1646 ** copy "here" address to the "leave" address allotted by "do"
1647 ** i,j,k -- COMPILE ONLY
1648 ** Runtime: Push loop indices on param stack (i is innermost loop...)
1649 ** Note: each loop has three values on the return stack:
1650 ** ( R: leave limit index )
1651 ** "leave" is the absolute address of the next cell after the loop
1652 ** limit and index are the loop control variables.
1653 ** leave -- COMPILE ONLY
1654 ** Runtime: pop the loop control variables, then pop the
1655 ** "leave" address and jump (absolute) there.
1656 **************************************************************************/
1658 static void doCoIm(FICL_VM *pVM)
1660 FICL_DICT *dp = ficlGetDict();
1664 dictAppendCell(dp, LVALUEtoCELL(pDoParen));
1666 ** Allot space for a pointer to the end
1667 ** of the loop - "leave" uses this...
1669 markBranch(dp, pVM, leaveTag);
1670 dictAppendUNS(dp, 0);
1672 ** Mark location of head of loop...
1674 markBranch(dp, pVM, doTag);
1680 static void doParen(FICL_VM *pVM)
1684 vmCheckStack(pVM, 2, 0);
1686 index = stackPop(pVM->pStack);
1687 limit = stackPop(pVM->pStack);
1689 /* copy "leave" target addr to stack */
1690 stackPushPtr(pVM->rStack, *(pVM->ip++));
1691 stackPush(pVM->rStack, limit);
1692 stackPush(pVM->rStack, index);
1698 static void qDoCoIm(FICL_VM *pVM)
1700 FICL_DICT *dp = ficlGetDict();
1704 dictAppendCell(dp, LVALUEtoCELL(pQDoParen));
1706 ** Allot space for a pointer to the end
1707 ** of the loop - "leave" uses this...
1709 markBranch(dp, pVM, leaveTag);
1710 dictAppendUNS(dp, 0);
1712 ** Mark location of head of loop...
1714 markBranch(dp, pVM, doTag);
1720 static void qDoParen(FICL_VM *pVM)
1724 vmCheckStack(pVM, 2, 0);
1726 index = stackPop(pVM->pStack);
1727 limit = stackPop(pVM->pStack);
1729 /* copy "leave" target addr to stack */
1730 stackPushPtr(pVM->rStack, *(pVM->ip++));
1732 if (limit.u == index.u)
1738 stackPush(pVM->rStack, limit);
1739 stackPush(pVM->rStack, index);
1747 ** Runtime code to break out of a do..loop construct
1748 ** Drop the loop control variables; the branch address
1749 ** past "loop" is next on the return stack.
1751 static void leaveCo(FICL_VM *pVM)
1754 stackDrop(pVM->rStack, 2);
1761 static void unloopCo(FICL_VM *pVM)
1763 stackDrop(pVM->rStack, 3);
1768 static void loopCoIm(FICL_VM *pVM)
1770 FICL_DICT *dp = ficlGetDict();
1774 dictAppendCell(dp, LVALUEtoCELL(pLoopParen));
1775 resolveBackBranch(dp, pVM, doTag);
1776 resolveAbsBranch(dp, pVM, leaveTag);
1781 static void plusLoopCoIm(FICL_VM *pVM)
1783 FICL_DICT *dp = ficlGetDict();
1785 assert(pPLoopParen);
1787 dictAppendCell(dp, LVALUEtoCELL(pPLoopParen));
1788 resolveBackBranch(dp, pVM, doTag);
1789 resolveAbsBranch(dp, pVM, leaveTag);
1794 static void loopParen(FICL_VM *pVM)
1796 FICL_INT index = stackGetTop(pVM->rStack).i;
1797 FICL_INT limit = stackFetch(pVM->rStack, 1).i;
1803 stackDrop(pVM->rStack, 3); /* nuke the loop indices & "leave" addr */
1804 vmBranchRelative(pVM, 1); /* fall through the loop */
1807 { /* update index, branch to loop head */
1808 stackSetTop(pVM->rStack, LVALUEtoCELL(index));
1809 vmBranchRelative(pVM, *(int *)(pVM->ip));
1816 static void plusLoopParen(FICL_VM *pVM)
1818 FICL_INT index = stackGetTop(pVM->rStack).i;
1819 FICL_INT limit = stackFetch(pVM->rStack, 1).i;
1820 FICL_INT increment = stackPop(pVM->pStack).i;
1826 flag = (index < limit);
1828 flag = (index >= limit);
1832 stackDrop(pVM->rStack, 3); /* nuke the loop indices & "leave" addr */
1833 vmBranchRelative(pVM, 1); /* fall through the loop */
1836 { /* update index, branch to loop head */
1837 stackSetTop(pVM->rStack, LVALUEtoCELL(index));
1838 vmBranchRelative(pVM, *(int *)(pVM->ip));
1845 static void loopICo(FICL_VM *pVM)
1847 CELL index = stackGetTop(pVM->rStack);
1848 stackPush(pVM->pStack, index);
1854 static void loopJCo(FICL_VM *pVM)
1856 CELL index = stackFetch(pVM->rStack, 3);
1857 stackPush(pVM->pStack, index);
1863 static void loopKCo(FICL_VM *pVM)
1865 CELL index = stackFetch(pVM->rStack, 6);
1866 stackPush(pVM->pStack, index);
1872 /**************************************************************************
1873 r e t u r n s t a c k
1875 **************************************************************************/
1877 static void toRStack(FICL_VM *pVM)
1879 stackPush(pVM->rStack, stackPop(pVM->pStack));
1883 static void fromRStack(FICL_VM *pVM)
1885 stackPush(pVM->pStack, stackPop(pVM->rStack));
1889 static void fetchRStack(FICL_VM *pVM)
1891 stackPush(pVM->pStack, stackGetTop(pVM->rStack));
1896 /**************************************************************************
1899 **************************************************************************/
1901 static void variableParen(FICL_VM *pVM)
1903 FICL_WORD *fw = pVM->runningWord;
1904 stackPushPtr(pVM->pStack, fw->param);
1909 static void variable(FICL_VM *pVM)
1911 FICL_DICT *dp = ficlGetDict();
1912 STRINGINFO si = vmGetWord(pVM);
1914 dictAppendWord2(dp, si, variableParen, FW_DEFAULT);
1915 dictAllotCells(dp, 1);
1921 /**************************************************************************
1922 b a s e & f r i e n d s
1924 **************************************************************************/
1926 static void base(FICL_VM *pVM)
1928 CELL *pBase = (CELL *)(&pVM->base);
1929 stackPush(pVM->pStack, LVALUEtoCELL(pBase));
1934 static void decimal(FICL_VM *pVM)
1941 static void hex(FICL_VM *pVM)
1948 /**************************************************************************
1949 a l l o t & f r i e n d s
1951 **************************************************************************/
1953 static void allot(FICL_VM *pVM)
1955 FICL_DICT *dp = ficlGetDict();
1956 FICL_INT i = stackPopINT(pVM->pStack);
1958 dictCheck(dp, pVM, i);
1965 static void here(FICL_VM *pVM)
1967 FICL_DICT *dp = ficlGetDict();
1968 stackPushPtr(pVM->pStack, dp->here);
1973 static void comma(FICL_VM *pVM)
1975 FICL_DICT *dp = ficlGetDict();
1976 CELL c = stackPop(pVM->pStack);
1977 dictAppendCell(dp, c);
1982 static void cComma(FICL_VM *pVM)
1984 FICL_DICT *dp = ficlGetDict();
1985 char c = (char)stackPopINT(pVM->pStack);
1986 dictAppendChar(dp, c);
1991 static void cells(FICL_VM *pVM)
1993 FICL_INT i = stackPopINT(pVM->pStack);
1994 stackPushINT(pVM->pStack, i * (FICL_INT)sizeof (CELL));
1999 static void cellPlus(FICL_VM *pVM)
2001 char *cp = stackPopPtr(pVM->pStack);
2002 stackPushPtr(pVM->pStack, cp + sizeof (CELL));
2007 /**************************************************************************
2009 ** tick CORE ( "<spaces>name" -- xt )
2010 ** Skip leading space delimiters. Parse name delimited by a space. Find
2011 ** name and return xt, the execution token for name. An ambiguous condition
2012 ** exists if name is not found.
2013 **************************************************************************/
2014 static void tick(FICL_VM *pVM)
2016 FICL_WORD *pFW = NULL;
2017 STRINGINFO si = vmGetWord(pVM);
2019 pFW = dictLookup(ficlGetDict(), si);
2022 int i = SI_COUNT(si);
2023 vmThrowErr(pVM, "%.*s not found", i, SI_PTR(si));
2025 stackPushPtr(pVM->pStack, pFW);
2030 static void bracketTickCoIm(FICL_VM *pVM)
2039 /**************************************************************************
2041 ** Lookup the next word in the input stream and compile code to
2042 ** insert it into definitions created by the resulting word
2043 ** (defers compilation, even of immediate words)
2044 **************************************************************************/
2046 static void postponeCoIm(FICL_VM *pVM)
2048 FICL_DICT *dp = ficlGetDict();
2053 pFW = stackGetTop(pVM->pStack).p;
2054 if (wordIsImmediate(pFW))
2056 dictAppendCell(dp, stackPop(pVM->pStack));
2061 dictAppendCell(dp, LVALUEtoCELL(pComma));
2069 /**************************************************************************
2071 ** Pop an execution token (pointer to a word) off the stack and
2073 **************************************************************************/
2075 static void execute(FICL_VM *pVM)
2079 vmCheckStack(pVM, 1, 0);
2082 pFW = stackPopPtr(pVM->pStack);
2083 vmExecute(pVM, pFW);
2089 /**************************************************************************
2091 ** Make the most recently compiled word IMMEDIATE -- it executes even
2092 ** in compile state (most often used for control compiling words
2093 ** such as IF, THEN, etc)
2094 **************************************************************************/
2096 static void immediate(FICL_VM *pVM)
2099 dictSetImmediate(ficlGetDict());
2104 static void compileOnly(FICL_VM *pVM)
2107 dictSetFlags(ficlGetDict(), FW_COMPILE, 0);
2112 /**************************************************************************
2114 ** IMMEDIATE word that compiles a string literal for later display
2115 ** Compile stringLit, then copy the bytes of the string from the TIB
2116 ** to the dictionary. Backpatch the count byte and align the dictionary.
2118 ** stringlit: Fetch the count from the dictionary, then push the address
2119 ** and count on the stack. Finally, update ip to point to the first
2120 ** aligned address after the string text.
2121 **************************************************************************/
2123 static void stringLit(FICL_VM *pVM)
2125 FICL_STRING *sp = (FICL_STRING *)(pVM->ip);
2126 FICL_COUNT count = sp->count;
2127 char *cp = sp->text;
2128 stackPushPtr(pVM->pStack, cp);
2129 stackPushUNS(pVM->pStack, count);
2132 pVM->ip = (IPTYPE)(void *)cp;
2136 static void dotQuoteCoIm(FICL_VM *pVM)
2138 FICL_DICT *dp = ficlGetDict();
2139 dictAppendCell(dp, LVALUEtoCELL(pStringLit));
2140 dp->here = PTRtoCELL vmGetString(pVM, (FICL_STRING *)dp->here, '\"');
2142 dictAppendCell(dp, LVALUEtoCELL(pType));
2147 static void dotParen(FICL_VM *pVM)
2149 char *pSrc = vmGetInBuf(pVM);
2150 char *pEnd = vmGetInBufEnd(pVM);
2151 char *pDest = pVM->pad;
2154 for (ch = *pSrc; (pEnd != pSrc) && (ch != ')'); ch = *++pSrc)
2158 if ((pEnd != pSrc) && (ch == ')'))
2161 vmTextOut(pVM, pVM->pad, 0);
2162 vmUpdateTib(pVM, pSrc);
2168 /**************************************************************************
2171 ** Interpretation: Interpretation semantics for this word are undefined.
2172 ** Compilation: ( c-addr1 u -- )
2173 ** Append the run-time semantics given below to the current definition.
2174 ** Run-time: ( -- c-addr2 u )
2175 ** Return c-addr2 u describing a string consisting of the characters
2176 ** specified by c-addr1 u during compilation. A program shall not alter
2177 ** the returned string.
2178 **************************************************************************/
2179 static void sLiteralCoIm(FICL_VM *pVM)
2181 FICL_DICT *dp = ficlGetDict();
2184 u = stackPopUNS(pVM->pStack);
2185 cp = stackPopPtr(pVM->pStack);
2187 dictAppendCell(dp, LVALUEtoCELL(pStringLit));
2188 cpDest = (char *) dp->here;
2189 *cpDest++ = (char) u;
2197 dp->here = PTRtoCELL alignPtr(cpDest);
2202 /**************************************************************************
2204 ** Return the address of the VM's state member (must be sized the
2205 ** same as a CELL for this reason)
2206 **************************************************************************/
2207 static void state(FICL_VM *pVM)
2209 stackPushPtr(pVM->pStack, &pVM->state);
2214 /**************************************************************************
2215 c r e a t e . . . d o e s >
2216 ** Make a new word in the dictionary with the run-time effect of
2217 ** a variable (push my address), but with extra space allotted
2218 ** for use by does> .
2219 **************************************************************************/
2221 static void createParen(FICL_VM *pVM)
2223 CELL *pCell = pVM->runningWord->param;
2224 stackPushPtr(pVM->pStack, pCell+1);
2229 static void create(FICL_VM *pVM)
2231 FICL_DICT *dp = ficlGetDict();
2232 STRINGINFO si = vmGetWord(pVM);
2234 dictAppendWord2(dp, si, createParen, FW_DEFAULT);
2235 dictAllotCells(dp, 1);
2240 static void doDoes(FICL_VM *pVM)
2242 CELL *pCell = pVM->runningWord->param;
2243 IPTYPE tempIP = (IPTYPE)((*pCell).p);
2244 stackPushPtr(pVM->pStack, pCell+1);
2245 vmPushIP(pVM, tempIP);
2250 static void doesParen(FICL_VM *pVM)
2252 FICL_DICT *dp = ficlGetDict();
2253 dp->smudge->code = doDoes;
2254 dp->smudge->param[0] = LVALUEtoCELL(pVM->ip);
2260 static void doesCoIm(FICL_VM *pVM)
2262 FICL_DICT *dp = ficlGetDict();
2263 #if FICL_WANT_LOCALS
2264 assert(pUnLinkParen);
2267 FICL_DICT *pLoc = ficlGetLoc();
2268 dictEmpty(pLoc, pLoc->pForthWords->size);
2269 dictAppendCell(dp, LVALUEtoCELL(pUnLinkParen));
2276 dictAppendCell(dp, LVALUEtoCELL(pDoesParen));
2281 /**************************************************************************
2283 ** to-body CORE ( xt -- a-addr )
2284 ** a-addr is the data-field address corresponding to xt. An ambiguous
2285 ** condition exists if xt is not for a word defined via CREATE.
2286 **************************************************************************/
2287 static void toBody(FICL_VM *pVM)
2289 FICL_WORD *pFW = stackPopPtr(pVM->pStack);
2290 stackPushPtr(pVM->pStack, pFW->param + 1);
2296 ** from-body ficl ( a-addr -- xt )
2297 ** Reverse effect of >body
2299 static void fromBody(FICL_VM *pVM)
2301 char *ptr = (char *) stackPopPtr(pVM->pStack) - sizeof (FICL_WORD);
2302 stackPushPtr(pVM->pStack, ptr);
2308 ** >name ficl ( xt -- c-addr u )
2309 ** Push the address and length of a word's name given its address
2312 static void toName(FICL_VM *pVM)
2314 FICL_WORD *pFW = stackPopPtr(pVM->pStack);
2315 stackPushPtr(pVM->pStack, pFW->name);
2316 stackPushUNS(pVM->pStack, pFW->nName);
2321 /**************************************************************************
2322 l b r a c k e t e t c
2324 **************************************************************************/
2326 static void lbracketCoIm(FICL_VM *pVM)
2328 pVM->state = INTERPRET;
2333 static void rbracket(FICL_VM *pVM)
2335 pVM->state = COMPILE;
2340 /**************************************************************************
2341 p i c t u r e d n u m e r i c w o r d s
2343 ** less-number-sign CORE ( -- )
2344 ** Initialize the pictured numeric output conversion process.
2346 **************************************************************************/
2347 static void lessNumberSign(FICL_VM *pVM)
2349 FICL_STRING *sp = PTRtoSTRING pVM->pad;
2355 ** number-sign CORE ( ud1 -- ud2 )
2356 ** Divide ud1 by the number in BASE giving the quotient ud2 and the remainder
2357 ** n. (n is the least-significant digit of ud1.) Convert n to external form
2358 ** and add the resulting character to the beginning of the pictured numeric
2359 ** output string. An ambiguous condition exists if # executes outside of a
2360 ** <# #> delimited number conversion.
2362 static void numberSign(FICL_VM *pVM)
2364 FICL_STRING *sp = PTRtoSTRING pVM->pad;
2368 u = u64Pop(pVM->pStack);
2369 rem = m64UMod(&u, (UNS16)(pVM->base));
2370 sp->text[sp->count++] = digit_to_char(rem);
2371 u64Push(pVM->pStack, u);
2376 ** number-sign-greater CORE ( xd -- c-addr u )
2377 ** Drop xd. Make the pictured numeric output string available as a character
2378 ** string. c-addr and u specify the resulting character string. A program
2379 ** may replace characters within the string.
2381 static void numberSignGreater(FICL_VM *pVM)
2383 FICL_STRING *sp = PTRtoSTRING pVM->pad;
2384 sp->text[sp->count] = '\0';
2386 stackDrop(pVM->pStack, 2);
2387 stackPushPtr(pVM->pStack, sp->text);
2388 stackPushUNS(pVM->pStack, sp->count);
2393 ** number-sign-s CORE ( ud1 -- ud2 )
2394 ** Convert one digit of ud1 according to the rule for #. Continue conversion
2395 ** until the quotient is zero. ud2 is zero. An ambiguous condition exists if
2396 ** #S executes outside of a <# #> delimited number conversion.
2397 ** TO DO: presently does not use ud1 hi cell - use it!
2399 static void numberSignS(FICL_VM *pVM)
2401 FICL_STRING *sp = PTRtoSTRING pVM->pad;
2405 u = u64Pop(pVM->pStack);
2409 rem = m64UMod(&u, (UNS16)(pVM->base));
2410 sp->text[sp->count++] = digit_to_char(rem);
2412 while (u.hi || u.lo);
2414 u64Push(pVM->pStack, u);
2419 ** HOLD CORE ( char -- )
2420 ** Add char to the beginning of the pictured numeric output string. An ambiguous
2421 ** condition exists if HOLD executes outside of a <# #> delimited number conversion.
2423 static void hold(FICL_VM *pVM)
2425 FICL_STRING *sp = PTRtoSTRING pVM->pad;
2426 int i = stackPopINT(pVM->pStack);
2427 sp->text[sp->count++] = (char) i;
2432 ** SIGN CORE ( n -- )
2433 ** If n is negative, add a minus sign to the beginning of the pictured
2434 ** numeric output string. An ambiguous condition exists if SIGN
2435 ** executes outside of a <# #> delimited number conversion.
2437 static void sign(FICL_VM *pVM)
2439 FICL_STRING *sp = PTRtoSTRING pVM->pad;
2440 int i = stackPopINT(pVM->pStack);
2442 sp->text[sp->count++] = '-';
2447 /**************************************************************************
2449 ** to-number CORE ( ud1 c-addr1 u1 -- ud2 c-addr2 u2 )
2450 ** ud2 is the unsigned result of converting the characters within the
2451 ** string specified by c-addr1 u1 into digits, using the number in BASE,
2452 ** and adding each into ud1 after multiplying ud1 by the number in BASE.
2453 ** Conversion continues left-to-right until a character that is not
2454 ** convertible, including any + or -, is encountered or the string is
2455 ** entirely converted. c-addr2 is the location of the first unconverted
2456 ** character or the first character past the end of the string if the string
2457 ** was entirely converted. u2 is the number of unconverted characters in the
2458 ** string. An ambiguous condition exists if ud2 overflows during the
2460 **************************************************************************/
2461 static void toNumber(FICL_VM *pVM)
2463 FICL_UNS count = stackPopUNS(pVM->pStack);
2464 char *cp = (char *)stackPopPtr(pVM->pStack);
2466 FICL_UNS base = pVM->base;
2470 accum = u64Pop(pVM->pStack);
2472 for (ch = *cp; count > 0; ch = *++cp, count--)
2480 digit = tolower(ch) - 'a' + 10;
2482 ** Note: following test also catches chars between 9 and a
2483 ** because 'digit' is unsigned!
2488 accum = m64Mac(accum, base, digit);
2491 u64Push(pVM->pStack, accum);
2492 stackPushPtr (pVM->pStack, cp);
2493 stackPushUNS(pVM->pStack, count);
2500 /**************************************************************************
2502 ** quit CORE ( -- ) ( R: i*x -- )
2503 ** Empty the return stack, store zero in SOURCE-ID if it is present, make
2504 ** the user input device the input source, and enter interpretation state.
2505 ** Do not display a message. Repeat the following:
2507 ** Accept a line from the input source into the input buffer, set >IN to
2508 ** zero, and interpret.
2509 ** Display the implementation-defined system prompt if in
2510 ** interpretation state, all processing has been completed, and no
2511 ** ambiguous condition exists.
2512 **************************************************************************/
2514 static void quit(FICL_VM *pVM)
2516 vmThrow(pVM, VM_QUIT);
2521 static void ficlAbort(FICL_VM *pVM)
2523 vmThrow(pVM, VM_ABORT);
2528 /**************************************************************************
2530 ** accept CORE ( c-addr +n1 -- +n2 )
2531 ** Receive a string of at most +n1 characters. An ambiguous condition
2532 ** exists if +n1 is zero or greater than 32,767. Display graphic characters
2533 ** as they are received. A program that depends on the presence or absence
2534 ** of non-graphic characters in the string has an environmental dependency.
2535 ** The editing functions, if any, that the system performs in order to
2536 ** construct the string are implementation-defined.
2538 ** (Although the standard text doesn't say so, I assume that the intent
2539 ** of 'accept' is to store the string at the address specified on
2541 ** Implementation: if there's more text in the TIB, use it. Otherwise
2542 ** throw out for more text. Copy characters up to the max count into the
2543 ** address given, and return the number of actual characters copied.
2545 ** Note (sobral) this may not be the behavior you'd expect if you're
2546 ** trying to get user input at load time!
2547 **************************************************************************/
2548 static void accept(FICL_VM *pVM)
2552 char *pBuf = vmGetInBuf(pVM);
2553 char *pEnd = vmGetInBufEnd(pVM);
2554 FICL_INT len = pEnd - pBuf;
2557 vmThrow(pVM, VM_RESTART);
2560 ** Now we have something in the text buffer - use it
2562 count = stackPopINT(pVM->pStack);
2563 cp = stackPopPtr(pVM->pStack);
2565 len = (count < len) ? count : len;
2566 strncpy(cp, vmGetInBuf(pVM), len);
2568 vmUpdateTib(pVM, pBuf);
2569 stackPushINT(pVM->pStack, len);
2575 /**************************************************************************
2577 ** 6.1.0705 ALIGN CORE ( -- )
2578 ** If the data-space pointer is not aligned, reserve enough space to
2580 **************************************************************************/
2581 static void align(FICL_VM *pVM)
2583 FICL_DICT *dp = ficlGetDict();
2590 /**************************************************************************
2593 **************************************************************************/
2594 static void aligned(FICL_VM *pVM)
2596 void *addr = stackPopPtr(pVM->pStack);
2597 stackPushPtr(pVM->pStack, alignPtr(addr));
2602 /**************************************************************************
2603 b e g i n & f r i e n d s
2604 ** Indefinite loop control structures
2607 ** : X ... BEGIN ... test UNTIL ;
2609 ** : X ... BEGIN ... test WHILE ... REPEAT ;
2610 **************************************************************************/
2611 static void beginCoIm(FICL_VM *pVM)
2613 FICL_DICT *dp = ficlGetDict();
2614 markBranch(dp, pVM, destTag);
2618 static void untilCoIm(FICL_VM *pVM)
2620 FICL_DICT *dp = ficlGetDict();
2624 dictAppendCell(dp, LVALUEtoCELL(pIfParen));
2625 resolveBackBranch(dp, pVM, destTag);
2629 static void whileCoIm(FICL_VM *pVM)
2631 FICL_DICT *dp = ficlGetDict();
2635 dictAppendCell(dp, LVALUEtoCELL(pIfParen));
2636 markBranch(dp, pVM, origTag);
2638 dictAppendUNS(dp, 1);
2642 static void repeatCoIm(FICL_VM *pVM)
2644 FICL_DICT *dp = ficlGetDict();
2646 assert(pBranchParen);
2647 dictAppendCell(dp, LVALUEtoCELL(pBranchParen));
2649 /* expect "begin" branch marker */
2650 resolveBackBranch(dp, pVM, destTag);
2651 /* expect "while" branch marker */
2652 resolveForwardBranch(dp, pVM, origTag);
2657 static void againCoIm(FICL_VM *pVM)
2659 FICL_DICT *dp = ficlGetDict();
2661 assert(pBranchParen);
2662 dictAppendCell(dp, LVALUEtoCELL(pBranchParen));
2664 /* expect "begin" branch marker */
2665 resolveBackBranch(dp, pVM, destTag);
2670 /**************************************************************************
2671 c h a r & f r i e n d s
2672 ** 6.1.0895 CHAR CORE ( "<spaces>name" -- char )
2673 ** Skip leading space delimiters. Parse name delimited by a space.
2674 ** Put the value of its first character onto the stack.
2676 ** bracket-char CORE
2677 ** Interpretation: Interpretation semantics for this word are undefined.
2678 ** Compilation: ( "<spaces>name" -- )
2679 ** Skip leading space delimiters. Parse name delimited by a space.
2680 ** Append the run-time semantics given below to the current definition.
2681 ** Run-time: ( -- char )
2682 ** Place char, the value of the first character of name, on the stack.
2683 **************************************************************************/
2684 static void ficlChar(FICL_VM *pVM)
2686 STRINGINFO si = vmGetWord(pVM);
2687 stackPushUNS(pVM->pStack, (FICL_UNS)(si.cp[0]));
2692 static void charCoIm(FICL_VM *pVM)
2699 /**************************************************************************
2701 ** char-plus CORE ( c-addr1 -- c-addr2 )
2702 ** Add the size in address units of a character to c-addr1, giving c-addr2.
2703 **************************************************************************/
2704 static void charPlus(FICL_VM *pVM)
2706 char *cp = stackPopPtr(pVM->pStack);
2707 stackPushPtr(pVM->pStack, cp + 1);
2711 /**************************************************************************
2713 ** chars CORE ( n1 -- n2 )
2714 ** n2 is the size in address units of n1 characters.
2715 ** For most processors, this function can be a no-op. To guarantee
2716 ** portability, we'll multiply by sizeof (char).
2717 **************************************************************************/
2718 #if defined (_M_IX86)
2719 #pragma warning(disable: 4127)
2721 static void ficlChars(FICL_VM *pVM)
2723 if (sizeof (char) > 1)
2725 FICL_INT i = stackPopINT(pVM->pStack);
2726 stackPushINT(pVM->pStack, i * sizeof (char));
2728 /* otherwise no-op! */
2731 #if defined (_M_IX86)
2732 #pragma warning(default: 4127)
2736 /**************************************************************************
2738 ** COUNT CORE ( c-addr1 -- c-addr2 u )
2739 ** Return the character string specification for the counted string stored
2740 ** at c-addr1. c-addr2 is the address of the first character after c-addr1.
2741 ** u is the contents of the character at c-addr1, which is the length in
2742 ** characters of the string at c-addr2.
2743 **************************************************************************/
2744 static void count(FICL_VM *pVM)
2746 FICL_STRING *sp = stackPopPtr(pVM->pStack);
2747 stackPushPtr(pVM->pStack, sp->text);
2748 stackPushUNS(pVM->pStack, sp->count);
2752 /**************************************************************************
2753 e n v i r o n m e n t ?
2754 ** environment-query CORE ( c-addr u -- false | i*x true )
2755 ** c-addr is the address of a character string and u is the string's
2756 ** character count. u may have a value in the range from zero to an
2757 ** implementation-defined maximum which shall not be less than 31. The
2758 ** character string should contain a keyword from 3.2.6 Environmental
2759 ** queries or the optional word sets to be checked for correspondence
2760 ** with an attribute of the present environment. If the system treats the
2761 ** attribute as unknown, the returned flag is false; otherwise, the flag
2762 ** is true and the i*x returned is of the type specified in the table for
2763 ** the attribute queried.
2764 **************************************************************************/
2765 static void environmentQ(FICL_VM *pVM)
2767 FICL_DICT *envp = ficlGetEnv();
2768 FICL_COUNT len = (FICL_COUNT)stackPopUNS(pVM->pStack);
2769 char *cp = stackPopPtr(pVM->pStack);
2774 &len; /* silence compiler warning... */
2776 pFW = dictLookup(envp, si);
2780 vmExecute(pVM, pFW);
2781 stackPushINT(pVM->pStack, FICL_TRUE);
2785 stackPushINT(pVM->pStack, FICL_FALSE);
2791 /**************************************************************************
2793 ** EVALUATE CORE ( i*x c-addr u -- j*x )
2794 ** Save the current input source specification. Store minus-one (-1) in
2795 ** SOURCE-ID if it is present. Make the string described by c-addr and u
2796 ** both the input source and input buffer, set >IN to zero, and interpret.
2797 ** When the parse area is empty, restore the prior input source
2798 ** specification. Other stack effects are due to the words EVALUATEd.
2800 **************************************************************************/
2801 static void evaluate(FICL_VM *pVM)
2803 FICL_INT count = stackPopINT(pVM->pStack);
2804 char *cp = stackPopPtr(pVM->pStack);
2809 pVM->sourceID.i = -1;
2810 result = ficlExecC(pVM, cp, count);
2812 if (result != VM_OUTOFTEXT)
2813 vmThrow(pVM, result);
2819 /**************************************************************************
2820 s t r i n g q u o t e
2821 ** Intrpreting: get string delimited by a quote from the input stream,
2822 ** copy to a scratch area, and put its count and address on the stack.
2823 ** Compiling: compile code to push the address and count of a string
2824 ** literal, compile the string from the input stream, and align the dict
2826 **************************************************************************/
2827 static void stringQuoteIm(FICL_VM *pVM)
2829 FICL_DICT *dp = ficlGetDict();
2831 if (pVM->state == INTERPRET)
2833 FICL_STRING *sp = (FICL_STRING *) dp->here;
2834 vmGetString(pVM, sp, '\"');
2835 stackPushPtr(pVM->pStack, sp->text);
2836 stackPushUNS(pVM->pStack, sp->count);
2838 else /* COMPILE state */
2840 dictAppendCell(dp, LVALUEtoCELL(pStringLit));
2841 dp->here = PTRtoCELL vmGetString(pVM, (FICL_STRING *)dp->here, '\"');
2849 /**************************************************************************
2851 ** Pop count and char address from stack and print the designated string.
2852 **************************************************************************/
2853 static void type(FICL_VM *pVM)
2855 FICL_UNS count = stackPopUNS(pVM->pStack);
2856 char *cp = stackPopPtr(pVM->pStack);
2857 char *pDest = (char *)ficlMalloc(count + 1);
2860 ** Since we don't have an output primitive for a counted string
2861 ** (oops), make sure the string is null terminated. If not, copy
2862 ** and terminate it.
2865 vmThrowErr(pVM, "Error: out of memory");
2867 strncpy(pDest, cp, count);
2868 pDest[count] = '\0';
2870 vmTextOut(pVM, pDest, 0);
2876 /**************************************************************************
2878 ** word CORE ( char "<chars>ccc<char>" -- c-addr )
2879 ** Skip leading delimiters. Parse characters ccc delimited by char. An
2880 ** ambiguous condition exists if the length of the parsed string is greater
2881 ** than the implementation-defined length of a counted string.
2883 ** c-addr is the address of a transient region containing the parsed word
2884 ** as a counted string. If the parse area was empty or contained no
2885 ** characters other than the delimiter, the resulting string has a zero
2886 ** length. A space, not included in the length, follows the string. A
2887 ** program may replace characters within the string.
2888 ** NOTE! Ficl also NULL-terminates the dest string.
2889 **************************************************************************/
2890 static void ficlWord(FICL_VM *pVM)
2892 FICL_STRING *sp = (FICL_STRING *)pVM->pad;
2893 char delim = (char)stackPopINT(pVM->pStack);
2896 si = vmParseStringEx(pVM, delim, 1);
2898 if (SI_COUNT(si) > nPAD-1)
2899 SI_SETLEN(si, nPAD-1);
2901 sp->count = (FICL_COUNT)SI_COUNT(si);
2902 strncpy(sp->text, SI_PTR(si), SI_COUNT(si));
2903 strcat(sp->text, " ");
2905 stackPushPtr(pVM->pStack, sp);
2910 /**************************************************************************
2912 ** ficl PARSE-WORD ( <spaces>name -- c-addr u )
2913 ** Skip leading spaces and parse name delimited by a space. c-addr is the
2914 ** address within the input buffer and u is the length of the selected
2915 ** string. If the parse area is empty, the resulting string has a zero length.
2916 **************************************************************************/
2917 static void parseNoCopy(FICL_VM *pVM)
2919 STRINGINFO si = vmGetWord0(pVM);
2920 stackPushPtr(pVM->pStack, SI_PTR(si));
2921 stackPushUNS(pVM->pStack, SI_COUNT(si));
2926 /**************************************************************************
2928 ** CORE EXT ( char "ccc<char>" -- c-addr u )
2929 ** Parse ccc delimited by the delimiter char.
2930 ** c-addr is the address (within the input buffer) and u is the length of
2931 ** the parsed string. If the parse area was empty, the resulting string has
2933 ** NOTE! PARSE differs from WORD: it does not skip leading delimiters.
2934 **************************************************************************/
2935 static void parse(FICL_VM *pVM)
2938 char delim = (char)stackPopINT(pVM->pStack);
2940 si = vmParseStringEx(pVM, delim, 0);
2941 stackPushPtr(pVM->pStack, SI_PTR(si));
2942 stackPushUNS(pVM->pStack, SI_COUNT(si));
2947 /**************************************************************************
2949 ** CORE ( c-addr u char -- )
2950 ** If u is greater than zero, store char in each of u consecutive
2951 ** characters of memory beginning at c-addr.
2952 **************************************************************************/
2953 static void fill(FICL_VM *pVM)
2955 char ch = (char)stackPopINT(pVM->pStack);
2956 FICL_UNS u = stackPopUNS(pVM->pStack);
2957 char *cp = (char *)stackPopPtr(pVM->pStack);
2969 /**************************************************************************
2971 ** FIND CORE ( c-addr -- c-addr 0 | xt 1 | xt -1 )
2972 ** Find the definition named in the counted string at c-addr. If the
2973 ** definition is not found, return c-addr and zero. If the definition is
2974 ** found, return its execution token xt. If the definition is immediate,
2975 ** also return one (1), otherwise also return minus-one (-1). For a given
2976 ** string, the values returned by FIND while compiling may differ from
2977 ** those returned while not compiling.
2978 **************************************************************************/
2979 static void find(FICL_VM *pVM)
2981 FICL_STRING *sp = stackPopPtr(pVM->pStack);
2986 pFW = dictLookup(ficlGetDict(), si);
2989 stackPushPtr(pVM->pStack, pFW);
2990 stackPushINT(pVM->pStack, (wordIsImmediate(pFW) ? 1 : -1));
2994 stackPushPtr(pVM->pStack, sp);
2995 stackPushUNS(pVM->pStack, 0);
3002 /**************************************************************************
3004 ** f-m-slash-mod CORE ( d1 n1 -- n2 n3 )
3005 ** Divide d1 by n1, giving the floored quotient n3 and the remainder n2.
3006 ** Input and output stack arguments are signed. An ambiguous condition
3007 ** exists if n1 is zero or if the quotient lies outside the range of a
3008 ** single-cell signed integer.
3009 **************************************************************************/
3010 static void fmSlashMod(FICL_VM *pVM)
3016 n1 = stackPopINT(pVM->pStack);
3017 d1 = i64Pop(pVM->pStack);
3018 qr = m64FlooredDivI(d1, n1);
3019 stackPushINT(pVM->pStack, qr.rem);
3020 stackPushINT(pVM->pStack, qr.quot);
3025 /**************************************************************************
3027 ** s-m-slash-rem CORE ( d1 n1 -- n2 n3 )
3028 ** Divide d1 by n1, giving the symmetric quotient n3 and the remainder n2.
3029 ** Input and output stack arguments are signed. An ambiguous condition
3030 ** exists if n1 is zero or if the quotient lies outside the range of a
3031 ** single-cell signed integer.
3032 **************************************************************************/
3033 static void smSlashRem(FICL_VM *pVM)
3039 n1 = stackPopINT(pVM->pStack);
3040 d1 = i64Pop(pVM->pStack);
3041 qr = m64SymmetricDivI(d1, n1);
3042 stackPushINT(pVM->pStack, qr.rem);
3043 stackPushINT(pVM->pStack, qr.quot);
3048 static void ficlMod(FICL_VM *pVM)
3054 n1 = stackPopINT(pVM->pStack);
3055 d1.lo = stackPopINT(pVM->pStack);
3057 qr = m64SymmetricDivI(d1, n1);
3058 stackPushINT(pVM->pStack, qr.rem);
3063 /**************************************************************************
3065 ** u-m-slash-mod CORE ( ud u1 -- u2 u3 )
3066 ** Divide ud by u1, giving the quotient u3 and the remainder u2.
3067 ** All values and arithmetic are unsigned. An ambiguous condition
3068 ** exists if u1 is zero or if the quotient lies outside the range of a
3069 ** single-cell unsigned integer.
3070 *************************************************************************/
3071 static void umSlashMod(FICL_VM *pVM)
3077 u1 = stackPopUNS(pVM->pStack);
3078 ud = u64Pop(pVM->pStack);
3079 qr = ficlLongDiv(ud, u1);
3080 stackPushUNS(pVM->pStack, qr.rem);
3081 stackPushUNS(pVM->pStack, qr.quot);
3086 /**************************************************************************
3088 ** l-shift CORE ( x1 u -- x2 )
3089 ** Perform a logical left shift of u bit-places on x1, giving x2.
3090 ** Put zeroes into the least significant bits vacated by the shift.
3091 ** An ambiguous condition exists if u is greater than or equal to the
3092 ** number of bits in a cell.
3094 ** r-shift CORE ( x1 u -- x2 )
3095 ** Perform a logical right shift of u bit-places on x1, giving x2.
3096 ** Put zeroes into the most significant bits vacated by the shift. An
3097 ** ambiguous condition exists if u is greater than or equal to the
3098 ** number of bits in a cell.
3099 **************************************************************************/
3100 static void lshift(FICL_VM *pVM)
3102 FICL_UNS nBits = stackPopUNS(pVM->pStack);
3103 FICL_UNS x1 = stackPopUNS(pVM->pStack);
3105 stackPushUNS(pVM->pStack, x1 << nBits);
3110 static void rshift(FICL_VM *pVM)
3112 FICL_UNS nBits = stackPopUNS(pVM->pStack);
3113 FICL_UNS x1 = stackPopUNS(pVM->pStack);
3115 stackPushUNS(pVM->pStack, x1 >> nBits);
3120 /**************************************************************************
3122 ** m-star CORE ( n1 n2 -- d )
3123 ** d is the signed product of n1 times n2.
3124 **************************************************************************/
3125 static void mStar(FICL_VM *pVM)
3127 FICL_INT n2 = stackPopINT(pVM->pStack);
3128 FICL_INT n1 = stackPopINT(pVM->pStack);
3131 d = m64MulI(n1, n2);
3132 i64Push(pVM->pStack, d);
3137 static void umStar(FICL_VM *pVM)
3139 FICL_UNS u2 = stackPopUNS(pVM->pStack);
3140 FICL_UNS u1 = stackPopUNS(pVM->pStack);
3143 ud = ficlLongMul(u1, u2);
3144 u64Push(pVM->pStack, ud);
3149 /**************************************************************************
3152 **************************************************************************/
3153 static void ficlMax(FICL_VM *pVM)
3155 FICL_INT n2 = stackPopINT(pVM->pStack);
3156 FICL_INT n1 = stackPopINT(pVM->pStack);
3158 stackPushINT(pVM->pStack, (n1 > n2) ? n1 : n2);
3162 static void ficlMin(FICL_VM *pVM)
3164 FICL_INT n2 = stackPopINT(pVM->pStack);
3165 FICL_INT n1 = stackPopINT(pVM->pStack);
3167 stackPushINT(pVM->pStack, (n1 < n2) ? n1 : n2);
3172 /**************************************************************************
3174 ** CORE ( addr1 addr2 u -- )
3175 ** If u is greater than zero, copy the contents of u consecutive address
3176 ** units at addr1 to the u consecutive address units at addr2. After MOVE
3177 ** completes, the u consecutive address units at addr2 contain exactly
3178 ** what the u consecutive address units at addr1 contained before the move.
3179 ** NOTE! This implementation assumes that a char is the same size as
3181 **************************************************************************/
3182 static void move(FICL_VM *pVM)
3184 FICL_UNS u = stackPopUNS(pVM->pStack);
3185 char *addr2 = stackPopPtr(pVM->pStack);
3186 char *addr1 = stackPopPtr(pVM->pStack);
3191 ** Do the copy carefully, so as to be
3192 ** correct even if the two ranges overlap
3197 *addr2++ = *addr1++;
3204 *addr2-- = *addr1--;
3211 /**************************************************************************
3214 **************************************************************************/
3215 static void recurseCoIm(FICL_VM *pVM)
3217 FICL_DICT *pDict = ficlGetDict();
3220 dictAppendCell(pDict, LVALUEtoCELL(pDict->smudge));
3225 /**************************************************************************
3227 ** s-to-d CORE ( n -- d )
3228 ** Convert the number n to the double-cell number d with the same
3230 **************************************************************************/
3231 static void sToD(FICL_VM *pVM)
3233 FICL_INT s = stackPopINT(pVM->pStack);
3235 /* sign extend to 64 bits.. */
3236 stackPushINT(pVM->pStack, s);
3237 stackPushINT(pVM->pStack, (s < 0) ? -1 : 0);
3242 /**************************************************************************
3244 ** CORE ( -- c-addr u )
3245 ** c-addr is the address of, and u is the number of characters in, the
3247 **************************************************************************/
3248 static void source(FICL_VM *pVM)
3250 stackPushPtr(pVM->pStack, pVM->tib.cp);
3251 stackPushINT(pVM->pStack, vmGetInBufLen(pVM));
3256 /**************************************************************************
3259 **************************************************************************/
3260 static void ficlVersion(FICL_VM *pVM)
3262 vmTextOut(pVM, "ficl Version " FICL_VER, 1);
3267 /**************************************************************************
3270 **************************************************************************/
3271 static void toIn(FICL_VM *pVM)
3273 stackPushPtr(pVM->pStack, &pVM->tib.index);
3278 /**************************************************************************
3279 d e f i n i t i o n s
3281 ** Make the compilation word list the same as the first word list in the
3282 ** search order. Specifies that the names of subsequent definitions will
3283 ** be placed in the compilation word list. Subsequent changes in the search
3284 ** order will not affect the compilation word list.
3285 **************************************************************************/
3286 static void definitions(FICL_VM *pVM)
3288 FICL_DICT *pDict = ficlGetDict();
3291 if (pDict->nLists < 1)
3293 vmThrowErr(pVM, "DEFINITIONS error - empty search order");
3296 pDict->pCompile = pDict->pSearch[pDict->nLists-1];
3301 /**************************************************************************
3302 f o r t h - w o r d l i s t
3303 ** SEARCH ( -- wid )
3304 ** Return wid, the identifier of the word list that includes all standard
3305 ** words provided by the implementation. This word list is initially the
3306 ** compilation word list and is part of the initial search order.
3307 **************************************************************************/
3308 static void forthWordlist(FICL_VM *pVM)
3310 FICL_HASH *pHash = ficlGetDict()->pForthWords;
3311 stackPushPtr(pVM->pStack, pHash);
3316 /**************************************************************************
3317 g e t - c u r r e n t
3318 ** SEARCH ( -- wid )
3319 ** Return wid, the identifier of the compilation word list.
3320 **************************************************************************/
3321 static void getCurrent(FICL_VM *pVM)
3323 ficlLockDictionary(TRUE);
3324 stackPushPtr(pVM->pStack, ficlGetDict()->pCompile);
3325 ficlLockDictionary(FALSE);
3330 /**************************************************************************
3332 ** SEARCH ( -- widn ... wid1 n )
3333 ** Returns the number of word lists n in the search order and the word list
3334 ** identifiers widn ... wid1 identifying these word lists. wid1 identifies
3335 ** the word list that is searched first, and widn the word list that is
3336 ** searched last. The search order is unaffected.
3337 **************************************************************************/
3338 static void getOrder(FICL_VM *pVM)
3340 FICL_DICT *pDict = ficlGetDict();
3341 int nLists = pDict->nLists;
3344 ficlLockDictionary(TRUE);
3345 for (i = 0; i < nLists; i++)
3347 stackPushPtr(pVM->pStack, pDict->pSearch[i]);
3350 stackPushUNS(pVM->pStack, nLists);
3351 ficlLockDictionary(FALSE);
3356 /**************************************************************************
3357 s e a r c h - w o r d l i s t
3358 ** SEARCH ( c-addr u wid -- 0 | xt 1 | xt -1 )
3359 ** Find the definition identified by the string c-addr u in the word list
3360 ** identified by wid. If the definition is not found, return zero. If the
3361 ** definition is found, return its execution token xt and one (1) if the
3362 ** definition is immediate, minus-one (-1) otherwise.
3363 **************************************************************************/
3364 static void searchWordlist(FICL_VM *pVM)
3369 FICL_HASH *pHash = stackPopPtr(pVM->pStack);
3371 si.count = (FICL_COUNT)stackPopUNS(pVM->pStack);
3372 si.cp = stackPopPtr(pVM->pStack);
3373 hashCode = hashHashCode(si);
3375 ficlLockDictionary(TRUE);
3376 pFW = hashLookup(pHash, si, hashCode);
3377 ficlLockDictionary(FALSE);
3381 stackPushPtr(pVM->pStack, pFW);
3382 stackPushINT(pVM->pStack, (wordIsImmediate(pFW) ? 1 : -1));
3386 stackPushUNS(pVM->pStack, 0);
3393 /**************************************************************************
3394 s e t - c u r r e n t
3395 ** SEARCH ( wid -- )
3396 ** Set the compilation word list to the word list identified by wid.
3397 **************************************************************************/
3398 static void setCurrent(FICL_VM *pVM)
3400 FICL_HASH *pHash = stackPopPtr(pVM->pStack);
3401 FICL_DICT *pDict = ficlGetDict();
3402 ficlLockDictionary(TRUE);
3403 pDict->pCompile = pHash;
3404 ficlLockDictionary(FALSE);
3409 /**************************************************************************
3411 ** SEARCH ( widn ... wid1 n -- )
3412 ** Set the search order to the word lists identified by widn ... wid1.
3413 ** Subsequently, word list wid1 will be searched first, and word list
3414 ** widn searched last. If n is zero, empty the search order. If n is minus
3415 ** one, set the search order to the implementation-defined minimum
3416 ** search order. The minimum search order shall include the words
3417 ** FORTH-WORDLIST and SET-ORDER. A system shall allow n to
3418 ** be at least eight.
3419 **************************************************************************/
3420 static void setOrder(FICL_VM *pVM)
3423 int nLists = stackPopINT(pVM->pStack);
3424 FICL_DICT *dp = ficlGetDict();
3426 if (nLists > FICL_DEFAULT_VOCS)
3428 vmThrowErr(pVM, "set-order error: list would be too large");
3431 ficlLockDictionary(TRUE);
3435 dp->nLists = nLists;
3436 for (i = nLists-1; i >= 0; --i)
3438 dp->pSearch[i] = stackPopPtr(pVM->pStack);
3443 dictResetSearchOrder(dp);
3446 ficlLockDictionary(FALSE);
3451 /**************************************************************************
3453 ** SEARCH ( -- wid )
3454 ** Create a new empty word list, returning its word list identifier wid.
3455 ** The new word list may be returned from a pool of preallocated word
3456 ** lists or may be dynamically allocated in data space. A system shall
3457 ** allow the creation of at least 8 new word lists in addition to any
3458 ** provided as part of the system.
3460 ** 1. ficl creates a new single-list hash in the dictionary and returns
3462 ** 2. ficl-wordlist takes an arg off the stack indicating the number of
3463 ** hash entries in the wordlist. Ficl 2.02 and later define WORDLIST as
3464 ** : wordlist 1 ficl-wordlist ;
3465 **************************************************************************/
3466 static void wordlist(FICL_VM *pVM)
3468 FICL_DICT *dp = ficlGetDict();
3473 vmCheckStack(pVM, 1, 1);
3475 nBuckets = stackPopUNS(pVM->pStack);
3478 pHash = (FICL_HASH *)dp->here;
3479 dictAllot(dp, sizeof (FICL_HASH)
3480 + (nBuckets-1) * sizeof (FICL_WORD *));
3482 pHash->size = nBuckets;
3485 stackPushPtr(pVM->pStack, pHash);
3490 /**************************************************************************
3493 ** Pop wid off the search order. Error if the search order is empty
3494 **************************************************************************/
3495 static void searchPop(FICL_VM *pVM)
3497 FICL_DICT *dp = ficlGetDict();
3500 ficlLockDictionary(TRUE);
3501 nLists = dp->nLists;
3504 vmThrowErr(pVM, "search> error: empty search order");
3506 stackPushPtr(pVM->pStack, dp->pSearch[--dp->nLists]);
3507 ficlLockDictionary(FALSE);
3512 /**************************************************************************
3515 ** Push wid onto the search order. Error if the search order is full.
3516 **************************************************************************/
3517 static void searchPush(FICL_VM *pVM)
3519 FICL_DICT *dp = ficlGetDict();
3521 ficlLockDictionary(TRUE);
3522 if (dp->nLists > FICL_DEFAULT_VOCS)
3524 vmThrowErr(pVM, ">search error: search order overflow");
3526 dp->pSearch[dp->nLists++] = stackPopPtr(pVM->pStack);
3527 ficlLockDictionary(FALSE);
3532 /**************************************************************************
3533 c o l o n N o N a m e
3534 ** CORE EXT ( C: -- colon-sys ) ( S: -- xt )
3535 ** Create an unnamed colon definition and push its address.
3536 ** Change state to compile.
3537 **************************************************************************/
3538 static void colonNoName(FICL_VM *pVM)
3540 FICL_DICT *dp = ficlGetDict();
3545 SI_SETPTR(si, NULL);
3547 pVM->state = COMPILE;
3548 pFW = dictAppendWord2(dp, si, colonParen, FW_DEFAULT | FW_SMUDGE);
3549 stackPushPtr(pVM->pStack, pFW);
3550 markControlTag(pVM, colonTag);
3555 /**************************************************************************
3556 u s e r V a r i a b l e
3557 ** user ( u -- ) "<spaces>name"
3558 ** Get a name from the input stream and create a user variable
3559 ** with the name and the index supplied. The run-time effect
3560 ** of a user variable is to push the address of the indexed cell
3561 ** in the running vm's user array.
3563 ** User variables are vm local cells. Each vm has an array of
3564 ** FICL_USER_CELLS of them when FICL_WANT_USER is nonzero.
3565 ** Ficl's user facility is implemented with two primitives,
3566 ** "user" and "(user)", a variable ("nUser") (in softcore.c) that
3567 ** holds the index of the next free user cell, and a redefinition
3568 ** (also in softcore) of "user" that defines a user word and increments
3570 **************************************************************************/
3572 static void userParen(FICL_VM *pVM)
3574 FICL_INT i = pVM->runningWord->param[0].i;
3575 stackPushPtr(pVM->pStack, &pVM->user[i]);
3580 static void userVariable(FICL_VM *pVM)
3582 FICL_DICT *dp = ficlGetDict();
3583 STRINGINFO si = vmGetWord(pVM);
3586 c = stackPop(pVM->pStack);
3587 if (c.i >= FICL_USER_CELLS)
3589 vmThrowErr(pVM, "Error - out of user space");
3592 dictAppendWord2(dp, si, userParen, FW_DEFAULT);
3593 dictAppendCell(dp, c);
3599 /**************************************************************************
3602 ** Interpretation: ( x "<spaces>name" -- )
3603 ** Skip leading spaces and parse name delimited by a space. Store x in
3604 ** name. An ambiguous condition exists if name was not defined by VALUE.
3605 ** NOTE: In ficl, VALUE is an alias of CONSTANT
3606 **************************************************************************/
3607 static void toValue(FICL_VM *pVM)
3609 STRINGINFO si = vmGetWord(pVM);
3610 FICL_DICT *dp = ficlGetDict();
3613 #if FICL_WANT_LOCALS
3614 if ((nLocals > 0) && (pVM->state == COMPILE))
3616 FICL_DICT *pLoc = ficlGetLoc();
3617 pFW = dictLookup(pLoc, si);
3618 if (pFW && (pFW->code == doLocalIm))
3620 dictAppendCell(dp, LVALUEtoCELL(pToLocalParen));
3621 dictAppendCell(dp, LVALUEtoCELL(pFW->param[0]));
3624 else if (pFW && pFW->code == do2LocalIm)
3626 dictAppendCell(dp, LVALUEtoCELL(pTo2LocalParen));
3627 dictAppendCell(dp, LVALUEtoCELL(pFW->param[0]));
3635 pFW = dictLookup(dp, si);
3638 int i = SI_COUNT(si);
3639 vmThrowErr(pVM, "%.*s not found", i, SI_PTR(si));
3642 if (pVM->state == INTERPRET)
3643 pFW->param[0] = stackPop(pVM->pStack);
3644 else /* compile code to store to word's param */
3646 stackPushPtr(pVM->pStack, &pFW->param[0]);
3648 dictAppendCell(dp, LVALUEtoCELL(pStore));
3654 #if FICL_WANT_LOCALS
3655 /**************************************************************************
3658 ** Link a frame on the return stack, reserving nCells of space for
3659 ** locals - the value of nCells is the next cell in the instruction
3661 **************************************************************************/
3662 static void linkParen(FICL_VM *pVM)
3664 FICL_INT nLink = *(FICL_INT *)(pVM->ip);
3665 vmBranchRelative(pVM, 1);
3666 stackLink(pVM->rStack, nLink);
3671 static void unlinkParen(FICL_VM *pVM)
3673 stackUnlink(pVM->rStack);
3678 /**************************************************************************
3680 ** Immediate - cfa of a local while compiling - when executed, compiles
3681 ** code to fetch the value of a local given the local's index in the
3683 **************************************************************************/
3684 static void getLocalParen(FICL_VM *pVM)
3686 FICL_INT nLocal = *(FICL_INT *)(pVM->ip++);
3687 stackPush(pVM->pStack, pVM->rStack->pFrame[nLocal]);
3692 static void toLocalParen(FICL_VM *pVM)
3694 FICL_INT nLocal = *(FICL_INT *)(pVM->ip++);
3695 pVM->rStack->pFrame[nLocal] = stackPop(pVM->pStack);
3700 static void getLocal0(FICL_VM *pVM)
3702 stackPush(pVM->pStack, pVM->rStack->pFrame[0]);
3707 static void toLocal0(FICL_VM *pVM)
3709 pVM->rStack->pFrame[0] = stackPop(pVM->pStack);
3714 static void getLocal1(FICL_VM *pVM)
3716 stackPush(pVM->pStack, pVM->rStack->pFrame[1]);
3721 static void toLocal1(FICL_VM *pVM)
3723 pVM->rStack->pFrame[1] = stackPop(pVM->pStack);
3729 ** Each local is recorded in a private locals dictionary as a
3730 ** word that does doLocalIm at runtime. DoLocalIm compiles code
3731 ** into the client definition to fetch the value of the
3732 ** corresponding local variable from the return stack.
3733 ** The private dictionary gets initialized at the end of each block
3734 ** that uses locals (in ; and does> for example).
3736 static void doLocalIm(FICL_VM *pVM)
3738 FICL_DICT *pDict = ficlGetDict();
3739 int nLocal = pVM->runningWord->param[0].i;
3741 if (pVM->state == INTERPRET)
3743 stackPush(pVM->pStack, pVM->rStack->pFrame[nLocal]);
3750 dictAppendCell(pDict, LVALUEtoCELL(pGetLocal0));
3752 else if (nLocal == 1)
3754 dictAppendCell(pDict, LVALUEtoCELL(pGetLocal1));
3758 dictAppendCell(pDict, LVALUEtoCELL(pGetLocalParen));
3759 dictAppendCell(pDict, LVALUEtoCELL(nLocal));
3766 /**************************************************************************
3768 ** paren-local-paren LOCAL
3769 ** Interpretation: Interpretation semantics for this word are undefined.
3770 ** Execution: ( c-addr u -- )
3771 ** When executed during compilation, (LOCAL) passes a message to the
3772 ** system that has one of two meanings. If u is non-zero,
3773 ** the message identifies a new local whose definition name is given by
3774 ** the string of characters identified by c-addr u. If u is zero,
3775 ** the message is last local and c-addr has no significance.
3777 ** The result of executing (LOCAL) during compilation of a definition is
3778 ** to create a set of named local identifiers, each of which is
3779 ** a definition name, that only have execution semantics within the scope
3780 ** of that definition's source.
3782 ** local Execution: ( -- x )
3784 ** Push the local's value, x, onto the stack. The local's value is
3785 ** initialized as described in 13.3.3 Processing locals and may be
3786 ** changed by preceding the local's name with TO. An ambiguous condition
3787 ** exists when local is executed while in interpretation state.
3788 **************************************************************************/
3789 static void localParen(FICL_VM *pVM)
3791 FICL_DICT *pDict = ficlGetDict();
3793 SI_SETLEN(si, stackPopUNS(pVM->pStack));
3794 SI_SETPTR(si, (char *)stackPopPtr(pVM->pStack));
3796 if (SI_COUNT(si) > 0)
3797 { /* add a local to the **locals** dict and update nLocals */
3798 FICL_DICT *pLoc = ficlGetLoc();
3799 if (nLocals >= FICL_MAX_LOCALS)
3801 vmThrowErr(pVM, "Error: out of local space");
3804 dictAppendWord2(pLoc, si, doLocalIm, FW_COMPIMMED);
3805 dictAppendCell(pLoc, LVALUEtoCELL(nLocals));
3808 { /* compile code to create a local stack frame */
3809 dictAppendCell(pDict, LVALUEtoCELL(pLinkParen));
3810 /* save location in dictionary for #locals */
3811 pMarkLocals = pDict->here;
3812 dictAppendCell(pDict, LVALUEtoCELL(nLocals));
3813 /* compile code to initialize first local */
3814 dictAppendCell(pDict, LVALUEtoCELL(pToLocal0));
3816 else if (nLocals == 1)
3818 dictAppendCell(pDict, LVALUEtoCELL(pToLocal1));
3822 dictAppendCell(pDict, LVALUEtoCELL(pToLocalParen));
3823 dictAppendCell(pDict, LVALUEtoCELL(nLocals));
3828 else if (nLocals > 0)
3829 { /* write nLocals to (link) param area in dictionary */
3830 *(FICL_INT *)pMarkLocals = nLocals;
3837 static void get2LocalParen(FICL_VM *pVM)
3839 FICL_INT nLocal = *(FICL_INT *)(pVM->ip++);
3840 stackPush(pVM->pStack, pVM->rStack->pFrame[nLocal]);
3841 stackPush(pVM->pStack, pVM->rStack->pFrame[nLocal+1]);
3846 static void do2LocalIm(FICL_VM *pVM)
3848 FICL_DICT *pDict = ficlGetDict();
3849 int nLocal = pVM->runningWord->param[0].i;
3851 if (pVM->state == INTERPRET)
3853 stackPush(pVM->pStack, pVM->rStack->pFrame[nLocal]);
3854 stackPush(pVM->pStack, pVM->rStack->pFrame[nLocal+1]);
3858 dictAppendCell(pDict, LVALUEtoCELL(pGet2LocalParen));
3859 dictAppendCell(pDict, LVALUEtoCELL(nLocal));
3865 static void to2LocalParen(FICL_VM *pVM)
3867 FICL_INT nLocal = *(FICL_INT *)(pVM->ip++);
3868 pVM->rStack->pFrame[nLocal+1] = stackPop(pVM->pStack);
3869 pVM->rStack->pFrame[nLocal] = stackPop(pVM->pStack);
3874 static void twoLocalParen(FICL_VM *pVM)
3876 FICL_DICT *pDict = ficlGetDict();
3878 SI_SETLEN(si, stackPopUNS(pVM->pStack));
3879 SI_SETPTR(si, (char *)stackPopPtr(pVM->pStack));
3881 if (SI_COUNT(si) > 0)
3882 { /* add a local to the **locals** dict and update nLocals */
3883 FICL_DICT *pLoc = ficlGetLoc();
3884 if (nLocals >= FICL_MAX_LOCALS)
3886 vmThrowErr(pVM, "Error: out of local space");
3889 dictAppendWord2(pLoc, si, do2LocalIm, FW_COMPIMMED);
3890 dictAppendCell(pLoc, LVALUEtoCELL(nLocals));
3893 { /* compile code to create a local stack frame */
3894 dictAppendCell(pDict, LVALUEtoCELL(pLinkParen));
3895 /* save location in dictionary for #locals */
3896 pMarkLocals = pDict->here;
3897 dictAppendCell(pDict, LVALUEtoCELL(nLocals));
3900 dictAppendCell(pDict, LVALUEtoCELL(pTo2LocalParen));
3901 dictAppendCell(pDict, LVALUEtoCELL(nLocals));
3905 else if (nLocals > 0)
3906 { /* write nLocals to (link) param area in dictionary */
3907 *(FICL_INT *)pMarkLocals = nLocals;
3915 /**************************************************************************
3918 ** setparentwid ( parent-wid wid -- )
3919 ** Set WID's link field to the parent-wid. search-wordlist will
3920 ** iterate through all the links when finding words in the child wid.
3921 **************************************************************************/
3922 static void setParentWid(FICL_VM *pVM)
3924 FICL_HASH *parent, *child;
3926 vmCheckStack(pVM, 2, 0);
3928 child = (FICL_HASH *)stackPopPtr(pVM->pStack);
3929 parent = (FICL_HASH *)stackPopPtr(pVM->pStack);
3931 child->link = parent;
3936 /**************************************************************************
3938 ** TOOLS ( "<spaces>name" -- )
3939 ** Display a human-readable representation of the named word's definition.
3940 ** The source of the representation (object-code decompilation, source
3941 ** block, etc.) and the particular form of the display is implementation
3943 ** NOTE: these funcs come late in the file because they reference all
3944 ** of the word-builder funcs without declaring them again. Call me lazy.
3945 **************************************************************************/
3948 ** Vet a candidate pointer carefully to make sure
3949 ** it's not some chunk o' inline data...
3950 ** It has to have a name, and it has to look
3951 ** like it's in the dictionary address range.
3952 ** NOTE: this excludes :noname words!
3954 static int isAFiclWord(FICL_WORD *pFW)
3956 FICL_DICT *pd = ficlGetDict();
3958 if (!dictIncludes(pd, pFW))
3961 if (!dictIncludes(pd, pFW->name))
3964 return ((pFW->nName > 0) && (pFW->name[pFW->nName] == '\0'));
3968 ** seeColon (for proctologists only)
3969 ** Walks a colon definition, decompiling
3970 ** on the fly. Knows about primitive control structures.
3972 static void seeColon(FICL_VM *pVM, CELL *pc)
3974 for (; pc->p != pSemiParen; pc++)
3976 FICL_WORD *pFW = (FICL_WORD *)(pc->p);
3978 if (isAFiclWord(pFW))
3980 if (pFW->code == literalParen)
3983 if (isAFiclWord(v.p))
3985 FICL_WORD *pLit = (FICL_WORD *)v.p;
3986 sprintf(pVM->pad, " literal %.*s (%#lx)",
3987 pLit->nName, pLit->name, v.u);
3990 sprintf(pVM->pad, " literal %ld (%#lx)", v.i, v.u);
3992 else if (pFW->code == stringLit)
3994 FICL_STRING *sp = (FICL_STRING *)(void *)++pc;
3995 pc = (CELL *)alignPtr(sp->text + sp->count + 1) - 1;
3996 sprintf(pVM->pad, " s\" %.*s\"", sp->count, sp->text);
3998 else if (pFW->code == ifParen)
4002 sprintf(pVM->pad, " if / while (branch rel %ld)", c.i);
4004 sprintf(pVM->pad, " until (branch rel %ld)", c.i);
4006 else if (pFW->code == branchParen)
4010 sprintf(pVM->pad, " else (branch rel %ld)", c.i);
4012 sprintf(pVM->pad, " repeat (branch rel %ld)", c.i);
4014 else if (pFW->code == qDoParen)
4017 sprintf(pVM->pad, " ?do (leave abs %#lx)", c.u);
4019 else if (pFW->code == doParen)
4022 sprintf(pVM->pad, " do (leave abs %#lx)", c.u);
4024 else if (pFW->code == loopParen)
4027 sprintf(pVM->pad, " loop (branch rel %#ld)", c.i);
4029 else if (pFW->code == plusLoopParen)
4032 sprintf(pVM->pad, " +loop (branch rel %#ld)", c.i);
4034 else /* default: print word's name */
4036 sprintf(pVM->pad, " %.*s", pFW->nName, pFW->name);
4039 vmTextOut(pVM, pVM->pad, 1);
4041 else /* probably not a word - punt and print value */
4043 sprintf(pVM->pad, " %ld (%#lx)", pc->i, pc->u);
4044 vmTextOut(pVM, pVM->pad, 1);
4048 vmTextOut(pVM, ";", 1);
4052 ** Here's the outer part of the decompiler. It's
4053 ** just a big nested conditional that checks the
4054 ** CFA of the word to decompile for each kind of
4055 ** known word-builder code, and tries to do
4056 ** something appropriate. If the CFA is not recognized,
4057 ** just indicate that it is a primitive.
4059 static void see(FICL_VM *pVM)
4064 pFW = (FICL_WORD *)stackPopPtr(pVM->pStack);
4066 if (pFW->code == colonParen)
4068 sprintf(pVM->pad, ": %.*s", pFW->nName, pFW->name);
4069 vmTextOut(pVM, pVM->pad, 1);
4070 seeColon(pVM, pFW->param);
4072 else if (pFW->code == doDoes)
4074 vmTextOut(pVM, "does>", 1);
4075 seeColon(pVM, (CELL *)pFW->param->p);
4077 else if (pFW->code == createParen)
4079 vmTextOut(pVM, "create", 1);
4081 else if (pFW->code == variableParen)
4083 sprintf(pVM->pad, "variable = %ld (%#lx)",
4084 pFW->param->i, pFW->param->u);
4085 vmTextOut(pVM, pVM->pad, 1);
4087 else if (pFW->code == userParen)
4089 sprintf(pVM->pad, "user variable %ld (%#lx)",
4090 pFW->param->i, pFW->param->u);
4091 vmTextOut(pVM, pVM->pad, 1);
4093 else if (pFW->code == constantParen)
4095 sprintf(pVM->pad, "constant = %ld (%#lx)",
4096 pFW->param->i, pFW->param->u);
4097 vmTextOut(pVM, pVM->pad, 1);
4101 vmTextOut(pVM, "primitive", 1);
4104 if (pFW->flags & FW_IMMEDIATE)
4106 vmTextOut(pVM, "immediate", 1);
4113 /**************************************************************************
4115 ** STRING ( c-addr1 u1 c-addr2 u2 -- n )
4116 ** Compare the string specified by c-addr1 u1 to the string specified by
4117 ** c-addr2 u2. The strings are compared, beginning at the given addresses,
4118 ** character by character, up to the length of the shorter string or until a
4119 ** difference is found. If the two strings are identical, n is zero. If the two
4120 ** strings are identical up to the length of the shorter string, n is minus-one
4121 ** (-1) if u1 is less than u2 and one (1) otherwise. If the two strings are not
4122 ** identical up to the length of the shorter string, n is minus-one (-1) if the
4123 ** first non-matching character in the string specified by c-addr1 u1 has a
4124 ** lesser numeric value than the corresponding character in the string specified
4125 ** by c-addr2 u2 and one (1) otherwise.
4126 **************************************************************************/
4127 static void compareString(FICL_VM *pVM)
4130 FICL_UNS u1, u2, uMin;
4133 vmCheckStack(pVM, 4, 1);
4134 u2 = stackPopUNS(pVM->pStack);
4135 cp2 = (char *)stackPopPtr(pVM->pStack);
4136 u1 = stackPopUNS(pVM->pStack);
4137 cp1 = (char *)stackPopPtr(pVM->pStack);
4139 uMin = (u1 < u2)? u1 : u2;
4140 for ( ; (uMin > 0) && (n == 0); uMin--)
4142 n = (int)(*cp1++ - *cp2++);
4153 stackPushINT(pVM->pStack, n);
4158 /**************************************************************************
4160 ** CORE EXT, FILE ( -- 0 | -1 | fileid )
4161 ** Identifies the input source as follows:
4163 ** SOURCE-ID Input source
4164 ** --------- ------------
4165 ** fileid Text file fileid
4166 ** -1 String (via EVALUATE)
4167 ** 0 User input device
4168 **************************************************************************/
4169 static void sourceid(FICL_VM *pVM)
4171 stackPushINT(pVM->pStack, pVM->sourceID.i);
4176 /**************************************************************************
4178 ** CORE EXT ( -- flag )
4179 ** Attempt to fill the input buffer from the input source, returning a true
4180 ** flag if successful.
4181 ** When the input source is the user input device, attempt to receive input
4182 ** into the terminal input buffer. If successful, make the result the input
4183 ** buffer, set >IN to zero, and return true. Receipt of a line containing no
4184 ** characters is considered successful. If there is no input available from
4185 ** the current input source, return false.
4186 ** When the input source is a string from EVALUATE, return false and
4187 ** perform no other action.
4188 **************************************************************************/
4189 static void refill(FICL_VM *pVM)
4191 static int tries = 0;
4193 FICL_INT ret = (pVM->sourceID.i == -1) ? FICL_FALSE : FICL_TRUE;
4194 if (ret && tries == 0) {
4196 vmThrow(pVM, VM_RESTART);
4200 stackPushINT(pVM->pStack, ret);
4205 /**************************************************************************
4207 ** TOOLS EXT ( "<spaces>name" -- )
4208 ** Skip leading space delimiters. Parse name delimited by a space.
4209 ** Find name, then delete name from the dictionary along with all
4210 ** words added to the dictionary after name. An ambiguous
4211 ** condition exists if name cannot be found.
4213 ** If the Search-Order word set is present, FORGET searches the
4214 ** compilation word list. An ambiguous condition exists if the
4215 ** compilation word list is deleted.
4216 **************************************************************************/
4217 static void forgetWid(FICL_VM *pVM)
4219 FICL_DICT *pDict = ficlGetDict();
4222 pHash = (FICL_HASH *)stackPopPtr(pVM->pStack);
4223 hashForget(pHash, pDict->here);
4229 static void forget(FICL_VM *pVM)
4232 FICL_DICT *pDict = ficlGetDict();
4233 FICL_HASH *pHash = pDict->pCompile;
4236 where = ((FICL_WORD *)stackPopPtr(pVM->pStack))->name;
4237 hashForget(pHash, where);
4238 pDict->here = PTRtoCELL where;
4243 /************************* freebsd added I/O words **************************/
4245 /* fopen - open a file and return new fd on stack.
4247 * fopen ( count ptr -- fd )
4249 static void pfopen(FICL_VM *pVM)
4255 vmCheckStack(pVM, 2, 1);
4257 (void)stackPopINT(pVM->pStack); /* don't need count value */
4258 p = stackPopPtr(pVM->pStack);
4259 fd = open(p, O_RDWR);
4260 stackPushINT(pVM->pStack, fd);
4264 /* fclose - close a file who's fd is on stack.
4268 static void pfclose(FICL_VM *pVM)
4273 vmCheckStack(pVM, 1, 0);
4275 fd = stackPopINT(pVM->pStack); /* get fd */
4281 /* fread - read file contents
4283 * fread ( fd buf nbytes -- nread )
4285 static void pfread(FICL_VM *pVM)
4291 vmCheckStack(pVM, 3, 1);
4293 len = stackPopINT(pVM->pStack); /* get number of bytes to read */
4294 buf = stackPopPtr(pVM->pStack); /* get buffer */
4295 fd = stackPopINT(pVM->pStack); /* get fd */
4296 if (len > 0 && buf && fd != -1)
4297 stackPushINT(pVM->pStack, read(fd, buf, len));
4299 stackPushINT(pVM->pStack, -1);
4303 /* fwrite - write file contents
4305 * fwrite ( fd buf nbytes -- nwritten )
4307 static void pfwrite(FICL_VM *pVM)
4312 vmCheckStack(pVM, 3, 1);
4314 len = stackPopINT(pVM->pStack); /* get number of bytes to write */
4315 buf = stackPopPtr(pVM->pStack); /* get buffer */
4316 fd = stackPopINT(pVM->pStack); /* get fd */
4317 if (len > 0 && buf && fd != -1)
4318 stackPushINT(pVM->pStack, write(fd, buf, len));
4320 stackPushINT(pVM->pStack, -1);
4323 /* flseek - seek to file offset
4325 * flseek ( fd offset whence -- whence )
4327 static void pflseek(FICL_VM *pVM)
4329 int fd, whence, offset;
4331 vmCheckStack(pVM, 3, 1);
4333 whence = stackPopINT(pVM->pStack); /* get whence */
4334 offset = stackPopINT(pVM->pStack); /* get offset */
4335 fd = stackPopINT(pVM->pStack); /* get fd */
4336 if (whence >= 0 && whence <= 2 && offset >= 0 && fd != -1)
4337 stackPushINT(pVM->pStack, lseek(fd, offset, whence));
4339 stackPushINT(pVM->pStack, -1);
4342 /* fload - interpret file contents
4346 static void pfload(FICL_VM *pVM)
4351 vmCheckStack(pVM, 1, 0);
4353 fd = stackPopINT(pVM->pStack); /* get fd */
4355 ficlExecFD(pVM, fd);
4359 /* key - get a character from stdin
4363 static void key(FICL_VM *pVM)
4366 vmCheckStack(pVM, 0, 1);
4368 stackPushINT(pVM->pStack, getchar());
4372 /* key? - check for a character from stdin (FACILITY)
4376 static void keyQuestion(FICL_VM *pVM)
4379 vmCheckStack(pVM, 0, 1);
4382 /* XXX Since we don't fiddle with termios, let it always succeed... */
4383 stackPushINT(pVM->pStack, FICL_TRUE);
4385 /* But here do the right thing. */
4386 stackPushINT(pVM->pStack, ischar()? FICL_TRUE : FICL_FALSE);
4391 /* seconds - gives number of seconds since beginning of time
4393 * beginning of time is defined as:
4395 * BTX - number of seconds since midnight
4396 * FreeBSD - number of seconds since Jan 1 1970
4400 static void pseconds(FICL_VM *pVM)
4403 vmCheckStack(pVM,0,1);
4405 stackPushUNS(pVM->pStack, (FICL_UNS) time(NULL));
4409 /* ms - wait at least that many milliseconds (FACILITY)
4414 static void ms(FICL_VM *pVM)
4417 vmCheckStack(pVM,1,0);
4420 usleep(stackPopUNS(pVM->pStack)*1000);
4422 delay(stackPopUNS(pVM->pStack)*1000);
4427 /* fkey - get a character from a file
4429 * fkey ( file -- char )
4431 static void fkey(FICL_VM *pVM)
4437 vmCheckStack(pVM, 1, 1);
4439 fd = stackPopINT(pVM->pStack);
4440 i = read(fd, &ch, 1);
4441 stackPushINT(pVM->pStack, i > 0 ? ch : -1);
4445 /**************************************************************************
4446 freebsd exception handling words
4447 ** Catch, from ANS Forth standard. Installs a safety net, then EXECUTE
4448 ** the word in ToS. If an exception happens, restore the state to what
4449 ** it was before, and pushes the exception value on the stack. If not,
4452 ** Notice that Catch implements an inner interpreter. This is ugly,
4453 ** but given how ficl works, it cannot be helped. The problem is that
4454 ** colon definitions will be executed *after* the function returns,
4455 ** while "code" definitions will be executed immediately. I considered
4456 ** other solutions to this problem, but all of them shared the same
4457 ** basic problem (with added disadvantages): if ficl ever changes it's
4458 ** inner thread modus operandi, one would have to fix this word.
4460 ** More comments can be found throughout catch's code.
4462 ** Daniel C. Sobral Jan 09/1999
4463 ** sadler may 2000 -- revised to follow ficl.c:ficlExecXT.
4464 **************************************************************************/
4466 static void ficlCatch(FICL_VM *pVM)
4468 static FICL_WORD *pQuit = NULL;
4478 pQuit = ficlLookup("exit-inner");
4486 ** We need this *before* we save the stack pointer, or
4487 ** we'll have to pop one element out of the stack after
4488 ** an exception. I prefer to get done with it up front. :-)
4491 vmCheckStack(pVM, 1, 0);
4493 pFW = stackPopPtr(pVM->pStack);
4496 ** Save vm's state -- a catch will not back out environmental
4499 ** We are *not* saving dictionary state, since it is
4500 ** global instead of per vm, and we are not saving
4501 ** stack contents, since we are not required to (and,
4502 ** thus, it would be useless). We save pVM, and pVM
4503 ** "stacks" (a structure containing general information
4504 ** about it, including the current stack pointer).
4506 memcpy((void*)&VM, (void*)pVM, sizeof(FICL_VM));
4507 memcpy((void*)&pStack, (void*)pVM->pStack, sizeof(FICL_STACK));
4508 memcpy((void*)&rStack, (void*)pVM->rStack, sizeof(FICL_STACK));
4511 ** Give pVM a jmp_buf
4513 pVM->pState = &vmState;
4518 except = setjmp(vmState);
4523 ** Setup condition - push poison pill so that the VM throws
4524 ** VM_INNEREXIT if the XT terminates normally, then execute
4528 vmPushIP(pVM, &pQuit); /* Open mouth, insert emetic */
4529 vmExecute(pVM, pFW);
4534 ** Normal exit from XT - lose the poison pill,
4535 ** restore old setjmp vector and push a zero.
4538 vmPopIP(pVM); /* Gack - hurl poison pill */
4539 pVM->pState = VM.pState; /* Restore just the setjmp vector */
4540 stackPushINT(pVM->pStack, 0); /* Push 0 -- everything is ok */
4544 ** Some other exception got thrown - restore pre-existing VM state
4545 ** and push the exception code
4548 /* Restore vm's state */
4549 memcpy((void*)pVM, (void*)&VM, sizeof(FICL_VM));
4550 memcpy((void*)pVM->pStack, (void*)&pStack, sizeof(FICL_STACK));
4551 memcpy((void*)pVM->rStack, (void*)&rStack, sizeof(FICL_STACK));
4553 stackPushINT(pVM->pStack, except);/* Push error */
4559 * Throw -- From ANS Forth standard.
4561 * Throw takes the ToS and, if that's different from zero,
4562 * returns to the last executed catch context. Further throws will
4563 * unstack previously executed "catches", in LIFO mode.
4565 * Daniel C. Sobral Jan 09/1999
4568 static void ficlThrow(FICL_VM *pVM)
4572 except = stackPopINT(pVM->pStack);
4575 vmThrow(pVM, except);
4579 static void ansAllocate(FICL_VM *pVM)
4584 size = stackPopINT(pVM->pStack);
4585 p = ficlMalloc(size);
4586 stackPushPtr(pVM->pStack, p);
4588 stackPushINT(pVM->pStack, 0);
4590 stackPushINT(pVM->pStack, 1);
4594 static void ansFree(FICL_VM *pVM)
4598 p = stackPopPtr(pVM->pStack);
4600 stackPushINT(pVM->pStack, 0);
4604 static void ansResize(FICL_VM *pVM)
4609 size = stackPopINT(pVM->pStack);
4610 old = stackPopPtr(pVM->pStack);
4611 new = ficlRealloc(old, size);
4614 stackPushPtr(pVM->pStack, new);
4615 stackPushINT(pVM->pStack, 0);
4619 stackPushPtr(pVM->pStack, old);
4620 stackPushINT(pVM->pStack, 1);
4625 ** Retrieves free space remaining on the dictionary
4628 static void freeHeap(FICL_VM *pVM)
4630 stackPushINT(pVM->pStack, dictCellsAvail(ficlGetDict()));
4635 ** Signals execXT that an inner loop has completed
4637 static void ficlExitInner(FICL_VM *pVM)
4639 vmThrow(pVM, VM_INNEREXIT);
4643 /**************************************************************************
4645 ** DOUBLE ( d1 -- d2 )
4646 ** d2 is the negation of d1.
4647 **************************************************************************/
4648 static void dnegate(FICL_VM *pVM)
4650 DPINT i = i64Pop(pVM->pStack);
4652 i64Push(pVM->pStack, i);
4657 /******************* Increase dictionary size on-demand ******************/
4659 static void ficlDictThreshold(FICL_VM *pVM)
4661 stackPushPtr(pVM->pStack, &dictThreshold);
4664 static void ficlDictIncrease(FICL_VM *pVM)
4666 stackPushPtr(pVM->pStack, &dictIncrease);
4669 /************************* freebsd added trace ***************************/
4672 static void ficlTrace(FICL_VM *pVM)
4675 vmCheckStack(pVM, 1, 1);
4678 ficl_trace = stackPopINT(pVM->pStack);
4682 /**************************************************************************
4683 f i c l C o m p i l e C o r e
4684 ** Builds the primitive wordset and the environment-query namespace.
4685 **************************************************************************/
4687 void ficlCompileCore(FICL_DICT *dp)
4693 ** see softcore.c for definitions of: abs bl space spaces abort"
4696 dictAppendWord(dp, "!", store, FW_DEFAULT);
4697 dictAppendWord(dp, "#", numberSign, FW_DEFAULT);
4698 dictAppendWord(dp, "#>", numberSignGreater,FW_DEFAULT);
4699 dictAppendWord(dp, "#s", numberSignS, FW_DEFAULT);
4700 dictAppendWord(dp, "\'", tick, FW_DEFAULT);
4701 dictAppendWord(dp, "(", commentHang, FW_IMMEDIATE);
4702 dictAppendWord(dp, "*", mul, FW_DEFAULT);
4703 dictAppendWord(dp, "*/", mulDiv, FW_DEFAULT);
4704 dictAppendWord(dp, "*/mod", mulDivRem, FW_DEFAULT);
4705 dictAppendWord(dp, "+", add, FW_DEFAULT);
4706 dictAppendWord(dp, "+!", plusStore, FW_DEFAULT);
4707 dictAppendWord(dp, "+loop", plusLoopCoIm, FW_COMPIMMED);
4709 dictAppendWord(dp, ",", comma, FW_DEFAULT);
4710 dictAppendWord(dp, "-", sub, FW_DEFAULT);
4711 dictAppendWord(dp, ".", displayCell, FW_DEFAULT);
4712 dictAppendWord(dp, ".#", displayCellNoPad, FW_DEFAULT);
4713 dictAppendWord(dp, ".\"", dotQuoteCoIm, FW_COMPIMMED);
4714 dictAppendWord(dp, "/", ficlDiv, FW_DEFAULT);
4715 dictAppendWord(dp, "/mod", slashMod, FW_DEFAULT);
4716 dictAppendWord(dp, "0<", zeroLess, FW_DEFAULT);
4717 dictAppendWord(dp, "0=", zeroEquals, FW_DEFAULT);
4718 dictAppendWord(dp, "0>", zeroGreater, FW_DEFAULT);
4719 dictAppendWord(dp, "1+", onePlus, FW_DEFAULT);
4720 dictAppendWord(dp, "1-", oneMinus, FW_DEFAULT);
4721 dictAppendWord(dp, "2!", twoStore, FW_DEFAULT);
4722 dictAppendWord(dp, "2*", twoMul, FW_DEFAULT);
4723 dictAppendWord(dp, "2/", twoDiv, FW_DEFAULT);
4724 dictAppendWord(dp, "2@", twoFetch, FW_DEFAULT);
4725 dictAppendWord(dp, "2drop", twoDrop, FW_DEFAULT);
4726 dictAppendWord(dp, "2dup", twoDup, FW_DEFAULT);
4727 dictAppendWord(dp, "2over", twoOver, FW_DEFAULT);
4728 dictAppendWord(dp, "2swap", twoSwap, FW_DEFAULT);
4729 dictAppendWord(dp, ":", colon, FW_DEFAULT);
4730 dictAppendWord(dp, ";", semicolonCoIm, FW_COMPIMMED);
4731 dictAppendWord(dp, "<", isLess, FW_DEFAULT);
4732 dictAppendWord(dp, "<#", lessNumberSign, FW_DEFAULT);
4733 dictAppendWord(dp, "=", isEqual, FW_DEFAULT);
4734 dictAppendWord(dp, ">", isGreater, FW_DEFAULT);
4735 dictAppendWord(dp, ">body", toBody, FW_DEFAULT);
4736 dictAppendWord(dp, ">in", toIn, FW_DEFAULT);
4737 dictAppendWord(dp, ">number", toNumber, FW_DEFAULT);
4738 dictAppendWord(dp, ">r", toRStack, FW_DEFAULT);
4739 dictAppendWord(dp, "?dup", questionDup, FW_DEFAULT);
4740 dictAppendWord(dp, "@", fetch, FW_DEFAULT);
4741 dictAppendWord(dp, "abort", ficlAbort, FW_DEFAULT);
4742 dictAppendWord(dp, "accept", accept, FW_DEFAULT);
4743 dictAppendWord(dp, "align", align, FW_DEFAULT);
4744 dictAppendWord(dp, "aligned", aligned, FW_DEFAULT);
4745 dictAppendWord(dp, "allot", allot, FW_DEFAULT);
4746 dictAppendWord(dp, "and", bitwiseAnd, FW_DEFAULT);
4747 dictAppendWord(dp, "base", base, FW_DEFAULT);
4748 dictAppendWord(dp, "begin", beginCoIm, FW_COMPIMMED);
4749 dictAppendWord(dp, "c!", cStore, FW_DEFAULT);
4750 dictAppendWord(dp, "c,", cComma, FW_DEFAULT);
4751 dictAppendWord(dp, "c@", cFetch, FW_DEFAULT);
4752 dictAppendWord(dp, "cell+", cellPlus, FW_DEFAULT);
4753 dictAppendWord(dp, "cells", cells, FW_DEFAULT);
4754 dictAppendWord(dp, "char", ficlChar, FW_DEFAULT);
4755 dictAppendWord(dp, "char+", charPlus, FW_DEFAULT);
4756 dictAppendWord(dp, "chars", ficlChars, FW_DEFAULT);
4757 dictAppendWord(dp, "constant", constant, FW_DEFAULT);
4758 dictAppendWord(dp, "count", count, FW_DEFAULT);
4759 dictAppendWord(dp, "cr", cr, FW_DEFAULT);
4760 dictAppendWord(dp, "create", create, FW_DEFAULT);
4761 dictAppendWord(dp, "decimal", decimal, FW_DEFAULT);
4762 dictAppendWord(dp, "depth", depth, FW_DEFAULT);
4763 dictAppendWord(dp, "do", doCoIm, FW_COMPIMMED);
4764 dictAppendWord(dp, "does>", doesCoIm, FW_COMPIMMED);
4765 dictAppendWord(dp, "drop", drop, FW_DEFAULT);
4766 dictAppendWord(dp, "dup", dup, FW_DEFAULT);
4767 dictAppendWord(dp, "else", elseCoIm, FW_COMPIMMED);
4768 dictAppendWord(dp, "emit", emit, FW_DEFAULT);
4769 dictAppendWord(dp, "environment?", environmentQ,FW_DEFAULT);
4770 dictAppendWord(dp, "evaluate", evaluate, FW_DEFAULT);
4771 dictAppendWord(dp, "execute", execute, FW_DEFAULT);
4772 dictAppendWord(dp, "exit", exitCoIm, FW_COMPIMMED);
4773 dictAppendWord(dp, "fill", fill, FW_DEFAULT);
4774 dictAppendWord(dp, "find", find, FW_DEFAULT);
4775 dictAppendWord(dp, "fm/mod", fmSlashMod, FW_DEFAULT);
4776 dictAppendWord(dp, "here", here, FW_DEFAULT);
4777 dictAppendWord(dp, "hex", hex, FW_DEFAULT);
4778 dictAppendWord(dp, "hold", hold, FW_DEFAULT);
4779 dictAppendWord(dp, "i", loopICo, FW_COMPILE);
4780 dictAppendWord(dp, "if", ifCoIm, FW_COMPIMMED);
4781 dictAppendWord(dp, "immediate", immediate, FW_DEFAULT);
4782 dictAppendWord(dp, "invert", bitwiseNot, FW_DEFAULT);
4783 dictAppendWord(dp, "j", loopJCo, FW_COMPILE);
4784 dictAppendWord(dp, "k", loopKCo, FW_COMPILE);
4785 dictAppendWord(dp, "leave", leaveCo, FW_COMPILE);
4786 dictAppendWord(dp, "literal", literalIm, FW_IMMEDIATE);
4787 dictAppendWord(dp, "loop", loopCoIm, FW_COMPIMMED);
4788 dictAppendWord(dp, "lshift", lshift, FW_DEFAULT);
4789 dictAppendWord(dp, "m*", mStar, FW_DEFAULT);
4790 dictAppendWord(dp, "max", ficlMax, FW_DEFAULT);
4791 dictAppendWord(dp, "min", ficlMin, FW_DEFAULT);
4792 dictAppendWord(dp, "mod", ficlMod, FW_DEFAULT);
4793 dictAppendWord(dp, "move", move, FW_DEFAULT);
4794 dictAppendWord(dp, "negate", negate, FW_DEFAULT);
4795 dictAppendWord(dp, "or", bitwiseOr, FW_DEFAULT);
4796 dictAppendWord(dp, "over", over, FW_DEFAULT);
4797 dictAppendWord(dp, "postpone", postponeCoIm, FW_COMPIMMED);
4798 dictAppendWord(dp, "quit", quit, FW_DEFAULT);
4799 dictAppendWord(dp, "r>", fromRStack, FW_DEFAULT);
4800 dictAppendWord(dp, "r@", fetchRStack, FW_DEFAULT);
4801 dictAppendWord(dp, "recurse", recurseCoIm, FW_COMPIMMED);
4802 dictAppendWord(dp, "repeat", repeatCoIm, FW_COMPIMMED);
4803 dictAppendWord(dp, "rot", rot, FW_DEFAULT);
4804 dictAppendWord(dp, "rshift", rshift, FW_DEFAULT);
4805 dictAppendWord(dp, "s\"", stringQuoteIm, FW_IMMEDIATE);
4806 dictAppendWord(dp, "s>d", sToD, FW_DEFAULT);
4807 dictAppendWord(dp, "sign", sign, FW_DEFAULT);
4808 dictAppendWord(dp, "sm/rem", smSlashRem, FW_DEFAULT);
4809 dictAppendWord(dp, "source", source, FW_DEFAULT);
4810 dictAppendWord(dp, "state", state, FW_DEFAULT);
4811 dictAppendWord(dp, "swap", swap, FW_DEFAULT);
4812 dictAppendWord(dp, "then", endifCoIm, FW_COMPIMMED);
4814 dictAppendWord(dp, "type", type, FW_DEFAULT);
4815 dictAppendWord(dp, "u.", uDot, FW_DEFAULT);
4816 dictAppendWord(dp, "u<", uIsLess, FW_DEFAULT);
4817 dictAppendWord(dp, "um*", umStar, FW_DEFAULT);
4818 dictAppendWord(dp, "um/mod", umSlashMod, FW_DEFAULT);
4819 dictAppendWord(dp, "unloop", unloopCo, FW_COMPILE);
4820 dictAppendWord(dp, "until", untilCoIm, FW_COMPIMMED);
4821 dictAppendWord(dp, "variable", variable, FW_DEFAULT);
4822 dictAppendWord(dp, "while", whileCoIm, FW_COMPIMMED);
4823 dictAppendWord(dp, "word", ficlWord, FW_DEFAULT);
4824 dictAppendWord(dp, "xor", bitwiseXor, FW_DEFAULT);
4825 dictAppendWord(dp, "[", lbracketCoIm, FW_COMPIMMED);
4826 dictAppendWord(dp, "[\']", bracketTickCoIm,FW_COMPIMMED);
4827 dictAppendWord(dp, "[char]", charCoIm, FW_COMPIMMED);
4828 dictAppendWord(dp, "]", rbracket, FW_DEFAULT);
4830 ** CORE EXT word set...
4831 ** see softcore.c for other definitions
4833 dictAppendWord(dp, ".(", dotParen, FW_DEFAULT);
4834 dictAppendWord(dp, ":noname", colonNoName, FW_DEFAULT);
4835 dictAppendWord(dp, "?do", qDoCoIm, FW_COMPIMMED);
4836 dictAppendWord(dp, "again", againCoIm, FW_COMPIMMED);
4837 dictAppendWord(dp, "parse", parse, FW_DEFAULT);
4838 dictAppendWord(dp, "pick", pick, FW_DEFAULT);
4839 dictAppendWord(dp, "roll", roll, FW_DEFAULT);
4840 dictAppendWord(dp, "refill", refill, FW_DEFAULT);
4841 dictAppendWord(dp, "source-id", sourceid, FW_DEFAULT);
4842 dictAppendWord(dp, "to", toValue, FW_IMMEDIATE);