home *** CD-ROM | disk | FTP | other *** search
/ ittybittycomputers.com / www.ittybittycomputers.com.tar / www.ittybittycomputers.com / IttyBitty / TinyBasic / TinyBasic.c < prev    next >
C/C++ Source or Header  |  2008-10-31  |  67KB  |  1,456 lines

  1. /* Tiny Basic Intermediate Language Interpreter -- 2004 July 19 */
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stdlib.h>  /* added 08 Oct 31 */
  6.  
  7. /* Default input/output file names, if defined (omit otherwise)... */
  8. #define DefaultInputFile "TBasm.txt"
  9. #define DefaultOutputFile "TBout.txt"
  10.  
  11. /* File input/output function macros (adjust for C++ framework) */
  12. #define FileType           FILE*
  13. #define IoFileClose(fi)    fclose(fi)
  14. #define InFileChar(fi)     CfileRead(fi)
  15. #define OutFileChar(fi,ch) fputc(ch,fi)
  16. #define ScreenChar(ch)     putchar(ch)
  17. #define KeyInChar          (char)getchar()
  18. #define NeedsEcho          false
  19. #define BreakTest          Broken
  20.  
  21. /* File input/output function macros (Qt examples:) */
  22. /* #define FileType           QFile* */
  23. /* #define IoFileClose(fi)    fi->close() */
  24. /* #define InFileChar(fi)     (fi->atEnd()?'\0':fi->getch()) */
  25. /* #define OutFileChar(fi,ch) fi->putch(ch) */
  26.  
  27. char CfileRead(FileType fi) {   /* C file reader, returns '\0' on eof */
  28.   int chn = fgetc(fi);
  29.   if (chn == EOF) return '\0';
  30.   return (char)chn;} /* ~CfileRead */
  31.  
  32. /* Constants: */
  33.  
  34. #define aByte unsigned char
  35. #define CoreTop 65536 /* Core size */
  36. #define UserProg 32   /* Core address of front of Basic program */
  37. #define EndUser 34    /* Core address of end of stack/user space */
  38. #define EndProg 36    /* Core address of end of Basic program */
  39. #define GoStkTop 38   /* Core address of Gosub stack top */
  40. #define LinoCore 40   /* Core address of "Current BASIC line number" */
  41. #define ILPCcore 42   /* Core address of "IL Program Counter" */
  42. #define BPcore 44     /* Core address of "Basic Pointer" */
  43. #define SvPtCore 46   /* Core address of "Saved Pointer" */
  44. #define InLine 48     /* Core address of input line */
  45. #define ExpnStk 128   /* Core address of expression stack (empty) */
  46. #define TabHere 191   /* Core address of output line size, for tabs */
  47. #define WachPoint 255 /* Core address of debug watchpoint USR */
  48. #define ColdGo 256    /* Core address of nominal restart USR */
  49. #define WarmGo 259    /* Core address of nominal warm start USR */
  50. #define InchSub 262   /* Core address of nominal char input USR */
  51. #define OutchSub 265  /* Core address of nominal char output USR */
  52. #define BreakSub 268  /* Core address of nominal break test USR */
  53. #define DumpSub 273   /* Core address of debug core dump USR */
  54. #define PeekSub 276   /* Core address of nominal byte peek USR */
  55. #define Peek2Sub 277  /* Core address of nominal 2-byte peek USR */
  56. #define PokeSub 280   /* Core address of nominal byte poke USR */
  57. #define TrLogSub 283  /* Core address of debug trace log USR */
  58. #define BScode 271    /* Core address of backspace code */
  59. #define CanCode 272   /* Core address of line cancel code */
  60. #define ILfront 286   /* Core address of IL code address */
  61. #define BadOp 15      /* illegal op, default IL code */
  62.   /* Pascal habits die hard.. */
  63. #define true 1
  64. #define false 0
  65.  
  66. /* debugging stuff... */
  67. #define DEBUGON 1     /* 1 enables \t Debugging toggle, 0 disables */
  68. #define LOGSIZE 4096  /* how much to log */
  69. static int Debugging = 0;    /* >0 enables debug code */
  70. int DebugLog[LOGSIZE];       /* quietly logs recent activity */
  71. int LogHere = 0;             /* current index in DebugLog */
  72. int Watcher = 0, Watchee;    /* memory watchpoint */
  73.  
  74. /* Static/global data: */
  75. aByte Core[CoreTop];    /* everything goes in here */
  76. aByte DeCaps[128];      /* capitalization table */
  77. int Lino, ILPC;         /* current line #, IL program counter */
  78. int BP, SvPt;           /* current, saved TB parse pointer */
  79. int SubStk, ExpnTop;    /* stack pointers */
  80. int InLend, SrcEnd;     /* current input line & TB source end */
  81. int UserEnd;
  82. int ILend, XQhere;      /* end of IL code, start of execute loop */
  83. int Broken = false;     /* =true to stop execution or listing */
  84. FileType inFile = NULL; /* from option '-i' or user menu/button */
  85. FileType oFile = NULL;  /* from option '-o' or user menu/button */
  86.  
  87. /************************* Memory Utilities.. *************************/
  88.  
  89. void Poke2(int loc, int valu) {         /* store integer as two bytes */
  90.   Core[loc] = (aByte)((valu>>8)&255);         /* nominally Big-Endian */
  91.   Core[loc+1] = (aByte)(valu&255);} /* ~Poke2 */
  92.  
  93. int Peek2(int loc) {                  /* fetch integer from two bytes */
  94.   return ((int)Core[loc])*256 + ((int)Core[loc+1]);} /* ~Peek2 */
  95.  
  96. /************************** I/O Utilities... **************************/
  97.  
  98. void Ouch(char ch) {                         /* output char to stdout */
  99.   if (oFile != NULL) {                   /* there is an output file.. */
  100.     if (ch>=' ') OutFileChar(oFile,ch);
  101.     else if (ch == '\r') OutFileChar(oFile,'\n');}
  102.   if (ch=='\r') {
  103.     Core[TabHere] = 0;         /* keep count of how long this line is */
  104.     ScreenChar('\n');}
  105.   else if (ch>=' ') if (ch<='~') {  /* ignore non-print control chars */
  106.     Core[TabHere]++;
  107.     ScreenChar(ch);}} /* ~Ouch */
  108.  
  109. char Inch(void) {          /* read input character from stdin or file */
  110.   char ch;
  111.   if (inFile != NULL) {        /* there is a file to get input from.. */
  112.     ch = InFileChar(inFile);
  113.     if (ch == '\n') ch = '\r';
  114.     if (ch == '\0') {          /* switch over to console input at eof */
  115.       IoFileClose(inFile);
  116.       inFile = NULL;}
  117.     else {
  118.       Ouch(ch);         /* echo input to screen (but not output file) */
  119.       return ch;}}
  120.   ch = KeyInChar;                             /* get input from stdin */
  121.   if (NeedsEcho) ScreenChar(ch);   /* alternative input may need this */
  122.   if (oFile != NULL) OutFileChar(oFile,ch); /* echo it to output file */
  123.   if (ch == '\n') {
  124.     ch = '\r';                     /* convert line end to TB standard */
  125.     Core[TabHere] = 0;}                          /* reset tab counter */
  126.   return ch;} /* ~Inch */
  127.  
  128. int StopIt(void) {return BreakTest;}   /* ~StopIt, .. not implemented */
  129.  
  130. void OutStr(char* theMsg) {         /* output a string to the console */
  131.   while (*theMsg != '\0') Ouch(*theMsg++);} /* ~OutStr */
  132.  
  133. void OutLn(void) {            /* terminate output line to the console */
  134.   OutStr("\r");} /* ~OutLn */
  135.  
  136. void OutInt(int theNum) {           /* output a number to the console */
  137.   if (theNum<0) {
  138.     Ouch('-');
  139.     theNum = -theNum;}
  140.   if (theNum>9) OutInt(theNum/10);
  141.   Ouch((char)(theNum%10+48));} /* ~OutInt */
  142.  
  143. /*********************** Debugging Utilities... ***********************/
  144.  
  145. void OutHex(int num, int nd) {  /* output a hex number to the console */
  146.   if (nd>1) OutHex(num>>4, nd-1);
  147.   num = num&15;
  148.   if (num>9) Ouch((char)(num+55));
  149.     else Ouch((char)(num+48));} /* ~OutHex */
  150.  
  151. void ShowSubs(void) {       /* display subroutine stack for debugging */
  152.   int ix;
  153.   OutLn(); OutStr(" [Stk "); OutHex(SubStk,5);
  154.   for (ix=SubStk; ix<UserEnd; ix++) {
  155.     OutStr(" ");
  156.     OutInt(Peek2(ix++));}
  157.   OutStr("]");} /* ~ShowSubs */
  158.  
  159. void ShowExSt(void) {       /* display expression stack for debugging */
  160.   int ix;
  161.   OutLn(); OutStr(" [Exp "); OutHex(ExpnTop,3);
  162.   if ((ExpnTop&1)==0) for (ix=ExpnTop; ix<ExpnStk; ix++) {
  163.     OutStr(" ");
  164.     OutInt((int)((short)Peek2(ix++)));}
  165.   else for (ix=ExpnTop; ix<ExpnStk; ix++) {
  166.     OutStr(".");
  167.     OutInt((int)Core[ix]);}
  168.   OutStr("]");} /* ~ShowExSt */
  169.  
  170. void ShowVars(int whom) {               /* display vars for debugging */
  171.   int ix, valu = 1, prior = 1;
  172.   if (whom==0) whom = 26; else {
  173.     whom = (whom>>1)&31;             /* whom is a specified var, or 0 */
  174.     valu = whom;}
  175.   OutLn(); OutStr("  [Vars");
  176.   for (ix=valu; ix<=whom; ix++) {  /* all non-zero vars, or else whom */
  177.     valu = (int)((short)Peek2(ix*2+ExpnStk));
  178.     if (valu==0) if (prior==0) continue;          /* omit multiple 0s */
  179.     prior = valu;
  180.     OutStr(" ");
  181.     Ouch((char)(ix+64));                             /* show var name */
  182.     OutStr("=");
  183.     OutInt(valu);}
  184.   OutStr("]");} /* ~ShowVars */
  185.  
  186. void ShoMemDump(int here, int nlocs) {     /* display hex memory dump */
  187.   int temp, thar = here&-16;
  188.   while (nlocs>0) {
  189.     temp = thar;
  190.     OutLn();
  191.     OutHex(here,4);
  192.     OutStr(": ");
  193.     while (thar<here) {OutStr("   "); thar++;}
  194.     do {
  195.       OutStr(" ");
  196.       if (nlocs-- >0) OutHex(Core[here],2);
  197.         else OutStr("  ");}
  198.       while (++here%16 !=0);
  199.     OutStr("  ");
  200.     while (temp<thar) {OutStr(" "); temp++;}
  201.     while (thar<here) {
  202.       if (nlocs<0) if ((thar&15) >= nlocs+16) break;
  203.       temp = Core[thar++];
  204.       if (temp == (int)'\r') Ouch('\\');
  205.       else if (temp<32) Ouch('`');
  206.       else if (temp>126) Ouch('~');
  207.         else Ouch((char)temp);}}
  208.   OutLn();} /* ~ShoMemDump */
  209.  
  210. void ShoLogVal(int item) {   /* format & output one activity log item */
  211.   int valu = DebugLog[item];
  212.   OutLn();
  213.   if (valu < -65536) {                         /* store to a variable */
  214.     Ouch((char)(((valu>>17)&31)+64));
  215.     OutStr("=");
  216.     OutInt((valu&0x7FFF)-(valu&0x8000));}
  217.   else if (valu < -32768) {                                /* error # */
  218.     OutStr("Err ");
  219.     OutInt(-valu-32768);}
  220.   else if (valu<0) {                 /* only logs IL sequence changes */
  221.     OutStr("  IL+");
  222.     OutHex(-Peek2(ILfront)-valu,3);}
  223.   else if (valu<65536) {                          /* TinyBasic line # */
  224.     OutStr("#");
  225.     OutInt(valu);}
  226.   else {                                          /* poke memory byte */
  227.     OutStr("!");
  228.     OutHex(valu,4);
  229.     OutStr("=");
  230.     OutInt(valu>>16);}} /* ~ShoLogVal */
  231.  
  232. void ShowLog(void) {            /* display activity log for debugging */
  233.   int ix;
  234.   OutLn();
  235.   OutStr("*** Activity Log @ ");
  236.   OutInt(LogHere);
  237.   OutStr(" ***");
  238.   if (LogHere >= LOGSIZE)   /* circular, show only last 4K activities */
  239.     for (ix=(LogHere&(LOGSIZE-1)); ix<LOGSIZE; ix++) ShoLogVal(ix);
  240.   for (ix=0; ix<(LogHere&(LOGSIZE-1)); ix++) ShoLogVal(ix);
  241.   OutLn();
  242.   OutStr("*****");
  243.   OutLn();} /* ~ShowLog */
  244.  
  245. void LogIt(int valu) {          /* insert this valu into activity log */
  246.   DebugLog[(LogHere++)&(LOGSIZE-1)] = valu;}
  247.  
  248. /************************ Utility functions... ************************/
  249.  
  250. void WarmStart(void) {                 /* initialize existing program */
  251.   UserEnd = Peek2(EndUser);
  252.   SubStk = UserEnd;            /* empty subroutine, expression stacks */
  253.   Poke2(GoStkTop,SubStk);
  254.   ExpnTop = ExpnStk;
  255.   Lino = 0;                                        /* not in any line */
  256.   ILPC = 0;                                      /* start IL at front */
  257.   SvPt = InLine;
  258.   BP = InLine;
  259.   Core[BP] = 0;
  260.   Core[TabHere] = 0;
  261.   InLend = InLine;} /* ~WarmStart */
  262.  
  263. void ColdStart(void) {                 /* initialize program to empty */
  264.   if (Peek2(ILfront) != ILfront+2) ILend = Peek2(ILfront)+0x800;
  265.   Poke2(UserProg,(ILend+255)&-256);   /* start Basic shortly after IL */
  266.   if (CoreTop>65535) {
  267.     Poke2(EndUser,65534);
  268.     Poke2(65534,0xDEAD);}
  269.   else Poke2(EndUser,CoreTop);
  270.   WarmStart();
  271.   SrcEnd = Peek2(UserProg);
  272.   Poke2(SrcEnd++,0);
  273.   Poke2(EndProg,++SrcEnd);} /* ~ColdStart */
  274.  
  275. void TBerror(void) {                      /* report interpreter error */
  276.   if (ILPC == 0) return;                       /* already reported it */
  277.   OutLn();
  278.   LogIt(-ILPC-32768);
  279.   OutStr("Tiny Basic error #");          /* IL address is the error # */
  280.   OutInt(ILPC-Peek2(ILfront));
  281.   if (Lino>0) {                          /* Lino=0 if in command line */
  282.     OutStr(" at line ");
  283.     OutInt(Lino);}
  284.   OutLn();
  285.   if (Debugging>0) {                /* some extra info if debugging.. */
  286.     ShowSubs();
  287.     ShowExSt();
  288.     ShowVars(0);
  289.     OutStr(" [BP=");
  290.     OutHex(BP,4);
  291.     OutStr(", TB@");
  292.     OutHex(Peek2(UserProg),4);
  293.     OutStr(", IL@");
  294.     OutHex(Peek2(ILfront),4);
  295.     OutStr("]");
  296.     ShoMemDump((BP-30)&-16,64);}
  297.   Lino = 0;                           /* restart interpreter at front */
  298.   ExpnTop = ExpnStk;                   /* with empty expression stack */
  299.   ILPC = 0;       /* cheap error test; interp reloads it from ILfront */
  300.   BP = InLine;} /* ~TBerror */
  301.  
  302. void PushSub(int valu) {               /* push value onto Gosub stack */
  303.   if (SubStk<=SrcEnd) TBerror(); /* overflow: bumped into program end */
  304.   else {
  305.     SubStk = SubStk-2;
  306.     Poke2(GoStkTop,SubStk);
  307.     Poke2(SubStk,valu);}
  308.   if (Debugging>0) ShowSubs();} /* ~PushSub */
  309.  
  310. int PopSub(void) {                       /* pop value off Gosub stack */
  311.   if (SubStk>=Peek2(EndUser)-1) {   /* underflow (nothing in stack).. */
  312.     TBerror();
  313.     return -1;}
  314.   else {
  315.       if (Debugging>1) ShowSubs();
  316.     SubStk = SubStk+2;
  317.     Poke2(GoStkTop,SubStk);
  318.     return Peek2(SubStk-2);}} /* ~PopSub */
  319.  
  320. void PushExBy(int valu) {          /* push byte onto expression stack */
  321.   if (ExpnTop<=InLend) TBerror(); /* overflow: bumped into input line */
  322.     else Core[--ExpnTop] = (aByte)(valu&255);
  323.   if (Debugging>0) ShowExSt();} /* ~PushExBy */
  324.  
  325. int PopExBy(void) {                  /* pop byte off expression stack */
  326.   if (ExpnTop<ExpnStk) return (int)Core[ExpnTop++];
  327.   TBerror();                          /* underflow (nothing in stack) */
  328.   return -1;} /* ~PopExBy */
  329.  
  330. void PushExInt(int valu) {      /* push integer onto expression stack */
  331.   ExpnTop = ExpnTop-2;
  332.   if (ExpnTop<InLend) TBerror();  /* overflow: bumped into input line */
  333.     else Poke2(ExpnTop,valu);
  334.   if (Debugging>0) ShowExSt();} /* ~PushExInt */
  335.  
  336. int PopExInt(void) {              /* pop integer off expression stack */
  337.   if (++ExpnTop<ExpnStk) return (int)((short)Peek2((ExpnTop++)-1));
  338.   TBerror();    /* underflow (nothing in stack) */
  339.   return -1;} /* ~PopExInt */
  340.  
  341. int DeHex(char* txt, int ndigs) {                /* decode hex -> int */
  342.   int num = 0;
  343.   char ch = ' ';
  344.   while (ch<'0')                              /* first skip to num... */
  345.     if (ch == '\0') return -1; else ch = DeCaps[((int)*txt++)&127];
  346.   if (ch>'F' || ch>'9' && ch<'A') return -1;               /* not hex */
  347.   while ((ndigs--) >0) {                 /* only get requested digits */
  348.     if (ch<'0' || ch>'F') return num;              /* not a hex digit */
  349.     if (ch>='A') num = num*16-55+((int)ch);      /* A-F */
  350.     else if (ch<='9') num = num*16-48+((int)ch); /* 0-9 */
  351.       else return num;          /* something in between, i.e. not hex */
  352.     ch = DeCaps[((int)*txt++)&127];}
  353.   return num;} /* ~DeHex */
  354.  
  355. int SkipTo(int here, char fch) {     /* search for'd past next marker */
  356.   while (true) {
  357.     char ch = (char)Core[here++];                /* look at next char */
  358.     if (ch == fch) return here;                             /* got it */
  359.     if (ch == '\0') return --here;}} /* ~SkipTo */
  360.  
  361. int FindLine(int theLine) {         /* find theLine in TB source code */
  362.   int ix;
  363.   int here = Peek2(UserProg);                       /* start at front */
  364.   while (true) {
  365.     ix = Peek2(here++);
  366.     if (theLine<=ix || ix==0) return --here;  /* found it or overshot */
  367.     here = SkipTo(++here, '\r');}         /* skip to end of this line */
  368.   } /* ~FindLine */
  369.  
  370. void GoToLino(void) {     /* find line # Lino and set BP to its front */
  371.   int here;
  372.   if (Lino <= 0) {              /* Lino=0 is just command line (OK).. */
  373.     BP = InLine;
  374.     if (DEBUGON>0) LogIt(0);
  375.     return;}
  376.   if (DEBUGON>0) LogIt(Lino);
  377.   if (Debugging>0) {OutStr(" [#"); OutInt(Lino); OutStr("]");}
  378.   BP = FindLine(Lino);                  /* otherwise try to find it.. */
  379.   here = Peek2(BP++);
  380.   if (here==0) TBerror();               /* ran off the end, error off */
  381.   else if (Lino != here) TBerror();                      /* not there */
  382.     else BP++;} /* ~GoToLino */                             /* got it */
  383.  
  384. void ListIt(int frm, int too) {            /* list the stored program */
  385.   char ch;
  386.   int here;
  387.   if (frm==0) {           /* 0,0 defaults to all; n,0 defaults to n,n */
  388.     too = 65535;
  389.     frm = 1;}
  390.   else if (too==0) too = frm;
  391.   here = FindLine(frm);                   /* try to find first line.. */
  392.   while (!StopIt()) {
  393.     frm = Peek2(here++);             /* get this line's # to print it */
  394.     if (frm>too || frm==0) break;
  395.     here++;
  396.     OutInt(frm);
  397.     Ouch(' ');
  398.     do {                                            /* print the text */
  399.       ch = (char)Core[here++];
  400.       Ouch(ch);}
  401.       while (ch>'\r');}} /* ~ListIt */
  402.  
  403. void ConvtIL(char* txt) {                 /* convert & load TBIL code */
  404.   int valu;
  405.   ILend = ILfront+2;
  406.   Poke2(ILfront,ILend);    /* initialize pointers as promised in TBEK */
  407.   Poke2(ColdGo+1,ILend);
  408.   Core[ILend] = (aByte)BadOp;   /* illegal op, in case nothing loaded */
  409.   if (txt == NULL) return;
  410.   while (*txt != '\0') {                            /* get the data.. */
  411.     while (*txt > '\r') txt++;               /* (no code on 1st line) */
  412.     if (*txt++ == '\0') break;                      /* no code at all */
  413.     while (*txt > ' ') txt++;                    /* skip over address */
  414.     if (*txt++ == '\0') break;
  415.     while (true) {
  416.       valu = DeHex(txt++, 2);                           /* get a byte */
  417.       if (valu<0) break;                      /* no more on this line */
  418.       Core[ILend++] = (aByte)valu;      /* insert this byte into code */
  419.       txt++;}}
  420.   XQhere = 0;                        /* requires new XQ to initialize */
  421.   Core[ILend] = 0;} /* ~ConvtIL */
  422.  
  423. void LineSwap(int here) {   /* swap SvPt/BP if here is not in InLine  */
  424.   if (here<InLine || here>=InLend) {
  425.     here = SvPt;
  426.     SvPt = BP;
  427.     BP = here;}
  428.   else SvPt = BP;} /* ~LineSwap */
  429.  
  430. /************************** Main Interpreter **************************/
  431.  
  432. void Interp(void) {
  433.   char ch;    /* comments from TinyBasic Experimenter's Kit, pp.15-21 */
  434.   int op, ix, here, chpt;                                    /* temps */
  435.   Broken = false;          /* initialize this for possible later test */
  436.   while (true) {
  437.     if (StopIt()) {
  438.       Broken = false;
  439.       OutLn();
  440.       OutStr("*** User Break ***");
  441.       TBerror();}
  442.     if (ILPC==0) {
  443.       ILPC = Peek2(ILfront);
  444.       if (DEBUGON>0) LogIt(-ILPC);
  445.       if (Debugging>0) {
  446.         OutLn(); OutStr("[IL="); OutHex(ILPC,4); OutStr("]");}}
  447.     if (DEBUGON>0) if (Watcher>0) {             /* check watchpoint.. */
  448.       if (((Watchee<0) && (Watchee+256+(int)Core[Watcher]) !=0)
  449.           || ((Watchee >= 0) && (Watchee==(int)Core[Watcher]))) {
  450.         OutLn();
  451.         OutStr("*** Watched ");
  452.         OutHex(Watcher,4);
  453.         OutStr(" = ");
  454.         OutInt((int)Core[Watcher]);
  455.         OutStr(" *** ");
  456.         Watcher = 0;
  457.         TBerror();
  458.         continue;}}
  459.     op = (int)Core[ILPC++];
  460.       if (Debugging>0) {
  461.         OutLn(); OutStr("[IL+"); OutHex(ILPC-Peek2(ILfront)-1,3);
  462.         OutStr("="); OutHex(op,2); OutStr("]");}
  463.     switch (op>>5) {
  464.     default: switch (op) {
  465.       case 15:
  466.         TBerror();
  467.         return;
  468.  
  469. /* SX n    00-07   Stack Exchange. */
  470. /*                 Exchange the top byte of computational stack with  */
  471. /* that "n" bytes into the stack. The top/left byte of the stack is   */
  472. /* considered to be byte 0, so SX 0 does nothing.                     */
  473.       case 1: case 2: case 3: case 4: case 5: case 6: case 7:
  474.         if (ExpnTop+op>=ExpnStk) {       /* swap is below stack depth */
  475.           TBerror();
  476.           return;}
  477.         ix = (int)Core[ExpnTop];
  478.         Core[ExpnTop] = Core[ExpnTop+op];
  479.         Core[ExpnTop+op] = (aByte)ix;
  480.         if (Debugging>0) ShowExSt();
  481.         break;
  482.  
  483. /* LB n    09nn    Push Literal Byte onto Stack.                      */
  484. /*                 This adds one byte to the expression stack, which  */
  485. /* is the second byte of the instruction. An error stop will occur if */
  486. /* the stack overflows. */
  487.       case 9:
  488.         PushExBy((int)Core[ILPC++]);                  /* push IL byte */
  489.         break;
  490.  
  491. /* LN n    0Annnn  Push Literal Number.                               */
  492. /*                 This adds the following two bytes to the           */
  493. /* computational stack, as a 16-bit number. Stack overflow results in */
  494. /* an error stop. Numbers are assumed to be Big-Endian.               */
  495.       case 10:
  496.         PushExInt(Peek2(ILPC++));              /* get next 2 IL bytes */
  497.         ILPC++;
  498.         break;
  499.  
  500. /* DS      0B      Duplicate Top Number (two bytes) on Stack.         */
  501. /*                 An error stop will occur if there are less than 2  */
  502. /* bytes (1 int) on the expression stack or if the stack overflows.   */
  503.       case 11:
  504.         op = ExpnTop;
  505.         ix = PopExInt();
  506.         if (ILPC == 0) break;                            /* underflow */
  507.         ExpnTop = op;
  508.         PushExInt(ix);
  509.         break;
  510.  
  511. /* SP      0C      Stack Pop.                                         */
  512. /*                 The top two bytes are removed from the expression  */
  513. /* stack and discarded. Underflow results in an error stop.           */
  514.       case 12:
  515.         ix = PopExInt();
  516.           if (Debugging>0) ShowExSt();
  517.         break;
  518.  
  519. /* SB      10      Save BASIC Pointer.                                */
  520. /*                 If BASIC pointer is pointing into the input line   */
  521. /* buffer, it is copied to the Saved Pointer; otherwise the two       */
  522. /* pointers are exchanged.                                            */
  523.       case 16:
  524.         LineSwap(BP);
  525.         break;
  526.  
  527. /* RB      11      Restore BASIC Pointer.                             */
  528. /*                 If the Saved Pointer points into the input line    */
  529. /* buffer, it is replaced by the value in the BASIC pointer;          */
  530. /* otherwise the two pointers are exchanged.                          */
  531.       case 17:
  532.         LineSwap(SvPt);
  533.         break;
  534.  
  535. /* FV      12      Fetch Variable.                                    */
  536. /*                 The top byte of the computational stack is used to */
  537. /* index into Page 00. It is replaced by the two bytes fetched. Error */
  538. /* stops occur with stack overflow or underflow.                      */
  539.       case 18:
  540.         op = PopExBy();
  541.         if (ILPC != 0) PushExInt(Peek2(op));
  542.           if (Debugging>1) ShowVars(op);
  543.         break;
  544.  
  545. /* SV      13      Store Variable.                                    */
  546. /*                 The top two bytes of the computational stack are   */
  547. /* stored into memory at the Page 00 address specified by the third   */
  548. /* byte on the stack. All three bytes are deleted from the stack.     */
  549. /* Underflow results in an error stop.                                */
  550.       case 19:
  551.         ix = PopExInt();
  552.         op = PopExBy();
  553.         if (ILPC == 0) break;
  554.         Poke2(op,ix);
  555.           if (DEBUGON>0) LogIt((ix&0xFFFF)+((op-256)<<16));
  556.           if (Debugging>0) {ShowVars(op); if (Debugging>1) ShowExSt();}
  557.         break;
  558.  
  559. /* GS      14      GOSUB Save.                                        */
  560. /*                 The current BASIC line number is pushed            */
  561. /* onto the BASIC region of the control stack. It is essential that   */
  562. /* the IL stack be empty for this to work properly but no check is    */
  563. /* made for that condition. An error stop occurs on stack overflow.   */
  564.       case 20:
  565.         PushSub(Lino);                   /* push line # (possibly =0) */
  566.         break;
  567.  
  568. /* RS      15      Restore Saved Line.                                */
  569. /*                 Pop the top two bytes off the BASIC region of the  */
  570. /* control stack, making them the current line number. Set the BASIC  */
  571. /* pointer at the beginning of that line. Note that this is the line  */
  572. /* containing the GOSUB which caused the line number to be saved. As  */
  573. /* with the GS opcode, it is essential that the IL region of the      */
  574. /* control stack be empty. If the line number popped off the stack    */
  575. /* does not correspond to a line in the BASIC program an error stop   */
  576. /* occurs. An error stop also results from stack underflow.           */
  577.       case 21:
  578.         Lino = PopSub();         /* get line # (possibly =0) from pop */
  579.         if (ILPC != 0) GoToLino() ;             /* stops run if error */
  580.         break;
  581.  
  582. /* GO      16      GOTO.                                              */
  583. /*                 Make current the BASIC line whose line number is   */
  584. /* equal to the value of the top two bytes in the expression stack.   */
  585. /* That is, the top two bytes are popped off the computational stack, */
  586. /* and the BASIC program is searched until a matching line number is  */
  587. /* found. The BASIC pointer is then positioned at the beginning of    */
  588. /* that line and the RUN mode flag is turned on. Stack underflow and  */
  589. /* non-existent BASIC line result in error stops.                     */
  590.       case 22:
  591.         ILPC = XQhere;                /* the IL assumes an implied NX */
  592.         if (DEBUGON>0) LogIt(-ILPC);
  593.         Lino = PopExInt();
  594.         if (ILPC != 0) GoToLino() ;             /* stops run if error */
  595.         break;
  596.  
  597. /* NE      17      Negate (two's complement).                         */
  598. /*                 The number in the top two bytes of the expression  */
  599. /* stack is replaced with its negative.                               */
  600.       case 23:
  601.         ix = PopExInt();
  602.         if (ILPC != 0) PushExInt(-ix);
  603.         break;
  604.  
  605. /* AD      18      Add.                                               */
  606. /*                 Add the two numbers represented by the top four    */
  607. /* bytes of the expression stack, and replace them with the two-byte  */
  608. /* sum. Stack underflow results in an error stop.                     */
  609.       case 24:
  610.         ix = PopExInt();
  611.         op = PopExInt();
  612.         if (ILPC != 0) PushExInt(op+ix);
  613.         break;
  614.  
  615. /* SU      19      Subtract.                                          */
  616. /*                 Subtract the two-byte number on the top of the     */
  617. /* expression stack from the next two bytes and replace the 4 bytes   */
  618. /* with the two-byte difference.                                      */
  619.       case 25:
  620.         ix = PopExInt();
  621.         op = PopExInt();
  622.         if (ILPC != 0) PushExInt(op-ix);
  623.         break;
  624.  
  625. /* MP      1A      Multiply.                                          */
  626. /*                 Multiply the two numbers represented by the top 4  */
  627. /* bytes of the computational stack, and replace them with the least  */
  628. /* significant 16 bits of the product. Stack underflow is possible.   */
  629.       case 26:
  630.         ix = PopExInt();
  631.         op = PopExInt();
  632.         if (ILPC != 0) PushExInt(op*ix);
  633.         break;
  634.  
  635. /* DV      1B      Divide.                                            */
  636. /*                 Divide the number represented by the top two bytes */
  637. /* of the computational stack into that represented by the next two.  */
  638. /* Replace the 4 bytes with the quotient and discard the remainder.   */
  639. /* This is a signed (two's complement) integer divide, resulting in a */
  640. /* signed integer quotient. Stack underflow or attempted division by  */
  641. /* zero result in an error stop. */
  642.       case 27:
  643.         ix = PopExInt();
  644.         op = PopExInt();
  645.         if (ix == 0) TBerror();                      /* divide by 0.. */
  646.         else if (ILPC != 0) PushExInt(op/ix);
  647.         break;
  648.  
  649. /* CP      1C      Compare.                                           */
  650. /*                 The number in the top two bytes of the expression  */
  651. /* stack is compared to (subtracted from) the number in the 4th and   */
  652. /* fifth bytes of the stack, and the result is determined to be       */
  653. /* Greater, Equal, or Less. The low three bits of the third byte mask */
  654. /* a conditional skip in the IL program to test these conditions; if  */
  655. /* the result corresponds to a one bit, the next byte of the IL code  */
  656. /* is skipped and not executed. The three bits correspond to the      */
  657. /* conditions as follows:                                             */
  658. /*         bit 0   Result is Less                                     */
  659. /*         bit 1   Result is Equal                                    */
  660. /*         bit 2   Result is Greater                                  */
  661. /* Whether the skip is taken or not, all five bytes are deleted from  */
  662. /* the stack. This is a signed (two's complement) comparison so that  */
  663. /* any positive number is greater than any negative number. Multiple  */
  664. /* conditions, such as greater-than-or-equal or unequal (i.e.greater- */
  665. /* than-or-less-than), may be tested by forming the condition mask    */
  666. /* byte of the sum of the respective bits. In particular, a mask byte */
  667. /* of 7 will force an unconditional skip and a mask byte of 0 will    */
  668. /* force no skip. The other 5 bits of the control byte are ignored.   */
  669. /* Stack underflow results in an error stop.                          */
  670.       case 28:
  671.         ix = PopExInt();
  672.         op = PopExBy();
  673.         ix = PopExInt()-ix;                         /* <0 or =0 or >0 */
  674.         if (ILPC == 0) return;                         /* underflow.. */
  675.         if (ix<0) ix = 1;
  676.         else if (ix>0) ix = 4;              /* choose the bit to test */
  677.           else ix = 2;
  678.         if ((ix&op)>0) ILPC++;           /* skip next IL op if bit =1 */
  679.           if (Debugging>0) ShowExSt();
  680.         break;
  681.  
  682. /* NX      1D      Next BASIC Statement.                              */
  683. /*                 Advance to next line in the BASIC program, if in   */
  684. /* RUN mode, or restart the IL program if in the command mode. The    */
  685. /* remainder of the current line is ignored. In the Run mode if there */
  686. /* is another line it becomes current with the pointer positioned at  */
  687. /* its beginning. At this time, if the Break condition returns true,  */
  688. /* execution is aborted and the IL program is restarted after         */
  689. /* printing an error message. Otherwise IL execution proceeds from    */
  690. /* the saved IL address (see the XQ instruction). If there are no     */
  691. /* more BASIC statements in the program an error stop occurs.         */
  692.       case 29:
  693.         if (Lino == 0) ILPC = 0;
  694.         else {
  695.           BP = SkipTo(BP, '\r');          /* skip to end of this line */
  696.           Lino = Peek2(BP++);                           /* get line # */
  697.           if (Lino==0) {                           /* ran off the end */
  698.             TBerror();
  699.             break;}
  700.           else BP++;
  701.           ILPC = XQhere;          /* restart at saved IL address (XQ) */
  702.           if (DEBUGON>0) LogIt(-ILPC);}
  703.         if (DEBUGON>0) LogIt(Lino);
  704.         if (Debugging>0) {OutStr(" [#"); OutInt(Lino); OutStr("]");}
  705.         break;
  706.  
  707. /* LS      1F      List The Program.                                  */
  708. /*                 The expression stack is assumed to have two 2-byte */
  709. /* numbers. The top number is the line number of the last line to be  */
  710. /* listed, and the next is the line number of the first line to be    */
  711. /* listed. If the specified line numbers do not exist in the program, */
  712. /* the next available line (i.e. with the next higher line number) is */
  713. /* assumed instead in each case. If the last line to be listed comes  */
  714. /* before the first, no lines are listed. If Break condition comes    */
  715. /* true during a List operation, the remainder of the listing is      */
  716. /* aborted. Zero is not a valid line number, and an error stop occurs */
  717. /* if either line number specification is zero. The line number       */
  718. /* specifications are deleted from the stack.                         */
  719.       case 31:
  720.         op = 0;
  721.         ix = 0;          /* The IL seems to assume we can handle zero */
  722.         while (ExpnTop<ExpnStk) {   /* or more numbers, so get them.. */
  723.           op = ix;
  724.           ix = PopExInt();}       /* get final line #, then initial.. */
  725.         if (op<0 || ix<0) TBerror();
  726.           else ListIt(ix,op);
  727.         break;
  728.  
  729. /* PN      20      Print Number.                                      */
  730. /*                 The number represented by the top two bytes of the */
  731. /* expression stack is printed in decimal with leading zero           */
  732. /* suppression. If it is negative, it is preceded by a minus sign     */
  733. /* and the magnitude is printed. Stack underflow is possible.         */
  734.       case 32:
  735.         ix = PopExInt();
  736.         if (ILPC != 0) OutInt(ix);
  737.         break;
  738.  
  739. /* PQ      21      Print BASIC String.                                */
  740. /*                 The ASCII characters beginning with the current    */
  741. /* position of BASIC pointer are printed on the console. The string   */
  742. /* to be printed is terminated by quotation mark ("), and the BASIC   */
  743. /* pointer is left at the character following the terminal quote. An  */
  744. /* error stop occurs if a carriage return is imbedded in the string.  */
  745.       case 33:
  746.         while (true) {
  747.           ch = (char)Core[BP++];
  748.           if (ch=='\"') break;                 /* done on final quote */
  749.           if (ch<' ') {      /* error if return or other control char */
  750.             TBerror();
  751.             break;}
  752.           Ouch(ch);}                                      /* print it */
  753.         break;
  754.  
  755. /* PT      22      Print Tab.                                         */
  756. /*                 Print one or more spaces on the console, ending at */
  757. /* the next multiple of eight character positions (from the left      */
  758. /* margin).                                                           */
  759.       case 34:
  760.         do {Ouch(' ');} while (Core[TabHere]%8>0);
  761.         break;
  762.  
  763. /* NL      23      New Line.                                          */
  764. /*                 Output a carriage-return-linefeed sequence to the  */
  765. /* console.                                                           */
  766.       case 35:
  767.         Ouch('\r');
  768.         break;
  769.  
  770. /* PC "xxxx"  24xxxxxxXx   Print Literal String.                      */
  771. /*                         The ASCII string follows opcode and its    */
  772. /* last byte has the most significant bit set to one.                 */
  773.       case 36:
  774.         do {
  775.           ix = (int)Core[ILPC++];
  776.           Ouch((char)(ix&127));          /* strip high bit for output */
  777.           } while ((ix&128)==0);
  778.         break;
  779.  
  780. /* GL      27      Get Input Line.                                    */
  781. /*                 ASCII characters are accepted from console input   */
  782. /* to fill the line buffer. If the line length exceeds the available  */
  783. /* space, the excess characters are ignored and bell characters are   */
  784. /* output. The line is terminated by a carriage return. On completing */
  785. /* one line of input, the BASIC pointer is set to point to the first  */
  786. /* character in the input line buffer, and a carriage-return-linefeed */
  787. /* sequence is [not] output.                                          */
  788.       case 39:
  789.         InLend = InLine;
  790.         while (true) {               /* read input line characters... */
  791.           ch = Inch();
  792.           if (ch=='\r') break;                     /* end of the line */
  793.           else if (ch=='\t') {
  794.             Debugging = (Debugging+DEBUGON)&1;  /* maybe toggle debug */
  795.             ch = ' ';}                       /* convert tabs to space */
  796.           else if (ch==(char)Core[BScode]) {        /* backspace code */
  797.             if (InLend>InLine) InLend--;    /* assume console already */
  798.             else {   /* backing up over front of line: just kill it.. */
  799.               Ouch('\r');
  800.               break;}}
  801.           else if (ch==(char)Core[CanCode]) {     /* cancel this line */
  802.             InLend = InLine;
  803.             Ouch('\r');                /* also start a new input line */
  804.             break;}
  805.           else if (ch<' ') continue;   /* ignore non-ASCII & controls */
  806.           else if (ch>'~') continue;
  807.           if (InLend>ExpnTop-2) continue;    /* discard overrun chars */
  808.           Core[InLend++] = (aByte)ch;}  /* insert this char in buffer */
  809.         while (InLend>InLine && Core[InLend-1] == ' ')
  810.           InLend--;                  /* delete excess trailing spaces */
  811.         Core[InLend++] = (aByte) '\r';  /* insert final return & null */
  812.         Core[InLend] = 0;
  813.         BP = InLine;
  814.         break;
  815.  
  816. /* IL      2A      Insert BASIC Line.                                 */
  817. /*                 Beginning with the current position of the BASIC   */
  818. /* pointer and continuing to the [end of it], the line is inserted    */
  819. /* into the BASIC program space; for a line number, the top two bytes */
  820. /* of the expression stack are used. If this number matches a line    */
  821. /* already in the program it is deleted and the new one replaces it.  */
  822. /* If the new line consists of only a carriage return, it is not      */
  823. /* inserted, though any previous line with the same number will have  */
  824. /* been deleted. The lines are maintained in the program space sorted */
  825. /* by line number. If the new line to be inserted is a different size */
  826. /* than the old line being replaced, the remainder of the program is  */
  827. /* shifted over to make room or to close up the gap as necessary. If  */
  828. /* there is insufficient memory to fit in the new line, the program   */
  829. /* space is unchanged and an error stop occurs (with the IL address   */
  830. /* decremented). A normal error stop occurs on expression stack       */
  831. /* underflow or if the number is zero, which is not a valid line      */
  832. /* number. After completing the insertion, the IL program is          */
  833. /* restarted in the command mode.                                     */
  834.       case 42:
  835.         Lino = PopExInt();                              /* get line # */
  836.         if (Lino <= 0) {          /* don't insert line #0 or negative */
  837.           if (ILPC != 0) TBerror();
  838.             else return;
  839.           break;}
  840.         while (((char)Core[BP]) == ' ') BP++;  /* skip leading spaces */
  841.         if (((char)Core[BP]) == '\r') ix = 0;       /* nothing to add */
  842.           else ix = InLend-BP+2;         /* the size of the insertion */
  843.         op = 0;         /* this will be the number of bytes to delete */
  844.         chpt = FindLine(Lino);             /* try to find this line.. */
  845.         if (Peek2(chpt) == Lino)       /* there is a line to delete.. */
  846.           op = (SkipTo(chpt+2, '\r')-chpt);
  847.         if (ix == 0) if (op==0) {  /* nothing to add nor delete; done */
  848.           Lino = 0;
  849.           break;}
  850.         op = ix-op;      /* = how many more bytes to add or (-)delete */
  851.         if (SrcEnd+op>=SubStk) {                         /* too big.. */
  852.           TBerror();
  853.           break;}
  854.         SrcEnd = SrcEnd+op;                               /* new size */
  855.         if (op>0) for (here=SrcEnd; (here--)>chpt+ix; )
  856.           Core[here] = Core[here-op];  /* shift backend over to right */
  857.         else if (op<0) for (here=chpt+ix; here<SrcEnd; here++)
  858.           Core[here] = Core[here-op];   /* shift it left to close gap */
  859.         if (ix>0) Poke2(chpt++,Lino);        /* insert the new line # */
  860.         while (ix>2) {                       /* insert the new line.. */
  861.           Core[++chpt] = Core[BP++];
  862.           ix--;}
  863.         Poke2(EndProg,SrcEnd);
  864.         ILPC = 0;
  865.         Lino = 0;
  866.           if (Debugging>0) ListIt(0,0);
  867.         break;
  868.  
  869. /* MT      2B      Mark the BASIC program space Empty.                */
  870. /*                 Also clears the BASIC region of the control stack  */
  871. /* and restart the IL program in the command mode. The memory bounds  */
  872. /* and stack pointers are reset by this instruction to signify empty  */
  873. /* program space, and the line number of the first line is set to 0,  */
  874. /* which is the indication of the end of the program.                 */
  875.       case 43:
  876.         ColdStart();
  877.           if (Debugging>0) {ShowSubs(); ShowExSt(); ShowVars(0);}
  878.         break;
  879.  
  880. /* XQ      2C      Execute.                                           */
  881. /*                 Turns on RUN mode. This instruction also saves     */
  882. /* the current value of the IL program counter for use of the NX      */
  883. /* instruction, and sets the BASIC pointer to the beginning of the    */
  884. /* BASIC program space. An error stop occurs if there is no BASIC     */
  885. /* program. This instruction must be executed at least once before    */
  886. /* the first execution of a NX instruction.                           */
  887.       case 44:
  888.         XQhere = ILPC;
  889.         BP = Peek2(UserProg);
  890.         Lino = Peek2(BP++);
  891.         BP++;
  892.         if (Lino == 0) TBerror();
  893.         else if (Debugging>0)
  894.           {OutStr(" [#"); OutInt(Lino); OutStr("]");}
  895.         break;
  896.  
  897. /* WS      2D      Stop.                                              */
  898. /*                 Stop execution and restart the IL program in the   */
  899. /* command mode. The entire control stack (including BASIC region)    */
  900. /* is also vacated by this instruction. This instruction effectively  */
  901. /* jumps to the Warm Start entry of the ML interpreter.               */
  902.       case 45:
  903.         WarmStart();
  904.           if (Debugging>0) ShowSubs();
  905.         break;
  906.  
  907. /* US      2E      Machine Language Subroutine Call.                  */
  908. /*                 The top six bytes of the expression stack contain  */
  909. /* 3 numbers with the following interpretations: The top number is    */
  910. /* loaded into the A (or A and B) register; the next number is loaded */
  911. /* into 16 bits of Index register; the third number is interpreted as */
  912. /* the address of a machine language subroutine to be called. These   */
  913. /* six bytes on the expression stack are replaced with the 16-bit     */
  914. /* result returned by the subroutine. Stack underflow results in an   */
  915. /* error stop.                                                        */
  916.       case 46:
  917.         Poke2(LinoCore,Lino);    /* bring these memory locations up.. */
  918.         Poke2(ILPCcore,ILPC);      /* ..to date, in case user looks.. */
  919.         Poke2(BPcore,BP);
  920.         Poke2(SvPtCore,SvPt);
  921.         ix = PopExInt()&0xFFFF;                            /* datum A */
  922.         here = PopExInt()&0xFFFF;                          /* datum X */
  923.         op = PopExInt()&0xFFFF;            /* nominal machine address */
  924.         if (ILPC == 0) break;
  925.         if (op>=Peek2(ILfront) && op<ILend) { /* call IL subroutine.. */
  926.           PushExInt(here);
  927.           PushExInt(ix);
  928.           PushSub(ILPC);                      /* push return location */
  929.           ILPC = op;
  930.           if (DEBUGON>0) LogIt(-ILPC);
  931.           break;}
  932.         switch (op) {
  933.         case WachPoint:    /* we only do a few predefined functions.. */
  934.           Watcher = here;
  935.           if (ix>32767) ix = -(int)Core[here]-256;
  936.           Watchee = ix;
  937.           if (Debugging>0) {
  938.             OutLn(); OutStr("[** Watch "); OutHex(here,4); OutStr("]");}
  939.           PushExInt((int)Core[here]);
  940.           break;
  941.         case ColdGo:
  942.           ColdStart();
  943.           break;
  944.         case WarmGo:
  945.           WarmStart();
  946.           break;
  947.         case InchSub:
  948.           PushExInt((int)Inch());
  949.           break;
  950.         case OutchSub:
  951.           Ouch((char)(ix&127));
  952.           PushExInt(0);
  953.           break;
  954.         case BreakSub:
  955.           PushExInt(StopIt());
  956.           break;
  957.         case PeekSub:
  958.           PushExInt((int)Core[here]);
  959.           break;
  960.         case Peek2Sub:
  961.           PushExInt(Peek2(here));
  962.           break;
  963.         case PokeSub:
  964.           ix = ix&0xFF;
  965.           Core[here] = (aByte)ix;
  966.           PushExInt(ix);
  967.           if (DEBUGON>0) LogIt(((ix+256)<<16)+here);
  968.           Lino = Peek2(LinoCore);         /* restore these pointers.. */
  969.           ILPC = Peek2(ILPCcore);    /* ..in case user changed them.. */
  970.           BP = Peek2(BPcore);
  971.           SvPt = Peek2(SvPtCore);
  972.           break;
  973.         case DumpSub:
  974.           ShoMemDump(here,ix);
  975.           PushExInt(here+ix);
  976.           break;
  977.         case TrLogSub:
  978.           ShowLog();
  979.           PushExInt(LogHere);
  980.           break;
  981.         default: TBerror();}
  982.         break;
  983.  
  984. /* RT      2F      IL Subroutine Return.                              */
  985. /*                 The IL control stack is popped to give the address */
  986. /* of the next IL instruction. An error stop occurs if the entire     */
  987. /* control stack (IL and BASIC) is empty.                             */
  988.       case 47:
  989.         ix = PopSub();                         /* get return from pop */
  990.         if (ix<Peek2(ILfront) || ix>=ILend) TBerror();
  991.         else if (ILPC != 0) {
  992.           ILPC = ix;
  993.           if (DEBUGON>0) LogIt(-ILPC);}
  994.         break;
  995.  
  996. /* JS a    3000-37FF       IL Subroutine Call.                        */
  997. /*                         The least significant eleven bits of this  */
  998. /* 2-byte instruction are added to the base address of the IL program */
  999. /* to become address of the next instruction. The previous contents   */
  1000. /* of the IL program counter are pushed onto the IL region of the     */
  1001. /* control stack. Stack overflow results in an error stop.            */
  1002.       case 48: case 49: case 50: case 51: case 52: case 53: case 54: case 55:
  1003.         PushSub(ILPC+1);                /* push return location there */
  1004.         if (ILPC == 0) break;
  1005.         ILPC = (Peek2(ILPC-1)&0x7FF)+Peek2(ILfront);
  1006.         if (DEBUGON>0) LogIt(-ILPC);
  1007.         break;
  1008.  
  1009. /* J a     3800-3FFF       Jump.                                      */
  1010. /*                         The low eleven bits of this 2-byte         */
  1011. /* instruction are added to the IL program base address to determine  */
  1012. /* the address of the next IL instruction. The previous contents of   */
  1013. /* the IL program counter is lost. */
  1014.       case 56: case 57: case 58: case 59: case 60: case 61: case 62: case 63:
  1015.         ILPC = (Peek2(ILPC-1)&0x7FF)+Peek2(ILfront);
  1016.         if (DEBUGON>0) LogIt(-ILPC);
  1017.         break;
  1018.  
  1019. /* NO      08      No Operation.                                      */
  1020. /*                 This may be used as a space filler (such as to     */
  1021. /* ignore a skip).                                                    */
  1022.       default: break;} /* last of inner switch cases */
  1023.       break; /* end of outer switch cases 0,1 */
  1024.  
  1025. /* BR a    40-7F   Relative Branch.                                   */
  1026. /*                 The low six bits of this instruction opcode are    */
  1027. /* added algebraically to the current value of the IL program counter */
  1028. /* to give the address of the next IL instruction. Bit 5 of opcode is */
  1029. /* the sign, with + signified by 1, - by 0. The range of this branch  */
  1030. /* is +/-31 bytes from address of the byte following the opcode. An   */
  1031. /* offset of zero (i.e. opcode 60) results in an error stop. The      */
  1032. /* branch operation is unconditional.                                 */
  1033.       case 2: case 3:
  1034.         ILPC = ILPC+op-96;
  1035.         if (DEBUGON>0) LogIt(-ILPC);
  1036.         break;
  1037.  
  1038. /* BC a "xxx"   80xxxxXx-9FxxxxXx  String Match Branch.               */
  1039. /*                                 The ASCII character string in IL   */
  1040. /* following this opcode is compared to the string beginning with the */
  1041. /* current position of the BASIC pointer, ignoring blanks in BASIC    */
  1042. /* program. The comparison continues until either a mismatch, or an   */
  1043. /* IL byte is reached with the most significant bit set to one. This  */
  1044. /* is the last byte of the string in the IL, compared as a 7-bit      */
  1045. /* character; if equal, the BASIC pointer is positioned after the     */
  1046. /* last matching character in the BASIC program and the IL continues  */
  1047. /* with the next instruction in sequence. Otherwise the BASIC pointer */
  1048. /* is not altered and the low five bits of the Branch opcode are      */
  1049. /* added to the IL program counter to form the address of the next    */
  1050. /* IL instruction. If the strings do not match and the branch offset  */
  1051. /* is zero an error stop occurs.                                      */
  1052.       case 4:
  1053.         if (op==128) here = 0;                /* to error if no match */
  1054.           else here = ILPC+op-128;
  1055.         chpt = BP;
  1056.         ix = 0;
  1057.         while ((ix&128)==0) {
  1058.           while (((char)Core[BP]) == ' ') BP++;   /* skip over spaces */
  1059.           ix = (int)Core[ILPC++];
  1060.           if (((char)(ix&127)) != DeCaps[((int)Core[BP++])&127]) {
  1061.             BP = chpt;         /* back up to front of string in Basic */
  1062.             if (here==0) TBerror();
  1063.               else ILPC = here;                 /* jump forward in IL */
  1064.             break;}}
  1065.         if (DEBUGON>0) if (ILPC>0) LogIt(-ILPC);
  1066.         break;
  1067.  
  1068. /* BV a    A0-BF   Branch if Not Variable.                            */
  1069. /*                 If the next non-blank character pointed to by the  */
  1070. /* BASIC pointer is a capital letter, its ASCII code is [doubled and] */
  1071. /* pushed onto the expression stack and the IL program advances to    */
  1072. /* next instruction in sequence, leaving the BASIC pointer positioned */
  1073. /* after the letter; if not a letter the branch is taken and BASIC    */
  1074. /* pointer is left pointing to that character. An error stop occurs   */
  1075. /* if the next character is not a letter and the offset of the branch */
  1076. /* is zero, or on stack overflow.                                     */
  1077.       case 5:
  1078.         while (((char)Core[BP]) == ' ') BP++;     /* skip over spaces */
  1079.         ch = (char)Core[BP];
  1080.         if (ch >= 'A' && ch <= 'Z' || ch >= 'a' && ch <= 'z')
  1081.           PushExBy((((int)Core[BP++])&0x5F)*2);
  1082.         else if (op==160) TBerror();           /* error if not letter */
  1083.           else ILPC = ILPC+op-160;
  1084.         if (DEBUGON>0) if (ILPC>0) LogIt(-ILPC);
  1085.         break;
  1086.  
  1087. /* BN a    C0-DF   Branch if Not a Number.                            */
  1088. /*                 If the next non-blank character pointed to by the  */
  1089. /* BASIC pointer is not a decimal digit, the low five bits of the     */
  1090. /* opcode are added to the IL program counter, or if zero an error    */
  1091. /* stop occurs. If the next character is a digit, then it and all     */
  1092. /* decimal digits following it (ignoring blanks) are converted to a   */
  1093. /* 16-bit binary number which is pushed onto the expression stack. In */
  1094. /* either case the BASIC pointer is positioned at the next character  */
  1095. /* which is neither blank nor digit. Stack overflow will result in an */
  1096. /* error stop.                                                        */
  1097.       case 6:
  1098.         while (((char)Core[BP]) == ' ') BP++;     /* skip over spaces */
  1099.         ch = (char)Core[BP];
  1100.         if (ch >= '0' && ch <= '9') {
  1101.           op = 0;
  1102.           while (true) {
  1103.             here = (int)Core[BP++];
  1104.             if (here==32) continue;               /* skip over spaces */
  1105.             if (here<48 || here>57) break;     /* not a decimal digit */
  1106.             op = op*10+here-48;}                 /* insert into value */
  1107.           BP--;                             /* back up over non-digit */
  1108.           PushExInt(op);}
  1109.         else if (op==192) TBerror();             /* error if no digit */
  1110.           else ILPC = ILPC+op-192;
  1111.         if (DEBUGON>0) if (ILPC>0) LogIt(-ILPC);
  1112.         break;
  1113.  
  1114. /* BE a    E0-FF   Branch if Not Endline.                             */
  1115. /*                 If the next non-blank character pointed to by the  */
  1116. /* BASIC pointer is a carriage return, the IL program advances to the */
  1117. /* next instruction in sequence; otherwise the low five bits of the   */
  1118. /* opcode (if not 0) are added to the IL program counter to form the  */
  1119. /* address of next IL instruction. In either case the BASIC pointer   */
  1120. /* is left pointing to the first non-blank character; this            */
  1121. /* instruction will not pass over the carriage return, which must     */
  1122. /* remain for testing by the NX instruction. As with the other        */
  1123. /* conditional branches, the branch may only advance the IL program   */
  1124. /* counter from 1 to 31 bytes; an offset of zero results in an error  */
  1125. /* stop.                                                              */
  1126.       case 7:
  1127.         while (((char)Core[BP]) == ' ') BP++;     /* skip over spaces */
  1128.         if (((char)Core[BP]) == '\r') ;
  1129.         else if (op==224) TBerror();            /* error if no offset */
  1130.           else ILPC = ILPC+op-224;
  1131.         if (DEBUGON>0) if (ILPC>0) LogIt(-ILPC);
  1132.         break;}}} /* ~Interp */
  1133.  
  1134. /***************** Intermediate Interpreter Assembled *****************/
  1135.  
  1136. char* DefaultIL() {
  1137.   static char s[9000];    /* be sure to increase size if you add text */
  1138.   strcpy(s,"0000 ;       1 .  ORIGINAL TINY BASIC INTERMEDIATE INTERPRETER\n");
  1139.   strcat(s,"0000 ;       2 .\n");
  1140.   strcat(s,"0000 ;       3 .  EXECUTIVE INITIALIZATION\n");
  1141.   strcat(s,"0000 ;       4 .\n");
  1142.   strcat(s,"0000 ;       5 :STRT PC \":Q^\"        COLON, X-ON\n");
  1143.   strcat(s,"0000 243A91;\n");
  1144.   strcat(s,"0003 ;       6       GL\n");
  1145.   strcat(s,"0003 27;     7       SB\n");
  1146.   strcat(s,"0004 10;     8       BE L0           BRANCH IF NOT EMPTY\n");
  1147.   strcat(s,"0005 E1;     9       BR STRT         TRY AGAIN IF NULL LINE\n");
  1148.   strcat(s,"0006 59;    10 :L0   BN STMT         TEST FOR LINE NUMBER\n");
  1149.   strcat(s,"0007 C5;    11       IL              IF SO, INSERT INTO PROGRAM\n");
  1150.   strcat(s,"0008 2A;    12       BR STRT         GO GET NEXT\n");
  1151.   strcat(s,"0009 56;    13 :XEC  SB              SAVE POINTERS FOR RUN WITH\n");
  1152.   strcat(s,"000A 10;    14       RB                CONCATENATED INPUT\n");
  1153.   strcat(s,"000B 11;    15       XQ\n");
  1154.   strcat(s,"000C 2C;    16 .\n");
  1155.   strcat(s,"000D ;      17 .  STATEMENT EXECUTOR\n");
  1156.   strcat(s,"000D ;      18 .\n");
  1157.   strcat(s,"000D ;      19 :STMT BC GOTO \"LET\"\n");
  1158.   strcat(s,"000D 8B4C45D4;\n");
  1159.   strcat(s,"0011 ;      20       BV *            MUST BE A VARIABLE NAME\n");
  1160.   strcat(s,"0011 A0;    21       BC * \"=\"\n");
  1161.   strcat(s,"0012 80BD;  22 :LET  JS EXPR         GO GET EXPRESSION\n");
  1162.   strcat(s,"0014 30BC;  23       BE *            IF STATEMENT END,\n");
  1163.   strcat(s,"0016 E0;    24       SV                STORE RESULT\n");
  1164.   strcat(s,"0017 13;    25       NX\n");
  1165.   strcat(s,"0018 1D;    26 .\n");
  1166.   strcat(s,"0019 ;      27 :GOTO BC PRNT \"GO\"\n");
  1167.   strcat(s,"0019 9447CF;\n");
  1168.   strcat(s,"001C ;      28       BC GOSB \"TO\"\n");
  1169.   strcat(s,"001C 8854CF;\n");
  1170.   strcat(s,"001F ;      29       JS EXPR         GET LINE NUMBER\n");
  1171.   strcat(s,"001F 30BC;  30       BE *\n");
  1172.   strcat(s,"0021 E0;    31       SB              (DO THIS FOR STARTING)\n");
  1173.   strcat(s,"0022 10;    32       RB\n");
  1174.   strcat(s,"0023 11;    33       GO              GO THERE\n");
  1175.   strcat(s,"0024 16;    34 .\n");
  1176.   strcat(s,"0025 ;      35 :GOSB BC * \"SUB\"      NO OTHER WORD BEGINS \"GO...\"\n");
  1177.   strcat(s,"0025 805355C2;\n");
  1178.   strcat(s,"0029 ;      36       JS EXPR\n");
  1179.   strcat(s,"0029 30BC;  37       BE *\n");
  1180.   strcat(s,"002B E0;    38       GS\n");
  1181.   strcat(s,"002C 14;    39       GO\n");
  1182.   strcat(s,"002D 16;    40 .\n");
  1183.   strcat(s,"002E ;      41 :PRNT BC SKIP \"PR\"\n");
  1184.   strcat(s,"002E 9050D2;\n");
  1185.   strcat(s,"0031 ;      42       BC P0 \"INT\"     OPTIONALLY OMIT \"INT\"\n");
  1186.   strcat(s,"0031 83494ED4;\n");
  1187.   strcat(s,"0035 ;      43 :P0   BE P3\n");
  1188.   strcat(s,"0035 E5;    44       BR P6           IF DONE, GO TO END\n");
  1189.   strcat(s,"0036 71;    45 :P1   BC P4 \";\"\n");
  1190.   strcat(s,"0037 88BB;  46 :P2   BE P3\n");
  1191.   strcat(s,"0039 E1;    47       NX              NO CRLF IF ENDED BY ; OR ,\n");
  1192.   strcat(s,"003A 1D;    48 :P3   BC P7 '\"'\n");
  1193.   strcat(s,"003B 8FA2;  49       PQ              QUOTE MARKS STRING\n");
  1194.   strcat(s,"003D 21;    50       BR P1           GO CHECK DELIMITER\n");
  1195.   strcat(s,"003E 58;    51 :SKIP BR IF           (ON THE WAY THRU)\n");
  1196.   strcat(s,"003F 6F;    52 :P4   BC P5 \",\"\n");
  1197.   strcat(s,"0040 83AC;  53       PT              COMMA SPACING\n");
  1198.   strcat(s,"0042 22;    54       BR P2\n");
  1199.   strcat(s,"0043 55;    55 :P5   BC P6 \":\"\n");
  1200.   strcat(s,"0044 83BA;  56       PC \"S^\"         OUTPUT X-OFF\n");
  1201.   strcat(s,"0046 2493;  57 :P6   BE *\n");
  1202.   strcat(s,"0048 E0;    58       NL              THEN CRLF\n");
  1203.   strcat(s,"0049 23;    59       NX\n");
  1204.   strcat(s,"004A 1D;    60 :P7   JS EXPR         TRY FOR AN EXPRESSION\n");
  1205.   strcat(s,"004B 30BC;  61       PN\n");
  1206.   strcat(s,"004D 20;    62       BR P1\n");
  1207.   strcat(s,"004E 48;    63 .\n");
  1208.   strcat(s,"004F ;      64 :IF   BC INPT \"IF\"\n");
  1209.   strcat(s,"004F 9149C6;\n");
  1210.   strcat(s,"0052 ;      65       JS EXPR\n");
  1211.   strcat(s,"0052 30BC;  66       JS RELO\n");
  1212.   strcat(s,"0054 3134;  67       JS EXPR\n");
  1213.   strcat(s,"0056 30BC;  68       BC I1 \"THEN\"    OPTIONAL NOISEWORD\n");
  1214.   strcat(s,"0058 84544845CE;\n");
  1215.   strcat(s,"005D ;      69 :I1   CP              COMPARE SKIPS NEXT IF TRUE\n");
  1216.   strcat(s,"005D 1C;    70       NX              FALSE.\n");
  1217.   strcat(s,"005E 1D;    71       J STMT          TRUE. GO PROCESS STATEMENT\n");
  1218.   strcat(s,"005F 380D;  72 .\n");
  1219.   strcat(s,"0061 ;      73 :INPT BC RETN \"INPUT\"\n");
  1220.   strcat(s,"0061 9A494E5055D4;\n");
  1221.   strcat(s,"0067 ;      74 :I2   BV *            GET VARIABLE\n");
  1222.   strcat(s,"0067 A0;    75       SB              SWAP POINTERS\n");
  1223.   strcat(s,"0068 10;    76       BE I4\n");
  1224.   strcat(s,"0069 E7;    77 :I3   PC \"? Q^\"       LINE IS EMPTY; TYPE PROMPT\n");
  1225.   strcat(s,"006A 243F2091;\n");
  1226.   strcat(s,"006E ;      78       GL              READ INPUT LINE\n");
  1227.   strcat(s,"006E 27;    79       BE I4           DID ANYTHING COME?\n");
  1228.   strcat(s,"006F E1;    80       BR I3           NO, TRY AGAIN\n");
  1229.   strcat(s,"0070 59;    81 :I4   BC I5 \",\"       OPTIONAL COMMA\n");
  1230.   strcat(s,"0071 81AC;  82 :I5   JS EXPR         READ A NUMBER\n");
  1231.   strcat(s,"0073 30BC;  83       SV              STORE INTO VARIABLE\n");
  1232.   strcat(s,"0075 13;    84       RB              SWAP BACK\n");
  1233.   strcat(s,"0076 11;    85       BC I6 \",\"       ANOTHER?\n");
  1234.   strcat(s,"0077 82AC;  86       BR I2           YES IF COMMA\n");
  1235.   strcat(s,"0079 4D;    87 :I6   BE *            OTHERWISE QUIT\n");
  1236.   strcat(s,"007A E0;    88       NX\n");
  1237.   strcat(s,"007B 1D;    89 .\n");
  1238.   strcat(s,"007C ;      90 :RETN BC END \"RETURN\"\n");
  1239.   strcat(s,"007C 895245545552CE;\n");
  1240.   strcat(s,"0083 ;      91       BE *\n");
  1241.   strcat(s,"0083 E0;    92       RS              RECOVER SAVED LINE\n");
  1242.   strcat(s,"0084 15;    93       NX\n");
  1243.   strcat(s,"0085 1D;    94 .\n");
  1244.   strcat(s,"0086 ;      95 :END  BC LIST \"END\"\n");
  1245.   strcat(s,"0086 85454EC4;\n");
  1246.   strcat(s,"008A ;      96       BE *\n");
  1247.   strcat(s,"008A E0;    97       WS\n");
  1248.   strcat(s,"008B 2D;    98 .\n");
  1249.   strcat(s,"008C ;      99 :LIST BC RUN \"LIST\"\n");
  1250.   strcat(s,"008C 984C4953D4;\n");
  1251.   strcat(s,"0091 ;     100       BE L2\n");
  1252.   strcat(s,"0091 EC;   101 :L1   PC \"@^@^@^@^J^@^\" PUNCH LEADER\n");
  1253.   strcat(s,"0092 24000000000A80;\n");
  1254.   strcat(s,"0099 ;     102       LS              LIST\n");
  1255.   strcat(s,"0099 1F;   103       PC \"S^\"         PUNCH X-OFF\n");
  1256.   strcat(s,"009A 2493; 104       NL\n");
  1257.   strcat(s,"009C 23;   105       NX\n");
  1258.   strcat(s,"009D 1D;   106 :L2   JS EXPR         GET A LINE NUMBER\n");
  1259.   strcat(s,"009E 30BC; 107       BE L3\n");
  1260.   strcat(s,"00A0 E1;   108       BR L1\n");
  1261.   strcat(s,"00A1 50;   109 :L3   BC * \",\"        SEPARATED BY COMMAS\n");
  1262.   strcat(s,"00A2 80AC; 110       BR L2\n");
  1263.   strcat(s,"00A4 59;   111 .\n");
  1264.   strcat(s,"00A5 ;     112 :RUN  BC CLER \"RUN\"\n");
  1265.   strcat(s,"00A5 855255CE;\n");
  1266.   strcat(s,"00A9 ;     113       J XEC\n");
  1267.   strcat(s,"00A9 380A; 114 .\n");
  1268.   strcat(s,"00AB ;     115 :CLER BC REM \"CLEAR\"\n");
  1269.   strcat(s,"00AB 86434C4541D2;\n");
  1270.   strcat(s,"00B1 ;     116       MT\n");
  1271.   strcat(s,"00B1 2B;   117 .\n");
  1272.   strcat(s,"00B2 ;     118 :REM  BC DFLT \"REM\"\n");
  1273.   strcat(s,"00B2 845245CD;\n");
  1274.   strcat(s,"00B6 ;     119       NX\n");
  1275.   strcat(s,"00B6 1D;   120 .\n");
  1276.   strcat(s,"00B7 ;     121 :DFLT BV *            NO KEYWORD...\n");
  1277.   strcat(s,"00B7 A0;   122       BC * \"=\"        TRY FOR LET\n");
  1278.   strcat(s,"00B8 80BD; 123       J LET           IT'S A GOOD BET.\n");
  1279.   strcat(s,"00BA 3814; 124 .\n");
  1280.   strcat(s,"00BC ;     125 .  SUBROUTINES\n");
  1281.   strcat(s,"00BC ;     126 .\n");
  1282.   strcat(s,"00BC ;     127 :EXPR BC E0 \"-\"       TRY FOR UNARY MINUS\n");
  1283.   strcat(s,"00BC 85AD; 128       JS TERM         AHA\n");
  1284.   strcat(s,"00BE 30D3; 129       NE\n");
  1285.   strcat(s,"00C0 17;   130       BR E1\n");
  1286.   strcat(s,"00C1 64;   131 :E0   BC E4 \"+\"       IGNORE UNARY PLUS\n");
  1287.   strcat(s,"00C2 81AB; 132 :E4   JS TERM\n");
  1288.   strcat(s,"00C4 30D3; 133 :E1   BC E2 \"+\"       TERMS SEPARATED BY PLUS\n");
  1289.   strcat(s,"00C6 85AB; 134       JS TERM\n");
  1290.   strcat(s,"00C8 30D3; 135       AD\n");
  1291.   strcat(s,"00CA 18;   136       BR E1\n");
  1292.   strcat(s,"00CB 5A;   137 :E2   BC E3 \"-\"       TERMS SEPARATED BY MINUS\n");
  1293.   strcat(s,"00CC 85AD; 138       JS TERM\n");
  1294.   strcat(s,"00CE 30D3; 139       SU\n");
  1295.   strcat(s,"00D0 19;   140       BR E1\n");
  1296.   strcat(s,"00D1 54;   141 :E3   RT\n");
  1297.   strcat(s,"00D2 2F;   142 .\n");
  1298.   strcat(s,"00D3 ;     143 :TERM JS FACT\n");
  1299.   strcat(s,"00D3 30E2; 144 :T0   BC T1 \"*\"       FACTORS SEPARATED BY TIMES\n");
  1300.   strcat(s,"00D5 85AA; 145       JS FACT\n");
  1301.   strcat(s,"00D7 30E2; 146       MP\n");
  1302.   strcat(s,"00D9 1A;   147       BR T0\n");
  1303.   strcat(s,"00DA 5A;   148 :T1   BC T2 \"/\"       FACTORS SEPARATED BY DIVIDE\n");
  1304.   strcat(s,"00DB 85AF; 149       JS  FACT\n");
  1305.   strcat(s,"00DD 30E2; 150       DV\n");
  1306.   strcat(s,"00DF 1B;   151       BR T0\n");
  1307.   strcat(s,"00E0 54;   152 :T2   RT\n");
  1308.   strcat(s,"00E1 2F;   153 .\n");
  1309.   strcat(s,"00E2 ;     154 :FACT BC F0 \"RND\"     *RND FUNCTION*\n");
  1310.   strcat(s,"00E2 97524EC4;\n");
  1311.   strcat(s,"00E6 ;     155       LN 257*128      STACK POINTER FOR STORE\n");
  1312.   strcat(s,"00E6 0A;\n");
  1313.   strcat(s,"00E7 8080; 156       FV              THEN GET RNDM\n");
  1314.   strcat(s,"00E9 12;   157       LN 2345         R:=R*2345+6789\n");
  1315.   strcat(s,"00EA 0A;\n");
  1316.   strcat(s,"00EB 0929; 158       MP\n");
  1317.   strcat(s,"00ED 1A;   159       LN 6789\n");
  1318.   strcat(s,"00EE 0A;\n");
  1319.   strcat(s,"00EF 1A85; 160       AD\n");
  1320.   strcat(s,"00F1 18;   161       SV\n");
  1321.   strcat(s,"00F2 13;   162       LB 128          GET IT AGAIN\n");
  1322.   strcat(s,"00F3 0980; 163       FV\n");
  1323.   strcat(s,"00F5 12;   164       DS\n");
  1324.   strcat(s,"00F6 0B;   165       JS FUNC         GET ARGUMENT\n");
  1325.   strcat(s,"00F7 3130; 166       BR F1\n");
  1326.   strcat(s,"00F9 61;   167 :F0   BR F2           (SKIPPING)\n");
  1327.   strcat(s,"00FA 73;   168 :F1   DS\n");
  1328.   strcat(s,"00FB 0B;   169       SX 2            PUSH TOP INTO STACK\n");
  1329.   strcat(s,"00FC 02;   170       SX 4\n");
  1330.   strcat(s,"00FD 04;   171       SX 2\n");
  1331.   strcat(s,"00FE 02;   172       SX 3\n");
  1332.   strcat(s,"00FF 03;   173       SX 5\n");
  1333.   strcat(s,"0100 05;   174       SX 3\n");
  1334.   strcat(s,"0101 03;   175       DV              PERFORM MOD FUNCTION\n");
  1335.   strcat(s,"0102 1B;   176       MP\n");
  1336.   strcat(s,"0103 1A;   177       SU\n");
  1337.   strcat(s,"0104 19;   178       DS              PERFORM ABS FUNCTION\n");
  1338.   strcat(s,"0105 0B;   179       LB 6\n");
  1339.   strcat(s,"0106 0906; 180       LN 0\n");
  1340.   strcat(s,"0108 0A;\n");
  1341.   strcat(s,"0109 0000; 181       CP              (SKIP IF + OR 0)\n");
  1342.   strcat(s,"010B 1C;   182       NE\n");
  1343.   strcat(s,"010C 17;   183       RT\n");
  1344.   strcat(s,"010D 2F;   184 :F2   BC F3 \"USR\"     *USR FUNCTION*\n");
  1345.   strcat(s,"010E 8F5553D2;\n");
  1346.   strcat(s,"0112 ;     185       BC * \"(\"        3 ARGUMENTS POSSIBLE\n");
  1347.   strcat(s,"0112 80A8; 186       JS EXPR         ONE REQUIRED\n");
  1348.   strcat(s,"0114 30BC; 187       JS ARG\n");
  1349.   strcat(s,"0116 312A; 188       JS ARG\n");
  1350.   strcat(s,"0118 312A; 189       BC * \")\"\n");
  1351.   strcat(s,"011A 80A9; 190       US              GO DO IT\n");
  1352.   strcat(s,"011C 2E;   191       RT\n");
  1353.   strcat(s,"011D 2F;   192 :F3   BV F4           VARIABLE?\n");
  1354.   strcat(s,"011E A2;   193       FV              YES.  GET IT\n");
  1355.   strcat(s,"011F 12;   194       RT\n");
  1356.   strcat(s,"0120 2F;   195 :F4   BN F5           NUMBER?\n");
  1357.   strcat(s,"0121 C1;   196       RT              GOT IT.\n");
  1358.   strcat(s,"0122 2F;   197 :F5   BC * \"(\"        OTHERWISE MUST BE (EXPR)\n");
  1359.   strcat(s,"0123 80A8; 198 :F6   JS EXPR\n");
  1360.   strcat(s,"0125 30BC; 199       BC * \")\"\n");
  1361.   strcat(s,"0127 80A9; 200       RT\n");
  1362.   strcat(s,"0129 2F;   201 .\n");
  1363.   strcat(s,"012A ;     202 :ARG  BC A0 \",\"        COMMA?\n");
  1364.   strcat(s,"012A 83AC; 203       J  EXPR          YES, GET EXPRESSION\n");
  1365.   strcat(s,"012C 38BC; 204 :A0   DS               NO, DUPLICATE STACK TOP\n");
  1366.   strcat(s,"012E 0B;   205       RT\n");
  1367.   strcat(s,"012F 2F;   206 .\n");
  1368.   strcat(s,"0130 ;     207 :FUNC BC * \"(\"\n");
  1369.   strcat(s,"0130 80A8; 208       BR F6\n");
  1370.   strcat(s,"0132 52;   209       RT\n");
  1371.   strcat(s,"0133 2F;   210 .\n");
  1372.   strcat(s,"0134 ;     211 :RELO BC R0 \"=\"        CONVERT RELATION OPERATORS\n");
  1373.   strcat(s,"0134 84BD; 212       LB 2             TO CODE BYTE ON STACK\n");
  1374.   strcat(s,"0136 0902; 213       RT               =\n");
  1375.   strcat(s,"0138 2F;   214 :R0   BC R4 \"<\"\n");
  1376.   strcat(s,"0139 8EBC; 215       BC R1 \"=\"\n");
  1377.   strcat(s,"013B 84BD; 216       LB 3             <=\n");
  1378.   strcat(s,"013D 0903; 217       RT\n");
  1379.   strcat(s,"013F 2F;   218 :R1   BC R3 \">\"\n");
  1380.   strcat(s,"0140 84BE; 219       LB 5             <>\n");
  1381.   strcat(s,"0142 0905; 220       RT\n");
  1382.   strcat(s,"0144 2F;   221 :R3   LB 1             <\n");
  1383.   strcat(s,"0145 0901; 222       RT\n");
  1384.   strcat(s,"0147 2F;   223 :R4   BC * \">\"\n");
  1385.   strcat(s,"0148 80BE; 224       BC R5 \"=\"\n");
  1386.   strcat(s,"014A 84BD; 225       LB 6             >=\n");
  1387.   strcat(s,"014C 0906; 226       RT\n");
  1388.   strcat(s,"014E 2F;   227 :R5   BC R6 \"<\"\n");
  1389.   strcat(s,"014F 84BC; 228       LB 5             ><\n");
  1390.   strcat(s,"0151 0905; 229       RT\n");
  1391.   strcat(s,"0153 2F;   230 :R6   LB 4             >\n");
  1392.   strcat(s,"0154 0904; 231       RT\n");
  1393.   strcat(s,"0156 2F;   232 .\n");
  1394.   strcat(s,"0157 ;    0000\n");
  1395.   return s;} /* ~DefaultIL */
  1396.  
  1397. /**************************** Startup Code ****************************/
  1398.  
  1399. void StartTinyBasic(char* ILtext) {
  1400.   int nx;
  1401.   for (nx=0; nx<CoreTop; nx++) Core[nx] = 0;          /* clear Core.. */
  1402.   Poke2(ExpnStk,8191);                          /* random number seed */
  1403.   Core[BScode] = 8; /* backspace */
  1404.   Core[CanCode] = 27; /*escape */
  1405.   for (nx=0; nx<32; nx++) DeCaps[nx] = '\0';     /* fill caps table.. */
  1406.   for (nx=32; nx<127; nx++) DeCaps[nx] = (char)nx;
  1407.   for (nx=65; nx<91; nx++) DeCaps[nx+32] = (char)nx;
  1408.   DeCaps[9] = ' ';
  1409.   DeCaps[10] = '\r';
  1410.   DeCaps[13] = '\r';
  1411.   DeCaps[127] = '\0';
  1412.   if (ILtext == NULL) ILtext = DefaultIL();  /* no IL given, use mine */
  1413.   ConvtIL(ILtext);              /* convert IL assembly code to binary */
  1414.   ColdStart();
  1415.   Interp();                                               /* go do it */
  1416.   if (oFile != NULL) IoFileClose(oFile);         /* close output file */
  1417.   if (inFile != NULL) IoFileClose(inFile);        /* close input file */
  1418.   oFile = NULL;
  1419.   inFile = NULL;} /* ~StartTinyBasic */
  1420.  
  1421. int main(int argc, char* argv[]) {
  1422.   int nx;
  1423.   long int len;
  1424.   char* IL = NULL;
  1425.   FileType tmpFile;
  1426.   inFile = NULL;
  1427.   oFile = NULL;
  1428.   for (nx=1; nx<argc; nx++) {         /* look for command-line args.. */
  1429.     if (strcmp(argv[nx],"-b")==0 && ++nx<argc) {     /* alt IL file.. */
  1430.       tmpFile = fopen(argv[nx],"r");
  1431.       if (tmpFile != NULL) if (fseek(tmpFile,0,SEEK_END)==0) {
  1432.         len = ftell(tmpFile);                      /* get file size.. */
  1433.         if (fseek(tmpFile,0,SEEK_SET)==0) if (len>9) {
  1434.           len = len/8+len;            /* allow for line end expansion */
  1435.           IL = (char*)malloc(len+1);
  1436.           if (IL != NULL) len = fread(IL,1,len,tmpFile);
  1437.           IL[len] = '\0';
  1438.           IoFileClose(tmpFile);}}
  1439.       else printf("Could not open file %s", argv[nx]);}
  1440.     else if (strcmp(argv[nx],"-o")==0 && ++nx<argc)    /* output file */
  1441.       oFile = fopen(argv[nx],"w");
  1442.     else if (strcmp(argv[nx],"-i")==0 && ++nx<argc)     /* input file */
  1443.       inFile = fopen(argv[nx],"r");
  1444.     else if (inFile==NULL)  /* default (unadorned) is also input file */
  1445.       inFile = fopen(argv[nx],"r");}             /* ignore other args */
  1446.  
  1447. #ifdef DefaultInputFile
  1448.   if (inFile==NULL) inFile = fopen(DefaultInputFile,"r");
  1449. #endif
  1450. #ifdef DefaultOutputFile
  1451.   if (oFile==NULL) oFile = fopen(DefaultOutputFile,"w");
  1452. #endif
  1453.  
  1454.   StartTinyBasic(IL);                                     /* go do it */
  1455.   return 0;} /* ~main */
  1456.