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

Subversion Repositories eco32

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

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 "cpp.h"
#include "cpp.h"
 
 
/*
/*
 * do a macro definition.  tp points to the name being defined in the line
 * do a macro definition.  tp points to the name being defined in the line
 */
 */
void
void
dodefine(Tokenrow *trp)
dodefine(Tokenrow *trp)
{
{
        Token *tp;
        Token *tp;
        Nlist *np;
        Nlist *np;
        Tokenrow *def, *args;
        Tokenrow *def, *args;
 
 
        tp = trp->tp+1;
        tp = trp->tp+1;
        if (tp>=trp->lp || tp->type!=NAME) {
        if (tp>=trp->lp || tp->type!=NAME) {
                error(ERROR, "#defined token is not a name");
                error(ERROR, "#defined token is not a name");
                return;
                return;
        }
        }
        np = lookup(tp, 1);
        np = lookup(tp, 1);
        if (np->flag&ISUNCHANGE) {
        if (np->flag&ISUNCHANGE) {
                error(ERROR, "#defined token %t can't be redefined", tp);
                error(ERROR, "#defined token %t can't be redefined", tp);
                return;
                return;
        }
        }
        /* collect arguments */
        /* collect arguments */
        tp += 1;
        tp += 1;
        args = NULL;
        args = NULL;
        if (tp<trp->lp && tp->type==LP && tp->wslen==0) {
        if (tp<trp->lp && tp->type==LP && tp->wslen==0) {
                /* macro with args */
                /* macro with args */
                int narg = 0;
                int narg = 0;
                tp += 1;
                tp += 1;
                args = new(Tokenrow);
                args = new(Tokenrow);
                maketokenrow(2, args);
                maketokenrow(2, args);
                if (tp->type!=RP) {
                if (tp->type!=RP) {
                        int err = 0;
                        int err = 0;
                        for (;;) {
                        for (;;) {
                                Token *atp;
                                Token *atp;
                                if (tp->type!=NAME) {
                                if (tp->type!=NAME) {
                                        err++;
                                        err++;
                                        break;
                                        break;
                                }
                                }
                                if (narg>=args->max)
                                if (narg>=args->max)
                                        growtokenrow(args);
                                        growtokenrow(args);
                                for (atp=args->bp; atp<args->lp; atp++)
                                for (atp=args->bp; atp<args->lp; atp++)
                                        if (atp->len==tp->len
                                        if (atp->len==tp->len
                                         && strncmp((char*)atp->t, (char*)tp->t, tp->len)==0)
                                         && strncmp((char*)atp->t, (char*)tp->t, tp->len)==0)
                                                error(ERROR, "Duplicate macro argument");
                                                error(ERROR, "Duplicate macro argument");
                                *args->lp++ = *tp;
                                *args->lp++ = *tp;
                                narg++;
                                narg++;
                                tp += 1;
                                tp += 1;
                                if (tp->type==RP)
                                if (tp->type==RP)
                                        break;
                                        break;
                                if (tp->type!=COMMA) {
                                if (tp->type!=COMMA) {
                                        err++;
                                        err++;
                                        break;
                                        break;
                                }
                                }
                                tp += 1;
                                tp += 1;
                        }
                        }
                        if (err) {
                        if (err) {
                                error(ERROR, "Syntax error in macro parameters");
                                error(ERROR, "Syntax error in macro parameters");
                                return;
                                return;
                        }
                        }
                }
                }
                tp += 1;
                tp += 1;
        }
        }
        trp->tp = tp;
        trp->tp = tp;
        if (((trp->lp)-1)->type==NL)
        if (((trp->lp)-1)->type==NL)
                trp->lp -= 1;
                trp->lp -= 1;
        def = normtokenrow(trp);
        def = normtokenrow(trp);
        if (np->flag&ISDEFINED) {
        if (np->flag&ISDEFINED) {
                if (comparetokens(def, np->vp)
                if (comparetokens(def, np->vp)
                 || (np->ap==NULL) != (args==NULL)
                 || (np->ap==NULL) != (args==NULL)
                 || np->ap && comparetokens(args, np->ap))
                 || np->ap && comparetokens(args, np->ap))
                        error(ERROR, "Macro redefinition of %t", trp->bp+2);
                        error(ERROR, "Macro redefinition of %t", trp->bp+2);
        }
        }
        if (args) {
        if (args) {
                Tokenrow *tap;
                Tokenrow *tap;
                tap = normtokenrow(args);
                tap = normtokenrow(args);
                dofree(args->bp);
                dofree(args->bp);
                args = tap;
                args = tap;
        }
        }
        np->ap = args;
        np->ap = args;
        np->vp = def;
        np->vp = def;
        np->flag |= ISDEFINED;
        np->flag |= ISDEFINED;
}
}
 
 
/*
/*
 * Definition received via -D or -U
 * Definition received via -D or -U
 */
 */
