home *** CD-ROM | disk | FTP | other *** search
- /*
- * 68K/386 32-bit C compiler.
- *
- * copyright (c) 1996, David Lindauer
- *
- * This compiler is intended for educational use. It may not be used
- * for profit without the express written consent of the author.
- *
- * It may be freely redistributed, as long as this notice remains intact
- * and sources are distributed along with any executables derived from them.
- *
- * The author is not responsible for damages, either direct or consequential,
- * that may arise from use of this software.
- *
- * v1.5 August 1996
- * David Lindauer, gclind01@starbase.spd.louisville.edu
- *
- * Credits to Mathew Brandt for original K&R C compiler
- *
- */
- #include <stdio.h>
- #include "expr.h"
- #include "c.h"
- #include "gen.h"
- #include "cglbdec.h"
-
- double floatexpr(void);
-
- /* Primary for constant floats
- * id
- * iconst
- * rconst
- * defined(MACRO)
- * (cast) floatexpr
- */
- static double feprimary(void)
- { double temp=0;
- SYM *sp;
- if(lastst == id) {
- if (!strcmp(lastid,"defined")) {
- getsym();
- needpunc(openpa,0);
- if (lastst != id)
- generror(ERR_IDEXPECT,0,0);
- else {
- if (search(lastid,&defsyms) != 0)
- temp = 1;
- getsym();
- }
- needpunc(closepa,0);
- return(temp);
- }
- else {
- sp = gsearch(lastid);
- if(sp == NULL) {
- gensymerror(ERR_UNDEFINED,lastid);
- getsym();
- return 0;
- }
- if(sp->storage_class != sc_const) {
- generror(ERR_NEEDCONST,0,0);
- getsym();
- return 0;
- }
- getsym();
- return sp->value.i;
- }
- }
- else if(lastst == iconst) {
- temp = ival;
- getsym();
- return temp;
- }
- else if (lastst = rconst) {
- temp = rval;
- getsym();
- return temp;
- }
- else if (lastst == openpa) {
- getsym();
- if (castbegin(lastst)) {
- decl(0,0);
- decl1();
- needpunc(closepa,0);
- return floatexpr();
- }
- else {
- temp = floatexpr();
- needpunc(closepa,0);
- return(temp);
- }
- }
- getsym();
- generror(ERR_NEEDCONST,0,0);
- return 0;
- }
- /* Unary for floating const
- * -unary
- * !unary
- * primary
- */
- static double feunary(void)
- {
- double temp;
- switch (lastst) {
- case minus:
- getsym();
- temp = -feunary();
- break;
- case en_not:
- getsym();
- temp = !feunary();
- break;
- default:
- temp = feprimary();
- break;
- }
- return(temp);
- }
- /* Multiply ops */
- static double femultops(void)
- {
- double val1 = feunary(),val2;
- while (lastst == star || lastst == divide) {
- int oper = lastst;
- getsym();
- val2 = feunary();
- switch(oper) {
- case star:
- val1 = val1 * val2;
- break;
- case divide:
- val1 = val1 / val2;
- break;
- }
- }
- return(val1);
- }
- /* Add ops */
- static double feaddops(void)
- {
- double val1 = femultops(),val2;
- while (lastst == plus || lastst == minus) {
- int oper = lastst;
- getsym();
- val2 = femultops();
- if (oper == plus)
- val1 = val1 + val2;
- else
- val1 = val1 - val2;
- }
- return(val1);
- }
- /* Floating point constant expression */
- double floatexpr(void)
- {
- return(feaddops());
- }