/*******************************************************************1** w o r d s . c2** Forth Inspired Command Language3** ANS Forth CORE word-set written in C4** Author: John Sadler ([email protected])5** Created: 19 July 19976** $Id: words.c,v 1.17 2001/12/05 07:21:34 jsadler Exp $7*******************************************************************/8/*9** Copyright (c) 1997-2001 John Sadler ([email protected])10** All rights reserved.11**12** Get the latest Ficl release at http://ficl.sourceforge.net13**14** I am interested in hearing from anyone who uses ficl. If you have15** a problem, a success story, a defect, an enhancement request, or16** if you would like to contribute to the ficl release, please17** contact me by email at the address above.18**19** L I C E N S E and D I S C L A I M E R20**21** Redistribution and use in source and binary forms, with or without22** modification, are permitted provided that the following conditions23** are met:24** 1. Redistributions of source code must retain the above copyright25** notice, this list of conditions and the following disclaimer.26** 2. Redistributions in binary form must reproduce the above copyright27** notice, this list of conditions and the following disclaimer in the28** documentation and/or other materials provided with the distribution.29**30** THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND31** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE32** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE33** ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE34** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL35** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS36** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)37** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT38** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY39** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF40** SUCH DAMAGE.41*/424344#ifdef TESTMAIN45#include <stdlib.h>46#include <stdio.h>47#include <ctype.h>48#include <fcntl.h>49#else50#include <stand.h>51#endif52#include <string.h>53#include "ficl.h"54#include "math64.h"5556static void colonParen(FICL_VM *pVM);57static void literalIm(FICL_VM *pVM);58static int ficlParseWord(FICL_VM *pVM, STRINGINFO si);5960/*61** Control structure building words use these62** strings' addresses as markers on the stack to63** check for structure completion.64*/65static char doTag[] = "do";66static char colonTag[] = "colon";67static char leaveTag[] = "leave";6869static char destTag[] = "target";70static char origTag[] = "origin";7172static char caseTag[] = "case";73static char ofTag[] = "of";74static char fallthroughTag[] = "fallthrough";7576#if FICL_WANT_LOCALS77static void doLocalIm(FICL_VM *pVM);78static void do2LocalIm(FICL_VM *pVM);79#endif808182/*83** C O N T R O L S T R U C T U R E B U I L D E R S84**85** Push current dict location for later branch resolution.86** The location may be either a branch target or a patch address...87*/88static void markBranch(FICL_DICT *dp, FICL_VM *pVM, char *tag)89{90PUSHPTR(dp->here);91PUSHPTR(tag);92return;93}9495static void markControlTag(FICL_VM *pVM, char *tag)96{97PUSHPTR(tag);98return;99}100101static void matchControlTag(FICL_VM *pVM, char *tag)102{103char *cp;104#if FICL_ROBUST > 1105vmCheckStack(pVM, 1, 0);106#endif107cp = (char *)stackPopPtr(pVM->pStack);108/*109** Changed the code below to compare the pointers first (by popular demand)110*/111if ( (cp != tag) && strcmp(cp, tag) )112{113vmThrowErr(pVM, "Error -- unmatched control structure \"%s\"", tag);114}115116return;117}118119/*120** Expect a branch target address on the param stack,121** compile a literal offset from the current dict location122** to the target address123*/124static void resolveBackBranch(FICL_DICT *dp, FICL_VM *pVM, char *tag)125{126FICL_INT offset;127CELL *patchAddr;128129matchControlTag(pVM, tag);130131#if FICL_ROBUST > 1132vmCheckStack(pVM, 1, 0);133#endif134patchAddr = (CELL *)stackPopPtr(pVM->pStack);135offset = patchAddr - dp->here;136dictAppendCell(dp, LVALUEtoCELL(offset));137138return;139}140141142/*143** Expect a branch patch address on the param stack,144** compile a literal offset from the patch location145** to the current dict location146*/147static void resolveForwardBranch(FICL_DICT *dp, FICL_VM *pVM, char *tag)148{149FICL_INT offset;150CELL *patchAddr;151152matchControlTag(pVM, tag);153154#if FICL_ROBUST > 1155vmCheckStack(pVM, 1, 0);156#endif157patchAddr = (CELL *)stackPopPtr(pVM->pStack);158offset = dp->here - patchAddr;159*patchAddr = LVALUEtoCELL(offset);160161return;162}163164/*165** Match the tag to the top of the stack. If success,166** sopy "here" address into the cell whose address is next167** on the stack. Used by do..leave..loop.168*/169static void resolveAbsBranch(FICL_DICT *dp, FICL_VM *pVM, char *tag)170{171CELL *patchAddr;172char *cp;173174#if FICL_ROBUST > 1175vmCheckStack(pVM, 2, 0);176#endif177cp = stackPopPtr(pVM->pStack);178/*179** Changed the comparison below to compare the pointers first (by popular demand)180*/181if ((cp != tag) && strcmp(cp, tag))182{183vmTextOut(pVM, "Warning -- Unmatched control word: ", 0);184vmTextOut(pVM, tag, 1);185}186187patchAddr = (CELL *)stackPopPtr(pVM->pStack);188*patchAddr = LVALUEtoCELL(dp->here);189190return;191}192193194/**************************************************************************195f i c l P a r s e N u m b e r196** Attempts to convert the NULL terminated string in the VM's pad to197** a number using the VM's current base. If successful, pushes the number198** onto the param stack and returns TRUE. Otherwise, returns FALSE.199** (jws 8/01) Trailing decimal point causes a zero cell to be pushed. (See200** the standard for DOUBLE wordset.201**************************************************************************/202203int ficlParseNumber(FICL_VM *pVM, STRINGINFO si)204{205FICL_INT accum = 0;206char isNeg = FALSE;207char hasDP = FALSE;208unsigned base = pVM->base;209char *cp = SI_PTR(si);210FICL_COUNT count= (FICL_COUNT)SI_COUNT(si);211unsigned ch;212unsigned digit;213214if (count > 1)215{216switch (*cp)217{218case '-':219cp++;220count--;221isNeg = TRUE;222break;223case '+':224cp++;225count--;226isNeg = FALSE;227break;228default:229break;230}231}232233if ((count > 0) && (cp[count-1] == '.')) /* detect & remove trailing decimal */234{235hasDP = TRUE;236count--;237}238239if (count == 0) /* detect "+", "-", ".", "+." etc */240return FALSE;241242while ((count--) && ((ch = *cp++) != '\0'))243{244if (!isalnum(ch))245return FALSE;246247digit = ch - '0';248249if (digit > 9)250digit = tolower(ch) - 'a' + 10;251252if (digit >= base)253return FALSE;254255accum = accum * base + digit;256}257258if (hasDP) /* simple (required) DOUBLE support */259PUSHINT(0);260261if (isNeg)262accum = -accum;263264PUSHINT(accum);265if (pVM->state == COMPILE)266literalIm(pVM);267268return TRUE;269}270271272/**************************************************************************273a d d & f r i e n d s274**275**************************************************************************/276277static void add(FICL_VM *pVM)278{279FICL_INT i;280#if FICL_ROBUST > 1281vmCheckStack(pVM, 2, 1);282#endif283i = stackPopINT(pVM->pStack);284i += stackGetTop(pVM->pStack).i;285stackSetTop(pVM->pStack, LVALUEtoCELL(i));286return;287}288289static void sub(FICL_VM *pVM)290{291FICL_INT i;292#if FICL_ROBUST > 1293vmCheckStack(pVM, 2, 1);294#endif295i = stackPopINT(pVM->pStack);296i = stackGetTop(pVM->pStack).i - i;297stackSetTop(pVM->pStack, LVALUEtoCELL(i));298return;299}300301static void mul(FICL_VM *pVM)302{303FICL_INT i;304#if FICL_ROBUST > 1305vmCheckStack(pVM, 2, 1);306#endif307i = stackPopINT(pVM->pStack);308i *= stackGetTop(pVM->pStack).i;309stackSetTop(pVM->pStack, LVALUEtoCELL(i));310return;311}312313static void negate(FICL_VM *pVM)314{315FICL_INT i;316#if FICL_ROBUST > 1317vmCheckStack(pVM, 1, 1);318#endif319i = -stackPopINT(pVM->pStack);320PUSHINT(i);321return;322}323324static void ficlDiv(FICL_VM *pVM)325{326FICL_INT i;327#if FICL_ROBUST > 1328vmCheckStack(pVM, 2, 1);329#endif330i = stackPopINT(pVM->pStack);331i = stackGetTop(pVM->pStack).i / i;332stackSetTop(pVM->pStack, LVALUEtoCELL(i));333return;334}335336/*337** slash-mod CORE ( n1 n2 -- n3 n4 )338** Divide n1 by n2, giving the single-cell remainder n3 and the single-cell339** quotient n4. An ambiguous condition exists if n2 is zero. If n1 and n2340** differ in sign, the implementation-defined result returned will be the341** same as that returned by either the phrase342** >R S>D R> FM/MOD or the phrase >R S>D R> SM/REM .343** NOTE: Ficl complies with the second phrase (symmetric division)344*/345static void slashMod(FICL_VM *pVM)346{347DPINT n1;348FICL_INT n2;349INTQR qr;350351#if FICL_ROBUST > 1352vmCheckStack(pVM, 2, 2);353#endif354n2 = stackPopINT(pVM->pStack);355n1.lo = stackPopINT(pVM->pStack);356i64Extend(n1);357358qr = m64SymmetricDivI(n1, n2);359PUSHINT(qr.rem);360PUSHINT(qr.quot);361return;362}363364static void onePlus(FICL_VM *pVM)365{366FICL_INT i;367#if FICL_ROBUST > 1368vmCheckStack(pVM, 1, 1);369#endif370i = stackGetTop(pVM->pStack).i;371i += 1;372stackSetTop(pVM->pStack, LVALUEtoCELL(i));373return;374}375376static void oneMinus(FICL_VM *pVM)377{378FICL_INT i;379#if FICL_ROBUST > 1380vmCheckStack(pVM, 1, 1);381#endif382i = stackGetTop(pVM->pStack).i;383i -= 1;384stackSetTop(pVM->pStack, LVALUEtoCELL(i));385return;386}387388static void twoMul(FICL_VM *pVM)389{390FICL_INT i;391#if FICL_ROBUST > 1392vmCheckStack(pVM, 1, 1);393#endif394i = stackGetTop(pVM->pStack).i;395i *= 2;396stackSetTop(pVM->pStack, LVALUEtoCELL(i));397return;398}399400static void twoDiv(FICL_VM *pVM)401{402FICL_INT i;403#if FICL_ROBUST > 1404vmCheckStack(pVM, 1, 1);405#endif406i = stackGetTop(pVM->pStack).i;407i >>= 1;408stackSetTop(pVM->pStack, LVALUEtoCELL(i));409return;410}411412static void mulDiv(FICL_VM *pVM)413{414FICL_INT x, y, z;415DPINT prod;416#if FICL_ROBUST > 1417vmCheckStack(pVM, 3, 1);418#endif419z = stackPopINT(pVM->pStack);420y = stackPopINT(pVM->pStack);421x = stackPopINT(pVM->pStack);422423prod = m64MulI(x,y);424x = m64SymmetricDivI(prod, z).quot;425426PUSHINT(x);427return;428}429430431static void mulDivRem(FICL_VM *pVM)432{433FICL_INT x, y, z;434DPINT prod;435INTQR qr;436#if FICL_ROBUST > 1437vmCheckStack(pVM, 3, 2);438#endif439z = stackPopINT(pVM->pStack);440y = stackPopINT(pVM->pStack);441x = stackPopINT(pVM->pStack);442443prod = m64MulI(x,y);444qr = m64SymmetricDivI(prod, z);445446PUSHINT(qr.rem);447PUSHINT(qr.quot);448return;449}450451452/**************************************************************************453c o l o n d e f i n i t i o n s454** Code to begin compiling a colon definition455** This function sets the state to COMPILE, then creates a456** new word whose name is the next word in the input stream457** and whose code is colonParen.458**************************************************************************/459460static void colon(FICL_VM *pVM)461{462FICL_DICT *dp = vmGetDict(pVM);463STRINGINFO si = vmGetWord(pVM);464465dictCheckThreshold(dp);466467pVM->state = COMPILE;468markControlTag(pVM, colonTag);469dictAppendWord2(dp, si, colonParen, FW_DEFAULT | FW_SMUDGE);470#if FICL_WANT_LOCALS471pVM->pSys->nLocals = 0;472#endif473return;474}475476477/**************************************************************************478c o l o n P a r e n479** This is the code that executes a colon definition. It assumes that the480** virtual machine is running a "next" loop (See the vm.c481** for its implementation of member function vmExecute()). The colon482** code simply copies the address of the first word in the list of words483** to interpret into IP after saving its old value. When we return to the484** "next" loop, the virtual machine will call the code for each word in485** turn.486**487**************************************************************************/488489static void colonParen(FICL_VM *pVM)490{491IPTYPE tempIP = (IPTYPE) (pVM->runningWord->param);492vmPushIP(pVM, tempIP);493494return;495}496497498/**************************************************************************499s e m i c o l o n C o I m500**501** IMMEDIATE code for ";". This function sets the state to INTERPRET and502** terminates a word under compilation by appending code for "(;)" to503** the definition. TO DO: checks for leftover branch target tags on the504** return stack and complains if any are found.505**************************************************************************/506static void semiParen(FICL_VM *pVM)507{508vmPopIP(pVM);509return;510}511512513static void semicolonCoIm(FICL_VM *pVM)514{515FICL_DICT *dp = vmGetDict(pVM);516517assert(pVM->pSys->pSemiParen);518matchControlTag(pVM, colonTag);519520#if FICL_WANT_LOCALS521assert(pVM->pSys->pUnLinkParen);522if (pVM->pSys->nLocals > 0)523{524FICL_DICT *pLoc = ficlGetLoc(pVM->pSys);525dictEmpty(pLoc, pLoc->pForthWords->size);526dictAppendCell(dp, LVALUEtoCELL(pVM->pSys->pUnLinkParen));527}528pVM->pSys->nLocals = 0;529#endif530531dictAppendCell(dp, LVALUEtoCELL(pVM->pSys->pSemiParen));532pVM->state = INTERPRET;533dictUnsmudge(dp);534return;535}536537538/**************************************************************************539e x i t540** CORE541** This function simply pops the previous instruction542** pointer and returns to the "next" loop. Used for exiting from within543** a definition. Note that exitParen is identical to semiParen - they544** are in two different functions so that "see" can correctly identify545** the end of a colon definition, even if it uses "exit".546**************************************************************************/547static void exitParen(FICL_VM *pVM)548{549vmPopIP(pVM);550return;551}552553static void exitCoIm(FICL_VM *pVM)554{555FICL_DICT *dp = vmGetDict(pVM);556assert(pVM->pSys->pExitParen);557IGNORE(pVM);558559#if FICL_WANT_LOCALS560if (pVM->pSys->nLocals > 0)561{562dictAppendCell(dp, LVALUEtoCELL(pVM->pSys->pUnLinkParen));563}564#endif565dictAppendCell(dp, LVALUEtoCELL(pVM->pSys->pExitParen));566return;567}568569570/**************************************************************************571c o n s t a n t P a r e n572** This is the run-time code for "constant". It simply returns the573** contents of its word's first data cell.574**575**************************************************************************/576577void constantParen(FICL_VM *pVM)578{579FICL_WORD *pFW = pVM->runningWord;580#if FICL_ROBUST > 1581vmCheckStack(pVM, 0, 1);582#endif583stackPush(pVM->pStack, pFW->param[0]);584return;585}586587void twoConstParen(FICL_VM *pVM)588{589FICL_WORD *pFW = pVM->runningWord;590#if FICL_ROBUST > 1591vmCheckStack(pVM, 0, 2);592#endif593stackPush(pVM->pStack, pFW->param[0]); /* lo */594stackPush(pVM->pStack, pFW->param[1]); /* hi */595return;596}597598599/**************************************************************************600c o n s t a n t601** IMMEDIATE602** Compiles a constant into the dictionary. Constants return their603** value when invoked. Expects a value on top of the parm stack.604**************************************************************************/605606static void constant(FICL_VM *pVM)607{608FICL_DICT *dp = vmGetDict(pVM);609STRINGINFO si = vmGetWord(pVM);610611#if FICL_ROBUST > 1612vmCheckStack(pVM, 1, 0);613#endif614dictAppendWord2(dp, si, constantParen, FW_DEFAULT);615dictAppendCell(dp, stackPop(pVM->pStack));616return;617}618619620static void twoConstant(FICL_VM *pVM)621{622FICL_DICT *dp = vmGetDict(pVM);623STRINGINFO si = vmGetWord(pVM);624CELL c;625626#if FICL_ROBUST > 1627vmCheckStack(pVM, 2, 0);628#endif629c = stackPop(pVM->pStack);630dictAppendWord2(dp, si, twoConstParen, FW_DEFAULT);631dictAppendCell(dp, stackPop(pVM->pStack));632dictAppendCell(dp, c);633return;634}635636637/**************************************************************************638d i s p l a y C e l l639** Drop and print the contents of the cell at the top of the param640** stack641**************************************************************************/642643static void displayCell(FICL_VM *pVM)644{645CELL c;646#if FICL_ROBUST > 1647vmCheckStack(pVM, 1, 0);648#endif649c = stackPop(pVM->pStack);650ltoa((c).i, pVM->pad, pVM->base);651strcat(pVM->pad, " ");652vmTextOut(pVM, pVM->pad, 0);653return;654}655656static void uDot(FICL_VM *pVM)657{658FICL_UNS u;659#if FICL_ROBUST > 1660vmCheckStack(pVM, 1, 0);661#endif662u = stackPopUNS(pVM->pStack);663ultoa(u, pVM->pad, pVM->base);664strcat(pVM->pad, " ");665vmTextOut(pVM, pVM->pad, 0);666return;667}668669670static void hexDot(FICL_VM *pVM)671{672FICL_UNS u;673#if FICL_ROBUST > 1674vmCheckStack(pVM, 1, 0);675#endif676u = stackPopUNS(pVM->pStack);677ultoa(u, pVM->pad, 16);678strcat(pVM->pad, " ");679vmTextOut(pVM, pVM->pad, 0);680return;681}682683684/**************************************************************************685s t r l e n686** FICL ( c-string -- length )687**688** Returns the length of a C-style (zero-terminated) string.689**690** --lch691**/692static void ficlStrlen(FICL_VM *ficlVM)693{694char *address = (char *)stackPopPtr(ficlVM->pStack);695stackPushINT(ficlVM->pStack, strlen(address));696}697698699/**************************************************************************700s p r i n t f701** FICL ( i*x c-addr-fmt u-fmt c-addr-buffer u-buffer -- c-addr-buffer u-written success-flag )702** Similar to the C sprintf() function. It formats into a buffer based on703** a "format" string. Each character in the format string is copied verbatim704** to the output buffer, until SPRINTF encounters a percent sign ("%").705** SPRINTF then skips the percent sign, and examines the next character706** (the "format character"). Here are the valid format characters:707** s - read a C-ADDR U-LENGTH string from the stack and copy it to708** the buffer709** d - read a cell from the stack, format it as a string (base-10,710** signed), and copy it to the buffer711** x - same as d, except in base-16712** u - same as d, but unsigned713** % - output a literal percent-sign to the buffer714** SPRINTF returns the c-addr-buffer argument unchanged, the number of bytes715** written, and a flag indicating whether or not it ran out of space while716** writing to the output buffer (TRUE if it ran out of space).717**718** If SPRINTF runs out of space in the buffer to store the formatted string,719** it still continues parsing, in an effort to preserve your stack (otherwise720** it might leave uneaten arguments behind).721**722** --lch723**************************************************************************/724static void ficlSprintf(FICL_VM *pVM) /* */725{726int bufferLength = stackPopINT(pVM->pStack);727char *buffer = (char *)stackPopPtr(pVM->pStack);728char *bufferStart = buffer;729730int formatLength = stackPopINT(pVM->pStack);731char *format = (char *)stackPopPtr(pVM->pStack);732char *formatStop = format + formatLength;733734int base = 10;735int unsignedInteger = FALSE;736737FICL_INT append = FICL_TRUE;738739while (format < formatStop)740{741char scratch[64];742char *source;743int actualLength;744int desiredLength;745int leadingZeroes;746747748if (*format != '%')749{750source = format;751actualLength = desiredLength = 1;752leadingZeroes = 0;753}754else755{756format++;757if (format == formatStop)758break;759760leadingZeroes = (*format == '0');761if (leadingZeroes)762{763format++;764if (format == formatStop)765break;766}767768desiredLength = isdigit(*format);769if (desiredLength)770{771desiredLength = strtol(format, &format, 10);772if (format == formatStop)773break;774}775else if (*format == '*')776{777desiredLength = stackPopINT(pVM->pStack);778format++;779if (format == formatStop)780break;781}782783784switch (*format)785{786case 's':787case 'S':788{789actualLength = stackPopINT(pVM->pStack);790source = (char *)stackPopPtr(pVM->pStack);791break;792}793case 'x':794case 'X':795base = 16;796case 'u':797case 'U':798unsignedInteger = TRUE;799case 'd':800case 'D':801{802int integer = stackPopINT(pVM->pStack);803if (unsignedInteger)804ultoa(integer, scratch, base);805else806ltoa(integer, scratch, base);807base = 10;808unsignedInteger = FALSE;809source = scratch;810actualLength = strlen(scratch);811break;812}813case '%':814source = format;815actualLength = 1;816default:817continue;818}819}820821if (append != FICL_FALSE)822{823if (!desiredLength)824desiredLength = actualLength;825if (desiredLength > bufferLength)826{827append = FICL_FALSE;828desiredLength = bufferLength;829}830while (desiredLength > actualLength)831{832*buffer++ = (char)((leadingZeroes) ? '0' : ' ');833bufferLength--;834desiredLength--;835}836memcpy(buffer, source, actualLength);837buffer += actualLength;838bufferLength -= actualLength;839}840841format++;842}843844stackPushPtr(pVM->pStack, bufferStart);845stackPushINT(pVM->pStack, buffer - bufferStart);846stackPushINT(pVM->pStack, append);847}848849850/**************************************************************************851d u p & f r i e n d s852**853**************************************************************************/854855static void depth(FICL_VM *pVM)856{857int i;858#if FICL_ROBUST > 1859vmCheckStack(pVM, 0, 1);860#endif861i = stackDepth(pVM->pStack);862PUSHINT(i);863return;864}865866867static void drop(FICL_VM *pVM)868{869#if FICL_ROBUST > 1870vmCheckStack(pVM, 1, 0);871#endif872stackDrop(pVM->pStack, 1);873return;874}875876877static void twoDrop(FICL_VM *pVM)878{879#if FICL_ROBUST > 1880vmCheckStack(pVM, 2, 0);881#endif882stackDrop(pVM->pStack, 2);883return;884}885886887static void dup(FICL_VM *pVM)888{889#if FICL_ROBUST > 1890vmCheckStack(pVM, 1, 2);891#endif892stackPick(pVM->pStack, 0);893return;894}895896897static void twoDup(FICL_VM *pVM)898{899#if FICL_ROBUST > 1900vmCheckStack(pVM, 2, 4);901#endif902stackPick(pVM->pStack, 1);903stackPick(pVM->pStack, 1);904return;905}906907908static void over(FICL_VM *pVM)909{910#if FICL_ROBUST > 1911vmCheckStack(pVM, 2, 3);912#endif913stackPick(pVM->pStack, 1);914return;915}916917static void twoOver(FICL_VM *pVM)918{919#if FICL_ROBUST > 1920vmCheckStack(pVM, 4, 6);921#endif922stackPick(pVM->pStack, 3);923stackPick(pVM->pStack, 3);924return;925}926927928static void pick(FICL_VM *pVM)929{930CELL c = stackPop(pVM->pStack);931#if FICL_ROBUST > 1932vmCheckStack(pVM, c.i+1, c.i+2);933#endif934stackPick(pVM->pStack, c.i);935return;936}937938939static void questionDup(FICL_VM *pVM)940{941CELL c;942#if FICL_ROBUST > 1943vmCheckStack(pVM, 1, 2);944#endif945c = stackGetTop(pVM->pStack);946947if (c.i != 0)948stackPick(pVM->pStack, 0);949950return;951}952953954static void roll(FICL_VM *pVM)955{956int i = stackPop(pVM->pStack).i;957i = (i > 0) ? i : 0;958#if FICL_ROBUST > 1959vmCheckStack(pVM, i+1, i+1);960#endif961stackRoll(pVM->pStack, i);962return;963}964965966static void minusRoll(FICL_VM *pVM)967{968int i = stackPop(pVM->pStack).i;969i = (i > 0) ? i : 0;970#if FICL_ROBUST > 1971vmCheckStack(pVM, i+1, i+1);972#endif973stackRoll(pVM->pStack, -i);974return;975}976977978static void rot(FICL_VM *pVM)979{980#if FICL_ROBUST > 1981vmCheckStack(pVM, 3, 3);982#endif983stackRoll(pVM->pStack, 2);984return;985}986987988static void swap(FICL_VM *pVM)989{990#if FICL_ROBUST > 1991vmCheckStack(pVM, 2, 2);992#endif993stackRoll(pVM->pStack, 1);994return;995}996997998static void twoSwap(FICL_VM *pVM)999{1000#if FICL_ROBUST > 11001vmCheckStack(pVM, 4, 4);1002#endif1003stackRoll(pVM->pStack, 3);1004stackRoll(pVM->pStack, 3);1005return;1006}100710081009/**************************************************************************1010e m i t & f r i e n d s1011**1012**************************************************************************/10131014static void emit(FICL_VM *pVM)1015{1016char cp[2];1017int i;10181019#if FICL_ROBUST > 11020vmCheckStack(pVM, 1, 0);1021#endif1022i = stackPopINT(pVM->pStack);1023cp[0] = (char)i;1024cp[1] = '\0';1025vmTextOut(pVM, cp, 0);1026return;1027}102810291030static void cr(FICL_VM *pVM)1031{1032vmTextOut(pVM, "", 1);1033return;1034}103510361037static void commentLine(FICL_VM *pVM)1038{1039char *cp = vmGetInBuf(pVM);1040char *pEnd = vmGetInBufEnd(pVM);1041char ch = *cp;10421043while ((cp != pEnd) && (ch != '\r') && (ch != '\n'))1044{1045ch = *++cp;1046}10471048/*1049** Cope with DOS or UNIX-style EOLs -1050** Check for /r, /n, /r/n, or /n/r end-of-line sequences,1051** and point cp to next char. If EOL is \0, we're done.1052*/1053if (cp != pEnd)1054{1055cp++;10561057if ( (cp != pEnd) && (ch != *cp)1058&& ((*cp == '\r') || (*cp == '\n')) )1059cp++;1060}10611062vmUpdateTib(pVM, cp);1063return;1064}106510661067/*1068** paren CORE1069** Compilation: Perform the execution semantics given below.1070** Execution: ( "ccc<paren>" -- )1071** Parse ccc delimited by ) (right parenthesis). ( is an immediate word.1072** The number of characters in ccc may be zero to the number of characters1073** in the parse area.1074**1075*/1076static void commentHang(FICL_VM *pVM)1077{1078vmParseStringEx(pVM, ')', 0);1079return;1080}108110821083/**************************************************************************1084F E T C H & S T O R E1085**1086**************************************************************************/10871088static void fetch(FICL_VM *pVM)1089{1090CELL *pCell;1091#if FICL_ROBUST > 11092vmCheckStack(pVM, 1, 1);1093#endif1094pCell = (CELL *)stackPopPtr(pVM->pStack);1095stackPush(pVM->pStack, *pCell);1096return;1097}10981099/*1100** two-fetch CORE ( a-addr -- x1 x2 )1101** Fetch the cell pair x1 x2 stored at a-addr. x2 is stored at a-addr and1102** x1 at the next consecutive cell. It is equivalent to the sequence1103** DUP CELL+ @ SWAP @ .1104*/1105static void twoFetch(FICL_VM *pVM)1106{1107CELL *pCell;1108#if FICL_ROBUST > 11109vmCheckStack(pVM, 1, 2);1110#endif1111pCell = (CELL *)stackPopPtr(pVM->pStack);1112stackPush(pVM->pStack, *pCell++);1113stackPush(pVM->pStack, *pCell);1114swap(pVM);1115return;1116}11171118/*1119** store CORE ( x a-addr -- )1120** Store x at a-addr.1121*/1122static void store(FICL_VM *pVM)1123{1124CELL *pCell;1125#if FICL_ROBUST > 11126vmCheckStack(pVM, 2, 0);1127#endif1128pCell = (CELL *)stackPopPtr(pVM->pStack);1129*pCell = stackPop(pVM->pStack);1130}11311132/*1133** two-store CORE ( x1 x2 a-addr -- )1134** Store the cell pair x1 x2 at a-addr, with x2 at a-addr and x1 at the1135** next consecutive cell. It is equivalent to the sequence1136** SWAP OVER ! CELL+ ! .1137*/1138static void twoStore(FICL_VM *pVM)1139{1140CELL *pCell;1141#if FICL_ROBUST > 11142vmCheckStack(pVM, 3, 0);1143#endif1144pCell = (CELL *)stackPopPtr(pVM->pStack);1145*pCell++ = stackPop(pVM->pStack);1146*pCell = stackPop(pVM->pStack);1147}11481149static void plusStore(FICL_VM *pVM)1150{1151CELL *pCell;1152#if FICL_ROBUST > 11153vmCheckStack(pVM, 2, 0);1154#endif1155pCell = (CELL *)stackPopPtr(pVM->pStack);1156pCell->i += stackPop(pVM->pStack).i;1157}115811591160static void quadFetch(FICL_VM *pVM)1161{1162UNS32 *pw;1163#if FICL_ROBUST > 11164vmCheckStack(pVM, 1, 1);1165#endif1166pw = (UNS32 *)stackPopPtr(pVM->pStack);1167PUSHUNS((FICL_UNS)*pw);1168return;1169}11701171static void quadStore(FICL_VM *pVM)1172{1173UNS32 *pw;1174#if FICL_ROBUST > 11175vmCheckStack(pVM, 2, 0);1176#endif1177pw = (UNS32 *)stackPopPtr(pVM->pStack);1178*pw = (UNS32)(stackPop(pVM->pStack).u);1179}11801181static void wFetch(FICL_VM *pVM)1182{1183UNS16 *pw;1184#if FICL_ROBUST > 11185vmCheckStack(pVM, 1, 1);1186#endif1187pw = (UNS16 *)stackPopPtr(pVM->pStack);1188PUSHUNS((FICL_UNS)*pw);1189return;1190}11911192static void wStore(FICL_VM *pVM)1193{1194UNS16 *pw;1195#if FICL_ROBUST > 11196vmCheckStack(pVM, 2, 0);1197#endif1198pw = (UNS16 *)stackPopPtr(pVM->pStack);1199*pw = (UNS16)(stackPop(pVM->pStack).u);1200}12011202static void cFetch(FICL_VM *pVM)1203{1204UNS8 *pc;1205#if FICL_ROBUST > 11206vmCheckStack(pVM, 1, 1);1207#endif1208pc = (UNS8 *)stackPopPtr(pVM->pStack);1209PUSHUNS((FICL_UNS)*pc);1210return;1211}12121213static void cStore(FICL_VM *pVM)1214{1215UNS8 *pc;1216#if FICL_ROBUST > 11217vmCheckStack(pVM, 2, 0);1218#endif1219pc = (UNS8 *)stackPopPtr(pVM->pStack);1220*pc = (UNS8)(stackPop(pVM->pStack).u);1221}122212231224/**************************************************************************1225b r a n c h P a r e n1226**1227** Runtime for "(branch)" -- expects a literal offset in the next1228** compilation address, and branches to that location.1229**************************************************************************/12301231static void branchParen(FICL_VM *pVM)1232{1233vmBranchRelative(pVM, (uintptr_t)*(pVM->ip));1234return;1235}123612371238/**************************************************************************1239b r a n c h 01240** Runtime code for "(branch0)"; pop a flag from the stack,1241** branch if 0. fall through otherwise. The heart of "if" and "until".1242**************************************************************************/12431244static void branch0(FICL_VM *pVM)1245{1246FICL_UNS flag;12471248#if FICL_ROBUST > 11249vmCheckStack(pVM, 1, 0);1250#endif1251flag = stackPopUNS(pVM->pStack);12521253if (flag)1254{ /* fall through */1255vmBranchRelative(pVM, 1);1256}1257else1258{ /* take branch (to else/endif/begin) */1259vmBranchRelative(pVM, (uintptr_t)*(pVM->ip));1260}12611262return;1263}126412651266/**************************************************************************1267i f C o I m1268** IMMEDIATE COMPILE-ONLY1269** Compiles code for a conditional branch into the dictionary1270** and pushes the branch patch address on the stack for later1271** patching by ELSE or THEN/ENDIF.1272**************************************************************************/12731274static void ifCoIm(FICL_VM *pVM)1275{1276FICL_DICT *dp = vmGetDict(pVM);12771278assert(pVM->pSys->pBranch0);12791280dictAppendCell(dp, LVALUEtoCELL(pVM->pSys->pBranch0));1281markBranch(dp, pVM, origTag);1282dictAppendUNS(dp, 1);1283return;1284}128512861287/**************************************************************************1288e l s e C o I m1289**1290** IMMEDIATE COMPILE-ONLY1291** compiles an "else"...1292** 1) Compile a branch and a patch address; the address gets patched1293** by "endif" to point past the "else" code.1294** 2) Pop the "if" patch address1295** 3) Patch the "if" branch to point to the current compile address.1296** 4) Push the "else" patch address. ("endif" patches this to jump past1297** the "else" code.1298**************************************************************************/12991300static void elseCoIm(FICL_VM *pVM)1301{1302CELL *patchAddr;1303FICL_INT offset;1304FICL_DICT *dp = vmGetDict(pVM);13051306assert(pVM->pSys->pBranchParen);1307/* (1) compile branch runtime */1308dictAppendCell(dp, LVALUEtoCELL(pVM->pSys->pBranchParen));1309matchControlTag(pVM, origTag);1310patchAddr =1311(CELL *)stackPopPtr(pVM->pStack); /* (2) pop "if" patch addr */1312markBranch(dp, pVM, origTag); /* (4) push "else" patch addr */1313dictAppendUNS(dp, 1); /* (1) compile patch placeholder */1314offset = dp->here - patchAddr;1315*patchAddr = LVALUEtoCELL(offset); /* (3) Patch "if" */13161317return;1318}131913201321/**************************************************************************1322e n d i f C o I m1323** IMMEDIATE COMPILE-ONLY1324**************************************************************************/13251326static void endifCoIm(FICL_VM *pVM)1327{1328FICL_DICT *dp = vmGetDict(pVM);1329resolveForwardBranch(dp, pVM, origTag);1330return;1331}133213331334/**************************************************************************1335c a s e C o I m1336** IMMEDIATE COMPILE-ONLY1337**1338**1339** At compile-time, a CASE-SYS (see DPANS94 6.2.0873) looks like this:1340** i*addr i caseTag1341** and an OF-SYS (see DPANS94 6.2.1950) looks like this:1342** i*addr i caseTag addr ofTag1343** The integer under caseTag is the count of fixup addresses that branch1344** to ENDCASE.1345**************************************************************************/13461347static void caseCoIm(FICL_VM *pVM)1348{1349#if FICL_ROBUST > 11350vmCheckStack(pVM, 0, 2);1351#endif13521353PUSHUNS(0);1354markControlTag(pVM, caseTag);1355return;1356}135713581359/**************************************************************************1360e n d c a s eC o I m1361** IMMEDIATE COMPILE-ONLY1362**************************************************************************/13631364static void endcaseCoIm(FICL_VM *pVM)1365{1366FICL_UNS fixupCount;1367FICL_DICT *dp;1368CELL *patchAddr;1369FICL_INT offset;13701371assert(pVM->pSys->pDrop);13721373/*1374** if the last OF ended with FALLTHROUGH,1375** just add the FALLTHROUGH fixup to the1376** ENDOF fixups1377*/1378if (stackGetTop(pVM->pStack).p == fallthroughTag)1379{1380matchControlTag(pVM, fallthroughTag);1381patchAddr = POPPTR();1382matchControlTag(pVM, caseTag);1383fixupCount = POPUNS();1384PUSHPTR(patchAddr);1385PUSHUNS(fixupCount + 1);1386markControlTag(pVM, caseTag);1387}13881389matchControlTag(pVM, caseTag);13901391#if FICL_ROBUST > 11392vmCheckStack(pVM, 1, 0);1393#endif1394fixupCount = POPUNS();1395#if FICL_ROBUST > 11396vmCheckStack(pVM, fixupCount, 0);1397#endif13981399dp = vmGetDict(pVM);14001401dictAppendCell(dp, LVALUEtoCELL(pVM->pSys->pDrop));14021403while (fixupCount--)1404{1405patchAddr = (CELL *)stackPopPtr(pVM->pStack);1406offset = dp->here - patchAddr;1407*patchAddr = LVALUEtoCELL(offset);1408}1409return;1410}141114121413static void ofParen(FICL_VM *pVM)1414{1415FICL_UNS a, b;14161417#if FICL_ROBUST > 11418vmCheckStack(pVM, 2, 1);1419#endif14201421a = POPUNS();1422b = stackGetTop(pVM->pStack).u;14231424if (a == b)1425{ /* fall through */1426stackDrop(pVM->pStack, 1);1427vmBranchRelative(pVM, 1);1428}1429else1430{ /* take branch to next of or endswitch */1431vmBranchRelative(pVM, *(int *)(pVM->ip));1432}14331434return;1435}143614371438/**************************************************************************1439o f C o I m1440** IMMEDIATE COMPILE-ONLY1441**************************************************************************/14421443static void ofCoIm(FICL_VM *pVM)1444{1445FICL_DICT *dp = vmGetDict(pVM);1446CELL *fallthroughFixup = NULL;14471448assert(pVM->pSys->pBranch0);14491450#if FICL_ROBUST > 11451vmCheckStack(pVM, 1, 3);1452#endif14531454if (stackGetTop(pVM->pStack).p == fallthroughTag)1455{1456matchControlTag(pVM, fallthroughTag);1457fallthroughFixup = POPPTR();1458}14591460matchControlTag(pVM, caseTag);14611462markControlTag(pVM, caseTag);14631464dictAppendCell(dp, LVALUEtoCELL(pVM->pSys->pOfParen));1465markBranch(dp, pVM, ofTag);1466dictAppendUNS(dp, 2);14671468if (fallthroughFixup != NULL)1469{1470FICL_INT offset = dp->here - fallthroughFixup;1471*fallthroughFixup = LVALUEtoCELL(offset);1472}14731474return;1475}147614771478/**************************************************************************1479e n d o f C o I m1480** IMMEDIATE COMPILE-ONLY1481**************************************************************************/14821483static void endofCoIm(FICL_VM *pVM)1484{1485CELL *patchAddr;1486FICL_UNS fixupCount;1487FICL_INT offset;1488FICL_DICT *dp = vmGetDict(pVM);14891490#if FICL_ROBUST > 11491vmCheckStack(pVM, 4, 3);1492#endif14931494assert(pVM->pSys->pBranchParen);14951496/* ensure we're in an OF, */1497matchControlTag(pVM, ofTag);1498/* grab the address of the branch location after the OF */1499patchAddr = (CELL *)stackPopPtr(pVM->pStack);1500/* ensure we're also in a "case" */1501matchControlTag(pVM, caseTag);1502/* grab the current number of ENDOF fixups */1503fixupCount = POPUNS();15041505/* compile branch runtime */1506dictAppendCell(dp, LVALUEtoCELL(pVM->pSys->pBranchParen));15071508/* push a new ENDOF fixup, the updated count of ENDOF fixups, and the caseTag */1509PUSHPTR(dp->here);1510PUSHUNS(fixupCount + 1);1511markControlTag(pVM, caseTag);15121513/* reserve space for the ENDOF fixup */1514dictAppendUNS(dp, 2);15151516/* and patch the original OF */1517offset = dp->here - patchAddr;1518*patchAddr = LVALUEtoCELL(offset);1519}152015211522/**************************************************************************1523f a l l t h r o u g h C o I m1524** IMMEDIATE COMPILE-ONLY1525**************************************************************************/15261527static void fallthroughCoIm(FICL_VM *pVM)1528{1529CELL *patchAddr;1530FICL_INT offset;1531FICL_DICT *dp = vmGetDict(pVM);15321533#if FICL_ROBUST > 11534vmCheckStack(pVM, 4, 3);1535#endif15361537/* ensure we're in an OF, */1538matchControlTag(pVM, ofTag);1539/* grab the address of the branch location after the OF */1540patchAddr = (CELL *)stackPopPtr(pVM->pStack);1541/* ensure we're also in a "case" */1542matchControlTag(pVM, caseTag);15431544/* okay, here we go. put the case tag back. */1545markControlTag(pVM, caseTag);15461547/* compile branch runtime */1548dictAppendCell(dp, LVALUEtoCELL(pVM->pSys->pBranchParen));15491550/* push a new FALLTHROUGH fixup and the fallthroughTag */1551PUSHPTR(dp->here);1552markControlTag(pVM, fallthroughTag);15531554/* reserve space for the FALLTHROUGH fixup */1555dictAppendUNS(dp, 2);15561557/* and patch the original OF */1558offset = dp->here - patchAddr;1559*patchAddr = LVALUEtoCELL(offset);1560}15611562/**************************************************************************1563h a s h1564** hash ( c-addr u -- code)1565** calculates hashcode of specified string and leaves it on the stack1566**************************************************************************/15671568static void hash(FICL_VM *pVM)1569{1570STRINGINFO si;1571SI_SETLEN(si, stackPopUNS(pVM->pStack));1572SI_SETPTR(si, stackPopPtr(pVM->pStack));1573PUSHUNS(hashHashCode(si));1574return;1575}157615771578/**************************************************************************1579i n t e r p r e t1580** This is the "user interface" of a Forth. It does the following:1581** while there are words in the VM's Text Input Buffer1582** Copy next word into the pad (vmGetWord)1583** Attempt to find the word in the dictionary (dictLookup)1584** If successful, execute the word.1585** Otherwise, attempt to convert the word to a number (isNumber)1586** If successful, push the number onto the parameter stack.1587** Otherwise, print an error message and exit loop...1588** End Loop1589**1590** From the standard, section 3.41591** Text interpretation (see 6.1.1360 EVALUATE and 6.1.2050 QUIT) shall1592** repeat the following steps until either the parse area is empty or an1593** ambiguous condition exists:1594** a) Skip leading spaces and parse a name (see 3.4.1);1595**************************************************************************/15961597static void interpret(FICL_VM *pVM)1598{1599STRINGINFO si;1600int i;1601FICL_SYSTEM *pSys;16021603assert(pVM);16041605pSys = pVM->pSys;1606si = vmGetWord0(pVM);16071608/*1609** Get next word...if out of text, we're done.1610*/1611if (si.count == 0)1612{1613vmThrow(pVM, VM_OUTOFTEXT);1614}16151616/*1617** Attempt to find the incoming token in the dictionary. If that fails...1618** run the parse chain against the incoming token until somebody eats it.1619** Otherwise emit an error message and give up.1620** Although ficlParseWord could be part of the parse list, I've hard coded it1621** in for robustness. ficlInitSystem adds the other default steps to the list.1622*/1623if (ficlParseWord(pVM, si))1624return;16251626for (i=0; i < FICL_MAX_PARSE_STEPS; i++)1627{1628FICL_WORD *pFW = pSys->parseList[i];16291630if (pFW == NULL)1631break;16321633if (pFW->code == parseStepParen)1634{1635FICL_PARSE_STEP pStep;1636pStep = (FICL_PARSE_STEP)(pFW->param->fn);1637if ((*pStep)(pVM, si))1638return;1639}1640else1641{1642stackPushPtr(pVM->pStack, SI_PTR(si));1643stackPushUNS(pVM->pStack, SI_COUNT(si));1644ficlExecXT(pVM, pFW);1645if (stackPopINT(pVM->pStack))1646return;1647}1648}16491650i = SI_COUNT(si);1651vmThrowErr(pVM, "%.*s not found", i, SI_PTR(si));16521653return; /* back to inner interpreter */1654}165516561657/**************************************************************************1658f i c l P a r s e W o r d1659** From the standard, section 3.41660** b) Search the dictionary name space (see 3.4.2). If a definition name1661** matching the string is found:1662** 1.if interpreting, perform the interpretation semantics of the definition1663** (see 3.4.3.2), and continue at a);1664** 2.if compiling, perform the compilation semantics of the definition1665** (see 3.4.3.3), and continue at a).1666**1667** c) If a definition name matching the string is not found, attempt to1668** convert the string to a number (see 3.4.1.3). If successful:1669** 1.if interpreting, place the number on the data stack, and continue at a);1670** 2.if compiling, compile code that when executed will place the number on1671** the stack (see 6.1.1780 LITERAL), and continue at a);1672**1673** d) If unsuccessful, an ambiguous condition exists (see 3.4.4).1674**1675** (jws 4/01) Modified to be a FICL_PARSE_STEP1676**************************************************************************/1677static int ficlParseWord(FICL_VM *pVM, STRINGINFO si)1678{1679FICL_DICT *dp = vmGetDict(pVM);1680FICL_WORD *tempFW;16811682#if FICL_ROBUST1683dictCheck(dp, pVM, 0);1684vmCheckStack(pVM, 0, 0);1685#endif16861687#if FICL_WANT_LOCALS1688if (pVM->pSys->nLocals > 0)1689{1690tempFW = ficlLookupLoc(pVM->pSys, si);1691}1692else1693#endif1694tempFW = dictLookup(dp, si);16951696if (pVM->state == INTERPRET)1697{1698if (tempFW != NULL)1699{1700if (wordIsCompileOnly(tempFW))1701{1702vmThrowErr(pVM, "Error: Compile only!");1703}17041705vmExecute(pVM, tempFW);1706return (int)FICL_TRUE;1707}1708}17091710else /* (pVM->state == COMPILE) */1711{1712if (tempFW != NULL)1713{1714if (wordIsImmediate(tempFW))1715{1716vmExecute(pVM, tempFW);1717}1718else1719{1720dictAppendCell(dp, LVALUEtoCELL(tempFW));1721}1722return (int)FICL_TRUE;1723}1724}17251726return FICL_FALSE;1727}172817291730/*1731** Surrogate precompiled parse step for ficlParseWord (this step is hard coded in1732** INTERPRET)1733*/1734static void lookup(FICL_VM *pVM)1735{1736STRINGINFO si;1737SI_SETLEN(si, stackPopUNS(pVM->pStack));1738SI_SETPTR(si, stackPopPtr(pVM->pStack));1739stackPushINT(pVM->pStack, ficlParseWord(pVM, si));1740return;1741}174217431744/**************************************************************************1745p a r e n P a r s e S t e p1746** (parse-step) ( c-addr u -- flag )1747** runtime for a precompiled parse step - pop a counted string off the1748** stack, run the parse step against it, and push the result flag (FICL_TRUE1749** if success, FICL_FALSE otherwise).1750**************************************************************************/17511752void parseStepParen(FICL_VM *pVM)1753{1754STRINGINFO si;1755FICL_WORD *pFW = pVM->runningWord;1756FICL_PARSE_STEP pStep = (FICL_PARSE_STEP)(pFW->param->fn);17571758SI_SETLEN(si, stackPopINT(pVM->pStack));1759SI_SETPTR(si, stackPopPtr(pVM->pStack));17601761PUSHINT((*pStep)(pVM, si));17621763return;1764}176517661767static void addParseStep(FICL_VM *pVM)1768{1769FICL_WORD *pStep;1770FICL_DICT *pd = vmGetDict(pVM);1771#if FICL_ROBUST > 11772vmCheckStack(pVM, 1, 0);1773#endif1774pStep = (FICL_WORD *)(stackPop(pVM->pStack).p);1775if ((pStep != NULL) && isAFiclWord(pd, pStep))1776ficlAddParseStep(pVM->pSys, pStep);1777return;1778}177917801781/**************************************************************************1782l i t e r a l P a r e n1783**1784** This is the runtime for (literal). It assumes that it is part of a colon1785** definition, and that the next CELL contains a value to be pushed on the1786** parameter stack at runtime. This code is compiled by "literal".1787**1788**************************************************************************/17891790static void literalParen(FICL_VM *pVM)1791{1792#if FICL_ROBUST > 11793vmCheckStack(pVM, 0, 1);1794#endif1795PUSHINT(*(FICL_INT *)(pVM->ip));1796vmBranchRelative(pVM, 1);1797return;1798}17991800static void twoLitParen(FICL_VM *pVM)1801{1802#if FICL_ROBUST > 11803vmCheckStack(pVM, 0, 2);1804#endif1805PUSHINT(*((FICL_INT *)(pVM->ip)+1));1806PUSHINT(*(FICL_INT *)(pVM->ip));1807vmBranchRelative(pVM, 2);1808return;1809}181018111812/**************************************************************************1813l i t e r a l I m1814**1815** IMMEDIATE code for "literal". This function gets a value from the stack1816** and compiles it into the dictionary preceded by the code for "(literal)".1817** IMMEDIATE1818**************************************************************************/18191820static void literalIm(FICL_VM *pVM)1821{1822FICL_DICT *dp = vmGetDict(pVM);1823assert(pVM->pSys->pLitParen);18241825dictAppendCell(dp, LVALUEtoCELL(pVM->pSys->pLitParen));1826dictAppendCell(dp, stackPop(pVM->pStack));18271828return;1829}183018311832static void twoLiteralIm(FICL_VM *pVM)1833{1834FICL_DICT *dp = vmGetDict(pVM);1835assert(pVM->pSys->pTwoLitParen);18361837dictAppendCell(dp, LVALUEtoCELL(pVM->pSys->pTwoLitParen));1838dictAppendCell(dp, stackPop(pVM->pStack));1839dictAppendCell(dp, stackPop(pVM->pStack));18401841return;1842}18431844/**************************************************************************1845l o g i c a n d c o m p a r i s o n s1846**1847**************************************************************************/18481849static void zeroEquals(FICL_VM *pVM)1850{1851CELL c;1852#if FICL_ROBUST > 11853vmCheckStack(pVM, 1, 1);1854#endif1855c.i = FICL_BOOL(stackPopINT(pVM->pStack) == 0);1856stackPush(pVM->pStack, c);1857return;1858}18591860static void zeroLess(FICL_VM *pVM)1861{1862CELL c;1863#if FICL_ROBUST > 11864vmCheckStack(pVM, 1, 1);1865#endif1866c.i = FICL_BOOL(stackPopINT(pVM->pStack) < 0);1867stackPush(pVM->pStack, c);1868return;1869}18701871static void zeroGreater(FICL_VM *pVM)1872{1873CELL c;1874#if FICL_ROBUST > 11875vmCheckStack(pVM, 1, 1);1876#endif1877c.i = FICL_BOOL(stackPopINT(pVM->pStack) > 0);1878stackPush(pVM->pStack, c);1879return;1880}18811882static void isEqual(FICL_VM *pVM)1883{1884CELL x, y;18851886#if FICL_ROBUST > 11887vmCheckStack(pVM, 2, 1);1888#endif1889x = stackPop(pVM->pStack);1890y = stackPop(pVM->pStack);1891PUSHINT(FICL_BOOL(x.i == y.i));1892return;1893}18941895static void isLess(FICL_VM *pVM)1896{1897CELL x, y;1898#if FICL_ROBUST > 11899vmCheckStack(pVM, 2, 1);1900#endif1901y = stackPop(pVM->pStack);1902x = stackPop(pVM->pStack);1903PUSHINT(FICL_BOOL(x.i < y.i));1904return;1905}19061907static void uIsLess(FICL_VM *pVM)1908{1909FICL_UNS u1, u2;1910#if FICL_ROBUST > 11911vmCheckStack(pVM, 2, 1);1912#endif1913u2 = stackPopUNS(pVM->pStack);1914u1 = stackPopUNS(pVM->pStack);1915PUSHINT(FICL_BOOL(u1 < u2));1916return;1917}19181919static void isGreater(FICL_VM *pVM)1920{1921CELL x, y;1922#if FICL_ROBUST > 11923vmCheckStack(pVM, 2, 1);1924#endif1925y = stackPop(pVM->pStack);1926x = stackPop(pVM->pStack);1927PUSHINT(FICL_BOOL(x.i > y.i));1928return;1929}19301931static void uIsGreater(FICL_VM *pVM)1932{1933FICL_UNS u1, u2;1934#if FICL_ROBUST > 11935vmCheckStack(pVM, 2, 1);1936#endif1937u2 = stackPopUNS(pVM->pStack);1938u1 = stackPopUNS(pVM->pStack);1939PUSHINT(FICL_BOOL(u1 > u2));1940return;1941}19421943static void bitwiseAnd(FICL_VM *pVM)1944{1945CELL x, y;1946#if FICL_ROBUST > 11947vmCheckStack(pVM, 2, 1);1948#endif1949x = stackPop(pVM->pStack);1950y = stackPop(pVM->pStack);1951PUSHINT(x.i & y.i);1952return;1953}19541955static void bitwiseOr(FICL_VM *pVM)1956{1957CELL x, y;1958#if FICL_ROBUST > 11959vmCheckStack(pVM, 2, 1);1960#endif1961x = stackPop(pVM->pStack);1962y = stackPop(pVM->pStack);1963PUSHINT(x.i | y.i);1964return;1965}19661967static void bitwiseXor(FICL_VM *pVM)1968{1969CELL x, y;1970#if FICL_ROBUST > 11971vmCheckStack(pVM, 2, 1);1972#endif1973x = stackPop(pVM->pStack);1974y = stackPop(pVM->pStack);1975PUSHINT(x.i ^ y.i);1976return;1977}19781979static void bitwiseNot(FICL_VM *pVM)1980{1981CELL x;1982#if FICL_ROBUST > 11983vmCheckStack(pVM, 1, 1);1984#endif1985x = stackPop(pVM->pStack);1986PUSHINT(~x.i);1987return;1988}198919901991/**************************************************************************1992D o / L o o p1993** do -- IMMEDIATE COMPILE ONLY1994** Compiles code to initialize a loop: compile (do),1995** allot space to hold the "leave" address, push a branch1996** target address for the loop.1997** (do) -- runtime for "do"1998** pops index and limit from the p stack and moves them1999** to the r stack, then skips to the loop body.2000** loop -- IMMEDIATE COMPILE ONLY2001** +loop2002** Compiles code for the test part of a loop:2003** compile (loop), resolve forward branch from "do", and2004** copy "here" address to the "leave" address allotted by "do"2005** i,j,k -- COMPILE ONLY2006** Runtime: Push loop indices on param stack (i is innermost loop...)2007** Note: each loop has three values on the return stack:2008** ( R: leave limit index )2009** "leave" is the absolute address of the next cell after the loop2010** limit and index are the loop control variables.2011** leave -- COMPILE ONLY2012** Runtime: pop the loop control variables, then pop the2013** "leave" address and jump (absolute) there.2014**************************************************************************/20152016static void doCoIm(FICL_VM *pVM)2017{2018FICL_DICT *dp = vmGetDict(pVM);20192020assert(pVM->pSys->pDoParen);20212022dictAppendCell(dp, LVALUEtoCELL(pVM->pSys->pDoParen));2023/*2024** Allot space for a pointer to the end2025** of the loop - "leave" uses this...2026*/2027markBranch(dp, pVM, leaveTag);2028dictAppendUNS(dp, 0);2029/*2030** Mark location of head of loop...2031*/2032markBranch(dp, pVM, doTag);20332034return;2035}203620372038static void doParen(FICL_VM *pVM)2039{2040CELL index, limit;2041#if FICL_ROBUST > 12042vmCheckStack(pVM, 2, 0);2043#endif2044index = stackPop(pVM->pStack);2045limit = stackPop(pVM->pStack);20462047/* copy "leave" target addr to stack */2048stackPushPtr(pVM->rStack, *(pVM->ip++));2049stackPush(pVM->rStack, limit);2050stackPush(pVM->rStack, index);20512052return;2053}205420552056static void qDoCoIm(FICL_VM *pVM)2057{2058FICL_DICT *dp = vmGetDict(pVM);20592060assert(pVM->pSys->pQDoParen);20612062dictAppendCell(dp, LVALUEtoCELL(pVM->pSys->pQDoParen));2063/*2064** Allot space for a pointer to the end2065** of the loop - "leave" uses this...2066*/2067markBranch(dp, pVM, leaveTag);2068dictAppendUNS(dp, 0);2069/*2070** Mark location of head of loop...2071*/2072markBranch(dp, pVM, doTag);20732074return;2075}207620772078static void qDoParen(FICL_VM *pVM)2079{2080CELL index, limit;2081#if FICL_ROBUST > 12082vmCheckStack(pVM, 2, 0);2083#endif2084index = stackPop(pVM->pStack);2085limit = stackPop(pVM->pStack);20862087/* copy "leave" target addr to stack */2088stackPushPtr(pVM->rStack, *(pVM->ip++));20892090if (limit.u == index.u)2091{2092vmPopIP(pVM);2093}2094else2095{2096stackPush(pVM->rStack, limit);2097stackPush(pVM->rStack, index);2098}20992100return;2101}210221032104/*2105** Runtime code to break out of a do..loop construct2106** Drop the loop control variables; the branch address2107** past "loop" is next on the return stack.2108*/2109static void leaveCo(FICL_VM *pVM)2110{2111/* almost unloop */2112stackDrop(pVM->rStack, 2);2113/* exit */2114vmPopIP(pVM);2115return;2116}211721182119static void unloopCo(FICL_VM *pVM)2120{2121stackDrop(pVM->rStack, 3);2122return;2123}212421252126static void loopCoIm(FICL_VM *pVM)2127{2128FICL_DICT *dp = vmGetDict(pVM);21292130assert(pVM->pSys->pLoopParen);21312132dictAppendCell(dp, LVALUEtoCELL(pVM->pSys->pLoopParen));2133resolveBackBranch(dp, pVM, doTag);2134resolveAbsBranch(dp, pVM, leaveTag);2135return;2136}213721382139static void plusLoopCoIm(FICL_VM *pVM)2140{2141FICL_DICT *dp = vmGetDict(pVM);21422143assert(pVM->pSys->pPLoopParen);21442145dictAppendCell(dp, LVALUEtoCELL(pVM->pSys->pPLoopParen));2146resolveBackBranch(dp, pVM, doTag);2147resolveAbsBranch(dp, pVM, leaveTag);2148return;2149}215021512152static void loopParen(FICL_VM *pVM)2153{2154FICL_INT index = stackGetTop(pVM->rStack).i;2155FICL_INT limit = stackFetch(pVM->rStack, 1).i;21562157index++;21582159if (index >= limit)2160{2161stackDrop(pVM->rStack, 3); /* nuke the loop indices & "leave" addr */2162vmBranchRelative(pVM, 1); /* fall through the loop */2163}2164else2165{ /* update index, branch to loop head */2166stackSetTop(pVM->rStack, LVALUEtoCELL(index));2167vmBranchRelative(pVM, (uintptr_t)*(pVM->ip));2168}21692170return;2171}217221732174static void plusLoopParen(FICL_VM *pVM)2175{2176FICL_INT index,limit,increment;2177int flag;21782179#if FICL_ROBUST > 12180vmCheckStack(pVM, 1, 0);2181#endif21822183index = stackGetTop(pVM->rStack).i;2184limit = stackFetch(pVM->rStack, 1).i;2185increment = POP().i;21862187index += increment;21882189if (increment < 0)2190flag = (index < limit);2191else2192flag = (index >= limit);21932194if (flag)2195{2196stackDrop(pVM->rStack, 3); /* nuke the loop indices & "leave" addr */2197vmBranchRelative(pVM, 1); /* fall through the loop */2198}2199else2200{ /* update index, branch to loop head */2201stackSetTop(pVM->rStack, LVALUEtoCELL(index));2202vmBranchRelative(pVM, (uintptr_t)*(pVM->ip));2203}22042205return;2206}220722082209static void loopICo(FICL_VM *pVM)2210{2211CELL index = stackGetTop(pVM->rStack);2212stackPush(pVM->pStack, index);22132214return;2215}221622172218static void loopJCo(FICL_VM *pVM)2219{2220CELL index = stackFetch(pVM->rStack, 3);2221stackPush(pVM->pStack, index);22222223return;2224}222522262227static void loopKCo(FICL_VM *pVM)2228{2229CELL index = stackFetch(pVM->rStack, 6);2230stackPush(pVM->pStack, index);22312232return;2233}223422352236/**************************************************************************2237r e t u r n s t a c k2238**2239**************************************************************************/2240static void toRStack(FICL_VM *pVM)2241{2242#if FICL_ROBUST > 12243vmCheckStack(pVM, 1, 0);2244#endif22452246stackPush(pVM->rStack, POP());2247}22482249static void fromRStack(FICL_VM *pVM)2250{2251#if FICL_ROBUST > 12252vmCheckStack(pVM, 0, 1);2253#endif22542255PUSH(stackPop(pVM->rStack));2256}22572258static void fetchRStack(FICL_VM *pVM)2259{2260#if FICL_ROBUST > 12261vmCheckStack(pVM, 0, 1);2262#endif22632264PUSH(stackGetTop(pVM->rStack));2265}22662267static void twoToR(FICL_VM *pVM)2268{2269#if FICL_ROBUST > 12270vmCheckStack(pVM, 2, 0);2271#endif2272stackRoll(pVM->pStack, 1);2273stackPush(pVM->rStack, stackPop(pVM->pStack));2274stackPush(pVM->rStack, stackPop(pVM->pStack));2275return;2276}22772278static void twoRFrom(FICL_VM *pVM)2279{2280#if FICL_ROBUST > 12281vmCheckStack(pVM, 0, 2);2282#endif2283stackPush(pVM->pStack, stackPop(pVM->rStack));2284stackPush(pVM->pStack, stackPop(pVM->rStack));2285stackRoll(pVM->pStack, 1);2286return;2287}22882289static void twoRFetch(FICL_VM *pVM)2290{2291#if FICL_ROBUST > 12292vmCheckStack(pVM, 0, 2);2293#endif2294stackPush(pVM->pStack, stackFetch(pVM->rStack, 1));2295stackPush(pVM->pStack, stackFetch(pVM->rStack, 0));2296return;2297}229822992300/**************************************************************************2301v a r i a b l e2302**2303**************************************************************************/23042305static void variableParen(FICL_VM *pVM)2306{2307FICL_WORD *fw;2308#if FICL_ROBUST > 12309vmCheckStack(pVM, 0, 1);2310#endif23112312fw = pVM->runningWord;2313PUSHPTR(fw->param);2314}231523162317static void variable(FICL_VM *pVM)2318{2319FICL_DICT *dp = vmGetDict(pVM);2320STRINGINFO si = vmGetWord(pVM);23212322dictAppendWord2(dp, si, variableParen, FW_DEFAULT);2323dictAllotCells(dp, 1);2324return;2325}232623272328static void twoVariable(FICL_VM *pVM)2329{2330FICL_DICT *dp = vmGetDict(pVM);2331STRINGINFO si = vmGetWord(pVM);23322333dictAppendWord2(dp, si, variableParen, FW_DEFAULT);2334dictAllotCells(dp, 2);2335return;2336}233723382339/**************************************************************************2340b a s e & f r i e n d s2341**2342**************************************************************************/23432344static void base(FICL_VM *pVM)2345{2346CELL *pBase;2347#if FICL_ROBUST > 12348vmCheckStack(pVM, 0, 1);2349#endif23502351pBase = (CELL *)(&pVM->base);2352stackPush(pVM->pStack, LVALUEtoCELL(pBase));2353return;2354}235523562357static void decimal(FICL_VM *pVM)2358{2359pVM->base = 10;2360return;2361}236223632364static void hex(FICL_VM *pVM)2365{2366pVM->base = 16;2367return;2368}236923702371/**************************************************************************2372a l l o t & f r i e n d s2373**2374**************************************************************************/23752376static void allot(FICL_VM *pVM)2377{2378FICL_DICT *dp;2379FICL_INT i;2380#if FICL_ROBUST > 12381vmCheckStack(pVM, 1, 0);2382#endif23832384dp = vmGetDict(pVM);2385i = POPINT();23862387#if FICL_ROBUST2388dictCheck(dp, pVM, i);2389#endif23902391dictAllot(dp, i);2392return;2393}239423952396static void here(FICL_VM *pVM)2397{2398FICL_DICT *dp;2399#if FICL_ROBUST > 12400vmCheckStack(pVM, 0, 1);2401#endif24022403dp = vmGetDict(pVM);2404PUSHPTR(dp->here);2405return;2406}24072408static void comma(FICL_VM *pVM)2409{2410FICL_DICT *dp;2411CELL c;2412#if FICL_ROBUST > 12413vmCheckStack(pVM, 1, 0);2414#endif24152416dp = vmGetDict(pVM);2417c = POP();2418dictAppendCell(dp, c);2419return;2420}24212422static void cComma(FICL_VM *pVM)2423{2424FICL_DICT *dp;2425char c;2426#if FICL_ROBUST > 12427vmCheckStack(pVM, 1, 0);2428#endif24292430dp = vmGetDict(pVM);2431c = (char)POPINT();2432dictAppendChar(dp, c);2433return;2434}24352436static void cells(FICL_VM *pVM)2437{2438FICL_INT i;2439#if FICL_ROBUST > 12440vmCheckStack(pVM, 1, 1);2441#endif24422443i = POPINT();2444PUSHINT(i * (FICL_INT)sizeof (CELL));2445return;2446}24472448static void cellPlus(FICL_VM *pVM)2449{2450char *cp;2451#if FICL_ROBUST > 12452vmCheckStack(pVM, 1, 1);2453#endif24542455cp = POPPTR();2456PUSHPTR(cp + sizeof (CELL));2457return;2458}2459246024612462/**************************************************************************2463t i c k2464** tick CORE ( "<spaces>name" -- xt )2465** Skip leading space delimiters. Parse name delimited by a space. Find2466** name and return xt, the execution token for name. An ambiguous condition2467** exists if name is not found.2468**************************************************************************/2469void ficlTick(FICL_VM *pVM)2470{2471FICL_WORD *pFW = NULL;2472STRINGINFO si = vmGetWord(pVM);2473#if FICL_ROBUST > 12474vmCheckStack(pVM, 0, 1);2475#endif24762477pFW = dictLookup(vmGetDict(pVM), si);2478if (!pFW)2479{2480int i = SI_COUNT(si);2481vmThrowErr(pVM, "%.*s not found", i, SI_PTR(si));2482}2483PUSHPTR(pFW);2484return;2485}248624872488static void bracketTickCoIm(FICL_VM *pVM)2489{2490ficlTick(pVM);2491literalIm(pVM);24922493return;2494}249524962497/**************************************************************************2498p o s t p o n e2499** Lookup the next word in the input stream and compile code to2500** insert it into definitions created by the resulting word2501** (defers compilation, even of immediate words)2502**************************************************************************/25032504static void postponeCoIm(FICL_VM *pVM)2505{2506FICL_DICT *dp = vmGetDict(pVM);2507FICL_WORD *pFW;2508FICL_WORD *pComma = ficlLookup(pVM->pSys, ",");2509assert(pComma);25102511ficlTick(pVM);2512pFW = stackGetTop(pVM->pStack).p;2513if (wordIsImmediate(pFW))2514{2515dictAppendCell(dp, stackPop(pVM->pStack));2516}2517else2518{2519literalIm(pVM);2520dictAppendCell(dp, LVALUEtoCELL(pComma));2521}25222523return;2524}2525252625272528/**************************************************************************2529e x e c u t e2530** Pop an execution token (pointer to a word) off the stack and2531** run it2532**************************************************************************/25332534static void execute(FICL_VM *pVM)2535{2536FICL_WORD *pFW;2537#if FICL_ROBUST > 12538vmCheckStack(pVM, 1, 0);2539#endif25402541pFW = stackPopPtr(pVM->pStack);2542vmExecute(pVM, pFW);25432544return;2545}254625472548/**************************************************************************2549i m m e d i a t e2550** Make the most recently compiled word IMMEDIATE -- it executes even2551** in compile state (most often used for control compiling words2552** such as IF, THEN, etc)2553**************************************************************************/25542555static void immediate(FICL_VM *pVM)2556{2557IGNORE(pVM);2558dictSetImmediate(vmGetDict(pVM));2559return;2560}256125622563static void compileOnly(FICL_VM *pVM)2564{2565IGNORE(pVM);2566dictSetFlags(vmGetDict(pVM), FW_COMPILE, 0);2567return;2568}256925702571static void setObjectFlag(FICL_VM *pVM)2572{2573IGNORE(pVM);2574dictSetFlags(vmGetDict(pVM), FW_ISOBJECT, 0);2575return;2576}25772578static void isObject(FICL_VM *pVM)2579{2580FICL_INT flag;2581FICL_WORD *pFW = (FICL_WORD *)stackPopPtr(pVM->pStack);25822583flag = ((pFW != NULL) && (pFW->flags & FW_ISOBJECT)) ? FICL_TRUE : FICL_FALSE;2584stackPushINT(pVM->pStack, flag);2585return;2586}25872588static void cstringLit(FICL_VM *pVM)2589{2590FICL_STRING *sp = (FICL_STRING *)(pVM->ip);25912592char *cp = sp->text;2593cp += sp->count + 1;2594cp = alignPtr(cp);2595pVM->ip = (IPTYPE)(void *)cp;25962597stackPushPtr(pVM->pStack, sp);2598return;2599}260026012602static void cstringQuoteIm(FICL_VM *pVM)2603{2604FICL_DICT *dp = vmGetDict(pVM);26052606if (pVM->state == INTERPRET)2607{2608FICL_STRING *sp = (FICL_STRING *) dp->here;2609vmGetString(pVM, sp, '\"');2610stackPushPtr(pVM->pStack, sp);2611/* move HERE past string so it doesn't get overwritten. --lch */2612dictAllot(dp, sp->count + sizeof(FICL_COUNT));2613}2614else /* COMPILE state */2615{2616dictAppendCell(dp, LVALUEtoCELL(pVM->pSys->pCStringLit));2617dp->here = PTRtoCELL vmGetString(pVM, (FICL_STRING *)dp->here, '\"');2618dictAlign(dp);2619}26202621return;2622}26232624/**************************************************************************2625d o t Q u o t e2626** IMMEDIATE word that compiles a string literal for later display2627** Compile stringLit, then copy the bytes of the string from the TIB2628** to the dictionary. Backpatch the count byte and align the dictionary.2629**2630** stringlit: Fetch the count from the dictionary, then push the address2631** and count on the stack. Finally, update ip to point to the first2632** aligned address after the string text.2633**************************************************************************/26342635static void stringLit(FICL_VM *pVM)2636{2637FICL_STRING *sp;2638FICL_COUNT count;2639char *cp;2640#if FICL_ROBUST > 12641vmCheckStack(pVM, 0, 2);2642#endif26432644sp = (FICL_STRING *)(pVM->ip);2645count = sp->count;2646cp = sp->text;2647PUSHPTR(cp);2648PUSHUNS(count);2649cp += count + 1;2650cp = alignPtr(cp);2651pVM->ip = (IPTYPE)(void *)cp;2652}26532654static void dotQuoteCoIm(FICL_VM *pVM)2655{2656FICL_DICT *dp = vmGetDict(pVM);2657FICL_WORD *pType = ficlLookup(pVM->pSys, "type");2658assert(pType);2659dictAppendCell(dp, LVALUEtoCELL(pVM->pSys->pStringLit));2660dp->here = PTRtoCELL vmGetString(pVM, (FICL_STRING *)dp->here, '\"');2661dictAlign(dp);2662dictAppendCell(dp, LVALUEtoCELL(pType));2663return;2664}266526662667static void dotParen(FICL_VM *pVM)2668{2669char *pSrc = vmGetInBuf(pVM);2670char *pEnd = vmGetInBufEnd(pVM);2671char *pDest = pVM->pad;2672char ch;26732674/*2675** Note: the standard does not want leading spaces skipped (apparently)2676*/2677for (ch = *pSrc; (pEnd != pSrc) && (ch != ')'); ch = *++pSrc)2678*pDest++ = ch;26792680*pDest = '\0';2681if ((pEnd != pSrc) && (ch == ')'))2682pSrc++;26832684vmTextOut(pVM, pVM->pad, 0);2685vmUpdateTib(pVM, pSrc);26862687return;2688}268926902691/**************************************************************************2692s l i t e r a l2693** STRING2694** Interpretation: Interpretation semantics for this word are undefined.2695** Compilation: ( c-addr1 u -- )2696** Append the run-time semantics given below to the current definition.2697** Run-time: ( -- c-addr2 u )2698** Return c-addr2 u describing a string consisting of the characters2699** specified by c-addr1 u during compilation. A program shall not alter2700** the returned string.2701**************************************************************************/2702static void sLiteralCoIm(FICL_VM *pVM)2703{2704FICL_DICT *dp;2705char *cp, *cpDest;2706FICL_UNS u;27072708#if FICL_ROBUST > 12709vmCheckStack(pVM, 2, 0);2710#endif27112712dp = vmGetDict(pVM);2713u = POPUNS();2714cp = POPPTR();27152716dictAppendCell(dp, LVALUEtoCELL(pVM->pSys->pStringLit));2717cpDest = (char *) dp->here;2718*cpDest++ = (char) u;27192720for (; u > 0; --u)2721{2722*cpDest++ = *cp++;2723}27242725*cpDest++ = 0;2726dp->here = PTRtoCELL alignPtr(cpDest);2727return;2728}272927302731/**************************************************************************2732s t a t e2733** Return the address of the VM's state member (must be sized the2734** same as a CELL for this reason)2735**************************************************************************/2736static void state(FICL_VM *pVM)2737{2738#if FICL_ROBUST > 12739vmCheckStack(pVM, 0, 1);2740#endif2741PUSHPTR(&pVM->state);2742return;2743}274427452746/**************************************************************************2747c r e a t e . . . d o e s >2748** Make a new word in the dictionary with the run-time effect of2749** a variable (push my address), but with extra space allotted2750** for use by does> .2751**************************************************************************/27522753static void createParen(FICL_VM *pVM)2754{2755CELL *pCell;27562757#if FICL_ROBUST > 12758vmCheckStack(pVM, 0, 1);2759#endif27602761pCell = pVM->runningWord->param;2762PUSHPTR(pCell+1);2763return;2764}276527662767static void create(FICL_VM *pVM)2768{2769FICL_DICT *dp = vmGetDict(pVM);2770STRINGINFO si = vmGetWord(pVM);27712772dictCheckThreshold(dp);27732774dictAppendWord2(dp, si, createParen, FW_DEFAULT);2775dictAllotCells(dp, 1);2776return;2777}277827792780static void doDoes(FICL_VM *pVM)2781{2782CELL *pCell;2783IPTYPE tempIP;2784#if FICL_ROBUST > 12785vmCheckStack(pVM, 0, 1);2786#endif27872788pCell = pVM->runningWord->param;2789tempIP = (IPTYPE)((*pCell).p);2790PUSHPTR(pCell+1);2791vmPushIP(pVM, tempIP);2792return;2793}279427952796static void doesParen(FICL_VM *pVM)2797{2798FICL_DICT *dp = vmGetDict(pVM);2799dp->smudge->code = doDoes;2800dp->smudge->param[0] = LVALUEtoCELL(pVM->ip);2801vmPopIP(pVM);2802return;2803}280428052806static void doesCoIm(FICL_VM *pVM)2807{2808FICL_DICT *dp = vmGetDict(pVM);2809#if FICL_WANT_LOCALS2810assert(pVM->pSys->pUnLinkParen);2811if (pVM->pSys->nLocals > 0)2812{2813FICL_DICT *pLoc = ficlGetLoc(pVM->pSys);2814dictEmpty(pLoc, pLoc->pForthWords->size);2815dictAppendCell(dp, LVALUEtoCELL(pVM->pSys->pUnLinkParen));2816}28172818pVM->pSys->nLocals = 0;2819#endif2820IGNORE(pVM);28212822dictAppendCell(dp, LVALUEtoCELL(pVM->pSys->pDoesParen));2823return;2824}282528262827/**************************************************************************2828t o b o d y2829** to-body CORE ( xt -- a-addr )2830** a-addr is the data-field address corresponding to xt. An ambiguous2831** condition exists if xt is not for a word defined via CREATE.2832**************************************************************************/2833static void toBody(FICL_VM *pVM)2834{2835FICL_WORD *pFW;2836/*#$-GUY CHANGE: Added robustness.-$#*/2837#if FICL_ROBUST > 12838vmCheckStack(pVM, 1, 1);2839#endif28402841pFW = POPPTR();2842PUSHPTR(pFW->param + 1);2843return;2844}284528462847/*2848** from-body ficl ( a-addr -- xt )2849** Reverse effect of >body2850*/2851static void fromBody(FICL_VM *pVM)2852{2853char *ptr;2854#if FICL_ROBUST > 12855vmCheckStack(pVM, 1, 1);2856#endif28572858ptr = (char *)POPPTR() - sizeof (FICL_WORD);2859PUSHPTR(ptr);2860return;2861}286228632864/*2865** >name ficl ( xt -- c-addr u )2866** Push the address and length of a word's name given its address2867** xt.2868*/2869static void toName(FICL_VM *pVM)2870{2871FICL_WORD *pFW;2872#if FICL_ROBUST > 12873vmCheckStack(pVM, 1, 2);2874#endif28752876pFW = POPPTR();2877PUSHPTR(pFW->name);2878PUSHUNS(pFW->nName);2879return;2880}288128822883static void getLastWord(FICL_VM *pVM)2884{2885FICL_DICT *pDict = vmGetDict(pVM);2886FICL_WORD *wp = pDict->smudge;2887assert(wp);2888vmPush(pVM, LVALUEtoCELL(wp));2889return;2890}289128922893/**************************************************************************2894l b r a c k e t e t c2895**2896**************************************************************************/28972898static void lbracketCoIm(FICL_VM *pVM)2899{2900pVM->state = INTERPRET;2901return;2902}290329042905static void rbracket(FICL_VM *pVM)2906{2907pVM->state = COMPILE;2908return;2909}291029112912/**************************************************************************2913p i c t u r e d n u m e r i c w o r d s2914**2915** less-number-sign CORE ( -- )2916** Initialize the pictured numeric output conversion process.2917** (clear the pad)2918**************************************************************************/2919static void lessNumberSign(FICL_VM *pVM)2920{2921FICL_STRING *sp = PTRtoSTRING pVM->pad;2922sp->count = 0;2923return;2924}29252926/*2927** number-sign CORE ( ud1 -- ud2 )2928** Divide ud1 by the number in BASE giving the quotient ud2 and the remainder2929** n. (n is the least-significant digit of ud1.) Convert n to external form2930** and add the resulting character to the beginning of the pictured numeric2931** output string. An ambiguous condition exists if # executes outside of a2932** <# #> delimited number conversion.2933*/2934static void numberSign(FICL_VM *pVM)2935{2936FICL_STRING *sp;2937DPUNS u;2938UNS16 rem;2939#if FICL_ROBUST > 12940vmCheckStack(pVM, 2, 2);2941#endif29422943sp = PTRtoSTRING pVM->pad;2944u = u64Pop(pVM->pStack);2945rem = m64UMod(&u, (UNS16)(pVM->base));2946sp->text[sp->count++] = digit_to_char(rem);2947u64Push(pVM->pStack, u);2948return;2949}29502951/*2952** number-sign-greater CORE ( xd -- c-addr u )2953** Drop xd. Make the pictured numeric output string available as a character2954** string. c-addr and u specify the resulting character string. A program2955** may replace characters within the string.2956*/2957static void numberSignGreater(FICL_VM *pVM)2958{2959FICL_STRING *sp;2960#if FICL_ROBUST > 12961vmCheckStack(pVM, 2, 2);2962#endif29632964sp = PTRtoSTRING pVM->pad;2965sp->text[sp->count] = 0;2966strrev(sp->text);2967DROP(2);2968PUSHPTR(sp->text);2969PUSHUNS(sp->count);2970return;2971}29722973/*2974** number-sign-s CORE ( ud1 -- ud2 )2975** Convert one digit of ud1 according to the rule for #. Continue conversion2976** until the quotient is zero. ud2 is zero. An ambiguous condition exists if2977** #S executes outside of a <# #> delimited number conversion.2978** TO DO: presently does not use ud1 hi cell - use it!2979*/2980static void numberSignS(FICL_VM *pVM)2981{2982FICL_STRING *sp;2983DPUNS u;2984UNS16 rem;2985#if FICL_ROBUST > 12986vmCheckStack(pVM, 2, 2);2987#endif29882989sp = PTRtoSTRING pVM->pad;2990u = u64Pop(pVM->pStack);29912992do2993{2994rem = m64UMod(&u, (UNS16)(pVM->base));2995sp->text[sp->count++] = digit_to_char(rem);2996}2997while (u.hi || u.lo);29982999u64Push(pVM->pStack, u);3000return;3001}30023003/*3004** HOLD CORE ( char -- )3005** Add char to the beginning of the pictured numeric output string. An ambiguous3006** condition exists if HOLD executes outside of a <# #> delimited number conversion.3007*/3008static void hold(FICL_VM *pVM)3009{3010FICL_STRING *sp;3011int i;3012#if FICL_ROBUST > 13013vmCheckStack(pVM, 1, 0);3014#endif30153016sp = PTRtoSTRING pVM->pad;3017i = POPINT();3018sp->text[sp->count++] = (char) i;3019return;3020}30213022/*3023** SIGN CORE ( n -- )3024** If n is negative, add a minus sign to the beginning of the pictured3025** numeric output string. An ambiguous condition exists if SIGN3026** executes outside of a <# #> delimited number conversion.3027*/3028static void sign(FICL_VM *pVM)3029{3030FICL_STRING *sp;3031int i;3032#if FICL_ROBUST > 13033vmCheckStack(pVM, 1, 0);3034#endif30353036sp = PTRtoSTRING pVM->pad;3037i = POPINT();3038if (i < 0)3039sp->text[sp->count++] = '-';3040return;3041}304230433044/**************************************************************************3045t o N u m b e r3046** to-number CORE ( ud1 c-addr1 u1 -- ud2 c-addr2 u2 )3047** ud2 is the unsigned result of converting the characters within the3048** string specified by c-addr1 u1 into digits, using the number in BASE,3049** and adding each into ud1 after multiplying ud1 by the number in BASE.3050** Conversion continues left-to-right until a character that is not3051** convertible, including any + or -, is encountered or the string is3052** entirely converted. c-addr2 is the location of the first unconverted3053** character or the first character past the end of the string if the string3054** was entirely converted. u2 is the number of unconverted characters in the3055** string. An ambiguous condition exists if ud2 overflows during the3056** conversion.3057**************************************************************************/3058static void toNumber(FICL_VM *pVM)3059{3060FICL_UNS count;3061char *cp;3062DPUNS accum;3063FICL_UNS base = pVM->base;3064FICL_UNS ch;3065FICL_UNS digit;30663067#if FICL_ROBUST > 13068vmCheckStack(pVM,4,4);3069#endif30703071count = POPUNS();3072cp = (char *)POPPTR();3073accum = u64Pop(pVM->pStack);30743075for (ch = *cp; count > 0; ch = *++cp, count--)3076{3077if (ch < '0')3078break;30793080digit = ch - '0';30813082if (digit > 9)3083digit = tolower(ch) - 'a' + 10;3084/*3085** Note: following test also catches chars between 9 and a3086** because 'digit' is unsigned!3087*/3088if (digit >= base)3089break;30903091accum = m64Mac(accum, base, digit);3092}30933094u64Push(pVM->pStack, accum);3095PUSHPTR(cp);3096PUSHUNS(count);30973098return;3099}3100310131023103/**************************************************************************3104q u i t & a b o r t3105** quit CORE ( -- ) ( R: i*x -- )3106** Empty the return stack, store zero in SOURCE-ID if it is present, make3107** the user input device the input source, and enter interpretation state.3108** Do not display a message. Repeat the following:3109**3110** Accept a line from the input source into the input buffer, set >IN to3111** zero, and interpret.3112** Display the implementation-defined system prompt if in3113** interpretation state, all processing has been completed, and no3114** ambiguous condition exists.3115**************************************************************************/31163117static void quit(FICL_VM *pVM)3118{3119vmThrow(pVM, VM_QUIT);3120return;3121}312231233124static void ficlAbort(FICL_VM *pVM)3125{3126vmThrow(pVM, VM_ABORT);3127return;3128}312931303131/**************************************************************************3132a c c e p t3133** accept CORE ( c-addr +n1 -- +n2 )3134** Receive a string of at most +n1 characters. An ambiguous condition3135** exists if +n1 is zero or greater than 32,767. Display graphic characters3136** as they are received. A program that depends on the presence or absence3137** of non-graphic characters in the string has an environmental dependency.3138** The editing functions, if any, that the system performs in order to3139** construct the string are implementation-defined.3140**3141** (Although the standard text doesn't say so, I assume that the intent3142** of 'accept' is to store the string at the address specified on3143** the stack.)3144** Implementation: if there's more text in the TIB, use it. Otherwise3145** throw out for more text. Copy characters up to the max count into the3146** address given, and return the number of actual characters copied.3147**3148** Note (sobral) this may not be the behavior you'd expect if you're3149** trying to get user input at load time!3150**************************************************************************/3151static void accept(FICL_VM *pVM)3152{3153FICL_UNS count, len;3154char *cp;3155char *pBuf, *pEnd;31563157#if FICL_ROBUST > 13158vmCheckStack(pVM,2,1);3159#endif31603161pBuf = vmGetInBuf(pVM);3162pEnd = vmGetInBufEnd(pVM);3163len = pEnd - pBuf;3164if (len == 0)3165vmThrow(pVM, VM_RESTART);31663167/*3168** Now we have something in the text buffer - use it3169*/3170count = stackPopINT(pVM->pStack);3171cp = stackPopPtr(pVM->pStack);31723173len = (count < len) ? count : len;3174strncpy(cp, vmGetInBuf(pVM), len);3175pBuf += len;3176vmUpdateTib(pVM, pBuf);3177PUSHINT(len);31783179return;3180}318131823183/**************************************************************************3184a l i g n3185** 6.1.0705 ALIGN CORE ( -- )3186** If the data-space pointer is not aligned, reserve enough space to3187** align it.3188**************************************************************************/3189static void align(FICL_VM *pVM)3190{3191FICL_DICT *dp = vmGetDict(pVM);3192IGNORE(pVM);3193dictAlign(dp);3194return;3195}319631973198/**************************************************************************3199a l i g n e d3200**3201**************************************************************************/3202static void aligned(FICL_VM *pVM)3203{3204void *addr;3205#if FICL_ROBUST > 13206vmCheckStack(pVM,1,1);3207#endif32083209addr = POPPTR();3210PUSHPTR(alignPtr(addr));3211return;3212}321332143215/**************************************************************************3216b e g i n & f r i e n d s3217** Indefinite loop control structures3218** A.6.1.0760 BEGIN3219** Typical use:3220** : X ... BEGIN ... test UNTIL ;3221** or3222** : X ... BEGIN ... test WHILE ... REPEAT ;3223**************************************************************************/3224static void beginCoIm(FICL_VM *pVM)3225{3226FICL_DICT *dp = vmGetDict(pVM);3227markBranch(dp, pVM, destTag);3228return;3229}32303231static void untilCoIm(FICL_VM *pVM)3232{3233FICL_DICT *dp = vmGetDict(pVM);32343235assert(pVM->pSys->pBranch0);32363237dictAppendCell(dp, LVALUEtoCELL(pVM->pSys->pBranch0));3238resolveBackBranch(dp, pVM, destTag);3239return;3240}32413242static void whileCoIm(FICL_VM *pVM)3243{3244FICL_DICT *dp = vmGetDict(pVM);32453246assert(pVM->pSys->pBranch0);32473248dictAppendCell(dp, LVALUEtoCELL(pVM->pSys->pBranch0));3249markBranch(dp, pVM, origTag);3250twoSwap(pVM);3251dictAppendUNS(dp, 1);3252return;3253}32543255static void repeatCoIm(FICL_VM *pVM)3256{3257FICL_DICT *dp = vmGetDict(pVM);32583259assert(pVM->pSys->pBranchParen);3260dictAppendCell(dp, LVALUEtoCELL(pVM->pSys->pBranchParen));32613262/* expect "begin" branch marker */3263resolveBackBranch(dp, pVM, destTag);3264/* expect "while" branch marker */3265resolveForwardBranch(dp, pVM, origTag);3266return;3267}326832693270static void againCoIm(FICL_VM *pVM)3271{3272FICL_DICT *dp = vmGetDict(pVM);32733274assert(pVM->pSys->pBranchParen);3275dictAppendCell(dp, LVALUEtoCELL(pVM->pSys->pBranchParen));32763277/* expect "begin" branch marker */3278resolveBackBranch(dp, pVM, destTag);3279return;3280}328132823283/**************************************************************************3284c h a r & f r i e n d s3285** 6.1.0895 CHAR CORE ( "<spaces>name" -- char )3286** Skip leading space delimiters. Parse name delimited by a space.3287** Put the value of its first character onto the stack.3288**3289** bracket-char CORE3290** Interpretation: Interpretation semantics for this word are undefined.3291** Compilation: ( "<spaces>name" -- )3292** Skip leading space delimiters. Parse name delimited by a space.3293** Append the run-time semantics given below to the current definition.3294** Run-time: ( -- char )3295** Place char, the value of the first character of name, on the stack.3296**************************************************************************/3297static void ficlChar(FICL_VM *pVM)3298{3299STRINGINFO si;3300#if FICL_ROBUST > 13301vmCheckStack(pVM,0,1);3302#endif33033304si = vmGetWord(pVM);3305PUSHUNS((FICL_UNS)(si.cp[0]));3306return;3307}33083309static void charCoIm(FICL_VM *pVM)3310{3311ficlChar(pVM);3312literalIm(pVM);3313return;3314}33153316/**************************************************************************3317c h a r P l u s3318** char-plus CORE ( c-addr1 -- c-addr2 )3319** Add the size in address units of a character to c-addr1, giving c-addr2.3320**************************************************************************/3321static void charPlus(FICL_VM *pVM)3322{3323char *cp;3324#if FICL_ROBUST > 13325vmCheckStack(pVM,1,1);3326#endif33273328cp = POPPTR();3329PUSHPTR(cp + 1);3330return;3331}33323333/**************************************************************************3334c h a r s3335** chars CORE ( n1 -- n2 )3336** n2 is the size in address units of n1 characters.3337** For most processors, this function can be a no-op. To guarantee3338** portability, we'll multiply by sizeof (char).3339**************************************************************************/3340#if defined (_M_IX86)3341#pragma warning(disable: 4127)3342#endif3343static void ficlChars(FICL_VM *pVM)3344{3345if (sizeof (char) > 1)3346{3347FICL_INT i;3348#if FICL_ROBUST > 13349vmCheckStack(pVM,1,1);3350#endif3351i = POPINT();3352PUSHINT(i * sizeof (char));3353}3354/* otherwise no-op! */3355return;3356}3357#if defined (_M_IX86)3358#pragma warning(default: 4127)3359#endif336033613362/**************************************************************************3363c o u n t3364** COUNT CORE ( c-addr1 -- c-addr2 u )3365** Return the character string specification for the counted string stored3366** at c-addr1. c-addr2 is the address of the first character after c-addr1.3367** u is the contents of the character at c-addr1, which is the length in3368** characters of the string at c-addr2.3369**************************************************************************/3370static void count(FICL_VM *pVM)3371{3372FICL_STRING *sp;3373#if FICL_ROBUST > 13374vmCheckStack(pVM,1,2);3375#endif33763377sp = POPPTR();3378PUSHPTR(sp->text);3379PUSHUNS(sp->count);3380return;3381}33823383/**************************************************************************3384e n v i r o n m e n t ?3385** environment-query CORE ( c-addr u -- false | i*x true )3386** c-addr is the address of a character string and u is the string's3387** character count. u may have a value in the range from zero to an3388** implementation-defined maximum which shall not be less than 31. The3389** character string should contain a keyword from 3.2.6 Environmental3390** queries or the optional word sets to be checked for correspondence3391** with an attribute of the present environment. If the system treats the3392** attribute as unknown, the returned flag is false; otherwise, the flag3393** is true and the i*x returned is of the type specified in the table for3394** the attribute queried.3395**************************************************************************/3396static void environmentQ(FICL_VM *pVM)3397{3398FICL_DICT *envp;3399FICL_WORD *pFW;3400STRINGINFO si;3401#if FICL_ROBUST > 13402vmCheckStack(pVM,2,1);3403#endif34043405envp = pVM->pSys->envp;3406si.count = (FICL_COUNT)stackPopUNS(pVM->pStack);3407si.cp = stackPopPtr(pVM->pStack);34083409pFW = dictLookup(envp, si);34103411if (pFW != NULL)3412{3413vmExecute(pVM, pFW);3414PUSHINT(FICL_TRUE);3415}3416else3417{3418PUSHINT(FICL_FALSE);3419}3420return;3421}34223423/**************************************************************************3424e v a l u a t e3425** EVALUATE CORE ( i*x c-addr u -- j*x )3426** Save the current input source specification. Store minus-one (-1) in3427** SOURCE-ID if it is present. Make the string described by c-addr and u3428** both the input source and input buffer, set >IN to zero, and interpret.3429** When the parse area is empty, restore the prior input source3430** specification. Other stack effects are due to the words EVALUATEd.3431**3432**************************************************************************/3433static void evaluate(FICL_VM *pVM)3434{3435FICL_UNS count;3436char *cp;3437CELL id;3438int result;3439#if FICL_ROBUST > 13440vmCheckStack(pVM,2,0);3441#endif34423443count = POPUNS();3444cp = POPPTR();34453446IGNORE(count);3447id = pVM->sourceID;3448pVM->sourceID.i = -1;3449result = ficlExecC(pVM, cp, count);3450pVM->sourceID = id;3451if (result != VM_OUTOFTEXT)3452vmThrow(pVM, result);34533454return;3455}345634573458/**************************************************************************3459s t r i n g q u o t e3460** Interpreting: get string delimited by a quote from the input stream,3461** copy to a scratch area, and put its count and address on the stack.3462** Compiling: compile code to push the address and count of a string3463** literal, compile the string from the input stream, and align the dict3464** pointer.3465**************************************************************************/3466static void stringQuoteIm(FICL_VM *pVM)3467{3468FICL_DICT *dp = vmGetDict(pVM);34693470if (pVM->state == INTERPRET)3471{3472FICL_STRING *sp = (FICL_STRING *) dp->here;3473vmGetString(pVM, sp, '\"');3474PUSHPTR(sp->text);3475PUSHUNS(sp->count);3476}3477else /* COMPILE state */3478{3479dictAppendCell(dp, LVALUEtoCELL(pVM->pSys->pStringLit));3480dp->here = PTRtoCELL vmGetString(pVM, (FICL_STRING *)dp->here, '\"');3481dictAlign(dp);3482}34833484return;3485}348634873488/**************************************************************************3489t y p e3490** Pop count and char address from stack and print the designated string.3491**************************************************************************/3492static void type(FICL_VM *pVM)3493{3494FICL_UNS count = stackPopUNS(pVM->pStack);3495char *cp = stackPopPtr(pVM->pStack);3496char *pDest = (char *)ficlMalloc(count + 1);34973498/*3499** Since we don't have an output primitive for a counted string3500** (oops), make sure the string is null terminated. If not, copy3501** and terminate it.3502*/3503if (!pDest)3504vmThrowErr(pVM, "Error: out of memory");35053506strncpy(pDest, cp, count);3507pDest[count] = '\0';35083509vmTextOut(pVM, pDest, 0);35103511ficlFree(pDest);3512return;3513}35143515/**************************************************************************3516w o r d3517** word CORE ( char "<chars>ccc<char>" -- c-addr )3518** Skip leading delimiters. Parse characters ccc delimited by char. An3519** ambiguous condition exists if the length of the parsed string is greater3520** than the implementation-defined length of a counted string.3521**3522** c-addr is the address of a transient region containing the parsed word3523** as a counted string. If the parse area was empty or contained no3524** characters other than the delimiter, the resulting string has a zero3525** length. A space, not included in the length, follows the string. A3526** program may replace characters within the string.3527** NOTE! Ficl also NULL-terminates the dest string.3528**************************************************************************/3529static void ficlWord(FICL_VM *pVM)3530{3531FICL_STRING *sp;3532char delim;3533STRINGINFO si;3534#if FICL_ROBUST > 13535vmCheckStack(pVM,1,1);3536#endif35373538sp = (FICL_STRING *)pVM->pad;3539delim = (char)POPINT();3540si = vmParseStringEx(pVM, delim, 1);35413542if (SI_COUNT(si) > nPAD-1)3543SI_SETLEN(si, nPAD-1);35443545sp->count = (FICL_COUNT)SI_COUNT(si);3546strncpy(sp->text, SI_PTR(si), SI_COUNT(si));3547/*#$-GUY CHANGE: I added this.-$#*/3548sp->text[sp->count] = 0;3549strcat(sp->text, " ");35503551PUSHPTR(sp);3552return;3553}355435553556/**************************************************************************3557p a r s e - w o r d3558** ficl PARSE-WORD ( <spaces>name -- c-addr u )3559** Skip leading spaces and parse name delimited by a space. c-addr is the3560** address within the input buffer and u is the length of the selected3561** string. If the parse area is empty, the resulting string has a zero length.3562**************************************************************************/3563static void parseNoCopy(FICL_VM *pVM)3564{3565STRINGINFO si;3566#if FICL_ROBUST > 13567vmCheckStack(pVM,0,2);3568#endif35693570si = vmGetWord0(pVM);3571PUSHPTR(SI_PTR(si));3572PUSHUNS(SI_COUNT(si));3573return;3574}357535763577/**************************************************************************3578p a r s e3579** CORE EXT ( char "ccc<char>" -- c-addr u )3580** Parse ccc delimited by the delimiter char.3581** c-addr is the address (within the input buffer) and u is the length of3582** the parsed string. If the parse area was empty, the resulting string has3583** a zero length.3584** NOTE! PARSE differs from WORD: it does not skip leading delimiters.3585**************************************************************************/3586static void parse(FICL_VM *pVM)3587{3588STRINGINFO si;3589char delim;35903591#if FICL_ROBUST > 13592vmCheckStack(pVM,1,2);3593#endif35943595delim = (char)POPINT();35963597si = vmParseStringEx(pVM, delim, 0);3598PUSHPTR(SI_PTR(si));3599PUSHUNS(SI_COUNT(si));3600return;3601}360236033604/**************************************************************************3605f i l l3606** CORE ( c-addr u char -- )3607** If u is greater than zero, store char in each of u consecutive3608** characters of memory beginning at c-addr.3609**************************************************************************/3610static void fill(FICL_VM *pVM)3611{3612char ch;3613FICL_UNS u;3614char *cp;3615#if FICL_ROBUST > 13616vmCheckStack(pVM,3,0);3617#endif3618ch = (char)POPINT();3619u = POPUNS();3620cp = (char *)POPPTR();36213622while (u > 0)3623{3624*cp++ = ch;3625u--;3626}3627return;3628}362936303631/**************************************************************************3632f i n d3633** FIND CORE ( c-addr -- c-addr 0 | xt 1 | xt -1 )3634** Find the definition named in the counted string at c-addr. If the3635** definition is not found, return c-addr and zero. If the definition is3636** found, return its execution token xt. If the definition is immediate,3637** also return one (1), otherwise also return minus-one (-1). For a given3638** string, the values returned by FIND while compiling may differ from3639** those returned while not compiling.3640**************************************************************************/3641static void do_find(FICL_VM *pVM, STRINGINFO si, void *returnForFailure)3642{3643FICL_WORD *pFW;36443645pFW = dictLookup(vmGetDict(pVM), si);3646if (pFW)3647{3648PUSHPTR(pFW);3649PUSHINT((wordIsImmediate(pFW) ? 1 : -1));3650}3651else3652{3653PUSHPTR(returnForFailure);3654PUSHUNS(0);3655}3656return;3657}3658365936603661/**************************************************************************3662f i n d3663** FIND CORE ( c-addr -- c-addr 0 | xt 1 | xt -1 )3664** Find the definition named in the counted string at c-addr. If the3665** definition is not found, return c-addr and zero. If the definition is3666** found, return its execution token xt. If the definition is immediate,3667** also return one (1), otherwise also return minus-one (-1). For a given3668** string, the values returned by FIND while compiling may differ from3669** those returned while not compiling.3670**************************************************************************/3671static void cFind(FICL_VM *pVM)3672{3673FICL_STRING *sp;3674STRINGINFO si;36753676#if FICL_ROBUST > 13677vmCheckStack(pVM,1,2);3678#endif3679sp = POPPTR();3680SI_PFS(si, sp);3681do_find(pVM, si, sp);3682}3683368436853686/**************************************************************************3687s f i n d3688** FICL ( c-addr u -- 0 0 | xt 1 | xt -1 )3689** Like FIND, but takes "c-addr u" for the string.3690**************************************************************************/3691static void sFind(FICL_VM *pVM)3692{3693STRINGINFO si;36943695#if FICL_ROBUST > 13696vmCheckStack(pVM,2,2);3697#endif36983699si.count = stackPopINT(pVM->pStack);3700si.cp = stackPopPtr(pVM->pStack);37013702do_find(pVM, si, NULL);3703}3704370537063707/**************************************************************************3708f m S l a s h M o d3709** f-m-slash-mod CORE ( d1 n1 -- n2 n3 )3710** Divide d1 by n1, giving the floored quotient n3 and the remainder n2.3711** Input and output stack arguments are signed. An ambiguous condition3712** exists if n1 is zero or if the quotient lies outside the range of a3713** single-cell signed integer.3714**************************************************************************/3715static void fmSlashMod(FICL_VM *pVM)3716{3717DPINT d1;3718FICL_INT n1;3719INTQR qr;3720#if FICL_ROBUST > 13721vmCheckStack(pVM,3,2);3722#endif37233724n1 = POPINT();3725d1 = i64Pop(pVM->pStack);3726qr = m64FlooredDivI(d1, n1);3727PUSHINT(qr.rem);3728PUSHINT(qr.quot);3729return;3730}373137323733/**************************************************************************3734s m S l a s h R e m3735** s-m-slash-rem CORE ( d1 n1 -- n2 n3 )3736** Divide d1 by n1, giving the symmetric quotient n3 and the remainder n2.3737** Input and output stack arguments are signed. An ambiguous condition3738** exists if n1 is zero or if the quotient lies outside the range of a3739** single-cell signed integer.3740**************************************************************************/3741static void smSlashRem(FICL_VM *pVM)3742{3743DPINT d1;3744FICL_INT n1;3745INTQR qr;3746#if FICL_ROBUST > 13747vmCheckStack(pVM,3,2);3748#endif37493750n1 = POPINT();3751d1 = i64Pop(pVM->pStack);3752qr = m64SymmetricDivI(d1, n1);3753PUSHINT(qr.rem);3754PUSHINT(qr.quot);3755return;3756}375737583759static void ficlMod(FICL_VM *pVM)3760{3761DPINT d1;3762FICL_INT n1;3763INTQR qr;3764#if FICL_ROBUST > 13765vmCheckStack(pVM,2,1);3766#endif37673768n1 = POPINT();3769d1.lo = POPINT();3770i64Extend(d1);3771qr = m64SymmetricDivI(d1, n1);3772PUSHINT(qr.rem);3773return;3774}377537763777/**************************************************************************3778u m S l a s h M o d3779** u-m-slash-mod CORE ( ud u1 -- u2 u3 )3780** Divide ud by u1, giving the quotient u3 and the remainder u2.3781** All values and arithmetic are unsigned. An ambiguous condition3782** exists if u1 is zero or if the quotient lies outside the range of a3783** single-cell unsigned integer.3784*************************************************************************/3785static void umSlashMod(FICL_VM *pVM)3786{3787DPUNS ud;3788FICL_UNS u1;3789UNSQR qr;37903791u1 = stackPopUNS(pVM->pStack);3792ud = u64Pop(pVM->pStack);3793qr = ficlLongDiv(ud, u1);3794PUSHUNS(qr.rem);3795PUSHUNS(qr.quot);3796return;3797}379837993800/**************************************************************************3801l s h i f t3802** l-shift CORE ( x1 u -- x2 )3803** Perform a logical left shift of u bit-places on x1, giving x2.3804** Put zeroes into the least significant bits vacated by the shift.3805** An ambiguous condition exists if u is greater than or equal to the3806** number of bits in a cell.3807**3808** r-shift CORE ( x1 u -- x2 )3809** Perform a logical right shift of u bit-places on x1, giving x2.3810** Put zeroes into the most significant bits vacated by the shift. An3811** ambiguous condition exists if u is greater than or equal to the3812** number of bits in a cell.3813**************************************************************************/3814static void lshift(FICL_VM *pVM)3815{3816FICL_UNS nBits;3817FICL_UNS x1;3818#if FICL_ROBUST > 13819vmCheckStack(pVM,2,1);3820#endif38213822nBits = POPUNS();3823x1 = POPUNS();3824PUSHUNS(x1 << nBits);3825return;3826}382738283829static void rshift(FICL_VM *pVM)3830{3831FICL_UNS nBits;3832FICL_UNS x1;3833#if FICL_ROBUST > 13834vmCheckStack(pVM,2,1);3835#endif38363837nBits = POPUNS();3838x1 = POPUNS();38393840PUSHUNS(x1 >> nBits);3841return;3842}384338443845/**************************************************************************3846m S t a r3847** m-star CORE ( n1 n2 -- d )3848** d is the signed product of n1 times n2.3849**************************************************************************/3850static void mStar(FICL_VM *pVM)3851{3852FICL_INT n2;3853FICL_INT n1;3854DPINT d;3855#if FICL_ROBUST > 13856vmCheckStack(pVM,2,2);3857#endif38583859n2 = POPINT();3860n1 = POPINT();38613862d = m64MulI(n1, n2);3863i64Push(pVM->pStack, d);3864return;3865}386638673868static void umStar(FICL_VM *pVM)3869{3870FICL_UNS u2;3871FICL_UNS u1;3872DPUNS ud;3873#if FICL_ROBUST > 13874vmCheckStack(pVM,2,2);3875#endif38763877u2 = POPUNS();3878u1 = POPUNS();38793880ud = ficlLongMul(u1, u2);3881u64Push(pVM->pStack, ud);3882return;3883}388438853886/**************************************************************************3887m a x & m i n3888**3889**************************************************************************/3890static void ficlMax(FICL_VM *pVM)3891{3892FICL_INT n2;3893FICL_INT n1;3894#if FICL_ROBUST > 13895vmCheckStack(pVM,2,1);3896#endif38973898n2 = POPINT();3899n1 = POPINT();39003901PUSHINT((n1 > n2) ? n1 : n2);3902return;3903}39043905static void ficlMin(FICL_VM *pVM)3906{3907FICL_INT n2;3908FICL_INT n1;3909#if FICL_ROBUST > 13910vmCheckStack(pVM,2,1);3911#endif39123913n2 = POPINT();3914n1 = POPINT();39153916PUSHINT((n1 < n2) ? n1 : n2);3917return;3918}391939203921/**************************************************************************3922m o v e3923** CORE ( addr1 addr2 u -- )3924** If u is greater than zero, copy the contents of u consecutive address3925** units at addr1 to the u consecutive address units at addr2. After MOVE3926** completes, the u consecutive address units at addr2 contain exactly3927** what the u consecutive address units at addr1 contained before the move.3928** NOTE! This implementation assumes that a char is the same size as3929** an address unit.3930**************************************************************************/3931static void move(FICL_VM *pVM)3932{3933FICL_UNS u;3934char *addr2;3935char *addr1;3936#if FICL_ROBUST > 13937vmCheckStack(pVM,3,0);3938#endif39393940u = POPUNS();3941addr2 = POPPTR();3942addr1 = POPPTR();39433944if (u == 0)3945return;3946/*3947** Do the copy carefully, so as to be3948** correct even if the two ranges overlap3949*/3950if (addr1 >= addr2)3951{3952for (; u > 0; u--)3953*addr2++ = *addr1++;3954}3955else3956{3957addr2 += u-1;3958addr1 += u-1;3959for (; u > 0; u--)3960*addr2-- = *addr1--;3961}39623963return;3964}396539663967/**************************************************************************3968r e c u r s e3969**3970**************************************************************************/3971static void recurseCoIm(FICL_VM *pVM)3972{3973FICL_DICT *pDict = vmGetDict(pVM);39743975IGNORE(pVM);3976dictAppendCell(pDict, LVALUEtoCELL(pDict->smudge));3977return;3978}397939803981/**************************************************************************3982s t o d3983** s-to-d CORE ( n -- d )3984** Convert the number n to the double-cell number d with the same3985** numerical value.3986**************************************************************************/3987static void sToD(FICL_VM *pVM)3988{3989FICL_INT s;3990#if FICL_ROBUST > 13991vmCheckStack(pVM,1,2);3992#endif39933994s = POPINT();39953996/* sign extend to 64 bits.. */3997PUSHINT(s);3998PUSHINT((s < 0) ? -1 : 0);3999return;4000}400140024003/**************************************************************************4004s o u r c e4005** CORE ( -- c-addr u )4006** c-addr is the address of, and u is the number of characters in, the4007** input buffer.4008**************************************************************************/4009static void source(FICL_VM *pVM)4010{4011#if FICL_ROBUST > 14012vmCheckStack(pVM,0,2);4013#endif4014PUSHPTR(pVM->tib.cp);4015PUSHINT(vmGetInBufLen(pVM));4016return;4017}401840194020/**************************************************************************4021v e r s i o n4022** non-standard...4023**************************************************************************/4024static void ficlVersion(FICL_VM *pVM)4025{4026vmTextOut(pVM, "ficl Version " FICL_VER, 1);4027return;4028}402940304031/**************************************************************************4032t o I n4033** to-in CORE4034**************************************************************************/4035static void toIn(FICL_VM *pVM)4036{4037#if FICL_ROBUST > 14038vmCheckStack(pVM,0,1);4039#endif4040PUSHPTR(&pVM->tib.index);4041return;4042}404340444045/**************************************************************************4046c o l o n N o N a m e4047** CORE EXT ( C: -- colon-sys ) ( S: -- xt )4048** Create an unnamed colon definition and push its address.4049** Change state to compile.4050**************************************************************************/4051static void colonNoName(FICL_VM *pVM)4052{4053FICL_DICT *dp = vmGetDict(pVM);4054FICL_WORD *pFW;4055STRINGINFO si;40564057SI_SETLEN(si, 0);4058SI_SETPTR(si, NULL);40594060pVM->state = COMPILE;4061pFW = dictAppendWord2(dp, si, colonParen, FW_DEFAULT | FW_SMUDGE);4062PUSHPTR(pFW);4063markControlTag(pVM, colonTag);4064return;4065}406640674068/**************************************************************************4069u s e r V a r i a b l e4070** user ( u -- ) "<spaces>name"4071** Get a name from the input stream and create a user variable4072** with the name and the index supplied. The run-time effect4073** of a user variable is to push the address of the indexed cell4074** in the running vm's user array.4075**4076** User variables are vm local cells. Each vm has an array of4077** FICL_USER_CELLS of them when FICL_WANT_USER is nonzero.4078** Ficl's user facility is implemented with two primitives,4079** "user" and "(user)", a variable ("nUser") (in softcore.c) that4080** holds the index of the next free user cell, and a redefinition4081** (also in softcore) of "user" that defines a user word and increments4082** nUser.4083**************************************************************************/4084#if FICL_WANT_USER4085static void userParen(FICL_VM *pVM)4086{4087FICL_INT i = pVM->runningWord->param[0].i;4088PUSHPTR(&pVM->user[i]);4089return;4090}409140924093static void userVariable(FICL_VM *pVM)4094{4095FICL_DICT *dp = vmGetDict(pVM);4096STRINGINFO si = vmGetWord(pVM);4097CELL c;40984099c = stackPop(pVM->pStack);4100if (c.i >= FICL_USER_CELLS)4101{4102vmThrowErr(pVM, "Error - out of user space");4103}41044105dictAppendWord2(dp, si, userParen, FW_DEFAULT);4106dictAppendCell(dp, c);4107return;4108}4109#endif411041114112/**************************************************************************4113t o V a l u e4114** CORE EXT4115** Interpretation: ( x "<spaces>name" -- )4116** Skip leading spaces and parse name delimited by a space. Store x in4117** name. An ambiguous condition exists if name was not defined by VALUE.4118** NOTE: In ficl, VALUE is an alias of CONSTANT4119**************************************************************************/4120static void toValue(FICL_VM *pVM)4121{4122STRINGINFO si = vmGetWord(pVM);4123FICL_DICT *dp = vmGetDict(pVM);4124FICL_WORD *pFW;41254126#if FICL_WANT_LOCALS4127if ((pVM->pSys->nLocals > 0) && (pVM->state == COMPILE))4128{4129FICL_DICT *pLoc = ficlGetLoc(pVM->pSys);4130pFW = dictLookup(pLoc, si);4131if (pFW && (pFW->code == doLocalIm))4132{4133dictAppendCell(dp, LVALUEtoCELL(pVM->pSys->pToLocalParen));4134dictAppendCell(dp, LVALUEtoCELL(pFW->param[0]));4135return;4136}4137else if (pFW && pFW->code == do2LocalIm)4138{4139dictAppendCell(dp, LVALUEtoCELL(pVM->pSys->pTo2LocalParen));4140dictAppendCell(dp, LVALUEtoCELL(pFW->param[0]));4141return;4142}4143}4144#endif41454146assert(pVM->pSys->pStore);41474148pFW = dictLookup(dp, si);4149if (!pFW)4150{4151int i = SI_COUNT(si);4152vmThrowErr(pVM, "%.*s not found", i, SI_PTR(si));4153}41544155if (pVM->state == INTERPRET)4156pFW->param[0] = stackPop(pVM->pStack);4157else /* compile code to store to word's param */4158{4159PUSHPTR(&pFW->param[0]);4160literalIm(pVM);4161dictAppendCell(dp, LVALUEtoCELL(pVM->pSys->pStore));4162}4163return;4164}416541664167#if FICL_WANT_LOCALS4168/**************************************************************************4169l i n k P a r e n4170** ( -- )4171** Link a frame on the return stack, reserving nCells of space for4172** locals - the value of nCells is the next cell in the instruction4173** stream.4174**************************************************************************/4175static void linkParen(FICL_VM *pVM)4176{4177FICL_INT nLink = *(FICL_INT *)(pVM->ip);4178vmBranchRelative(pVM, 1);4179stackLink(pVM->rStack, nLink);4180return;4181}418241834184static void unlinkParen(FICL_VM *pVM)4185{4186stackUnlink(pVM->rStack);4187return;4188}418941904191/**************************************************************************4192d o L o c a l I m4193** Immediate - cfa of a local while compiling - when executed, compiles4194** code to fetch the value of a local given the local's index in the4195** word's pfa4196**************************************************************************/4197static void getLocalParen(FICL_VM *pVM)4198{4199FICL_INT nLocal = *(FICL_INT *)(pVM->ip++);4200stackPush(pVM->pStack, pVM->rStack->pFrame[nLocal]);4201return;4202}420342044205static void toLocalParen(FICL_VM *pVM)4206{4207FICL_INT nLocal = *(FICL_INT *)(pVM->ip++);4208pVM->rStack->pFrame[nLocal] = stackPop(pVM->pStack);4209return;4210}421142124213static void getLocal0(FICL_VM *pVM)4214{4215stackPush(pVM->pStack, pVM->rStack->pFrame[0]);4216return;4217}421842194220static void toLocal0(FICL_VM *pVM)4221{4222pVM->rStack->pFrame[0] = stackPop(pVM->pStack);4223return;4224}422542264227static void getLocal1(FICL_VM *pVM)4228{4229stackPush(pVM->pStack, pVM->rStack->pFrame[1]);4230return;4231}423242334234static void toLocal1(FICL_VM *pVM)4235{4236pVM->rStack->pFrame[1] = stackPop(pVM->pStack);4237return;4238}423942404241/*4242** Each local is recorded in a private locals dictionary as a4243** word that does doLocalIm at runtime. DoLocalIm compiles code4244** into the client definition to fetch the value of the4245** corresponding local variable from the return stack.4246** The private dictionary gets initialized at the end of each block4247** that uses locals (in ; and does> for example).4248*/4249static void doLocalIm(FICL_VM *pVM)4250{4251FICL_DICT *pDict = vmGetDict(pVM);4252FICL_INT nLocal = pVM->runningWord->param[0].i;42534254if (pVM->state == INTERPRET)4255{4256stackPush(pVM->pStack, pVM->rStack->pFrame[nLocal]);4257}4258else4259{42604261if (nLocal == 0)4262{4263dictAppendCell(pDict, LVALUEtoCELL(pVM->pSys->pGetLocal0));4264}4265else if (nLocal == 1)4266{4267dictAppendCell(pDict, LVALUEtoCELL(pVM->pSys->pGetLocal1));4268}4269else4270{4271dictAppendCell(pDict, LVALUEtoCELL(pVM->pSys->pGetLocalParen));4272dictAppendCell(pDict, LVALUEtoCELL(nLocal));4273}4274}4275return;4276}427742784279/**************************************************************************4280l o c a l P a r e n4281** paren-local-paren LOCAL4282** Interpretation: Interpretation semantics for this word are undefined.4283** Execution: ( c-addr u -- )4284** When executed during compilation, (LOCAL) passes a message to the4285** system that has one of two meanings. If u is non-zero,4286** the message identifies a new local whose definition name is given by4287** the string of characters identified by c-addr u. If u is zero,4288** the message is last local and c-addr has no significance.4289**4290** The result of executing (LOCAL) during compilation of a definition is4291** to create a set of named local identifiers, each of which is4292** a definition name, that only have execution semantics within the scope4293** of that definition's source.4294**4295** local Execution: ( -- x )4296**4297** Push the local's value, x, onto the stack. The local's value is4298** initialized as described in 13.3.3 Processing locals and may be4299** changed by preceding the local's name with TO. An ambiguous condition4300** exists when local is executed while in interpretation state.4301**************************************************************************/4302static void localParen(FICL_VM *pVM)4303{4304FICL_DICT *pDict;4305STRINGINFO si;4306#if FICL_ROBUST > 14307vmCheckStack(pVM,2,0);4308#endif43094310pDict = vmGetDict(pVM);4311SI_SETLEN(si, POPUNS());4312SI_SETPTR(si, (char *)POPPTR());43134314if (SI_COUNT(si) > 0)4315{ /* add a local to the **locals** dict and update nLocals */4316FICL_DICT *pLoc = ficlGetLoc(pVM->pSys);4317if (pVM->pSys->nLocals >= FICL_MAX_LOCALS)4318{4319vmThrowErr(pVM, "Error: out of local space");4320}43214322dictAppendWord2(pLoc, si, doLocalIm, FW_COMPIMMED);4323dictAppendCell(pLoc, LVALUEtoCELL(pVM->pSys->nLocals));43244325if (pVM->pSys->nLocals == 0)4326{ /* compile code to create a local stack frame */4327dictAppendCell(pDict, LVALUEtoCELL(pVM->pSys->pLinkParen));4328/* save location in dictionary for #locals */4329pVM->pSys->pMarkLocals = pDict->here;4330dictAppendCell(pDict, LVALUEtoCELL(pVM->pSys->nLocals));4331/* compile code to initialize first local */4332dictAppendCell(pDict, LVALUEtoCELL(pVM->pSys->pToLocal0));4333}4334else if (pVM->pSys->nLocals == 1)4335{4336dictAppendCell(pDict, LVALUEtoCELL(pVM->pSys->pToLocal1));4337}4338else4339{4340dictAppendCell(pDict, LVALUEtoCELL(pVM->pSys->pToLocalParen));4341dictAppendCell(pDict, LVALUEtoCELL(pVM->pSys->nLocals));4342}43434344(pVM->pSys->nLocals)++;4345}4346else if (pVM->pSys->nLocals > 0)4347{ /* write nLocals to (link) param area in dictionary */4348*(FICL_INT *)(pVM->pSys->pMarkLocals) = pVM->pSys->nLocals;4349}43504351return;4352}435343544355static void get2LocalParen(FICL_VM *pVM)4356{4357FICL_INT nLocal = *(FICL_INT *)(pVM->ip++);4358stackPush(pVM->pStack, pVM->rStack->pFrame[nLocal]);4359stackPush(pVM->pStack, pVM->rStack->pFrame[nLocal+1]);4360return;4361}436243634364static void do2LocalIm(FICL_VM *pVM)4365{4366FICL_DICT *pDict = vmGetDict(pVM);4367FICL_INT nLocal = pVM->runningWord->param[0].i;43684369if (pVM->state == INTERPRET)4370{4371stackPush(pVM->pStack, pVM->rStack->pFrame[nLocal]);4372stackPush(pVM->pStack, pVM->rStack->pFrame[nLocal+1]);4373}4374else4375{4376dictAppendCell(pDict, LVALUEtoCELL(pVM->pSys->pGet2LocalParen));4377dictAppendCell(pDict, LVALUEtoCELL(nLocal));4378}4379return;4380}438143824383static void to2LocalParen(FICL_VM *pVM)4384{4385FICL_INT nLocal = *(FICL_INT *)(pVM->ip++);4386pVM->rStack->pFrame[nLocal+1] = stackPop(pVM->pStack);4387pVM->rStack->pFrame[nLocal] = stackPop(pVM->pStack);4388return;4389}439043914392static void twoLocalParen(FICL_VM *pVM)4393{4394FICL_DICT *pDict = vmGetDict(pVM);4395STRINGINFO si;4396SI_SETLEN(si, stackPopUNS(pVM->pStack));4397SI_SETPTR(si, (char *)stackPopPtr(pVM->pStack));43984399if (SI_COUNT(si) > 0)4400{ /* add a local to the **locals** dict and update nLocals */4401FICL_DICT *pLoc = ficlGetLoc(pVM->pSys);4402if (pVM->pSys->nLocals >= FICL_MAX_LOCALS)4403{4404vmThrowErr(pVM, "Error: out of local space");4405}44064407dictAppendWord2(pLoc, si, do2LocalIm, FW_COMPIMMED);4408dictAppendCell(pLoc, LVALUEtoCELL(pVM->pSys->nLocals));44094410if (pVM->pSys->nLocals == 0)4411{ /* compile code to create a local stack frame */4412dictAppendCell(pDict, LVALUEtoCELL(pVM->pSys->pLinkParen));4413/* save location in dictionary for #locals */4414pVM->pSys->pMarkLocals = pDict->here;4415dictAppendCell(pDict, LVALUEtoCELL(pVM->pSys->nLocals));4416}44174418dictAppendCell(pDict, LVALUEtoCELL(pVM->pSys->pTo2LocalParen));4419dictAppendCell(pDict, LVALUEtoCELL(pVM->pSys->nLocals));44204421pVM->pSys->nLocals += 2;4422}4423else if (pVM->pSys->nLocals > 0)4424{ /* write nLocals to (link) param area in dictionary */4425*(FICL_INT *)(pVM->pSys->pMarkLocals) = pVM->pSys->nLocals;4426}44274428return;4429}443044314432#endif4433/**************************************************************************4434c o m p a r e4435** STRING ( c-addr1 u1 c-addr2 u2 -- n )4436** Compare the string specified by c-addr1 u1 to the string specified by4437** c-addr2 u2. The strings are compared, beginning at the given addresses,4438** character by character, up to the length of the shorter string or until a4439** difference is found. If the two strings are identical, n is zero. If the two4440** strings are identical up to the length of the shorter string, n is minus-one4441** (-1) if u1 is less than u2 and one (1) otherwise. If the two strings are not4442** identical up to the length of the shorter string, n is minus-one (-1) if the4443** first non-matching character in the string specified by c-addr1 u1 has a4444** lesser numeric value than the corresponding character in the string specified4445** by c-addr2 u2 and one (1) otherwise.4446**************************************************************************/4447static void compareInternal(FICL_VM *pVM, int caseInsensitive)4448{4449char *cp1, *cp2;4450FICL_UNS u1, u2, uMin;4451int n = 0;44524453vmCheckStack(pVM, 4, 1);4454u2 = stackPopUNS(pVM->pStack);4455cp2 = (char *)stackPopPtr(pVM->pStack);4456u1 = stackPopUNS(pVM->pStack);4457cp1 = (char *)stackPopPtr(pVM->pStack);44584459uMin = (u1 < u2)? u1 : u2;4460for ( ; (uMin > 0) && (n == 0); uMin--)4461{4462char c1 = *cp1++;4463char c2 = *cp2++;4464if (caseInsensitive)4465{4466c1 = (char)tolower(c1);4467c2 = (char)tolower(c2);4468}4469n = (int)(c1 - c2);4470}44714472if (n == 0)4473n = (int)(u1 - u2);44744475if (n < 0)4476n = -1;4477else if (n > 0)4478n = 1;44794480PUSHINT(n);4481return;4482}448344844485static void compareString(FICL_VM *pVM)4486{4487compareInternal(pVM, FALSE);4488}448944904491static void compareStringInsensitive(FICL_VM *pVM)4492{4493compareInternal(pVM, TRUE);4494}449544964497/**************************************************************************4498p a d4499** CORE EXT ( -- c-addr )4500** c-addr is the address of a transient region that can be used to hold4501** data for intermediate processing.4502**************************************************************************/4503static void pad(FICL_VM *pVM)4504{4505stackPushPtr(pVM->pStack, pVM->pad);4506}450745084509/**************************************************************************4510s o u r c e - i d4511** CORE EXT, FILE ( -- 0 | -1 | fileid )4512** Identifies the input source as follows:4513**4514** SOURCE-ID Input source4515** --------- ------------4516** fileid Text file fileid4517** -1 String (via EVALUATE)4518** 0 User input device4519**************************************************************************/4520static void sourceid(FICL_VM *pVM)4521{4522PUSHINT(pVM->sourceID.i);4523return;4524}452545264527/**************************************************************************4528r e f i l l4529** CORE EXT ( -- flag )4530** Attempt to fill the input buffer from the input source, returning a true4531** flag if successful.4532** When the input source is the user input device, attempt to receive input4533** into the terminal input buffer. If successful, make the result the input4534** buffer, set >IN to zero, and return true. Receipt of a line containing no4535** characters is considered successful. If there is no input available from4536** the current input source, return false.4537** When the input source is a string from EVALUATE, return false and4538** perform no other action.4539**************************************************************************/4540static void refill(FICL_VM *pVM)4541{4542FICL_INT ret = (pVM->sourceID.i == -1) ? FICL_FALSE : FICL_TRUE;4543if (ret && (pVM->fRestart == 0))4544vmThrow(pVM, VM_RESTART);45454546PUSHINT(ret);4547return;4548}454945504551/**************************************************************************4552freebsd exception handling words4553** Catch, from ANS Forth standard. Installs a safety net, then EXECUTE4554** the word in ToS. If an exception happens, restore the state to what4555** it was before, and pushes the exception value on the stack. If not,4556** push zero.4557**4558** Notice that Catch implements an inner interpreter. This is ugly,4559** but given how ficl works, it cannot be helped. The problem is that4560** colon definitions will be executed *after* the function returns,4561** while "code" definitions will be executed immediately. I considered4562** other solutions to this problem, but all of them shared the same4563** basic problem (with added disadvantages): if ficl ever changes it's4564** inner thread modus operandi, one would have to fix this word.4565**4566** More comments can be found throughout catch's code.4567**4568** Daniel C. Sobral Jan 09/19994569** sadler may 2000 -- revised to follow ficl.c:ficlExecXT.4570**************************************************************************/45714572static void ficlCatch(FICL_VM *pVM)4573{4574int except;4575jmp_buf vmState;4576FICL_VM VM;4577FICL_STACK pStack;4578FICL_STACK rStack;4579FICL_WORD *pFW;45804581assert(pVM);4582assert(pVM->pSys->pExitInner);458345844585/*4586** Get xt.4587** We need this *before* we save the stack pointer, or4588** we'll have to pop one element out of the stack after4589** an exception. I prefer to get done with it up front. :-)4590*/4591#if FICL_ROBUST > 14592vmCheckStack(pVM, 1, 0);4593#endif4594pFW = stackPopPtr(pVM->pStack);45954596/*4597** Save vm's state -- a catch will not back out environmental4598** changes.4599**4600** We are *not* saving dictionary state, since it is4601** global instead of per vm, and we are not saving4602** stack contents, since we are not required to (and,4603** thus, it would be useless). We save pVM, and pVM4604** "stacks" (a structure containing general information4605** about it, including the current stack pointer).4606*/4607memcpy((void*)&VM, (void*)pVM, sizeof(FICL_VM));4608memcpy((void*)&pStack, (void*)pVM->pStack, sizeof(FICL_STACK));4609memcpy((void*)&rStack, (void*)pVM->rStack, sizeof(FICL_STACK));46104611/*4612** Give pVM a jmp_buf4613*/4614pVM->pState = &vmState;46154616/*4617** Safety net4618*/4619except = setjmp(vmState);46204621switch (except)4622{4623/*4624** Setup condition - push poison pill so that the VM throws4625** VM_INNEREXIT if the XT terminates normally, then execute4626** the XT4627*/4628case 0:4629vmPushIP(pVM, &(pVM->pSys->pExitInner)); /* Open mouth, insert emetic */4630vmExecute(pVM, pFW);4631vmInnerLoop(pVM);4632break;46334634/*4635** Normal exit from XT - lose the poison pill,4636** restore old setjmp vector and push a zero.4637*/4638case VM_INNEREXIT:4639vmPopIP(pVM); /* Gack - hurl poison pill */4640pVM->pState = VM.pState; /* Restore just the setjmp vector */4641PUSHINT(0); /* Push 0 -- everything is ok */4642break;46434644/*4645** Some other exception got thrown - restore pre-existing VM state4646** and push the exception code4647*/4648default:4649/* Restore vm's state */4650memcpy((void*)pVM, (void*)&VM, sizeof(FICL_VM));4651memcpy((void*)pVM->pStack, (void*)&pStack, sizeof(FICL_STACK));4652memcpy((void*)pVM->rStack, (void*)&rStack, sizeof(FICL_STACK));46534654PUSHINT(except);/* Push error */4655break;4656}4657}46584659/**************************************************************************4660** t h r o w4661** EXCEPTION4662** Throw -- From ANS Forth standard.4663**4664** Throw takes the ToS and, if that's different from zero,4665** returns to the last executed catch context. Further throws will4666** unstack previously executed "catches", in LIFO mode.4667**4668** Daniel C. Sobral Jan 09/19994669**************************************************************************/4670static void ficlThrow(FICL_VM *pVM)4671{4672int except;46734674except = stackPopINT(pVM->pStack);46754676if (except)4677vmThrow(pVM, except);4678}467946804681/**************************************************************************4682** a l l o c a t e4683** MEMORY4684**************************************************************************/4685static void ansAllocate(FICL_VM *pVM)4686{4687size_t size;4688void *p;46894690size = stackPopINT(pVM->pStack);4691p = ficlMalloc(size);4692PUSHPTR(p);4693if (p)4694PUSHINT(0);4695else4696PUSHINT(1);4697}469846994700/**************************************************************************4701** f r e e4702** MEMORY4703**************************************************************************/4704static void ansFree(FICL_VM *pVM)4705{4706void *p;47074708p = stackPopPtr(pVM->pStack);4709ficlFree(p);4710PUSHINT(0);4711}471247134714/**************************************************************************4715** r e s i z e4716** MEMORY4717**************************************************************************/4718static void ansResize(FICL_VM *pVM)4719{4720size_t size;4721void *new, *old;47224723size = stackPopINT(pVM->pStack);4724old = stackPopPtr(pVM->pStack);4725new = ficlRealloc(old, size);4726if (new)4727{4728PUSHPTR(new);4729PUSHINT(0);4730}4731else4732{4733PUSHPTR(old);4734PUSHINT(1);4735}4736}473747384739/**************************************************************************4740** e x i t - i n n e r4741** Signals execXT that an inner loop has completed4742**************************************************************************/4743static void ficlExitInner(FICL_VM *pVM)4744{4745vmThrow(pVM, VM_INNEREXIT);4746}474747484749/**************************************************************************4750d n e g a t e4751** DOUBLE ( d1 -- d2 )4752** d2 is the negation of d1.4753**************************************************************************/4754static void dnegate(FICL_VM *pVM)4755{4756DPINT i = i64Pop(pVM->pStack);4757i = m64Negate(i);4758i64Push(pVM->pStack, i);47594760return;4761}476247634764#if 04765/**************************************************************************47664767**4768**************************************************************************/4769static void funcname(FICL_VM *pVM)4770{4771IGNORE(pVM);4772return;4773}477447754776#endif4777/**************************************************************************4778f i c l W o r d C l a s s i f y4779** This public function helps to classify word types for SEE4780** and the deugger in tools.c. Given a pointer to a word, it returns4781** a member of WOR4782**************************************************************************/4783WORDKIND ficlWordClassify(FICL_WORD *pFW)4784{4785typedef struct4786{4787WORDKIND kind;4788FICL_CODE code;4789} CODEtoKIND;47904791static CODEtoKIND codeMap[] =4792{4793{BRANCH, branchParen},4794{COLON, colonParen},4795{CONSTANT, constantParen},4796{CREATE, createParen},4797{DO, doParen},4798{DOES, doDoes},4799{IF, branch0},4800{LITERAL, literalParen},4801{LOOP, loopParen},4802{OF, ofParen},4803{PLOOP, plusLoopParen},4804{QDO, qDoParen},4805{CSTRINGLIT, cstringLit},4806{STRINGLIT, stringLit},4807#if FICL_WANT_USER4808{USER, userParen},4809#endif4810{VARIABLE, variableParen},4811};48124813#define nMAP (sizeof(codeMap) / sizeof(CODEtoKIND))48144815FICL_CODE code = pFW->code;4816int i;48174818for (i=0; i < nMAP; i++)4819{4820if (codeMap[i].code == code)4821return codeMap[i].kind;4822}48234824return PRIMITIVE;4825}482648274828#ifdef TESTMAIN4829/**************************************************************************4830** r a n d o m4831** FICL-specific4832**************************************************************************/4833static void ficlRandom(FICL_VM *pVM)4834{4835PUSHUNS(random());4836}483748384839/**************************************************************************4840** s e e d - r a n d o m4841** FICL-specific4842**************************************************************************/4843static void ficlSeedRandom(FICL_VM *pVM)4844{4845srandom(POPUNS());4846}4847#endif484848494850/**************************************************************************4851f i c l C o m p i l e C o r e4852** Builds the primitive wordset and the environment-query namespace.4853**************************************************************************/48544855void ficlCompileCore(FICL_SYSTEM *pSys)4856{4857FICL_DICT *dp = pSys->dp;4858assert (dp);485948604861/*4862** CORE word set4863** see softcore.c for definitions of: abs bl space spaces abort"4864*/4865pSys->pStore =4866dictAppendWord(dp, "!", store, FW_DEFAULT);4867dictAppendWord(dp, "#", numberSign, FW_DEFAULT);4868dictAppendWord(dp, "#>", numberSignGreater,FW_DEFAULT);4869dictAppendWord(dp, "#s", numberSignS, FW_DEFAULT);4870dictAppendWord(dp, "\'", ficlTick, FW_DEFAULT);4871dictAppendWord(dp, "(", commentHang, FW_IMMEDIATE);4872dictAppendWord(dp, "*", mul, FW_DEFAULT);4873dictAppendWord(dp, "*/", mulDiv, FW_DEFAULT);4874dictAppendWord(dp, "*/mod", mulDivRem, FW_DEFAULT);4875dictAppendWord(dp, "+", add, FW_DEFAULT);4876dictAppendWord(dp, "+!", plusStore, FW_DEFAULT);4877dictAppendWord(dp, "+loop", plusLoopCoIm, FW_COMPIMMED);4878dictAppendWord(dp, ",", comma, FW_DEFAULT);4879dictAppendWord(dp, "-", sub, FW_DEFAULT);4880dictAppendWord(dp, ".", displayCell, FW_DEFAULT);4881dictAppendWord(dp, ".\"", dotQuoteCoIm, FW_COMPIMMED);4882dictAppendWord(dp, "/", ficlDiv, FW_DEFAULT);4883dictAppendWord(dp, "/mod", slashMod, FW_DEFAULT);4884dictAppendWord(dp, "0<", zeroLess, FW_DEFAULT);4885dictAppendWord(dp, "0=", zeroEquals, FW_DEFAULT);4886dictAppendWord(dp, "1+", onePlus, FW_DEFAULT);4887dictAppendWord(dp, "1-", oneMinus, FW_DEFAULT);4888dictAppendWord(dp, "2!", twoStore, FW_DEFAULT);4889dictAppendWord(dp, "2*", twoMul, FW_DEFAULT);4890dictAppendWord(dp, "2/", twoDiv, FW_DEFAULT);4891dictAppendWord(dp, "2@", twoFetch, FW_DEFAULT);4892dictAppendWord(dp, "2drop", twoDrop, FW_DEFAULT);4893dictAppendWord(dp, "2dup", twoDup, FW_DEFAULT);4894dictAppendWord(dp, "2over", twoOver, FW_DEFAULT);4895dictAppendWord(dp, "2swap", twoSwap, FW_DEFAULT);4896dictAppendWord(dp, ":", colon, FW_DEFAULT);4897dictAppendWord(dp, ";", semicolonCoIm, FW_COMPIMMED);4898dictAppendWord(dp, "<", isLess, FW_DEFAULT);4899dictAppendWord(dp, "<#", lessNumberSign, FW_DEFAULT);4900dictAppendWord(dp, "=", isEqual, FW_DEFAULT);4901dictAppendWord(dp, ">", isGreater, FW_DEFAULT);4902dictAppendWord(dp, ">body", toBody, FW_DEFAULT);4903dictAppendWord(dp, ">in", toIn, FW_DEFAULT);4904dictAppendWord(dp, ">number", toNumber, FW_DEFAULT);4905dictAppendWord(dp, ">r", toRStack, FW_COMPILE);4906dictAppendWord(dp, "?dup", questionDup, FW_DEFAULT);4907dictAppendWord(dp, "@", fetch, FW_DEFAULT);4908dictAppendWord(dp, "abort", ficlAbort, FW_DEFAULT);4909dictAppendWord(dp, "accept", accept, FW_DEFAULT);4910dictAppendWord(dp, "align", align, FW_DEFAULT);4911dictAppendWord(dp, "aligned", aligned, FW_DEFAULT);4912dictAppendWord(dp, "allot", allot, FW_DEFAULT);4913dictAppendWord(dp, "and", bitwiseAnd, FW_DEFAULT);4914dictAppendWord(dp, "base", base, FW_DEFAULT);4915dictAppendWord(dp, "begin", beginCoIm, FW_COMPIMMED);4916dictAppendWord(dp, "c!", cStore, FW_DEFAULT);4917dictAppendWord(dp, "c,", cComma, FW_DEFAULT);4918dictAppendWord(dp, "c@", cFetch, FW_DEFAULT);4919dictAppendWord(dp, "case", caseCoIm, FW_COMPIMMED);4920dictAppendWord(dp, "cell+", cellPlus, FW_DEFAULT);4921dictAppendWord(dp, "cells", cells, FW_DEFAULT);4922dictAppendWord(dp, "char", ficlChar, FW_DEFAULT);4923dictAppendWord(dp, "char+", charPlus, FW_DEFAULT);4924dictAppendWord(dp, "chars", ficlChars, FW_DEFAULT);4925dictAppendWord(dp, "constant", constant, FW_DEFAULT);4926dictAppendWord(dp, "count", count, FW_DEFAULT);4927dictAppendWord(dp, "cr", cr, FW_DEFAULT);4928dictAppendWord(dp, "create", create, FW_DEFAULT);4929dictAppendWord(dp, "decimal", decimal, FW_DEFAULT);4930dictAppendWord(dp, "depth", depth, FW_DEFAULT);4931dictAppendWord(dp, "do", doCoIm, FW_COMPIMMED);4932dictAppendWord(dp, "does>", doesCoIm, FW_COMPIMMED);4933pSys->pDrop =4934dictAppendWord(dp, "drop", drop, FW_DEFAULT);4935dictAppendWord(dp, "dup", dup, FW_DEFAULT);4936dictAppendWord(dp, "else", elseCoIm, FW_COMPIMMED);4937dictAppendWord(dp, "emit", emit, FW_DEFAULT);4938dictAppendWord(dp, "endcase", endcaseCoIm, FW_COMPIMMED);4939dictAppendWord(dp, "endof", endofCoIm, FW_COMPIMMED);4940dictAppendWord(dp, "environment?", environmentQ,FW_DEFAULT);4941dictAppendWord(dp, "evaluate", evaluate, FW_DEFAULT);4942dictAppendWord(dp, "execute", execute, FW_DEFAULT);4943dictAppendWord(dp, "exit", exitCoIm, FW_COMPIMMED);4944dictAppendWord(dp, "fallthrough",fallthroughCoIm,FW_COMPIMMED);4945dictAppendWord(dp, "fill", fill, FW_DEFAULT);4946dictAppendWord(dp, "find", cFind, FW_DEFAULT);4947dictAppendWord(dp, "fm/mod", fmSlashMod, FW_DEFAULT);4948dictAppendWord(dp, "here", here, FW_DEFAULT);4949dictAppendWord(dp, "hold", hold, FW_DEFAULT);4950dictAppendWord(dp, "i", loopICo, FW_COMPILE);4951dictAppendWord(dp, "if", ifCoIm, FW_COMPIMMED);4952dictAppendWord(dp, "immediate", immediate, FW_DEFAULT);4953dictAppendWord(dp, "invert", bitwiseNot, FW_DEFAULT);4954dictAppendWord(dp, "j", loopJCo, FW_COMPILE);4955dictAppendWord(dp, "k", loopKCo, FW_COMPILE);4956dictAppendWord(dp, "leave", leaveCo, FW_COMPILE);4957dictAppendWord(dp, "literal", literalIm, FW_IMMEDIATE);4958dictAppendWord(dp, "loop", loopCoIm, FW_COMPIMMED);4959dictAppendWord(dp, "lshift", lshift, FW_DEFAULT);4960dictAppendWord(dp, "m*", mStar, FW_DEFAULT);4961dictAppendWord(dp, "max", ficlMax, FW_DEFAULT);4962dictAppendWord(dp, "min", ficlMin, FW_DEFAULT);4963dictAppendWord(dp, "mod", ficlMod, FW_DEFAULT);4964dictAppendWord(dp, "move", move, FW_DEFAULT);4965dictAppendWord(dp, "negate", negate, FW_DEFAULT);4966dictAppendWord(dp, "of", ofCoIm, FW_COMPIMMED);4967dictAppendWord(dp, "or", bitwiseOr, FW_DEFAULT);4968dictAppendWord(dp, "over", over, FW_DEFAULT);4969dictAppendWord(dp, "postpone", postponeCoIm, FW_COMPIMMED);4970dictAppendWord(dp, "quit", quit, FW_DEFAULT);4971dictAppendWord(dp, "r>", fromRStack, FW_COMPILE);4972dictAppendWord(dp, "r@", fetchRStack, FW_COMPILE);4973dictAppendWord(dp, "recurse", recurseCoIm, FW_COMPIMMED);4974dictAppendWord(dp, "repeat", repeatCoIm, FW_COMPIMMED);4975dictAppendWord(dp, "rot", rot, FW_DEFAULT);4976dictAppendWord(dp, "rshift", rshift, FW_DEFAULT);4977dictAppendWord(dp, "s\"", stringQuoteIm, FW_IMMEDIATE);4978dictAppendWord(dp, "s>d", sToD, FW_DEFAULT);4979dictAppendWord(dp, "sign", sign, FW_DEFAULT);4980dictAppendWord(dp, "sm/rem", smSlashRem, FW_DEFAULT);4981dictAppendWord(dp, "source", source, FW_DEFAULT);4982dictAppendWord(dp, "state", state, FW_DEFAULT);4983dictAppendWord(dp, "swap", swap, FW_DEFAULT);4984dictAppendWord(dp, "then", endifCoIm, FW_COMPIMMED);4985dictAppendWord(dp, "type", type, FW_DEFAULT);4986dictAppendWord(dp, "u.", uDot, FW_DEFAULT);4987dictAppendWord(dp, "u<", uIsLess, FW_DEFAULT);4988dictAppendWord(dp, "u>", uIsGreater, FW_DEFAULT);4989dictAppendWord(dp, "um*", umStar, FW_DEFAULT);4990dictAppendWord(dp, "um/mod", umSlashMod, FW_DEFAULT);4991dictAppendWord(dp, "unloop", unloopCo, FW_COMPILE);4992dictAppendWord(dp, "until", untilCoIm, FW_COMPIMMED);4993dictAppendWord(dp, "variable", variable, FW_DEFAULT);4994dictAppendWord(dp, "while", whileCoIm, FW_COMPIMMED);4995dictAppendWord(dp, "word", ficlWord, FW_DEFAULT);4996dictAppendWord(dp, "xor", bitwiseXor, FW_DEFAULT);4997dictAppendWord(dp, "[", lbracketCoIm, FW_COMPIMMED);4998dictAppendWord(dp, "[\']", bracketTickCoIm,FW_COMPIMMED);4999dictAppendWord(dp, "[char]", charCoIm, FW_COMPIMMED);5000dictAppendWord(dp, "]", rbracket, FW_DEFAULT);5001/*5002** CORE EXT word set...5003** see softcore.fr for other definitions5004*/5005/* "#tib" */5006dictAppendWord(dp, ".(", dotParen, FW_IMMEDIATE);5007/* ".r" */5008dictAppendWord(dp, "0>", zeroGreater, FW_DEFAULT);5009dictAppendWord(dp, "2>r", twoToR, FW_COMPILE);5010dictAppendWord(dp, "2r>", twoRFrom, FW_COMPILE);5011dictAppendWord(dp, "2r@", twoRFetch, FW_COMPILE);5012dictAppendWord(dp, ":noname", colonNoName, FW_DEFAULT);5013dictAppendWord(dp, "?do", qDoCoIm, FW_COMPIMMED);5014dictAppendWord(dp, "again", againCoIm, FW_COMPIMMED);5015dictAppendWord(dp, "c\"", cstringQuoteIm, FW_IMMEDIATE);5016dictAppendWord(dp, "hex", hex, FW_DEFAULT);5017dictAppendWord(dp, "pad", pad, FW_DEFAULT);5018dictAppendWord(dp, "parse", parse, FW_DEFAULT);5019dictAppendWord(dp, "pick", pick, FW_DEFAULT);5020/* query restore-input save-input tib u.r u> unused [compile] */5021dictAppendWord(dp, "roll", roll, FW_DEFAULT);5022dictAppendWord(dp, "refill", refill, FW_DEFAULT);5023dictAppendWord(dp, "source-id", sourceid, FW_DEFAULT);5024dictAppendWord(dp, "to", toValue, FW_IMMEDIATE);5025dictAppendWord(dp, "value", constant, FW_DEFAULT);5026dictAppendWord(dp, "\\", commentLine, FW_IMMEDIATE);502750285029/*5030** Set CORE environment query values5031*/5032ficlSetEnv(pSys, "/counted-string", FICL_STRING_MAX);5033ficlSetEnv(pSys, "/hold", nPAD);5034ficlSetEnv(pSys, "/pad", nPAD);5035ficlSetEnv(pSys, "address-unit-bits", 8);5036ficlSetEnv(pSys, "core", FICL_TRUE);5037ficlSetEnv(pSys, "core-ext", FICL_FALSE);5038ficlSetEnv(pSys, "floored", FICL_FALSE);5039ficlSetEnv(pSys, "max-char", UCHAR_MAX);5040ficlSetEnvD(pSys,"max-d", 0x7fffffff, 0xffffffff);5041ficlSetEnv(pSys, "max-n", 0x7fffffff);5042ficlSetEnv(pSys, "max-u", 0xffffffff);5043ficlSetEnvD(pSys,"max-ud", 0xffffffff, 0xffffffff);5044ficlSetEnv(pSys, "return-stack-cells",FICL_DEFAULT_STACK);5045ficlSetEnv(pSys, "stack-cells", FICL_DEFAULT_STACK);50465047/*5048** DOUBLE word set (partial)5049*/5050dictAppendWord(dp, "2constant", twoConstant, FW_IMMEDIATE);5051dictAppendWord(dp, "2literal", twoLiteralIm, FW_IMMEDIATE);5052dictAppendWord(dp, "2variable", twoVariable, FW_IMMEDIATE);5053dictAppendWord(dp, "dnegate", dnegate, FW_DEFAULT);505450555056/*5057** EXCEPTION word set5058*/5059dictAppendWord(dp, "catch", ficlCatch, FW_DEFAULT);5060dictAppendWord(dp, "throw", ficlThrow, FW_DEFAULT);50615062ficlSetEnv(pSys, "exception", FICL_TRUE);5063ficlSetEnv(pSys, "exception-ext", FICL_TRUE);50645065/*5066** LOCAL and LOCAL EXT5067** see softcore.c for implementation of locals|5068*/5069#if FICL_WANT_LOCALS5070pSys->pLinkParen =5071dictAppendWord(dp, "(link)", linkParen, FW_COMPILE);5072pSys->pUnLinkParen =5073dictAppendWord(dp, "(unlink)", unlinkParen, FW_COMPILE);5074dictAppendWord(dp, "doLocal", doLocalIm, FW_COMPIMMED);5075pSys->pGetLocalParen =5076dictAppendWord(dp, "(@local)", getLocalParen, FW_COMPILE);5077pSys->pToLocalParen =5078dictAppendWord(dp, "(toLocal)", toLocalParen, FW_COMPILE);5079pSys->pGetLocal0 =5080dictAppendWord(dp, "(@local0)", getLocal0, FW_COMPILE);5081pSys->pToLocal0 =5082dictAppendWord(dp, "(toLocal0)",toLocal0, FW_COMPILE);5083pSys->pGetLocal1 =5084dictAppendWord(dp, "(@local1)", getLocal1, FW_COMPILE);5085pSys->pToLocal1 =5086dictAppendWord(dp, "(toLocal1)",toLocal1, FW_COMPILE);5087dictAppendWord(dp, "(local)", localParen, FW_COMPILE);50885089pSys->pGet2LocalParen =5090dictAppendWord(dp, "(@2local)", get2LocalParen, FW_COMPILE);5091pSys->pTo2LocalParen =5092dictAppendWord(dp, "(to2Local)",to2LocalParen, FW_COMPILE);5093dictAppendWord(dp, "(2local)", twoLocalParen, FW_COMPILE);50945095ficlSetEnv(pSys, "locals", FICL_TRUE);5096ficlSetEnv(pSys, "locals-ext", FICL_TRUE);5097ficlSetEnv(pSys, "#locals", FICL_MAX_LOCALS);5098#endif50995100/*5101** Optional MEMORY-ALLOC word set5102*/51035104dictAppendWord(dp, "allocate", ansAllocate, FW_DEFAULT);5105dictAppendWord(dp, "free", ansFree, FW_DEFAULT);5106dictAppendWord(dp, "resize", ansResize, FW_DEFAULT);51075108ficlSetEnv(pSys, "memory-alloc", FICL_TRUE);51095110/*5111** optional SEARCH-ORDER word set5112*/5113ficlCompileSearch(pSys);51145115/*5116** TOOLS and TOOLS EXT5117*/5118ficlCompileTools(pSys);51195120/*5121** FILE and FILE EXT5122*/5123#if FICL_WANT_FILE5124ficlCompileFile(pSys);5125#endif51265127/*5128** Ficl extras5129*/5130#if FICL_WANT_FLOAT5131dictAppendWord(dp, ".hash", dictHashSummary,FW_DEFAULT);5132#endif5133dictAppendWord(dp, ".ver", ficlVersion, FW_DEFAULT);5134dictAppendWord(dp, "-roll", minusRoll, FW_DEFAULT);5135dictAppendWord(dp, ">name", toName, FW_DEFAULT);5136dictAppendWord(dp, "add-parse-step",5137addParseStep, FW_DEFAULT);5138dictAppendWord(dp, "body>", fromBody, FW_DEFAULT);5139dictAppendWord(dp, "compare", compareString, FW_DEFAULT); /* STRING */5140dictAppendWord(dp, "compare-insensitive", compareStringInsensitive, FW_DEFAULT); /* STRING */5141dictAppendWord(dp, "compile-only",5142compileOnly, FW_DEFAULT);5143dictAppendWord(dp, "endif", endifCoIm, FW_COMPIMMED);5144dictAppendWord(dp, "last-word", getLastWord, FW_DEFAULT);5145dictAppendWord(dp, "hash", hash, FW_DEFAULT);5146dictAppendWord(dp, "objectify", setObjectFlag, FW_DEFAULT);5147dictAppendWord(dp, "?object", isObject, FW_DEFAULT);5148dictAppendWord(dp, "parse-word",parseNoCopy, FW_DEFAULT);5149dictAppendWord(dp, "sfind", sFind, FW_DEFAULT);5150dictAppendWord(dp, "sliteral", sLiteralCoIm, FW_COMPIMMED); /* STRING */5151dictAppendWord(dp, "sprintf", ficlSprintf, FW_DEFAULT);5152dictAppendWord(dp, "strlen", ficlStrlen, FW_DEFAULT);5153dictAppendWord(dp, "q@", quadFetch, FW_DEFAULT);5154dictAppendWord(dp, "q!", quadStore, FW_DEFAULT);5155dictAppendWord(dp, "w@", wFetch, FW_DEFAULT);5156dictAppendWord(dp, "w!", wStore, FW_DEFAULT);5157dictAppendWord(dp, "x.", hexDot, FW_DEFAULT);5158#if FICL_WANT_USER5159dictAppendWord(dp, "(user)", userParen, FW_DEFAULT);5160dictAppendWord(dp, "user", userVariable, FW_DEFAULT);5161#endif5162#ifdef TESTMAIN5163dictAppendWord(dp, "random", ficlRandom, FW_DEFAULT);5164dictAppendWord(dp, "seed-random",ficlSeedRandom,FW_DEFAULT);5165#endif51665167/*5168** internal support words5169*/5170dictAppendWord(dp, "(create)", createParen, FW_COMPILE);5171pSys->pExitParen =5172dictAppendWord(dp, "(exit)", exitParen, FW_COMPILE);5173pSys->pSemiParen =5174dictAppendWord(dp, "(;)", semiParen, FW_COMPILE);5175pSys->pLitParen =5176dictAppendWord(dp, "(literal)", literalParen, FW_COMPILE);5177pSys->pTwoLitParen =5178dictAppendWord(dp, "(2literal)",twoLitParen, FW_COMPILE);5179pSys->pStringLit =5180dictAppendWord(dp, "(.\")", stringLit, FW_COMPILE);5181pSys->pCStringLit =5182dictAppendWord(dp, "(c\")", cstringLit, FW_COMPILE);5183pSys->pBranch0 =5184dictAppendWord(dp, "(branch0)", branch0, FW_COMPILE);5185pSys->pBranchParen =5186dictAppendWord(dp, "(branch)", branchParen, FW_COMPILE);5187pSys->pDoParen =5188dictAppendWord(dp, "(do)", doParen, FW_COMPILE);5189pSys->pDoesParen =5190dictAppendWord(dp, "(does>)", doesParen, FW_COMPILE);5191pSys->pQDoParen =5192dictAppendWord(dp, "(?do)", qDoParen, FW_COMPILE);5193pSys->pLoopParen =5194dictAppendWord(dp, "(loop)", loopParen, FW_COMPILE);5195pSys->pPLoopParen =5196dictAppendWord(dp, "(+loop)", plusLoopParen, FW_COMPILE);5197pSys->pInterpret =5198dictAppendWord(dp, "interpret", interpret, FW_DEFAULT);5199dictAppendWord(dp, "lookup", lookup, FW_DEFAULT);5200pSys->pOfParen =5201dictAppendWord(dp, "(of)", ofParen, FW_DEFAULT);5202dictAppendWord(dp, "(variable)",variableParen, FW_COMPILE);5203dictAppendWord(dp, "(constant)",constantParen, FW_COMPILE);5204dictAppendWord(dp, "(parse-step)",5205parseStepParen, FW_DEFAULT);5206pSys->pExitInner =5207dictAppendWord(dp, "exit-inner",ficlExitInner, FW_DEFAULT);52085209/*5210** Set up system's outer interpreter loop - maybe this should be in initSystem?5211*/5212pSys->pInterp[0] = pSys->pInterpret;5213pSys->pInterp[1] = pSys->pBranchParen;5214pSys->pInterp[2] = (FICL_WORD *)(void *)(-2);52155216assert(dictCellsAvail(dp) > 0);52175218return;5219}522052215222