void
void
doadefine(Tokenrow *trp, int type)
doadefine(Tokenrow *trp, int type)
{
{
        Nlist *np;
        Nlist *np;
        static unsigned char one[] = "1";
        static unsigned char one[] = "1";
        static Token onetoken[1] = {{ NUMBER, 0, 0, 0, 1, one }};
        static Token onetoken[1] = {{ NUMBER, 0, 0, 0, 1, one }};
        static Tokenrow onetr = { onetoken, onetoken, onetoken+1, 1 };
        static Tokenrow onetr = { onetoken, onetoken, onetoken+1, 1 };
 
 
        trp->tp = trp->bp;
        trp->tp = trp->bp;
        if (type=='U') {
        if (type=='U') {
                if (trp->lp-trp->tp != 2 || trp->tp->type!=NAME)
                if (trp->lp-trp->tp != 2 || trp->tp->type!=NAME)
                        goto syntax;
                        goto syntax;
                if ((np = lookup(trp->tp, 0)) == NULL)
                if ((np = lookup(trp->tp, 0)) == NULL)
                        return;
                        return;
                np->flag &= ~ISDEFINED;
                np->flag &= ~ISDEFINED;
                return;
                return;
        }
        }
        if (trp->tp >= trp->lp || trp->tp->type!=NAME)
        if (trp->tp >= trp->lp || trp->tp->type!=NAME)
                goto syntax;
                goto syntax;
        np = lookup(trp->tp, 1);
        np = lookup(trp->tp, 1);
        np->flag |= ISDEFINED;
        np->flag |= ISDEFINED;
        trp->tp += 1;
        trp->tp += 1;
        if (trp->tp >= trp->lp || trp->tp->type==END) {
        if (trp->tp >= trp->lp || trp->tp->type==END) {
                np->vp = &onetr;
                np->vp = &onetr;
                return;
                return;
        }
        }
        if (trp->tp->type!=ASGN)
        if (trp->tp->type!=ASGN)
                goto syntax;
                goto syntax;
        trp->tp += 1;
        trp->tp += 1;
        if ((trp->lp-1)->type == END)
        if ((trp->lp-1)->type == END)
                trp->lp -= 1;
                trp->lp -= 1;
        np->vp = normtokenrow(trp);
        np->vp = normtokenrow(trp);
        return;
        return;
syntax:
syntax:
        error(FATAL, "Illegal -D or -U argument %r", trp);
        error(FATAL, "Illegal -D or -U argument %r", trp);
}
}
 
 
/*
/*
 * Do macro expansion in a row of tokens.
 * Do macro expansion in a row of tokens.
 * Flag is NULL if more input can be gathered.
 * Flag is NULL if more input can be gathered.
 */
 */
