⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 preproc.c

📁 CC386 is a general-purpose 32-bit C compiler. It is not an optimizing compiler but given that the co
💻 C
📖 第 1 页 / 共 4 页
字号:

//-------------------------------------------------------------------------

void filemac(short *string)
{
    short *p = string;
    char *q = fullqualify(infile);
    *p++ = '"' ;
    while(*q)
    {
        if (*q == '\\')
            *p++ = *q ;
        *p++ = *q++;
    }
    *p++ = '"';
    *p++ = 0;
}

//-------------------------------------------------------------------------

void datemac(short *string)
{
    char str1[40];
    struct tm *t1;
    time_t t2;
    time(&t2);
    t1 = localtime(&t2);
    strftime(str1, 40, "\"%b %d %Y\"", t1);
    cnvt(string, str1);
}

//-------------------------------------------------------------------------

void timemac(short *string)
{
    char str1[40];
    struct tm *t1;
    time_t t2;
    time(&t2);
    t1 = localtime(&t2);
    str1[0] = '"';
    strftime(str1, 40, "\"%X\"", t1);
    cnvt(string, str1);
}

//-------------------------------------------------------------------------

void linemac(short *string)
{
    char str1[40];
    sprintf(str1, "%d", lineno);
    cnvt(string, str1);
} 
/* Scan for default macros and replace them */
void defmacroreplace(short *macro, short *name)
{
    int i;
    macro[0] = 0;
    for (i = 0; i < INGROWNMACROS; i++)
    if (!strcmp(name, ingrownmacros[i].s))
    {
        (ingrownmacros[i].func)(macro);
        break;
    }
}


//-------------------------------------------------------------------------

