home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 348_01 / z80aeval.c < prev    next >
Text File  |  1991-05-02  |  11KB  |  466 lines

  1. /*
  2.  
  3. This file contains the assembler's expression evaluator and lexical analyzer.
  4. The lexical analyzer chops the input character stream up into discrete tokens
  5. that are processed by the expression analyzer and the line assembler.  The
  6. expression analyzer processes the token stream into unsigned results of
  7. arithmetic expressions.
  8. */
  9.  
  10. /*  Get global goodies:  */
  11.  
  12. #include "z80a.h"
  13.  
  14. /*  Get access to global mailboxes defined in A48.C:            */
  15.  
  16. extern char line[];
  17. extern int filesp, forwd, pass;
  18. extern unsigned pc;
  19. extern FILE *filestk[], *source;
  20. extern TOKEN token;
  21.  
  22. /*  Expression analysis routine.  The token stream from the lexical    */
  23. /*  analyzer is processed as an arithmetic expression and reduced to an    */
  24. /*  unsigned value.  If an error occurs during the evaluation, the    */
  25. /*  global flag    forwd is set to indicate to the line assembler that it    */
  26. /*  should not base certain decisions on the result of the evaluation.    */
  27.  
  28. static int bad;
  29.  
  30. unsigned expr()
  31. {
  32.     SCRATCH unsigned u;
  33.     unsigned eval();
  34.  
  35.     bad = FALSE;
  36.     u = eval(START);
  37.     return bad ? 0 : u;
  38. }
  39.  
  40. static int need_op = FALSE;
  41.  
  42. static unsigned eval(pre)
  43. unsigned pre;
  44. {
  45.     register unsigned op, u, v;
  46.     TOKEN *lex();
  47.     void exp_error(), unlex(), pops();
  48.     unsigned get_len();
  49.  
  50.     for (;;) {
  51.     u = op = lex() -> valu;
  52.     switch (token.attr & TYPE) {
  53.         default:    if (pre != START) unlex();
  54.         case EOL:    exp_error('E');  return;
  55.  
  56.         case OPR:    if (!(token.attr & UNARY)) { exp_error('E');  break; }
  57.             u = (op == '*' ? pc :
  58.                 eval((op == '+' || op == '-') ?
  59.                 (unsigned) UOP1 : token.attr & PREC));
  60.             switch (op) {
  61.                 case '-':    u = word(-u);  break;
  62.  
  63.                 case NOT:    u ^= 0xffff;  break;
  64.  
  65.                 case HIGH:    u = high(u);  break;
  66.  
  67.                 case LOW:    u = low(u);  break;
  68.                 
  69.                 case LENOF:
  70.                         if (!(u = get_len(token.sval))) exp_error('v'); 
  71.                         break;
  72.             }
  73.  
  74.         case VAL:    
  75.         case STR:    for (;;) {
  76.                 need_op = TRUE; /* need operator here or else comments */
  77.                             /* come back as undefined symbols */
  78.                 op = lex() -> valu;
  79.                 need_op = FALSE;
  80.                 switch (token.attr & TYPE) {
  81.                 default:   if (pre != START) unlex();
  82.                 case EOL:   if (pre == LPREN) exp_error('(');
  83.                         return u;
  84.  
  85.                 case STR:
  86.                 case VAL:   exp_error('E');  break;
  87.  
  88.                 case OPR:   if (!(token.attr & BINARY)) {
  89.                         exp_error('E');  break;
  90.                         }
  91.                         if ((token.attr & PREC) >= pre) {
  92.                         unlex();  return u;
  93.                         }
  94.                         if (op != ')')
  95.                         v = eval(token.attr & PREC);
  96.                         switch (op) {
  97.                         case '+':   u += v;  break;
  98.  
  99.                         case '-':   u -= v;  break;
  100.  
  101.                         case '*':   u *= v;  break;
  102.  
  103.                         case '/':   u /= v;  break;
  104.  
  105.                         case MOD:   u %= v;  break;
  106.  
  107.                         case AND:   u &= v;  break;
  108.  
  109.                         case OR:    u |= v;  break;
  110.  
  111.                         case XOR:   u ^= v;  break;
  112.  
  113.                         case '<':   u = u < v;  break;
  114.  
  115.                         case LE:    u = u <= v;  break;
  116.  
  117.                         case '=':   u = u == v;  break;
  118.  
  119.                         case GE:    u = u >= v;  break;
  120.  
  121.                         case '>':   u = u > v;  break;
  122.  
  123.                         case NE:    u = u != v;  break;
  124.  
  125.                         case SHL:   if (v > 15)
  126.                                 exp_error('E');
  127.                                 else u <<= v;
  128.                                 break;
  129.  
  130.                         case SHR:   if (v > 15)
  131.                                 exp_error('E');
  132.                                 else u >>= v;
  133.                                 break;
  134.  
  135.                         case ')':   if (pre == LPREN)
  136.                                 return u;
  137.                                 exp_error('(');
  138.                                 break;
  139.                         }
  140.                         clamp(u);
  141.                         break;
  142.                 }
  143.             }
  144.             break;
  145.     }
  146.     }
  147. }
  148.  
  149. static void exp_error(c)
  150. char c;
  151. {
  152.     forwd = bad = TRUE;  error_s(c);
  153. }
  154.  
  155. /*  Lexical analyzer.  The source input character stream is chopped up    */
  156. /*  into its component parts and the pieces are evaluated.  Symbols are    */
  157. /*  looked up, operators are looked up, etc.  Everything gets reduced    */
  158. /*  to an attribute word, a numeric value, and (possibly) a string    */
  159. /*  value.                                */
  160.  
  161. static int oldt = FALSE;
  162. static int quote = FALSE;
  163. static int inlex = FALSE;
  164.  
  165. TOKEN *lex()
  166. {
  167.     SCRATCH char c, t, u, *p;
  168.     SCRATCH unsigned b;
  169.     SCRATCH OPCODE *o;
  170.     SCRATCH SYMBOL *s;
  171.     OPCODE *find_operator();
  172.     SYMBOL *find_symbol();
  173.     void exp_error(), make_number(), pops(), pushc(), trash();
  174.  
  175.     if (oldt) { oldt = FALSE;  return &token; }
  176.     inlex = TRUE;
  177.     /* trash(); spaces not allowed in expressions */
  178.     if (isalph(c = popc())) {
  179.     pushc(c);  pops(token.sval);
  180.     if (o = find_operator(token.sval)) {
  181.         token.attr = o -> attr;
  182.         token.valu = o -> valu;
  183.     }
  184.     else {
  185.         if (need_op) {
  186.             token.attr = EOL;
  187.             token.valu = 0;
  188.         } else {
  189.             token.attr = VAL;  token.valu = 0;
  190.             if (s = find_symbol(token.sval)) {
  191.             token.attr = s -> attr;
  192.             token.valu = s -> valu;
  193.             if (s -> attr & PIN_VAL) {
  194.                 if (!(s -> attr & PIN))    token.valu += (s -> len - 1);
  195.             }
  196.             if (pass == 2 && s -> attr & FORWD) forwd = TRUE;
  197.             }
  198.             else exp_error('U');
  199.         }
  200.     }
  201.     }
  202.     else if (isnum(c)) {
  203.     pushc(c);  pops(token.sval);
  204.     for (p = token.sval; *p; ++p);
  205.     switch (toupper(*--p)) {
  206.         case 'B':    b = 2;  break;
  207.  
  208.         case 'O':
  209.         case 'Q':    b = 8;  break;
  210.  
  211.         default:    ++p;
  212.         case 'D':    b = 10;  break;
  213.  
  214.         case 'H':    b = 16;  break;
  215.     }
  216.     *p = '\0';  make_number(b);
  217.     }
  218.     else switch (c) {
  219.     case '%':   b = 2;  goto num;
  220.  
  221. /*    case '@':   b = 8;  goto num;
  222. */
  223.     case '$': 
  224.             b = 16;
  225. num:            pops(token.sval);
  226.             make_number(b);
  227.             break;
  228.  
  229.     case '(':   token.attr = UNARY + LPREN + OPR;  goto opr1;
  230.  
  231.     case ')':   token.attr = BINARY + RPREN + OPR;  goto opr1;
  232.  
  233.     case '+':   token.attr = BINARY + UNARY + ADDIT + OPR;  goto opr1;
  234.  
  235.     case '-':   token.attr = BINARY + UNARY + ADDIT + OPR;  goto opr1;
  236.  
  237.     case '*':   token.attr = BINARY + UNARY + MULT + OPR;  goto opr1;
  238.  
  239.     case '/':   token.attr = BINARY + MULT + OPR;
  240. opr1:            token.valu = c;  break;
  241.  
  242.     case '<':   token.valu = c;
  243.             if ((c = popc()) == '=') token.valu = LE;
  244.             else if (c == '>') token.valu = NE;
  245.             else pushc(c);
  246.             goto opr2;
  247.  
  248.     case '=':   token.valu = c;
  249.             if ((c = popc()) == '<') token.valu = LE;
  250.             else if (c == '>') token.valu = GE;
  251.             else pushc(c);
  252.             goto opr2;
  253.  
  254.     case '>':   token.valu = c;
  255.             if ((c = popc()) == '<') token.valu = NE;
  256.             else if (c == '=') token.valu = GE;
  257.             else pushc(c);
  258. opr2:            token.attr = BINARY + RELAT + OPR;  break;
  259.  
  260.     case '\'':
  261.     case '"':   quote = TRUE;  token.attr = STR;
  262.             for (p = token.sval; (*p = popc()) != c; ++p) {
  263.             if (*p == '\n') { exp_error('"');  break; }
  264.             if (*p == '&') {
  265.                 switch (c = popc()) {
  266.                     case 'N': c = 0x00; break;
  267.                     case 'B': c = 0x08; break;
  268.                     case 'T': c = 0x09; break;
  269.                     case 'V': c = 0x0b; break;
  270.                     case 'I': c = 0x0d; break;
  271.                     case '&': break;
  272.                     case 'F': c = 0xff; break;
  273.                     case '$': /* Hex number */
  274.                         for (c=t=0; t != 2; t++) {
  275.                             if (ishex(u = popc())) {
  276.                                 c=(c*16)+(toupper(u)
  277.                                 -(isnum(u) ? '0' : 'A'-10));
  278.                             } else { pushc(u); break; }
  279.                         }
  280.                         break;
  281.                     default:  c = '?'; error_s('s');
  282.                 }
  283.                 *p = c;
  284.             }
  285.             }
  286.             *p = '\0';  quote = FALSE;
  287.             if ((token.valu = token.sval[0]) && token.sval[1])
  288.             token.valu = (token.valu << 8) + token.sval[1];
  289.             break;
  290.  
  291.     case '.':   token.attr = UNARY + UOP3 + OPR;
  292.             token.valu = LENOF;
  293.             break;
  294.  
  295.     case ';':   token.attr = SEP;  break;
  296.     
  297.     case ',':   token.attr = SIZE; break;
  298.     
  299.     case ' ':
  300.         case '\n':  token.attr = EOL;  break;
  301.     }
  302.     inlex = FALSE;
  303.     return &token;
  304. }
  305.  
  306. TOKEN *lex_op()
  307. {
  308.     TOKEN *lex();
  309.     TOKEN *t;
  310.     
  311.     need_op = TRUE;
  312.     t = lex();
  313.     need_op = FALSE;
  314.     return t;
  315. }
  316.  
  317. static void make_number(base)
  318. unsigned base;
  319. {
  320.     SCRATCH char *p;
  321.     SCRATCH unsigned d;
  322.     void exp_error();
  323.  
  324.     token.attr = VAL;
  325.     token.valu = 0;
  326.     for (p = token.sval; *p && *p != ' '; ++p) {
  327.     d = toupper(*p) - (isnum(*p) ? '0' : 'A' - 10);
  328.     token.valu = token.valu * base + d;
  329.     if (!ishex(*p) || d >= base) { exp_error('D');  break; }
  330.     }
  331.     clamp(token.valu);
  332.     return;
  333. }
  334.  
  335. int isalph(c)
  336. char c;
  337. {
  338.     return (c >= 'A' && c <= '~') || c == '!' ||
  339.     c == '&' || c == ':' || c == '?' || c == '@';
  340. }
  341.  
  342. int isnum(c)
  343. char c;
  344. {
  345.     return c >= '0' && c <= '9';
  346. }
  347.  
  348. int ishex(c)
  349. char c;
  350. {
  351.     return isnum(c) || ((c = toupper(c)) >= 'A' && c <= 'F');
  352. }
  353.  
  354. static int isalnum(c)
  355. char c;
  356. {
  357.     return isalph(c) || isnum(c);
  358. }
  359.  
  360. /*  Push back the current token into the input stream.  One level of    */
  361. /*  pushback is supported.                        */
  362.  
  363. void unlex()
  364. {
  365.     oldt = TRUE;
  366.     return;
  367. }
  368.  
  369. /*  Get an alphanumeric string into the string value part of the    */
  370. /*  current token.  Leading blank space is trashed.            */
  371.  
  372. void pops(s)
  373. char *s;
  374. {
  375.     void pushc(), trash();
  376.  
  377.     trash();
  378.     for (; isalnum(*s = popc()); ++s);
  379.     pushc(*s);  *s = '\0';
  380.     return;
  381. }
  382.  
  383. /*  Trash blank space and push back the character following it.        */
  384.  
  385. void trash()
  386. {
  387.     SCRATCH char c;
  388.     void pushc();
  389.  
  390.     while ((c = popc()) == ' ');
  391.     pushc(c);
  392.     return;
  393. }
  394.  
  395. /*  Trash blank space and push back the character following it.        */
  396. /*  but don't eat '*'    */
  397.  
  398. void trash_lex()
  399. {
  400.     SCRATCH char c;
  401.     void pushc();
  402.  
  403.     inlex = TRUE;
  404.     while ((c = popc()) == ' ');
  405.     pushc(c);
  406.     inlex = FALSE;
  407.     return;
  408.     }
  409.  
  410. /*  Get character from input stream.  This routine does a number of    */
  411. /*  other things while it's passing back characters.  All control    */
  412. /*  characters except \t and \n are ignored.  \t is mapped into ' '.    */
  413. /*  Semicolon is mapped to \n.  In addition, a copy of all input is set    */
  414. /*  up in a line buffer for the benefit of the listing.            */
  415.  
  416. static int oldc, eol;
  417. static char *lptr;
  418.  
  419. int popc()
  420. {
  421.     SCRATCH int c;
  422.  
  423.     if (oldc) { c = oldc;  oldc = '\0';  return c; }
  424.     if (eol) return '\n';
  425.     for (;;) {
  426.     if ((c = getc(source)) != EOF && (c &= 0377) == '*' && !quote && !inlex) {
  427.         do *lptr++ = c;
  428.         while ((c = getc(source)) != EOF && (c &= 0377) != '\n');
  429.     }
  430.     if (c == EOF) c = '\n';
  431.     if ((*lptr++ = c) >= ' ' && c <= '~') return c;
  432.     if (c == '\n') { eol = TRUE;  *lptr = '\0';  return '\n'; }
  433.     if (c == '\t') return quote ? '\t' : ' ';
  434.     }
  435. }
  436.  
  437. /*  Push character back onto input stream.  Only one level of push-back    */
  438. /*  supported.  \0 cannot be pushed back, but nobody would want to.    */
  439.  
  440. void pushc(c)
  441. char c;
  442. {
  443.     oldc = c;
  444.     return;
  445. }
  446.  
  447. /*  Begin new line of source input.  This routine returns non-zero if    */
  448. /*  EOF    has been reached on the main source file, zero otherwise.    */
  449.  
  450. int newline()
  451. {
  452.     void fatal_error();
  453.  
  454.     oldc = '\0';  lptr = line;
  455.     oldt = eol = FALSE;
  456.     while (feof(source)) {
  457.     if (ferror(source)) fatal_error(ASMREAD);
  458.     if (filesp) {
  459.         fclose(source);
  460.         source = filestk[--filesp];
  461.     }
  462.     else return TRUE;
  463.     }
  464.     return FALSE;
  465. }
  466.