void
void
expandrow(Tokenrow *trp, char *flag)
expandrow(Tokenrow *trp, char *flag)
{
{
        Token *tp;
        Token *tp;
        Nlist *np;
        Nlist *np;
 
 
        if (flag)
        if (flag)
                setsource(flag, NULL, "");
                setsource(flag, NULL, "");
        for (tp = trp->tp; tp<trp->lp; ) {
        for (tp = trp->tp; tp<trp->lp; ) {
                if (tp->type!=NAME
                if (tp->type!=NAME
                 || quicklook(tp->t[0], tp->len>1?tp->t[1]:0)==0
                 || quicklook(tp->t[0], tp->len>1?tp->t[1]:0)==0
                 || (np = lookup(tp, 0))==NULL
                 || (np = lookup(tp, 0))==NULL
                 || (np->flag&(ISDEFINED|ISMAC))==0
                 || (np->flag&(ISDEFINED|ISMAC))==0
                 || tp->hideset && checkhideset(tp->hideset, np)) {
                 || tp->hideset && checkhideset(tp->hideset, np)) {
                        tp++;
                        tp++;
                        continue;
                        continue;
                }
                }
                trp->tp = tp;
                trp->tp = tp;
                if (np->val==KDEFINED) {
                if (np->val==KDEFINED) {
                        tp->type = DEFINED;
                        tp->type = DEFINED;
                        if ((tp+1)<trp->lp && (tp+1)->type==NAME)
                        if ((tp+1)<trp->lp && (tp+1)->type==NAME)
                                (tp+1)->type = NAME1;
                                (tp+1)->type = NAME1;
                        else if ((tp+3)<trp->lp && (tp+1)->type==LP
                        else if ((tp+3)<trp->lp && (tp+1)->type==LP
                         && (tp+2)->type==NAME && (tp+3)->type==RP)
                         && (tp+2)->type==NAME && (tp+3)->type==RP)
                                (tp+2)->type = NAME1;
                                (tp+2)->type = NAME1;
                        else
                        else
                                error(ERROR, "Incorrect syntax for `defined'");
                                error(ERROR, "Incorrect syntax for `defined'");
                        tp++;
                        tp++;
                        continue;
                        continue;
                }
                }
                if (np->flag&ISMAC)
                if (np->flag&ISMAC)
                        builtin(trp, np->val);
                        builtin(trp, np->val);
                else {
                else {
                        expand(trp, np);
                        expand(trp, np);
                }
                }
                tp = trp->tp;
                tp = trp->tp;
        }
        }
        if (flag)
        if (flag)
                unsetsource();
                unsetsource();
}
}
 
 
/*
/*
 * Expand the macro whose name is np, at token trp->tp, in the tokenrow.
 * Expand the macro whose name is np, at token trp->tp, in the tokenrow.
 * Return trp->tp at the first token next to be expanded
 * Return trp->tp at the first token next to be expanded
 * (ordinarily the beginning of the expansion)
 * (ordinarily the beginning of the expansion)
 */
 */