int replacesegment(short *start, short *end, int *inbuffer, int totallen, short **pptr)
{
    short *args[100];
    short macro[MACRO_REPLACE_SIZE],varargs[500];
    char ascii[256];
    short name[256];
    int waiting = FALSE, rv;
    int size = 0;
    short *p,  *q, *r;
    SYM *sp;
    int insize,rv1;
    short * orig_end = end ;
    p = start;
    while (p < end)
    {
        q = p;
        if (!waiting && (*p == '"' 
            ||  *p == '\'') && (*(p - 1) != '\\' || *(p - 2) == '\\'))
        {
            waiting =  *p;
            p++;
        }
        else if (waiting)
        {
            if (*p == waiting && (*(p - 1) != '\\' || *(p - 2) == '\\'))
                waiting = 0;
            p++;
        }
        else if (isstartchar(*p))
        {
            defid(name, &p, ascii);
            if ((sp = basesearch(ascii, &defsyms, 0)) != 0 && !indefine(sp))
            {
                DEFSTRUCT *def = sp->value.s;
                if (def->argcount)
                {
                    int count = 0;
                    short *q = p;
                    
                    varargs[0] = 0;
                    
                    while (isspace(*q)) q++ ;
                    if (*(q - 1) == '\n')
                    {
                        return INT_MIN + 1;
                    }
                    if (*q++ != '(')
                    {
                        goto join;
                    }
                    p = q;
                    if (def->argcount > 1)
                    {
                        do
                        {
                            short *nm = macro;
                            int nestedparen = 0, nestedstring = 0;
                            insize = 0;
                            while (isspace(*p)) p++;
                            while (*p && (((*p != ',' &&  *p != ')') ||
                                nestedparen || nestedstring) &&  *p != '\n'))
                            {
                                if (*p == '(')nestedparen++;
                                if (*p == ')' && nestedparen)
                                    nestedparen--;
                                if (nestedstring)
                                {
                                    if (*p == nestedstring && (*(p - 1) != '\\'
                                        || *(p - 2) == '\\'))
                                        nestedstring = 0;
                                }
                                else if ((*p == '\'' ||  *p == '"') 
                                    && (*(p - 1) != '\\' || *(p - 2) == '\\'))
                                    nestedstring =  *p;
                                *nm++ =  *p++;
                            }
                            while (nm != macro && isspace(nm[-1])) 
                            {
                                nm--;
                            }
                            *nm = 0;
                            size = pstrlen(macro) ;
                            rv = replacesegment(macro, macro + size, &insize, totallen,0);
                            if (rv <-MACRO_REPLACE_SIZE) {
                                return rv;
                            }
                            macro[rv+size] = 0;
                            args[count++] = plitlate(macro);
                        }
                        while (*p && *p++ == ',' && count != def->argcount-1)
                            ;
                    }
                    else 
                    {
                        count = def->argcount - 1;
                        while (isspace(*p)) p++;
                        if (*p == ')')
                            p++;
                    }
                    if (*(p - 1) != ')' || count != def->argcount - 1)
                    {
                        if (count == def->argcount-1 && prm_c99 && (def->flags & DS_VARARGS)) 
                        {
                            short *q = varargs ;
                            int nestedparen=0;
                            if (!(def->flags & DS_VARARGS))
                                return INT_MIN;
                            while (*p != '\n' && *p && (*p != ')' || nestedparen)) {
                                if (*p == '(')nestedparen++;
                                if (*p == ')' && nestedparen)
                                    nestedparen--;
                                *q++ = *p++;
                            }   
                            *q = 0 ;
                            p++ ;
                            count = def->argcount - 1;
                        }
                        if (*(p - 1) != ')' || count != def->argcount - 1)
                        {
                            if (*(p - 1) == '\n')
                                return  INT_MIN + 1;
                            gensymerror(ERR_WRONGMACROARGS, ascii);
                            return  INT_MIN;
                        }
                    }
                    else if (def->flags & DS_VARARGS)
                    {
                        gensymerror(ERR_WRONGMACROARGS, ascii);
                    }
                    pstrcpy(macro,def->string);
                    if (count != 0 || varargs)
                        if (!defreplaceargs(macro, count, def->args, args, varargs)) {
                            return  INT_MIN;
                        }
                } else {
                    pstrcpy(macro,def->string);
                }
                deftokenizing(macro);
                if ((rv1 = definsert(start, p, q, macro, totallen -  *inbuffer,
                    p - q)) < -MACRO_REPLACE_SIZE)
                    return  rv1;
                insize = rv1 - (p - q);
                *inbuffer += insize;
                end += insize;
                p += insize;
                enterdefine(sp);
                rv = replacesegment(q, p, inbuffer, totallen, &p) ;
                exitdefine();
                if (rv <-MACRO_REPLACE_SIZE)
                    return rv;
                end += rv ;
            }
            else
            {
                join: defmacroreplace(macro, ascii);
                if (macro[0])
                {
                    if ((rv = definsert(start, p, q, macro, totallen -  *inbuffer, p -
                        q)) < - MACRO_REPLACE_SIZE)
                        return  rv;
                    end += rv - (p - q);
                    *inbuffer += rv - (p - q);
                    p += rv - (p-q); 
                }
            }
        }
        else
            p++;
    }
    if (pptr)
        *pptr = p ;
    return end - orig_end ;
}

/* Scan line for macros and do replacements */
int defcheck(short *line)
{
    short *p = line, *q = p;
    int inbuffer = pstrlen(line),rv;
    
    nodefines();
    return replacesegment(line, line + inbuffer, &inbuffer, MACRO_REPLACE_SIZE,0);
}

//-------------------------------------------------------------------------

