home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga 13 / MA_Cover_13.bin / source / c / apl / lex.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-11-27  |  5.0 KB  |  345 lines

  1. #define lv yylval
  2. #define v yyval
  3.  
  4. int xxpeek[2] = {0,0};
  5.  
  6. yylex()
  7. {
  8.     int c, rval;
  9.     struct tab *tp;
  10.  
  11.     if(nlexsym != -1) {                /* first token is lexical context */
  12.         c = nlexsym;
  13.         nlexsym = -1;
  14.         return(c);
  15.     }
  16.     while(litflag > 0) {            /* comment */
  17.         c = *iline++;
  18.         if(c == '\n') {
  19.             nlexsym = 0;
  20.             return(eol);
  21.         }
  22.     }
  23.     if(xxpeek[0] != 0){
  24.         lv.charval = xxpeek[0];        /* may want charptr here */
  25.         xxpeek[0] = 0;
  26.         return(xxpeek[1]);
  27.     }
  28.     do
  29.         c = *iline++;
  30.     while(c == ' ');
  31.     if(c == '\n') {
  32.         nlexsym = 0;
  33.         return(eol);
  34.     }
  35.     if(alpha(c)) return(getnam(c));
  36.     if(digit(c) || c == '`' || (c=='.' && digit(*iline))) return(getnum(c));
  37.     c &= 0377;
  38.     rval = unk;
  39.     for(tp = tab; tp->input; tp++) {
  40.         if(tp->input == c) {
  41.             lv.charval = tp->lexval;
  42.             rval = tp->retval;
  43.             break;
  44.         }
  45.     }
  46.                                     /*    If it's a comment, skip to the end
  47.                                         of the line and return eol instead.  */
  48.     if (lv.charval == COMNT) {
  49.         while (1) {
  50.             c = *iline++;
  51.             if (c == '\n') {
  52.                 nlexsym = 0;
  53.                 return(eol);
  54.             }
  55.         }
  56.     }
  57.  
  58.     if(lv.charval == QUAD) return(getquad());
  59.  
  60.     if(lv.charval == ISP){
  61.         lv.charval = ISP2;
  62.         xxpeek[0] = ISP1;
  63.         xxpeek[1] = m;
  64.         return(d);
  65.     }
  66.  
  67.     if(lv.charval == PSI){
  68.         lv.charval = PSI2;
  69.         xxpeek[0] = PSI1;
  70.         xxpeek[1] = m;
  71.         return(d);
  72.     }
  73.  
  74.     return(rval);
  75. }
  76.  
  77. getquad()
  78. {
  79.     char c, *p1;
  80.     struct qtab *p2;
  81.     char qbuf[10];
  82.  
  83.     p1 = qbuf;
  84.     while(alpha(*iline)) *p1++ = *iline++;
  85.     *p1++ = 0;
  86.     if(*qbuf == 0) return(quad);        /* ordinary quad */
  87.     for(p2 = qtab; p2->qname; p2++){
  88.         if(equal(p2->qname, qbuf)){
  89.             lv.charval = p2->qtype;
  90.             return(p2->rtype);
  91.         }
  92.     }
  93.     return(unk);
  94. }
  95.  
  96. getnam(ic)
  97. {
  98.     char name[NAMS], *cp;
  99.     int c;
  100.     struct nlist *np;
  101.  
  102.     c = ic;
  103.     cp = name;
  104.     do {
  105.         if(cp >= &name[NAMS]) error("var name D");
  106.         *cp++ = c;
  107.         c = *iline++;
  108.     } while(alpha(c) || digit(c));
  109.     *cp++ = 0;
  110.     iline--;
  111.                                     /*    commands  */
  112.     if(litflag == -1) {
  113.         litflag = -2;
  114.         for(c=0; comtab[c].ct_name; c++) {
  115.             if(equal(name, comtab[c].ct_name)) break;
  116.         }
  117.         immedcmd = lv.charval = comtab[c].ct_ylval;
  118.         return(comtab[c].ct_ytype);
  119.     }
  120.     for(np=nlist; np->namep; np++) {
  121.         if(equal(np->namep, name)) {
  122.             lv.charptr = (char *)np;
  123.             switch(np->use) {
  124.     
  125.             case NF:
  126.                 if (context == lex2) sichk(np);
  127.                 return(nfun);
  128.  
  129.             case MF:
  130.                 if (context == lex2) sichk(np);
  131.                 return(mfun);
  132.  
  133.             case DF:
  134.                 if (context == lex2) sichk(np);
  135.                 return(dfun);
  136.             }
  137.             return(nam);
  138.         }
  139.     }
  140.     np->namep = alloc(cp-name);
  141.     copy(CH, name, np->namep, cp-name);
  142.     np->type = LV;
  143.     lv.charptr = (char *)np;
  144.     return(nam);
  145. }
  146.  
  147. getnum(ic)
  148. {
  149.     double d1, d2;
  150.     int c, n, n1, s, s1;
  151.  
  152.     s = 0;
  153.     n = 0;
  154.     d1 = 0.;
  155.     c = ic;
  156.     if(c == '`') {                    /* '`' was '"' */
  157.         s++;
  158.         c = *iline++;
  159.     }
  160.     while(digit(c)) {
  161.         d1 = d1*10. + c - '0';
  162.         c = *iline++;
  163.     }
  164.     if(c == '.') {
  165.         c = *iline++;
  166.         while(digit(c)) {
  167.             d1 = d1*10. + c - '0';
  168.             c = *iline++;
  169.             n--;
  170.         }
  171.     }
  172.     if(c == 'e') {
  173.         s1 = 0;
  174.         n1 = 0;
  175.         c = *iline++;
  176.         if(c == '`') {                /* '`' was '"' */
  177.             s1++;
  178.             c = *iline++;
  179.         }
  180.         while(digit(c)) {
  181.             n1 = n1*10 + c - '0';
  182.             c = *iline++;
  183.         }
  184.         if(s1) n -= n1;
  185.         else n += n1;
  186.     }
  187.     n1 = n;
  188.     if(n1 < 0) n1 = -n1;
  189.     d2 = 1.;
  190.     while(n1--) d2 *= 10.;
  191.     if(n < 0) d1 /= d2;
  192.     else d1 *= d2;
  193.     if(s) d1 = -d1;
  194.     iline--;
  195.     datum = d1;
  196.     return(numb);
  197. }
  198.  
  199. alpha(s)
  200. {
  201.     int c;
  202.  
  203.     c = s & 0377;
  204.     return(
  205.         (c >= 'a' && c <= 'z')
  206.         || (c == 'F')
  207.         || (c >= 0243)
  208.         || (litflag == -2 && (
  209.                c == '/'
  210.             || c == '.'
  211.            ))
  212.     );
  213. }
  214.  
  215. digit(s)
  216. {
  217.     int c;
  218.  
  219.     c = s;
  220.     if(c >='0' && c <= '9') return(1);
  221.     return(0);
  222. }
  223.  
  224. /*
  225.  * s is statement
  226.  * f is execution flag:
  227.  *    0 compile immediate
  228.  *    1 compile L
  229.  *    2 function definition
  230.  *    3 function prolog
  231.  *    4 function epilog
  232.  *    5 function body
  233.  */
  234. int     ilex[] =
  235. {
  236.     lex0, lex1, lex2, lex3, lex4, lex5, lex6
  237. };
  238.  
  239.  
  240. char *
  241. compile(s, f)
  242. char *s;
  243. {
  244.     char *p, *q;
  245.  
  246.     iline = s;
  247.     ccharp = oline;
  248.     litflag = 0;
  249.     nlexsym = ilex[f];
  250.     context = nlexsym;
  251.     if(yyparse()) {
  252.         pline(s, iline-s, lineNumber);
  253.         if(iline-s > 1) printf("syntax error\n");
  254.         return(0);
  255.     }
  256.     *ccharp++ = EOF;
  257.     iline = p = alloc(ccharp-oline);
  258.     for(q=oline; q<ccharp;) *p++ = *q++;
  259.     return(iline);
  260. }
  261.  
  262. yyerror()
  263. {
  264. }
  265.  
  266.                         /*    Byte-order dependency here?  */
  267.                         /*    Potential alignment problem here?  */
  268. //char *
  269. name(np, c)
  270. {
  271.     char *p, *npp;
  272.     int temp;
  273.  
  274.     temp = np;
  275.     npp = (char *)&temp;
  276.     p = ccharp;
  277.     *ccharp++ = c;
  278.     *ccharp++ = *npp++;
  279.     *ccharp++ = *npp++;
  280.     *ccharp++ = *npp++;
  281.     *ccharp++ = *npp++;
  282.     return(p);
  283. }
  284.  
  285. invert(a, b)
  286. {
  287.     flop(a, b);
  288.     flop(b, ccharp);
  289.     flop(a, ccharp);
  290. }
  291.  
  292. flop(a, b)
  293. char *a, *b;
  294. {
  295.     char *a1, *a2;
  296.     int c;
  297.  
  298.     a1 = a;
  299.     a2 = b;
  300.     while(a1 < a2) {
  301.         c = *a1;
  302.         *a1++ = *--a2;
  303.         *a2 = c;
  304.     }
  305. }
  306.  
  307. /*
  308.  * genlab -- generates label code onto label stacks.
  309.  *
  310.  * prologue:    AUTO-lab, CONST-linenum, NAME-lab LABEL
  311.  *
  312.  * epilog:    REST-lab
  313.  */
  314. genlab(np)
  315. struct nlist *np;
  316. {
  317.     /* label prologue */
  318.  
  319.     *labcpp++ = AUTO;
  320.     labcpp += copy(IN, &np, labcpp, 1);
  321.     *labcpp++ = CONST;
  322.     *labcpp++ = 1;
  323.     labcpp += copy(DA, &lnumb, labcpp, 1);
  324.     *labcpp++ = NAME;
  325.     labcpp += copy(IN, &np, labcpp, 1);
  326.     *labcpp++ = LABEL;
  327.     *labcpp = EOF;
  328.  
  329.     /* label epilog */
  330.  
  331.     *labcpe++ = REST;
  332.     labcpe += copy(IN, &np, labcpe, 1);
  333.     *labcpe = EOF;
  334. }
  335.  
  336.  
  337. int
  338. isodigit(c)
  339. int c;
  340. {
  341.     if (c < '0') return 0;
  342.     if (c > '7') return 0;
  343.     return 1;
  344. }
  345.