void
void
expand(Tokenrow *trp, Nlist *np)
expand(Tokenrow *trp, Nlist *np)
{
{
        Tokenrow ntr;
        Tokenrow ntr;
        int ntokc, narg, i;
        int ntokc, narg, i;
        Token *tp;
        Token *tp;
        Tokenrow *atr[NARG+1];
        Tokenrow *atr[NARG+1];
        int hs;
        int hs;
 
 
        copytokenrow(&ntr, np->vp);             /* copy macro value */
        copytokenrow(&ntr, np->vp);             /* copy macro value */
        if (np->ap==NULL)                       /* parameterless */
        if (np->ap==NULL)                       /* parameterless */
                ntokc = 1;
                ntokc = 1;
        else {
        else {
                ntokc = gatherargs(trp, atr, &narg);
                ntokc = gatherargs(trp, atr, &narg);
                if (narg<0) {                    /* not actually a call (no '(') */
                if (narg<0) {                    /* not actually a call (no '(') */
                        /* gatherargs has already pushed trp->tr to the next token */
                        /* gatherargs has already pushed trp->tr to the next token */
                        return;
                        return;
                }
                }
                if (narg != rowlen(np->ap)) {
                if (narg != rowlen(np->ap)) {
                        error(ERROR, "Disagreement in number of macro arguments");
                        error(ERROR, "Disagreement in number of macro arguments");
                        trp->tp->hideset = newhideset(trp->tp->hideset, np);
                        trp->tp->hideset = newhideset(trp->tp->hideset, np);
                        trp->tp += ntokc;
                        trp->tp += ntokc;
                        return;
                        return;
                }
                }
                substargs(np, &ntr, atr);       /* put args into replacement */
                substargs(np, &ntr, atr);       /* put args into replacement */
                for (i=0; i<narg; i++) {
                for (i=0; i<narg; i++) {
                        dofree(atr[i]->bp);
                        dofree(atr[i]->bp);
                        dofree(atr[i]);
                        dofree(atr[i]);
                }
                }
        }
        }
        doconcat(&ntr);                         /* execute ## operators */
        doconcat(&ntr);                         /* execute ## operators */
        hs = newhideset(trp->tp->hideset, np);
        hs = newhideset(trp->tp->hideset, np);
        for (tp=ntr.bp; tp<ntr.lp; tp++) {      /* distribute hidesets */
        for (tp=ntr.bp; tp<ntr.lp; tp++) {      /* distribute hidesets */
                if (tp->type==NAME) {
                if (tp->type==NAME) {
                        if (tp->hideset==0)
                        if (tp->hideset==0)
                                tp->hideset = hs;
                                tp->hideset = hs;
                        else
                        else
                                tp->hideset = unionhideset(tp->hideset, hs);
                                tp->hideset = unionhideset(tp->hideset, hs);
                }
                }
        }
        }
        ntr.tp = ntr.bp;
        ntr.tp = ntr.bp;
        insertrow(trp, ntokc, &ntr);
        insertrow(trp, ntokc, &ntr);
        trp->tp -= rowlen(&ntr);
        trp->tp -= rowlen(&ntr);
        dofree(ntr.bp);
        dofree(ntr.bp);
        return;
        return;
}
}
 
 
/*
/*
 * Gather an arglist, starting in trp with tp pointing at the macro name.
 * Gather an arglist, starting in trp with tp pointing at the macro name.
 * Return total number of tokens passed, stash number of args found.
 * Return total number of tokens passed, stash number of args found.
 * trp->tp is not changed relative to the tokenrow.
 * trp->tp is not changed relative to the tokenrow.
 */
 */
