OpenCores
URL https://opencores.org/ocsvn/eco32/eco32/trunk

Subversion Repositories eco32

[/] [eco32/] [tags/] [eco32-0.23/] [lcc/] [cpp/] [cpp.c] - Diff between revs 4 and 157

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 4 Rev 157
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <string.h>
#include <string.h>
#include <time.h>
#include <time.h>
#include <stdarg.h>
#include <stdarg.h>
#include "cpp.h"
#include "cpp.h"
 
 
#define OUTS    16384
#define OUTS    16384
char    outbuf[OUTS];
char    outbuf[OUTS];
char    *outp = outbuf;
char    *outp = outbuf;
Source  *cursource;
Source  *cursource;
int     nerrs;
int     nerrs;
struct  token nltoken = { NL, 0, 0, 0, 1, (uchar*)"\n" };
struct  token nltoken = { NL, 0, 0, 0, 1, (uchar*)"\n" };
char    *curtime;
char    *curtime;
int     incdepth;
int     incdepth;
int     ifdepth;
int     ifdepth;
int     ifsatisfied[NIF];
int     ifsatisfied[NIF];
int     skipping;
int     skipping;
 
 
char rcsid[] = "$Revision: 1.6 $ $Date: 2001/03/27 19:37:59 $";
char rcsid[] = "$Revision: 1.6 $ $Date: 2001/03/27 19:37:59 $";
 
 
int
int
main(int argc, char **argv)
main(int argc, char **argv)
{
{
        Tokenrow tr;
        Tokenrow tr;
        time_t t;
        time_t t;
        char ebuf[BUFSIZ];
        char ebuf[BUFSIZ];
 
 
        setbuf(stderr, ebuf);
        setbuf(stderr, ebuf);
        t = time(NULL);
        t = time(NULL);
        curtime = ctime(&t);
        curtime = ctime(&t);
        maketokenrow(3, &tr);
        maketokenrow(3, &tr);
        expandlex();
        expandlex();
        setup(argc, argv);
        setup(argc, argv);
        fixlex();
        fixlex();
        iniths();
        iniths();
        genline();
        genline();
        process(&tr);
        process(&tr);
        flushout();
        flushout();
        fflush(stderr);
        fflush(stderr);
        exit(nerrs > 0);
        exit(nerrs > 0);
        return 0;
        return 0;
}
}
 
 
void
void
process(Tokenrow *trp)
process(Tokenrow *trp)
{
{
        int anymacros = 0;
        int anymacros = 0;
 
 
        for (;;) {
        for (;;) {
                if (trp->tp >= trp->lp) {
                if (trp->tp >= trp->lp) {
                        trp->tp = trp->lp = trp->bp;
                        trp->tp = trp->lp = trp->bp;
                        outp = outbuf;
                        outp = outbuf;
                        anymacros |= gettokens(trp, 1);
                        anymacros |= gettokens(trp, 1);
                        trp->tp = trp->bp;
                        trp->tp = trp->bp;
                }
                }
                if (trp->tp->type == END) {
                if (trp->tp->type == END) {
                        if (--incdepth>=0) {
                        if (--incdepth>=0) {
                                if (cursource->ifdepth)
                                if (cursource->ifdepth)
                                        error(ERROR,
                                        error(ERROR,
                                         "Unterminated conditional in #include");
                                         "Unterminated conditional in #include");
                                unsetsource();
                                unsetsource();
                                cursource->line += cursource->lineinc;
                                cursource->line += cursource->lineinc;
                                trp->tp = trp->lp;
                                trp->tp = trp->lp;
                                genline();
                                genline();
                                continue;
                                continue;
                        }
                        }
                        if (ifdepth)
                        if (ifdepth)
                                error(ERROR, "Unterminated #if/#ifdef/#ifndef");
                                error(ERROR, "Unterminated #if/#ifdef/#ifndef");
                        break;
                        break;
                }
                }
                if (trp->tp->type==SHARP) {
                if (trp->tp->type==SHARP) {
                        trp->tp += 1;
                        trp->tp += 1;
                        control(trp);
                        control(trp);
                } else if (!skipping && anymacros)
                } else if (!skipping && anymacros)
                        expandrow(trp, NULL);
                        expandrow(trp, NULL);
                if (skipping)
                if (skipping)
                        setempty(trp);
                        setempty(trp);
                puttokens(trp);
                puttokens(trp);
                anymacros = 0;
                anymacros = 0;
                cursource->line += cursource->lineinc;
                cursource->line += cursource->lineinc;
                if (cursource->lineinc>1) {
                if (cursource->lineinc>1) {
                        genline();
                        genline();
                }
                }
        }
        }
}
}
 
 
void
void
control(Tokenrow *trp)
control(Tokenrow *trp)
{
{
        Nlist *np;
        Nlist *np;
        Token *tp;
        Token *tp;
 
 
        tp = trp->tp;
        tp = trp->tp;
        if (tp->type!=NAME) {
        if (tp->type!=NAME) {
                if (tp->type==NUMBER)
                if (tp->type==NUMBER)
                        goto kline;
                        goto kline;
                if (tp->type != NL)
                if (tp->type != NL)
                        error(ERROR, "Unidentifiable control line");
                        error(ERROR, "Unidentifiable control line");
                return;                 /* else empty line */
                return;                 /* else empty line */
        }
        }
        if ((np = lookup(tp, 0))==NULL || (np->flag&ISKW)==0 && !skipping) {
        if ((np = lookup(tp, 0))==NULL || (np->flag&ISKW)==0 && !skipping) {
                error(WARNING, "Unknown preprocessor control %t", tp);
                error(WARNING, "Unknown preprocessor control %t", tp);
                return;
                return;
        }
        }
        if (skipping) {
        if (skipping) {
                if ((np->flag&ISKW)==0)
                if ((np->flag&ISKW)==0)
                        return;
                        return;
                switch (np->val) {
                switch (np->val) {
                case KENDIF:
                case KENDIF:
                        if (--ifdepth<skipping)
                        if (--ifdepth<skipping)
                                skipping = 0;
                                skipping = 0;
                        --cursource->ifdepth;
                        --cursource->ifdepth;
                        setempty(trp);
                        setempty(trp);
                        return;
                        return;
 
 
                case KIFDEF:
                case KIFDEF:
                case KIFNDEF:
                case KIFNDEF:
                case KIF:
                case KIF:
                        if (++ifdepth >= NIF)
                        if (++ifdepth >= NIF)
                                error(FATAL, "#if too deeply nested");
                                error(FATAL, "#if too deeply nested");
                        ++cursource->ifdepth;
                        ++cursource->ifdepth;
                        return;
                        return;
 
 
                case KELIF:
                case KELIF:
                case KELSE:
                case KELSE:
                        if (ifdepth<=skipping)
                        if (ifdepth<=skipping)
                                break;
                                break;
                        return;
                        return;
 
 
                default:
                default:
                        return;
                        return;
                }
                }
        }
        }
        switch (np->val) {
        switch (np->val) {
        case KDEFINE:
        case KDEFINE:
                dodefine(trp);
                dodefine(trp);
                break;
                break;
 
 
        case KUNDEF:
        case KUNDEF:
                tp += 1;
                tp += 1;
                if (tp->type!=NAME || trp->lp - trp->bp != 4) {
                if (tp->type!=NAME || trp->lp - trp->bp != 4) {
                        error(ERROR, "Syntax error in #undef");
                        error(ERROR, "Syntax error in #undef");
                        break;
                        break;
                }
                }
                if ((np = lookup(tp, 0)) != NULL)
                if ((np = lookup(tp, 0)) != NULL)
                        np->flag &= ~ISDEFINED;
                        np->flag &= ~ISDEFINED;
                break;
                break;
 
 
        case KPRAGMA:
        case KPRAGMA:
                return;
                return;
 
 
        case KIFDEF:
        case KIFDEF:
        case KIFNDEF:
        case KIFNDEF:
        case KIF:
        case KIF:
                if (++ifdepth >= NIF)
                if (++ifdepth >= NIF)
                        error(FATAL, "#if too deeply nested");
                        error(FATAL, "#if too deeply nested");
                ++cursource->ifdepth;
                ++cursource->ifdepth;
                ifsatisfied[ifdepth] = 0;
                ifsatisfied[ifdepth] = 0;
                if (eval(trp, np->val))
                if (eval(trp, np->val))
                        ifsatisfied[ifdepth] = 1;
                        ifsatisfied[ifdepth] = 1;
                else
                else
                        skipping = ifdepth;
                        skipping = ifdepth;
                break;
                break;
 
 
        case KELIF:
        case KELIF:
                if (ifdepth==0) {
                if (ifdepth==0) {
                        error(ERROR, "#elif with no #if");
                        error(ERROR, "#elif with no #if");
                        return;
                        return;
                }
                }
                if (ifsatisfied[ifdepth]==2)
                if (ifsatisfied[ifdepth]==2)
                        error(ERROR, "#elif after #else");
                        error(ERROR, "#elif after #else");
                if (eval(trp, np->val)) {
                if (eval(trp, np->val)) {
                        if (ifsatisfied[ifdepth])
                        if (ifsatisfied[ifdepth])
                                skipping = ifdepth;
                                skipping = ifdepth;
                        else {
                        else {
                                skipping = 0;
                                skipping = 0;
                                ifsatisfied[ifdepth] = 1;
                                ifsatisfied[ifdepth] = 1;
                        }
                        }
                } else
                } else
                        skipping = ifdepth;
                        skipping = ifdepth;
                break;
                break;
 
 
        case KELSE:
        case KELSE:
                if (ifdepth==0 || cursource->ifdepth==0) {
                if (ifdepth==0 || cursource->ifdepth==0) {
                        error(ERROR, "#else with no #if");
                        error(ERROR, "#else with no #if");
                        return;
                        return;
                }
                }
                if (ifsatisfied[ifdepth]==2)
                if (ifsatisfied[ifdepth]==2)
                        error(ERROR, "#else after #else");
                        error(ERROR, "#else after #else");
                if (trp->lp - trp->bp != 3)
                if (trp->lp - trp->bp != 3)
                        error(ERROR, "Syntax error in #else");
                        error(ERROR, "Syntax error in #else");
                skipping = ifsatisfied[ifdepth]? ifdepth: 0;
                skipping = ifsatisfied[ifdepth]? ifdepth: 0;
                ifsatisfied[ifdepth] = 2;
                ifsatisfied[ifdepth] = 2;
                break;
                break;
 
 
        case KENDIF:
        case KENDIF:
                if (ifdepth==0 || cursource->ifdepth==0) {
                if (ifdepth==0 || cursource->ifdepth==0) {
                        error(ERROR, "#endif with no #if");
                        error(ERROR, "#endif with no #if");
                        return;
                        return;
                }
                }
                --ifdepth;
                --ifdepth;
                --cursource->ifdepth;
                --cursource->ifdepth;
                if (trp->lp - trp->bp != 3)
                if (trp->lp - trp->bp != 3)
                        error(WARNING, "Syntax error in #endif");
                        error(WARNING, "Syntax error in #endif");
                break;
                break;
 
 
        case KERROR:
        case KERROR:
                trp->tp = tp+1;
                trp->tp = tp+1;
                error(WARNING, "#error directive: %r", trp);
                error(WARNING, "#error directive: %r", trp);
                break;
                break;
 
 
        case KLINE:
        case KLINE:
                trp->tp = tp+1;
                trp->tp = tp+1;
                expandrow(trp, "<line>");
                expandrow(trp, "<line>");
                tp = trp->bp+2;
                tp = trp->bp+2;
        kline:
        kline:
                if (tp+1>=trp->lp || tp->type!=NUMBER || tp+3<trp->lp
                if (tp+1>=trp->lp || tp->type!=NUMBER || tp+3<trp->lp
                 || (tp+3==trp->lp && ((tp+1)->type!=STRING)||*(tp+1)->t=='L')){
                 || (tp+3==trp->lp && ((tp+1)->type!=STRING)||*(tp+1)->t=='L')){
                        error(ERROR, "Syntax error in #line");
                        error(ERROR, "Syntax error in #line");
                        return;
                        return;
                }
                }
                cursource->line = atol((char*)tp->t)-1;
                cursource->line = atol((char*)tp->t)-1;
                if (cursource->line<0 || cursource->line>=32768)
                if (cursource->line<0 || cursource->line>=32768)
                        error(WARNING, "#line specifies number out of range");
                        error(WARNING, "#line specifies number out of range");
                tp = tp+1;
                tp = tp+1;
                if (tp+1<trp->lp)
                if (tp+1<trp->lp)
                        cursource->filename=(char*)newstring(tp->t+1,tp->len-2,0);
                        cursource->filename=(char*)newstring(tp->t+1,tp->len-2,0);
                return;
                return;
 
 
        case KDEFINED:
        case KDEFINED:
                error(ERROR, "Bad syntax for control line");
                error(ERROR, "Bad syntax for control line");
                break;
                break;
 
 
        case KINCLUDE:
        case KINCLUDE:
                doinclude(trp);
                doinclude(trp);
                trp->lp = trp->bp;
                trp->lp = trp->bp;
                return;
                return;
 
 
        case KEVAL:
        case KEVAL:
                eval(trp, np->val);
                eval(trp, np->val);
                break;
                break;
 
 
        default:
        default:
                error(ERROR, "Preprocessor control `%t' not yet implemented", tp);
                error(ERROR, "Preprocessor control `%t' not yet implemented", tp);
                break;
                break;
        }
        }
        setempty(trp);
        setempty(trp);
        return;
        return;
}
}
 
 
void *
void *
domalloc(int size)
domalloc(int size)
{
{
        void *p = malloc(size);
        void *p = malloc(size);
 
 
        if (p==NULL)
        if (p==NULL)
                error(FATAL, "Out of memory from malloc");
                error(FATAL, "Out of memory from malloc");
        return p;
        return p;
}
}
 
 
void
void
dofree(void *p)
dofree(void *p)
{
{
        free(p);
        free(p);
}
}
 
 
void
void
error(enum errtype type, char *string, ...)
error(enum errtype type, char *string, ...)
{
{
        va_list ap;
        va_list ap;
        char *cp, *ep;
        char *cp, *ep;
        Token *tp;
        Token *tp;
        Tokenrow *trp;
        Tokenrow *trp;
        Source *s;
        Source *s;
        int i;
        int i;
 
 
        fprintf(stderr, "cpp: ");
        fprintf(stderr, "cpp: ");
        for (s=cursource; s; s=s->next)
        for (s=cursource; s; s=s->next)
                if (*s->filename)
                if (*s->filename)
                        fprintf(stderr, "%s:%d ", s->filename, s->line);
                        fprintf(stderr, "%s:%d ", s->filename, s->line);
        va_start(ap, string);
        va_start(ap, string);
        for (ep=string; *ep; ep++) {
        for (ep=string; *ep; ep++) {
                if (*ep=='%') {
                if (*ep=='%') {
                        switch (*++ep) {
                        switch (*++ep) {
 
 
                        case 's':
                        case 's':
                                cp = va_arg(ap, char *);
                                cp = va_arg(ap, char *);
                                fprintf(stderr, "%s", cp);
                                fprintf(stderr, "%s", cp);
                                break;
                                break;
                        case 'd':
                        case 'd':
                                i = va_arg(ap, int);
                                i = va_arg(ap, int);
                                fprintf(stderr, "%d", i);
                                fprintf(stderr, "%d", i);
                                break;
                                break;
                        case 't':
                        case 't':
                                tp = va_arg(ap, Token *);
                                tp = va_arg(ap, Token *);
                                fprintf(stderr, "%.*s", tp->len, tp->t);
                                fprintf(stderr, "%.*s", tp->len, tp->t);
                                break;
                                break;
 
 
                        case 'r':
                        case 'r':
                                trp = va_arg(ap, Tokenrow *);
                                trp = va_arg(ap, Tokenrow *);
                                for (tp=trp->tp; tp<trp->lp&&tp->type!=NL; tp++) {
                                for (tp=trp->tp; tp<trp->lp&&tp->type!=NL; tp++) {
                                        if (tp>trp->tp && tp->wslen)
                                        if (tp>trp->tp && tp->wslen)
                                                fputc(' ', stderr);
                                                fputc(' ', stderr);
                                        fprintf(stderr, "%.*s", tp->len, tp->t);
                                        fprintf(stderr, "%.*s", tp->len, tp->t);
                                }
                                }
                                break;
                                break;
 
 
                        default:
                        default:
                                fputc(*ep, stderr);
                                fputc(*ep, stderr);
                                break;
                                break;
                        }
                        }
                } else
                } else
                        fputc(*ep, stderr);
                        fputc(*ep, stderr);
        }
        }
        va_end(ap);
        va_end(ap);
        fputc('\n', stderr);
        fputc('\n', stderr);
        if (type==FATAL)
        if (type==FATAL)
                exit(1);
                exit(1);
        if (type!=WARNING)
        if (type!=WARNING)
                nerrs = 1;
                nerrs = 1;
        fflush(stderr);
        fflush(stderr);
}
}
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.