home *** CD-ROM | disk | FTP | other *** search
/ CD Shareware Magazine 1996 December / CD_shareware_12-96.iso / DOS / Programa / CCDL122.ZIP / SOURCE / INTEXPR.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-06  |  6.0 KB  |  295 lines

  1. /*
  2.  * 68K/386 32-bit C compiler.
  3.  *
  4.  * copyright (c) 1996, David Lindauer
  5.  * 
  6.  * This compiler is intended for educational use.  It may not be used
  7.  * for profit without the express written consent of the author.
  8.  *
  9.  * It may be freely redistributed, as long as this notice remains intact
  10.  * and sources are distributed along with any executables derived from them.
  11.  *
  12.  * The author is not responsible for damages, either direct or consequential,
  13.  * that may arise from use of this software.
  14.  *
  15.  * v1.5 August 1996
  16.  * David Lindauer, gclind01@starbase.spd.louisville.edu
  17.  *
  18.  * Credits to Mathew Brandt for original K&R C compiler
  19.  *
  20.  */
  21. #include        <stdio.h>
  22. #include        "expr.h"
  23. #include        "c.h"
  24. #include        "gen.h"
  25. #include        "cglbdec.h"
  26.  
  27. extern TYP stdint, *head;
  28. extern int prm_cmangle;
  29. static long ieprimary(TYP **tp)   
  30. /*
  31.  * PRimary integer
  32.  *    defined(MACRO)
  33.  *    id
  34.  *    iconst
  35.  *    (cast )intexpr
  36.  *    (intexpr)
  37.  */
  38. {       long     temp=0;
  39.         SYM     *sp;
  40.                 if (tp)
  41.                     *tp = &stdint;
  42.         if(lastst == id) {
  43.                     char *lid = lastid;
  44.                     if (prm_cmangle)
  45.                         lid++;
  46.                                 if (!strcmp(lid,"defined")) {
  47.                                     getsym();
  48.                                     needpunc(openpa,0);
  49.                                     if (lastst != id) 
  50.                                         generror(ERR_IDEXPECT,0,0);
  51.                                     else {
  52.                                         if (search(lastid,&defsyms) != 0)
  53.                                             temp = 1;
  54.                                         getsym();    
  55.                                     }
  56.                                     needpunc(closepa,0);
  57.                                     return(temp);
  58.                                 }
  59.                                 else {
  60.                     sp = gsearch(lastid);
  61.                     if(sp == NULL) {
  62.                                                 gensymerror(ERR_UNDEFINED,lastid);
  63.                         getsym();
  64.                         return 0;
  65.                         }
  66.                     if(sp->storage_class != sc_const) {
  67.                         generror(ERR_NEEDCONST,0,0);
  68.                         getsym();
  69.                         return 0;
  70.                         }
  71.                     getsym();
  72.                     return sp->value.i;
  73.                                 }                
  74.         }
  75.         else if(lastst == iconst) {
  76.                 temp = ival;
  77.                 getsym();
  78.                 return temp;
  79.                 }
  80.                 else if (lastst == openpa) {
  81.                     getsym();
  82.                     if (castbegin(lastst)) {
  83.                         decl(0,0);
  84.                         decl1();
  85.                         needpunc(closepa,0);
  86.                         if (tp)
  87.                           *tp = head;
  88.                         return intexpr(0);
  89.                     }
  90.                     else {
  91.                       temp = intexpr(tp);
  92.                         return(temp);
  93.                     }
  94.                 }
  95.         getsym();
  96.         generror(ERR_NEEDCONST,0,0);
  97.         return 0;
  98. }
  99. /*
  100.  * Integer unary
  101.  *   - unary
  102.  *   ! unary
  103.  *   ~unary
  104.  *   primary
  105.  */
  106. static long ieunary(TYP **tp)
  107. {
  108.     long temp;
  109.     switch (lastst) {
  110.         case minus:
  111.                 getsym();
  112.                 temp = -ieunary(tp);
  113.                 break;
  114.         case not:
  115.                 getsym();
  116.                 temp = !ieunary(tp);
  117.                 break;
  118.         case compl:
  119.                 getsym();
  120.                 temp = ~ieunary(tp);
  121.                 break;
  122.         default:
  123.                 temp = ieprimary(tp);
  124.                 break;
  125.     }
  126.     return(temp);
  127. }
  128. static long iemultops(TYP **tp)
  129. /* Multiply ops */
  130. {
  131.     long val1 = ieunary(tp),val2;
  132.     while (lastst == star || lastst == divide || lastst == modop) {
  133.         long oper = lastst;
  134.         getsym();
  135.         val2 = ieunary(tp);
  136.         switch(oper) {
  137.             case star:
  138.                     val1 = val1 * val2;
  139.                     break;
  140.             case divide:
  141.                     val1 = val1 / val2;
  142.                     break;
  143.             case modop:
  144.                     val1 = val1 % val2;
  145.                     break;
  146.         }
  147.     }
  148.     return(val1);
  149. }
  150. static long ieaddops(TYP **tp)
  151. /* Add ops */
  152. {
  153.     long val1 = iemultops(tp),val2;
  154.     while (lastst == plus || lastst == minus)    {
  155.         long oper = lastst;
  156.         getsym();
  157.         val2 = iemultops(tp);
  158.         if (oper == plus) 
  159.             val1 = val1 + val2;
  160.         else
  161.             val1 = val1 - val2;
  162.     }
  163.     return(val1);
  164. }
  165. static long ieshiftops(TYP **tp)
  166. /* Shift ops */
  167. {
  168.     long val1 = ieaddops(tp), val2;
  169.     while (lastst == lshift || lastst == rshift) {
  170.         long oper = lastst;
  171.         getsym();
  172.         val2 = ieaddops(tp);
  173.         if (oper == lshift)
  174.             val1 <<= val2;
  175.         else
  176.             val1 >>= val2;
  177.     }
  178.     return(val1);
  179. }
  180. static long ierelation(TYP **tp)
  181. /* non-eq relations */
  182. {
  183.     long val1 = ieshiftops(tp), val2;
  184.     while (lastst == lt || lastst == gt || lastst == leq || lastst == geq) {
  185.         long oper = lastst;
  186.         getsym();
  187.         val2 = ieshiftops(tp);
  188.         switch(oper) {
  189.             case lt:
  190.                     val1 = val1 < val2;
  191.                     break;
  192.             case gt:
  193.                     val1 = val1 > val2;
  194.                     break;
  195.             case leq:
  196.                     val1 = val1 <= val2;
  197.                     break;
  198.             case geq:
  199.                     val1 = val1 >= val2;
  200.                     break;
  201.         }
  202.     }
  203.     return(val1);
  204. }
  205. static long ieequalops(TYP **tp)
  206. /* eq relations */
  207. {
  208.     long val1 = ierelation(tp),val2;
  209.     while (lastst == eq || lastst == neq) {
  210.         long oper = lastst;
  211.         getsym();
  212.         val2 = ierelation(tp);
  213.         if (oper == neq)
  214.             val1 = val1 != val2;
  215.         else
  216.             val1 = val1 == val2;
  217.     }
  218.     return(val1);
  219. }
  220. static long ieandop(TYP **tp)
  221. /* and op */
  222. {
  223.     long val1 = ieequalops(tp),val2;
  224.     while (lastst == and) {
  225.         getsym();
  226.         val2 = ieequalops(tp);
  227.         val1 = val1 & val2;
  228.     }
  229.     return(val1);
  230. }
  231. static long iexorop(TYP **tp)
  232. /* xor op */
  233. {
  234.     long val1 = ieandop(tp),val2;
  235.     while (lastst == uparrow) {
  236.         getsym();
  237.         val2 = ieandop(tp);
  238.         val1 = val1 ^ val2;
  239.     }
  240.     return(val1);
  241. }
  242. static long ieorop(TYP **tp)
  243. /* or op */
  244. {
  245.     long val1 = iexorop(tp),val2;
  246.     while (lastst == or) {
  247.         getsym();
  248.         val2 = iexorop(tp);
  249.         val1 = val1 | val2;
  250.     }
  251.     return(val1);
  252. }
  253. static long ielandop(TYP **tp)
  254. /* logical and op */
  255. {
  256.     long val1 = ieorop(tp),val2;
  257.     while (lastst == land) {
  258.         getsym();
  259.         val2 = ieorop(tp);
  260.         val1 = val1 && val2;
  261.     }
  262.     return(val1);
  263. }
  264. static long ielorop(TYP **tp)
  265. /* logical or op */
  266. {
  267.     long val1 = ielandop(tp),val2;
  268.     while (lastst == lor) {
  269.         getsym();
  270.         val2 = ielandop(tp);
  271.         val1 = val1 || val2;
  272.     }
  273.     return(val1);
  274. }
  275. static long iecondop(TYP **tp)
  276. /* Hook op */
  277. {
  278.     long val1 = ielorop(tp),val2, val3;
  279.         if (lastst == hook) {
  280.             getsym();
  281.             val2 = iecondop(tp);
  282.             needpunc(colon,0);
  283.             val3 = iecondop(tp);
  284.             if (val1)
  285.                 val1 = val2;
  286.             else
  287.                 val1 = val3;
  288.         }
  289.     return(val1);
  290. }
  291. long intexpr(TYP **tp)
  292. /* Integer expressions */
  293. {
  294.     return(iecondop(tp));
  295. }