int
int
gatherargs(Tokenrow *trp, Tokenrow **atr, int *narg)
gatherargs(Tokenrow *trp, Tokenrow **atr, int *narg)
{
{
        int parens = 1;
        int parens = 1;
        int ntok = 0;
        int ntok = 0;
        Token *bp, *lp;
        Token *bp, *lp;
        Tokenrow ttr;
        Tokenrow ttr;
        int ntokp;
        int ntokp;
        int needspace;
        int needspace;
 
 
        *narg = -1;                     /* means that there is no macro call */
        *narg = -1;                     /* means that there is no macro call */
        /* look for the ( */
        /* look for the ( */
        for (;;) {
        for (;;) {
                trp->tp++;
                trp->tp++;
                ntok++;
                ntok++;
                if (trp->tp >= trp->lp) {
                if (trp->tp >= trp->lp) {
                        gettokens(trp, 0);
                        gettokens(trp, 0);
                        if ((trp->lp-1)->type==END) {
                        if ((trp->lp-1)->type==END) {
                                trp->lp -= 1;
                                trp->lp -= 1;
                                trp->tp -= ntok;
                                trp->tp -= ntok;
                                return ntok;
                                return ntok;
                        }
                        }
                }
                }
                if (trp->tp->type==LP)
                if (trp->tp->type==LP)
                        break;
                        break;
                if (trp->tp->type!=NL)
                if (trp->tp->type!=NL)
                        return ntok;
                        return ntok;
        }
        }
        *narg = 0;
        *narg = 0;
        ntok++;
        ntok++;
        ntokp = ntok;
        ntokp = ntok;
        trp->tp++;
        trp->tp++;
        /* search for the terminating ), possibly extending the row */
        /* search for the terminating ), possibly extending the row */
        needspace = 0;
        needspace = 0;
        while (parens>0) {
        while (parens>0) {
                if (trp->tp >= trp->lp)
                if (trp->tp >= trp->lp)
                        gettokens(trp, 0);
                        gettokens(trp, 0);
                if (needspace) {
                if (needspace) {
                        needspace = 0;
                        needspace = 0;
                        makespace(trp);
                        makespace(trp);
                }
                }
                if (trp->tp->type==END) {
                if (trp->tp->type==END) {
                        trp->lp -= 1;
                        trp->lp -= 1;
                        trp->tp -= ntok;
                        trp->tp -= ntok;
                        error(ERROR, "EOF in macro arglist");
                        error(ERROR, "EOF in macro arglist");
                        return ntok;
                        return ntok;
                }
                }
                if (trp->tp->type==NL) {
                if (trp->tp->type==NL) {
                        trp->tp += 1;
                        trp->tp += 1;
                        adjustrow(trp, -1);
                        adjustrow(trp, -1);
                        trp->tp -= 1;
                        trp->tp -= 1;
                        makespace(trp);
                        makespace(trp);
                        needspace = 1;
                        needspace = 1;
                        continue;
                        continue;
                }
                }
                if (trp->tp->type==LP)
                if (trp->tp->type==LP)
                        parens++;
                        parens++;
                else if (trp->tp->type==RP)
                else if (trp->tp->type==RP)
                        parens--;
                        parens--;
                trp->tp++;
                trp->tp++;
                ntok++;
                ntok++;
        }
        }
        trp->tp -= ntok;
        trp->tp -= ntok;
        /* Now trp->tp won't move underneath us */
        /* Now trp->tp won't move underneath us */
        lp = bp = trp->tp+ntokp;
        lp = bp = trp->tp+ntokp;
        for (; parens>=0; lp++) {
        for (; parens>=0; lp++) {
                if (lp->type == LP) {
                if (lp->type == LP) {
                        parens++;
                        parens++;
                        continue;
                        continue;
                }
                }
                if (lp->type==RP)
                if (lp->type==RP)
                        parens--;
                        parens--;
                if (lp->type==DSHARP)
                if (lp->type==DSHARP)
                        lp->type = DSHARP1;     /* ## not special in arg */
                        lp->type = DSHARP1;     /* ## not special in arg */
                if (lp->type==COMMA && parens==0 || parens<0 && (lp-1)->type!=LP) {
                if (lp->type==COMMA && parens==0 || parens<0 && (lp-1)->type!=LP) {
                        if (*narg>=NARG-1)
                        if (*narg>=NARG-1)
                                error(FATAL, "Sorry, too many macro arguments");
                                error(FATAL, "Sorry, too many macro arguments");
                        ttr.bp = ttr.tp = bp;
                        ttr.bp = ttr.tp = bp;
                        ttr.lp = lp;
                        ttr.lp = lp;
                        atr[(*narg)++] = normtokenrow(&ttr);
                        atr[(*narg)++] = normtokenrow(&ttr);
                        bp = lp+1;
                        bp = lp+1;
                }
                }
        }
        }
        return ntok;
        return ntok;
}
}
 
 
/*
/*
 * substitute the argument list into the replacement string
 * substitute the argument list into the replacement string
 *  This would be simple except for ## and #
 *  This would be simple except for ## and #
 */
 */