static void repdefines(short *lptr)
/*
 * replace 'defined' keyword in #IF and #ELIF statements
 */
{
    short *q = lptr;
    short name[100];
    char ascii[200];
    while (*lptr)
    {
        if (!pstrncmp(lptr, defkw, 7))
        {
            int needend = FALSE;
            lptr += 7;
            while (isspace(*lptr))
                lptr++;
            if (*lptr == '(')
            {
                lptr++;
                needend = TRUE;
            }
            while (isspace(*lptr))
                lptr++;
            defid(name, &lptr, ascii);
            while (isspace(*lptr))
                lptr++;
            if (needend)
                if (*lptr == ')')
                    lptr++;
                else
                    expecttoken(closepa, 0);
            if (basesearch(ascii, &defsyms, 0) != 0)
                *q++ = '1';
            else
                *q++ = '0';
            *q++ = ' ';

        }
        else
        {
            *q++ =  *lptr++;
        }
    }
    *q = 0;
}

//-------------------------------------------------------------------------

void pushif(void)
/* Push an if context */
{
    IFSTRUCT *p;
    global_flag++;
    p = xalloc(sizeof(IFSTRUCT));
    global_flag--;
    p->link = ifs;
    p->iflevel = ifskip;
    p->elsetaken = elsetaken;
    elsetaken = FALSE;
    ifs = p;
}

//-------------------------------------------------------------------------

void popif(void)
/* Pop an if context */
{
    if (ifs)
    {
        ifskip = ifs->iflevel;
        elsetaken = ifs->elsetaken;
        ifs = ifs->link;
    }
    else
    {
        ifskip = 0;
        elsetaken = 0;
    }
}

//-------------------------------------------------------------------------

void ansieol(void)
{
    if (prm_ansi)
    {
        while (isspace(*lptr))
            lptr++;
        if (*lptr)
        {
            lastch =  *lptr;
            lastst = kw_if;
            generror(ERR_UNEXPECT, 0, 0);
        }
    }
}

//-------------------------------------------------------------------------

int doifdef(int flag)
/* Handle IFDEF */
{
    SYM *sp;
    if (ifskip)
    {
        skiplevel++;
        return (incldepth == 0);
    }
    getch();
    while (isspace(lastch))
        getch();
    if (!isstartchar(lastch))
    {
        generror(ERR_IDEXPECT, 0, 0);
        return incldepth == 0;
    }
    else
        getid();
    sp = basesearch(unmangid, &defsyms, 0);
    pushif();
    if (sp && !flag || !sp && flag)
        ifskip = TRUE;
    ansieol();
    return (incldepth == 0);
}

//-------------------------------------------------------------------------

int doif(int flag)
/* Handle #if */
{
    if (ifskip)
    {
        skiplevel++;
        return (incldepth == 0);
    }
    cantnewline = TRUE;
    getsym();
    pushif();
    if (!intexpr(0))
        ifskip = TRUE;
    else
        elsetaken = TRUE;
    cantnewline = FALSE;
    ansieol();
    return (incldepth == 0);
}

//-------------------------------------------------------------------------

int doelif(void)
/* Handle #elif */
{
    int is;
    if (skiplevel)
    {
        return (incldepth == 0);
    }

    cantnewline = TRUE;
    getsym();
    is = !intexpr(0);
    cantnewline = FALSE;
    if (!elsetaken)
    {
        if (ifs)
        {
            if (!ifs->iflevel)
                ifskip = !ifskip || is || elsetaken;
            if (!ifskip)
                elsetaken = TRUE;
        }
        else
            generror(ERR_PREPROCMATCH, 0, 0);
    }
    else
        ifskip = TRUE;
    ansieol();
    return (incldepth == 0);
}

/* handle else */
int doelse(void)
{
    if (skiplevel)
    {
        return (incldepth == 0);
    }
    if (ifs)
    {
        if (!ifs->iflevel)
            ifskip = !ifskip || elsetaken;
    }
    else
        generror(ERR_PREPROCMATCH, 0, 0);
    ansieol();
    return (incldepth == 0);
}

/* HAndle endif */
int doendif(void)
{
    if (skiplevel)
    {
        skiplevel--;
        return (incldepth == 0);
    }
    if (!ifs)
        generror(ERR_PREPROCMATCH, 0, 0);
    popif();
    ansieol();
    return (incldepth == 0);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -