home *** CD-ROM | disk | FTP | other *** search
/ CD Shareware Magazine 1996 December / CD_shareware_12-96.iso / DOS / Programa / CCDL122.ZIP / SOURCE / FLOATEXP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-16  |  3.5 KB  |  158 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. double floatexpr(void);
  28.  
  29. /* Primary for constant floats
  30.  *   id
  31.  *   iconst
  32.  *   rconst
  33.  *   defined(MACRO)
  34.  *   (cast) floatexpr
  35.  */
  36. static double feprimary(void)   
  37. {       double     temp=0;
  38.         SYM     *sp;
  39.         if(lastst == id) {
  40.                                 if (!strcmp(lastid,"defined")) {
  41.                                     getsym();
  42.                                     needpunc(openpa,0);
  43.                                     if (lastst != id) 
  44.                                         generror(ERR_IDEXPECT,0,0);
  45.                                     else {
  46.                                         if (search(lastid,&defsyms) != 0)
  47.                                             temp = 1;
  48.                                         getsym();    
  49.                                     }
  50.                                     needpunc(closepa,0);
  51.                                     return(temp);
  52.                                 }
  53.                                 else {
  54.                     sp = gsearch(lastid);
  55.                     if(sp == NULL) {
  56.                                                 gensymerror(ERR_UNDEFINED,lastid);
  57.                         getsym();
  58.                         return 0;
  59.                         }
  60.                     if(sp->storage_class != sc_const) {
  61.                         generror(ERR_NEEDCONST,0,0);
  62.                         getsym();
  63.                         return 0;
  64.                         }
  65.                     getsym();
  66.                     return sp->value.i;
  67.                                 }                
  68.         }
  69.         else if(lastst == iconst) {
  70.                 temp = ival;
  71.                 getsym();
  72.                 return temp;
  73.                 }
  74.                 else if (lastst = rconst) {
  75.                                 temp = rval;
  76.                                 getsym();
  77.                                 return temp;
  78.                 }
  79.                 else if (lastst == openpa) {
  80.                     getsym();
  81.                     if (castbegin(lastst)) {
  82.                         decl(0,0);
  83.                         decl1();
  84.                         needpunc(closepa,0);
  85.                         return floatexpr();
  86.                     }
  87.                     else {
  88.                       temp = floatexpr();
  89.                         needpunc(closepa,0);
  90.                         return(temp);
  91.                     }
  92.                 }
  93.         getsym();
  94.         generror(ERR_NEEDCONST,0,0);
  95.         return 0;
  96. }
  97. /* Unary for floating const
  98.  *    -unary
  99.  *    !unary
  100.  *    primary
  101.  */
  102. static double feunary(void)
  103. {
  104.     double temp;
  105.     switch (lastst) {
  106.         case minus:
  107.                 getsym();
  108.                 temp = -feunary();
  109.                 break;
  110.         case  en_not:
  111.                 getsym();
  112.                 temp = !feunary();
  113.                 break;
  114.         default:
  115.                 temp = feprimary();
  116.                 break;
  117.     }
  118.     return(temp);
  119. }
  120. /* Multiply ops */
  121. static double femultops(void)
  122. {
  123.     double val1 = feunary(),val2;
  124.     while (lastst == star || lastst == divide) {
  125.         int oper = lastst;
  126.         getsym();
  127.         val2 = feunary();
  128.         switch(oper) {
  129.             case star:
  130.                     val1 = val1 * val2;
  131.                     break;
  132.             case divide:
  133.                     val1 = val1 / val2;
  134.                     break;
  135.         }
  136.     }
  137.     return(val1);
  138. }
  139. /* Add ops */
  140. static double feaddops(void)
  141. {
  142.     double val1 = femultops(),val2;
  143.     while (lastst == plus || lastst == minus)    {
  144.         int oper = lastst;
  145.         getsym();
  146.         val2 = femultops();
  147.         if (oper == plus) 
  148.             val1 = val1 + val2;
  149.         else
  150.             val1 = val1 - val2;
  151.     }
  152.     return(val1);
  153. }
  154. /* Floating point constant expression */
  155. double floatexpr(void)
  156. {
  157.     return(feaddops());
  158. }