void
void
substargs(Nlist *np, Tokenrow *rtr, Tokenrow **atr)
substargs(Nlist *np, Tokenrow *rtr, Tokenrow **atr)
{
{
        Tokenrow tatr;
        Tokenrow tatr;
        Token *tp;
        Token *tp;
        int ntok, argno;
        int ntok, argno;
 
 
        for (rtr->tp=rtr->bp; rtr->tp<rtr->lp; ) {
        for (rtr->tp=rtr->bp; rtr->tp<rtr->lp; ) {
                if (rtr->tp->type==SHARP) {     /* string operator */
                if (rtr->tp->type==SHARP) {     /* string operator */
                        tp = rtr->tp;
                        tp = rtr->tp;
                        rtr->tp += 1;
                        rtr->tp += 1;
                        if ((argno = lookuparg(np, rtr->tp))<0) {
                        if ((argno = lookuparg(np, rtr->tp))<0) {
                                error(ERROR, "# not followed by macro parameter");
                                error(ERROR, "# not followed by macro parameter");
                                continue;
                                continue;
                        }
                        }
                        ntok = 1 + (rtr->tp - tp);
                        ntok = 1 + (rtr->tp - tp);
                        rtr->tp = tp;
                        rtr->tp = tp;
                        insertrow(rtr, ntok, stringify(atr[argno]));
                        insertrow(rtr, ntok, stringify(atr[argno]));
                        continue;
                        continue;
                }
                }
                if (rtr->tp->type==NAME
                if (rtr->tp->type==NAME
                 && (argno = lookuparg(np, rtr->tp)) >= 0) {
                 && (argno = lookuparg(np, rtr->tp)) >= 0) {
                        if ((rtr->tp+1)->type==DSHARP
                        if ((rtr->tp+1)->type==DSHARP
                         || rtr->tp!=rtr->bp && (rtr->tp-1)->type==DSHARP)
                         || rtr->tp!=rtr->bp && (rtr->tp-1)->type==DSHARP)
                                insertrow(rtr, 1, atr[argno]);
                                insertrow(rtr, 1, atr[argno]);
                        else {
                        else {
                                copytokenrow(&tatr, atr[argno]);
                                copytokenrow(&tatr, atr[argno]);
                                expandrow(&tatr, "<macro>");
                                expandrow(&tatr, "<macro>");
                                insertrow(rtr, 1, &tatr);
                                insertrow(rtr, 1, &tatr);
                                dofree(tatr.bp);
                                dofree(tatr.bp);
                        }
                        }
                        continue;
                        continue;
                }
                }
                rtr->tp++;
                rtr->tp++;
        }
        }
}
}
 
 
/*
/*
 * Evaluate the ## operators in a tokenrow
 * Evaluate the ## operators in a tokenrow
 */
 */
