home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 498a.lha / SC_v6.7 / lex.c < prev    next >
C/C++ Source or Header  |  1991-04-08  |  14KB  |  691 lines

  1. /*    SC    A Spreadsheet Calculator
  2.  *        Lexical analyser
  3.  *
  4.  *        original by James Gosling, September 1982
  5.  *        modifications by Mark Weiser and Bruce Israel,
  6.  *            University of Maryland
  7.  *
  8.  *              More mods Robert Bond, 12/86
  9.  *        More mods by Alan Silverstein, 3/88, see list of changes.
  10.  *        $Revision: 6.8 $
  11.  *
  12.  */
  13.  
  14.  
  15.  
  16. #if defined(BSD42) || defined(BSD43)
  17. #include <sys/ioctl.h>
  18. #endif 
  19.  
  20. #ifdef IEEE_MATH
  21. #include <ieeefp.h>
  22. #endif /* IEEE_MATH */
  23.  
  24. #include <curses.h>
  25. #include <signal.h>
  26. #include <setjmp.h>
  27. #include "sc.h"
  28. #include <ctype.h>
  29.  
  30. #ifdef BSD42
  31. #include <strings.h>
  32. #else
  33. #ifndef SYSIII
  34. #include <string.h>
  35. #endif
  36. #endif
  37.  
  38. #ifdef VMS
  39. #include "gram_tab.h"
  40. typedef union {
  41.     int ival;
  42.     double fval;
  43.     struct ent *ent;
  44.     struct enode *enode;
  45.     char *sval;
  46.     struct range_s rval;
  47. } YYSTYPE;
  48. extern YYSTYPE yylval;
  49. extern int VMS_read_raw;   /*sigh*/
  50. #else    /* VMS */
  51. #include "y.tab.h"
  52. extern YYSTYPE yylval;
  53. #endif /* VMS */
  54.  
  55. char *strtof();
  56.  
  57. jmp_buf wakeup;
  58. jmp_buf fpe_buf;
  59.  
  60. struct key {
  61.     char *key;
  62.     int val;
  63. };
  64.  
  65. struct key experres[] = {
  66. #include "experres.h"
  67.     0, 0};
  68.  
  69. struct key statres[] = {
  70. #include "statres.h"
  71.     0, 0};
  72.  
  73. yylex ()
  74. {
  75.     register char *p = line+linelim;
  76.     int ret;
  77.     while (isspace(*p)) p++;
  78.     if (*p == '\0') ret = -1;
  79.     else if (isalpha(*p)) {
  80.     char *tokenst = p;
  81.     register tokenl;
  82.     register struct key *tblp;
  83.     tokenl = 0;
  84.     /*
  85.      * This picks up either 1 or 2 alpha characters (a column) or
  86.      * tokens with at least three leading alphas and '_' or digits
  87.      * (a function or token or command or a range name)
  88.     */
  89.     while (isalpha(*p) || ((*p == '_') || isdigit(*p)) && (tokenl > 2)) {
  90.         p++;
  91.         tokenl++;
  92.     }
  93.     if (tokenl <= 2) { /* a COL is 1 or 2 char alpha
  94.         (but not pi, ln, fv, pv, if -- this should be fixed!) */
  95.         if (tokenl == 2 && tokenst[0] == 'p' && tokenst[1] == 'i') {
  96.         ret = K_PI;
  97.         } else if (tokenl == 2 && tokenst[0] == 'l' && tokenst[1] == 'n') {
  98.         ret = K_LN;
  99.         } else if (tokenl == 2 && tokenst[0] == 'f' && tokenst[1] == 'v') {
  100.         ret = K_FV;
  101.         } else if (tokenl == 2 && tokenst[0] == 'p' && tokenst[1] == 'v') {
  102.         ret = K_PV;
  103.         } else if (tokenl == 2 && tokenst[0] == 'i' && tokenst[1] == 'f') {
  104.         ret = K_IF;
  105.  
  106.         } else {
  107.         ret = COL;
  108.         yylval.ival = atocol (tokenst, tokenl);
  109.         }
  110.     } else {
  111.         ret = WORD;
  112.         for (tblp = linelim ? experres : statres; tblp->key; tblp++)
  113.             if (((tblp->key[0]^tokenst[0])&0137)==0
  114.              && tblp->key[tokenl]==0) {
  115.             register i = 1;
  116.             while (i<tokenl && ((tokenst[i]^tblp->key[i])&0137)==0)
  117.                 i++;
  118.             if (i>=tokenl) {
  119.                 ret = tblp->val;
  120.                 break;
  121.             }
  122.             }
  123.         if (ret==WORD) { 
  124.         struct range *r;
  125.         if (r = find_range(tokenst, tokenl,
  126.                    (struct ent *)0, (struct ent *)0)) {
  127.             yylval.rval.left = r->r_left;
  128.             yylval.rval.right = r->r_right;
  129.             if (r->r_is_range)
  130.                 ret = RANGE;
  131.             else
  132.             ret = VAR;
  133.         } else {
  134.             linelim = p-line;
  135.             yyerror ("Unintelligible word");
  136.         }
  137.         }
  138.     }
  139.     } else if ((*p == '.') || isdigit(*p)) {
  140.     double v = 0;
  141.     int temp;
  142.     char *nstart = p;
  143.     if (*p != '.') {
  144.         do v = v*10 + (double)(*p-'0');
  145.         while (isdigit(*++p));
  146.     }
  147.     if (*p=='.' || *p == 'e' || *p == 'E') {
  148.         ret = FNUMBER;
  149.         p = strtof(nstart, &yylval.fval);
  150.     } else {
  151.         /* A NUMBER must hold at least MAXROW and MAXCOL */
  152.         /* This is consistent with a short row and col in struct ent */
  153.         if (v > (double)32767 || v < (double)-32768) {
  154.         ret = FNUMBER;
  155.         yylval.fval = v;
  156.         } else {
  157.         temp = (int)v;
  158.         if((double)temp != v) {
  159.             ret = FNUMBER;
  160.             yylval.fval = v;
  161.         } else {
  162.             ret = NUMBER;
  163.             yylval.ival = temp;
  164.         }
  165.         }
  166.     }
  167.     } else if (*p=='"') {
  168.     char *ptr;
  169.         ptr = p+1;
  170.         while(*ptr && *ptr++ != '"');
  171.         ptr = xmalloc((unsigned)(ptr-p));
  172.     yylval.sval = ptr;
  173.     p += 1;
  174.     while (*p && *p!='"') *ptr++ = *p++;
  175.     *ptr = 0;
  176.     if (*p) p += 1;
  177.     ret = STRING;
  178.     } else if (*p=='[') {
  179.     while (*p && *p!=']') p++;
  180.     if (*p) p++;
  181.     linelim = p-line;
  182.     return yylex();
  183.     } else ret = *p++;
  184.     linelim = p-line;
  185.     return ret;
  186. }
  187.  
  188.  
  189. /*
  190.  * Given a token string starting with a symbolic column name and its valid
  191.  * length, convert column name ("A"-"Z" or "AA"-"ZZ") to a column number (0-N).
  192.  * Never mind if the column number is illegal (too high).  The procedure's name
  193.  * and function are the inverse of coltoa().
  194.  * 
  195.  * Case-insensitivity is done crudely, by ignoring the 040 bit.
  196.  */
  197.  
  198. int
  199. atocol (string, len)
  200.     char    *string;
  201.     int    len;
  202. {
  203.     register int col;
  204.  
  205.     col = (string [0] & 0137) - 'A';
  206.  
  207.     if (len == 2)        /* has second char */
  208.         col = ((col + 1) * 26) + ((string [1] & 0137) - 'A');
  209.  
  210.     return (col);
  211. }
  212.  
  213.  
  214. #ifdef SIMPLE
  215.  
  216. initkbd()
  217. {}
  218.  
  219. kbd_again()
  220. {}
  221.  
  222. resetkbd()
  223. {}
  224.  
  225. #ifndef VMS
  226.  
  227. nmgetch()
  228. {
  229.     return (toascii(getchar()));
  230. }
  231.  
  232. #else /* VMS */
  233.  
  234. nmgetch()
  235. /*
  236.    This is not perfect, it doesn't move the cursor when goraw changes
  237.    over to deraw, but it works well enough since the whole sc package
  238.    is incredibly stable (loop constantly positions cursor).
  239.  
  240.    Question, why didn't the VMS people just implement cbreak?
  241.  
  242.    NOTE: During testing it was discovered that the DEBUGGER and curses
  243.    and this method of reading would collide (the screen was not updated
  244.    when continuing from screen mode in the debugger).
  245. */
  246. {
  247.     short c;
  248.     static int key_id=0;
  249.     int status;
  250. #define VMScheck(a) {if (~(status = (a)) & 1) VMS_MSG (status);}
  251.  
  252.     if (VMS_read_raw) {
  253.       VMScheck(smg$read_keystroke (&stdkb->_id, &c, 0, 0, 0));
  254.     }
  255.     else
  256.        c = getchar();
  257.  
  258.     switch (c) {
  259.     case SMG$K_TRM_LEFT:  c = ctl('b'); break;
  260.     case SMG$K_TRM_RIGHT: c = ctl('f'); break;
  261.     case SMG$K_TRM_UP:    c = ctl('p'); break;
  262.     case SMG$K_TRM_DOWN:  c = ctl('n'); break;
  263.     default:   c = c & 0x7f;
  264.     }
  265.     return (c);
  266. }
  267.  
  268.  
  269. VMS_MSG (status)
  270. int status;
  271. /*
  272.    Routine to put out the VMS operating system error (if one occurs).
  273. */
  274. {
  275. #include <descrip.h>
  276.    char errstr[81], buf[120];
  277.    $DESCRIPTOR(errdesc, errstr);
  278.    short int length;
  279. #define err_out(msg) fprintf (stderr,msg)
  280.  
  281. /* Check for no error or standard error */
  282.  
  283.    if (~status & 1) {
  284.       status = status & 0x8000 ? status & 0xFFFFFFF : status & 0xFFFF;
  285.       if (SYS$GETMSG(status, &length, &errdesc, 1, 0) == SS$_NORMAL) {
  286.          errstr[length] = '\0';
  287.          sprintf (buf, "<0x%x> %s", status, errdesc.dsc$a_pointer);
  288.          err_out (buf);
  289.       }
  290.       else
  291.          err_out ("System error");
  292.    }
  293. }
  294. #endif /* VMS */
  295.  
  296. #else /*SIMPLE*/
  297.  
  298. #if defined(BSD42) || defined (SYSIII) || defined(BSD43)
  299.  
  300. #define N_KEY 4
  301.  
  302. struct key_map {
  303.     char *k_str;
  304.     char k_val;
  305.     char k_index;
  306. }; 
  307.  
  308. struct key_map km[N_KEY];
  309.  
  310. char keyarea[N_KEY*30];
  311.  
  312. char *tgetstr();
  313. char *getenv();
  314. char *ks;
  315. char ks_buf[20];
  316. char *ke;
  317. char ke_buf[20];
  318.  
  319. #ifdef TIOCSLTC
  320. struct ltchars old_chars, new_chars;
  321. #endif
  322.  
  323. char dont_use[] = {
  324.     ctl('['), ctl('a'), ctl('b'), ctl('c'), ctl('e'), ctl('f'), ctl('g'), ctl('h'),
  325.     ctl('i'), ctl('j'),  ctl('l'), ctl('m'), ctl('n'), ctl('p'), ctl('q'),
  326.     ctl('r'), ctl('s'), ctl('t'), ctl('u'), ctl('v'),  ctl('w'), ctl('x'),
  327.     ctl('z'), 0
  328. };
  329.  
  330. charout(c)
  331. int c;
  332. {
  333.     (void)putchar(c);
  334. }
  335.  
  336. initkbd()
  337. {
  338.     register struct key_map *kp;
  339.     register i,j;
  340.     char *p = keyarea;
  341.     char *ktmp;
  342.     static char buf[1024]; /* Why do I have to do this again? */
  343.  
  344.     if (tgetent(buf, getenv("TERM")) <= 0)
  345.     return;
  346.  
  347.     km[0].k_str = tgetstr("kl", &p); km[0].k_val = ctl('b');
  348.     km[1].k_str = tgetstr("kr", &p); km[1].k_val = ctl('f');
  349.     km[2].k_str = tgetstr("ku", &p); km[2].k_val = ctl('p');
  350.     km[3].k_str = tgetstr("kd", &p); km[3].k_val = ctl('n');
  351.     ktmp = tgetstr("ks",&p);
  352.     if (ktmp)  {
  353.     (void) strcpy(ks_buf, ktmp);
  354.     ks = ks_buf;
  355.     tputs(ks, 1, charout);
  356.     }
  357.     ktmp = tgetstr("ke",&p);
  358.     if (ktmp)  {
  359.     (void) strcpy(ke_buf, ktmp);
  360.     ke = ke_buf;
  361.     }
  362.  
  363.     /* Unmap arrow keys which conflict with our ctl keys   */
  364.     /* Ignore unset, longer than length 1, and 1-1 mapped keys */
  365.  
  366.     for (i = 0; i < N_KEY; i++) {
  367.     kp = &km[i];
  368.     if (kp->k_str && (kp->k_str[1] == 0) && (kp->k_str[0] != kp->k_val))
  369.         for (j = 0; dont_use[j] != 0; j++)
  370.             if (kp->k_str[0] == dont_use[j]) {
  371.              kp->k_str = (char *)0;
  372.              break;
  373.         }
  374.     }
  375.  
  376.  
  377. #ifdef TIOCSLTC
  378.     (void)ioctl(fileno(stdin), TIOCGLTC, (char *)&old_chars);
  379.     new_chars = old_chars;
  380.     if (old_chars.t_lnextc == ctl('v'))
  381.     new_chars.t_lnextc = -1;
  382.     if (old_chars.t_rprntc == ctl('r'))
  383.     new_chars.t_rprntc = -1;
  384.     (void)ioctl(fileno(stdin), TIOCSLTC, (char *)&new_chars);
  385. #endif
  386. }
  387.  
  388. void
  389. kbd_again()
  390. {
  391.     if (ks) 
  392.     tputs(ks, 1, charout);
  393.  
  394. #ifdef TIOCSLTC
  395.     (void)ioctl(fileno(stdin), TIOCSLTC, (char *)&new_chars);
  396. #endif
  397. }
  398.  
  399. void
  400. resetkbd()
  401. {
  402.     if (ke) 
  403.     tputs(ke, 1, charout);
  404.  
  405. #ifdef TIOCSLTC
  406.     (void)ioctl(fileno(stdin), TIOCSLTC, (char *)&old_chars);
  407. #endif
  408. }
  409.  
  410. nmgetch() 
  411. {
  412.     register int c;
  413.     register struct key_map *kp;
  414.     register struct key_map *biggest;
  415.     register int i;
  416.     int almost;
  417.     int maybe;
  418.  
  419.     static char dumpbuf[10];
  420.     static char *dumpindex;
  421.  
  422. #ifdef SIGVOID
  423.     void time_out();
  424. #else
  425.     int time_out();
  426. #endif
  427.  
  428.     if (dumpindex && *dumpindex)
  429.         return (*dumpindex++);
  430.  
  431.     c = toascii(getchar());
  432.     biggest = 0;
  433.     almost = 0;
  434.  
  435.     for (kp = &km[0]; kp < &km[N_KEY]; kp++) {
  436.     if (!kp->k_str)
  437.         continue;
  438.     if (c == kp->k_str[kp->k_index]) {
  439.         almost = 1;
  440.         kp->k_index++;
  441.         if (kp->k_str[kp->k_index] == 0) {
  442.         c = kp->k_val;
  443.             for (kp = &km[0]; kp < &km[N_KEY]; kp++)
  444.                 kp->k_index = 0;
  445.             return(c);
  446.         }
  447.     }
  448.     if (!biggest && kp->k_index)
  449.         biggest = kp;
  450.         else if (kp->k_index && biggest->k_index < kp->k_index)
  451.         biggest = kp;
  452.     }
  453.  
  454.     if (almost) { 
  455.         (void) signal(SIGALRM, time_out);
  456.         (void) alarm(1);
  457.  
  458.     if (setjmp(wakeup) == 0) { 
  459.         maybe = nmgetch();
  460.         (void) alarm(0);
  461.         return(maybe);
  462.     }
  463.     }
  464.     
  465.     if (biggest) {
  466.     for (i = 0; i<biggest->k_index; i++) 
  467.         dumpbuf[i] = biggest->k_str[i];
  468.     if (!almost)
  469.         dumpbuf[i++] = c;
  470.     dumpbuf[i] = '\0';
  471.     dumpindex = &dumpbuf[1];
  472.     for (kp = &km[0]; kp < &km[N_KEY]; kp++)
  473.         kp->k_index = 0;
  474.     return (dumpbuf[0]);
  475.     }
  476.  
  477.     return(c);
  478. }
  479.  
  480. #endif
  481.  
  482. #if defined(SYSV2) || defined(SYSV3)
  483.  
  484. initkbd()
  485. {
  486.     keypad(stdscr, TRUE);
  487. }
  488.  
  489. void
  490. kbd_again()
  491. {
  492.     keypad(stdscr, TRUE);
  493. }
  494.  
  495. void
  496. resetkbd()
  497. {
  498.     keypad(stdscr, FALSE);
  499. }
  500.  
  501. nmgetch()
  502. {
  503.     register int c;
  504.  
  505.     c = getch();
  506.     switch (c) {
  507.     case KEY_LEFT:  c = ctl('b'); break;
  508.     case KEY_RIGHT: c = ctl('f'); break;
  509.     case KEY_UP:    c = ctl('p'); break;
  510.     case KEY_DOWN:  c = ctl('n'); break;
  511. #ifdef KEY_C1
  512. /* This stuff works for a wyse wy75 in ANSI mode under 5.3.  Good luck. */
  513. /* It is supposed to map the curses keypad back to the numeric equiv. */
  514.     case KEY_C1:    c = '0'; break;
  515.     case KEY_A1:    c = '1'; break;
  516.     case KEY_B2:    c = '2'; break;
  517.     case KEY_A3:    c = '3'; break;
  518.     case KEY_F(5):  c = '4'; break;
  519.     case KEY_F(6):  c = '5'; break;
  520.     case KEY_F(7):  c = '6'; break;
  521.     case KEY_F(9):  c = '7'; break;
  522.     case KEY_F(10): c = '8'; break;
  523.     case KEY_F0:    c = '9'; break;
  524.     case KEY_C3:    c = '.'; break;
  525.     case KEY_ENTER: c = ctl('m'); break;
  526. #endif
  527.     default:   c = toascii(c); 
  528.     break;
  529.     }
  530.     return (c);
  531. }
  532.  
  533. #endif /* SYSV2 || SYSV3 */
  534.  
  535. #endif /* SIMPLE */
  536.  
  537. #ifdef AMIGA
  538. initkbd()
  539. {
  540.     keypad(stdscr, TRUE);
  541. }
  542.  
  543. void
  544. kbd_again()
  545. {
  546.     keypad(stdscr, TRUE);
  547. }
  548.  
  549. void
  550. resetkbd()
  551. {
  552.     keypad(stdscr, FALSE);
  553. }
  554.  
  555. nmgetch()
  556. {
  557.     register int c;
  558.  
  559.     c = getch();
  560.     switch (c) {
  561.     case KEY_LEFT:  c = ctl('b'); break;
  562.     case KEY_RIGHT: c = ctl('f'); break;
  563.     case KEY_UP:    c = ctl('p'); break;
  564.     case KEY_DOWN:  c = ctl('n'); break;
  565.     case KEY_HELP:    c = '?'; break;    /* SJR 07.Nov.90 */
  566.     default:   c = toascii(c); 
  567.     break;
  568.     }
  569.     return (c);
  570. }
  571. #endif /* AMIGA */
  572.  
  573. #ifdef SIGVOID
  574. void
  575. #endif
  576. time_out(signo)
  577. int signo;
  578. {
  579. #ifdef IEEE_MATH
  580.     (void)fpsetsticky((fp_except)0);         /* Clear exception */
  581. #endif /* IEEE_MATH */
  582.     longjmp(wakeup, -1);
  583. }
  584.  
  585. #ifdef SIGVOID
  586. void
  587. #endif
  588. fpe_trap(signo)
  589. int signo;
  590. {
  591.     longjmp(fpe_buf, 1);
  592. }
  593.  
  594. /*
  595.  * This converts a floating point number of the form
  596.  * [s]ddd[.d*][esd*]  where s can be a + or - and e is E or e.
  597.  * to floating point. 
  598.  * p is advanced.
  599.  */
  600.  
  601. char *
  602. strtof(p, res)
  603. register char *p;
  604. double *res;
  605. {
  606.     double acc;
  607.     int sign;
  608.     double fpos;
  609.     int exp;
  610.     int exps;
  611. #ifdef SIGVOID
  612.     void (*sig_save)();
  613. #else
  614.     int (*sig_save)();
  615. #endif
  616.  
  617.     sig_save = signal(SIGFPE, fpe_trap);
  618.     if (setjmp(fpe_buf)) {
  619.     error("Floating point exception\n");
  620.     *res = 0.0; 
  621.         (void) signal(SIGFPE, sig_save);
  622.     return(p);
  623.     }
  624.     acc = 0.0;
  625.     sign = 1;
  626.     exp = 0;
  627.     exps = 1;
  628.     if (*p == '+')
  629.         p++;
  630.     else if (*p == '-') {
  631.         p++;
  632.         sign = -1;
  633.     }
  634.     while (isdigit(*p)) {
  635.         acc = acc * 10.0 + (double)(*p - '0');
  636.         p++;
  637.     }
  638.     if (*p == 'e' || *p == 'E') {
  639.         p++;
  640.         if (*p == '+')
  641.         p++;
  642.         else if (*p == '-') {
  643.         p++;
  644.         exps = -1;
  645.         }
  646.         while(isdigit(*p)) {
  647.         exp = exp * 10 + (*p - '0');
  648.         p++;
  649.         }
  650.     }
  651.     if (*p == '.') {
  652.     fpos = 1.0/10.0;
  653.     p++;
  654.     while(isdigit(*p)) {
  655.         acc += (*p - '0') * fpos;
  656.         fpos *= 1.0/10.0;
  657.         p++;
  658.     }
  659.     }
  660.     if (*p == 'e' || *p == 'E') {
  661.     exp = 0;
  662.     exps = 1;
  663.         p++;
  664.     if (*p == '+')
  665.         p++;
  666.     else if (*p == '-') {
  667.         p++;
  668.         exps = -1;
  669.     }
  670.     while(isdigit(*p)) {
  671.         exp = exp * 10 + (*p - '0');
  672.         p++;
  673.     }
  674.     }
  675.     if (exp) {
  676.     if (exps > 0)
  677.         while (exp--)
  678.         acc *= 10.0;
  679.     else
  680.         while (exp--)
  681.         acc *= 1.0/10.0;
  682.     }
  683.     if (sign > 0)
  684.         *res = acc;
  685.     else
  686.     *res = -acc;
  687.  
  688.     (void) signal(SIGFPE, sig_save);
  689.     return(p);
  690. }
  691.