void
void
doconcat(Tokenrow *trp)
doconcat(Tokenrow *trp)
{
{
        Token *ltp, *ntp;
        Token *ltp, *ntp;
        Tokenrow ntr;
        Tokenrow ntr;
        int len;
        int len;
 
 
        for (trp->tp=trp->bp; trp->tp<trp->lp; trp->tp++) {
        for (trp->tp=trp->bp; trp->tp<trp->lp; trp->tp++) {
                if (trp->tp->type==DSHARP1)
                if (trp->tp->type==DSHARP1)
                        trp->tp->type = DSHARP;
                        trp->tp->type = DSHARP;
                else if (trp->tp->type==DSHARP) {
                else if (trp->tp->type==DSHARP) {
                        char tt[128];
                        char tt[128];
                        ltp = trp->tp-1;
                        ltp = trp->tp-1;
                        ntp = trp->tp+1;
                        ntp = trp->tp+1;
                        if (ltp<trp->bp || ntp>=trp->lp) {
                        if (ltp<trp->bp || ntp>=trp->lp) {
                                error(ERROR, "## occurs at border of replacement");
                                error(ERROR, "## occurs at border of replacement");
                                continue;
                                continue;
                        }
                        }
                        len = ltp->len + ntp->len;
                        len = ltp->len + ntp->len;
                        strncpy((char*)tt, (char*)ltp->t, ltp->len);
                        strncpy((char*)tt, (char*)ltp->t, ltp->len);
                        strncpy((char*)tt+ltp->len, (char*)ntp->t, ntp->len);
                        strncpy((char*)tt+ltp->len, (char*)ntp->t, ntp->len);
                        tt[len] = '\0';
                        tt[len] = '\0';
                        setsource("<##>", NULL, tt);
                        setsource("<##>", NULL, tt);
                        maketokenrow(3, &ntr);
                        maketokenrow(3, &ntr);
                        gettokens(&ntr, 1);
                        gettokens(&ntr, 1);
                        unsetsource();
                        unsetsource();
                        if (ntr.lp-ntr.bp!=2 || ntr.bp->type==UNCLASS)
                        if (ntr.lp-ntr.bp!=2 || ntr.bp->type==UNCLASS)
                                error(WARNING, "Bad token %r produced by ##", &ntr);
                                error(WARNING, "Bad token %r produced by ##", &ntr);
                        ntr.lp = ntr.bp+1;
                        ntr.lp = ntr.bp+1;
                        trp->tp = ltp;
                        trp->tp = ltp;
                        makespace(&ntr);
                        makespace(&ntr);
                        insertrow(trp, (ntp-ltp)+1, &ntr);
                        insertrow(trp, (ntp-ltp)+1, &ntr);
                        dofree(ntr.bp);
                        dofree(ntr.bp);
                        trp->tp--;
                        trp->tp--;
                }
                }
        }
        }
}
}
 
 
/*
/*
 * tp is a potential parameter name of macro mac;
 * tp is a potential parameter name of macro mac;
 * look it up in mac's arglist, and if found, return the
 * look it up in mac's arglist, and if found, return the
 * corresponding index in the argname array.  Return -1 if not found.
 * corresponding index in the argname array.  Return -1 if not found.
 */
 */
int
int
lookuparg(Nlist *mac, Token *tp)
lookuparg(Nlist *mac, Token *tp)
{
{
        Token *ap;
        Token *ap;
 
 
        if (tp->type!=NAME || mac->ap==NULL)
        if (tp->type!=NAME || mac->ap==NULL)
                return -1;
                return -1;
        for (ap=mac->ap->bp; ap<mac->ap->lp; ap++) {
        for (ap=mac->ap->bp; ap<mac->ap->lp; ap++) {
                if (ap->len==tp->len && strncmp((char*)ap->t,(char*)tp->t,ap->len)==0)
                if (ap->len==tp->len && strncmp((char*)ap->t,(char*)tp->t,ap->len)==0)
                        return ap - mac->ap->bp;
                        return ap - mac->ap->bp;
        }
        }
        return -1;
        return -1;
}
}
 
 
/*
/*
 * Return a quoted version of the tokenrow (from # arg)
 * Return a quoted version of the tokenrow (from # arg)
 */
 */
#define STRLEN  512
#define STRLEN  512
Tokenrow *
Tokenrow *
stringify(Tokenrow *vp)
stringify(Tokenrow *vp)
{
{
        static Token t = { STRING };
        static Token t = { STRING };
        static Tokenrow tr = { &t, &t, &t+1, 1 };
        static Tokenrow tr = { &t, &t, &t+1, 1 };
        Token *tp;
        Token *tp;
        uchar s[STRLEN];
        uchar s[STRLEN];
        uchar *sp = s, *cp;
        uchar *sp = s, *cp;
        int i, instring;
        int i, instring;
 
 
        *sp++ = '"';
        *sp++ = '"';
        for (tp = vp->bp; tp < vp->lp; tp++) {
        for (tp = vp->bp; tp < vp->lp; tp++) {
                instring = tp->type==STRING || tp->type==CCON;
                instring = tp->type==STRING || tp->type==CCON;
                if (sp+2*tp->len >= &s[STRLEN-10]) {
                if (sp+2*tp->len >= &s[STRLEN-10]) {
                        error(ERROR, "Stringified macro arg is too long");
                        error(ERROR, "Stringified macro arg is too long");
                        break;
                        break;
                }
                }
                if (tp->wslen && (tp->flag&XPWS)==0)
                if (tp->wslen && (tp->flag&XPWS)==0)
                        *sp++ = ' ';
                        *sp++ = ' ';
                for (i=0, cp=tp->t; i<tp->len; i++) {
                for (i=0, cp=tp->t; i<tp->len; i++) {
                        if (instring && (*cp=='"' || *cp=='\\'))
                        if (instring && (*cp=='"' || *cp=='\\'))
                                *sp++ = '\\';
                                *sp++ = '\\';
                        *sp++ = *cp++;
                        *sp++ = *cp++;
                }
                }
        }
        }
        *sp++ = '"';
        *sp++ = '"';
        *sp = '\0';
        *sp = '\0';
        sp = s;
        sp = s;
        t.len = strlen((char*)sp);
        t.len = strlen((char*)sp);
        t.t = newstring(sp, t.len, 0);
        t.t = newstring(sp, t.len, 0);
        return &tr;
        return &tr;
}
}
 
 
/*
/*
 * expand a builtin name
 * expand a builtin name
 */
 */
void
void
builtin(Tokenrow *trp, int biname)
builtin(Tokenrow *trp, int biname)
{
{
        char *op;
        char *op;
        Token *tp;
        Token *tp;
        Source *s;
        Source *s;
 
 
        tp = trp->tp;
        tp = trp->tp;
        trp->tp++;
        trp->tp++;
        /* need to find the real source */
        /* need to find the real source */
        s = cursource;
        s = cursource;
        while (s && s->fd==NULL)
        while (s && s->fd==NULL)
                s = s->next;
                s = s->next;
        if (s==NULL)
        if (s==NULL)
                s = cursource;
                s = cursource;
        /* most are strings */
        /* most are strings */
        tp->type = STRING;
        tp->type = STRING;
        if (tp->wslen) {
        if (tp->wslen) {
                *outp++ = ' ';
                *outp++ = ' ';
                tp->wslen = 1;
                tp->wslen = 1;
        }
        }
        op = outp;
        op = outp;
        *op++ = '"';
        *op++ = '"';
        switch (biname) {
        switch (biname) {
 
 
        case KLINENO:
        case KLINENO:
                tp->type = NUMBER;
                tp->type = NUMBER;
                op = outnum(op-1, s->line);
                op = outnum(op-1, s->line);
                break;
                break;
 
 
        case KFILE: {
        case KFILE: {
                char *src = s->filename;
                char *src = s->filename;
                while ((*op++ = *src++) != 0)
                while ((*op++ = *src++) != 0)
                        if (src[-1] == '\\')
                        if (src[-1] == '\\')
                                *op++ = '\\';
                                *op++ = '\\';
                op--;
                op--;
                break;
                break;
                }
                }
 
 
        case KDATE:
        case KDATE:
                strncpy(op, curtime+4, 7);
                strncpy(op, curtime+4, 7);
                strncpy(op+7, curtime+20, 4);
                strncpy(op+7, curtime+20, 4);
                op += 11;
                op += 11;
                break;
                break;
 
 
        case KTIME:
        case KTIME:
                strncpy(op, curtime+11, 8);
                strncpy(op, curtime+11, 8);
                op += 8;
                op += 8;
                break;
                break;
 
 
        default:
        default:
                error(ERROR, "cpp botch: unknown internal macro");
                error(ERROR, "cpp botch: unknown internal macro");
                return;
                return;
        }
        }
        if (tp->type==STRING)
        if (tp->type==STRING)
                *op++ = '"';
                *op++ = '"';
        tp->t = (uchar*)outp;
        tp->t = (uchar*)outp;
        tp->len = op - outp;
        tp->len = op - outp;
        outp = op;
        outp = op;
}
}
 
 

powered by: WebSVN 2.1.0

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