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

📄 scanner.c

📁 cg编译器
💻 C
📖 第 1 页 / 共 3 页
字号:
        fprintf(Cg->options.listfd, "(%d) : error C%04d: ", loc->line, num);
    }
    va_start(args, mess);
    vfprintf(Cg->options.listfd, mess, args);
    va_end(args);
    fprintf(Cg->options.listfd, "\n");
    bumpErrorCount();
} // InternalError

/*
 * FatalError() - Fatal internal compiler error.
 *
 */

void FatalError(const char *mess, ...)
{
    va_list args;

    fprintf(Cg->options.listfd, "(%d) : fatal error C9999: ", Cg->lineCount);
    va_start(args, mess);
    vfprintf(Cg->options.listfd, mess, args);
    va_end(args);
    fprintf(Cg->options.listfd, "\n");
    bumpErrorCount();
    CloseOutputFiles("Compilation terminated due to fatal error");
    exit(9999);
} // InternalError

/*
 * SemanticWarning() - Compiler generated semantic warning.
 *
 */

void SemanticWarning(SourceLoc *loc, int num, const char *mess, ...)
{
    va_list args;

    if (!Cg->options.NoWarnings) {
        if (!Cg->options.ErrorMode) {
            if (loc->file) {
                fprintf(Cg->options.listfd, "%s(%d) : warning C%04d: ",
                        GetAtomString(atable, loc->file), loc->line, num);
            } else {
                fprintf(Cg->options.listfd, "(%d) : warning C%04d: ", loc->line, num);
            }
            va_start(args, mess);
            vfprintf(Cg->options.listfd, mess, args);
            va_end(args);
            fprintf(Cg->options.listfd, "\n");
        }
        bumpWarningCount();
    }
} // SemanticWarning

/*
 * InformationalNotice() - Print a message from the compiler.
 *
 */

void InformationalNotice(SourceLoc *loc, int num, const char *mess, ...)
{
    va_list args;

    if (!Cg->options.NoWarnings) {
        if (!Cg->options.ErrorMode) {
            if (loc->file) {
                fprintf(Cg->options.listfd, "%s(%d) : notice C%04d: ",
                        GetAtomString(atable, loc->file), loc->line, num);
            } else {
                fprintf(Cg->options.listfd, "(%d) : notice C%04d: ", loc->line, num);
            }
            va_start(args, mess);
            vfprintf(Cg->options.listfd, mess, args);
            va_end(args);
            fprintf(Cg->options.listfd, "\n");
        }
    }
} // InformationalNotice

// The scanner:

static int nextchar(FileInputSrc *in)
{
    int ch;

    if (in->save_cnt) {
        ch = in->save[--in->save_cnt];
    } else {
        ch = getc(in->fd);
        if (ch == EOF) {
            Cg->currentInput = in->base.prev;
            fclose(in->fd);
            free(in);
            return '\n';
        }
    }
    if (ch == '\n') {
        Cg->lineCount++;
        in->base.line++;
    }

    return ch;
} // nextchar

static void ungetchar(FileInputSrc *in, int ch)
{
    if (&in->base == Cg->currentInput) {
        if (in->save_cnt < sizeof(in->save))
            in->save[in->save_cnt++] = ch;
        if (ch == '\n') {
            in->base.line--;
            Cg->lineCount--;
        }
    }

} // ungetchar

///////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////// Floating point constants: /////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////

/*
 * lBuildFloatValue() - Quick and dirty conversion to floating point.  Since all
 *         we need is single precision this should be quite precise.
 */

static float lBuildFloatValue(const char *str, int len, int exp)
{
    double val, expval, ten;
    int ii, llen, absexp;
    float rv;

    val = 0.0;
    llen = len;
    for (ii = 0; ii < len; ii++)
        val = val*10.0 + (str[ii] - '0');
    if (exp != 0) {
        absexp = exp > 0 ? exp : -exp;
        expval = 1.0f;
        ten = 10.0;
        while (absexp) {
            if (absexp & 1)
                expval *= ten;
            ten *= ten;
            absexp >>= 1;
        }
        if (exp >= 0) {
            val *= expval;
        } else {
            val /= expval;
        }
    }
    rv = (float)val;
    if (isinff(rv)) {
        SemanticError(Cg->tokenLoc, ERROR___FP_CONST_OVERFLOW);
    }
    return rv;
} // lBuildFloatValue

/*
 * lFloatConst() - Scan a floating point constant.  Assumes that the scanner
 *         has seen at least one digit, followed by either a decimal '.' or the
 *         letter 'e'.
 */

static int lFloatConst(char *str, int len, int ch)
{
    int HasDecimal, declen, exp, ExpSign;
    float lval;

    HasDecimal = 0;
    declen = 0;
    exp = 0;
    if (ch == '.') {
        HasDecimal = 1;
        ch = Cg->currentInput->getch(Cg->currentInput);
        while (ch >= '0' && ch <= '9') {
            if (len < MAX_SYMBOL_NAME_LEN) {
                declen++;
                if (len > 0 || ch != '0') {
                    str[len] = ch;
                    len++;
                }
                ch = Cg->currentInput->getch(Cg->currentInput);
            } else {
                SemanticError(Cg->tokenLoc, ERROR___FP_CONST_TOO_LONG);
                len = 1;
            }
        }
    }

    // Exponent:

    if (ch == 'e' || ch == 'E') {
        ExpSign = 1;
        ch = Cg->currentInput->getch(Cg->currentInput);
        if (ch == '+') {
            ch = Cg->currentInput->getch(Cg->currentInput);
        } else if (ch == '-') {
            ExpSign = -1;
            ch = Cg->currentInput->getch(Cg->currentInput);
        }
        if (ch >= '0' && ch <= '9') {
            while (ch >= '0' && ch <= '9') {
                exp = exp*10 + ch - '0';
                ch = Cg->currentInput->getch(Cg->currentInput);
            }
        } else {
            SemanticError(Cg->tokenLoc, ERROR___ERROR_IN_EXPONENT);
        }
        exp *= ExpSign;
    }

    if (len == 0) {
        lval = 0.0f;
    } else {
        lval = lBuildFloatValue(str, len, exp - declen);
    }

    // Suffix:

    yylval.sc_fval = lval;
    if (ch == 'h') {
        return FLOATHCONST_SY;
    } else {
        if (ch == 'x') {
            return FLOATXCONST_SY;
        } else {
            if (ch == 'f') {
                return FLOATCONST_SY;
            } else {
                Cg->currentInput->ungetch(Cg->currentInput, ch);
                return CFLOATCONST_SY;
            }
        }
    }
} // lFloatConst

///////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////// Normal Scanner //////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////

static int byte_scan(InputSrc *in)
{
    char symbol_name[MAX_SYMBOL_NAME_LEN + 1];
    char string_val[MAX_STRING_LEN + 1];
    int AlreadyComplained;
    int len, ch, ii, ival;

    for (;;) {
        yylval.sc_int = 0;
        ch = Cg->currentInput->getch(Cg->currentInput);
        while (ch == ' ' || ch == '\t' || ch == '\r') {
            yylval.sc_int = 1;
            ch = Cg->currentInput->getch(Cg->currentInput);
        }
        Cg->ltokenLoc.file = Cg->currentInput->name;
        Cg->ltokenLoc.line = Cg->currentInput->line;
        switch (ch) {
        default:
            return ch; // Single character token
        case EOF:
            return 0;
        case 'A': case 'B': case 'C': case 'D': case 'E':
        case 'F': case 'G': case 'H': case 'I': case 'J':
        case 'K': case 'L': case 'M': case 'N': case 'O':
        case 'P': case 'Q': case 'R': case 'S': case 'T':
        case 'U': case 'V': case 'W': case 'X': case 'Y':
        case 'Z': case '_':
        case 'a': case 'b': case 'c': case 'd': case 'e':
        case 'f': case 'g': case 'h': case 'i': case 'j':
        case 'k': case 'l': case 'm': case 'n': case 'o':
        case 'p': case 'q': case 'r': case 's': case 't':
        case 'u': case 'v': case 'w': case 'x': case 'y':
        case 'z':
            len = 0;
            do {
                if (len < MAX_SYMBOL_NAME_LEN) {
                    symbol_name[len] = ch;
                    len++;
                    ch = Cg->currentInput->getch(Cg->currentInput);
                }
            } while ((ch >= 'a' && ch <= 'z') ||
                     (ch >= 'A' && ch <= 'Z') ||
                     (ch >= '0' && ch <= '9') ||
                     ch == '_');
            symbol_name[len] = '\0';
            Cg->currentInput->ungetch(Cg->currentInput, ch);
            yylval.sc_ident = LookUpAddString(atable, symbol_name);
            return IDENT_SY;
            break;
        case '0':
            ch = Cg->currentInput->getch(Cg->currentInput);
            if (ch == 'x' || ch == 'X') {
                ch = Cg->currentInput->getch(Cg->currentInput);
                if ((ch >= '0' && ch <= '9') ||
                    (ch >= 'A' && ch <= 'F') ||
                    (ch >= 'a' && ch <= 'f'))
                {
                    AlreadyComplained = 0;
                    ival = 0;
                    do {
                        if (ival <= 0x0fffffff) {
                            if (ch >= '0' && ch <= '9') {
                                ii = ch - '0';
                            } else if (ch >= 'A' && ch <= 'F') {
                                ii = ch - 'A' + 10;
                            } else {
                                ii = ch - 'a' + 10;
                            }
                            ival = (ival << 4) | ii;
                        } else {
                            if (!AlreadyComplained)
                                SemanticError(Cg->tokenLoc, ERROR___HEX_CONST_OVERFLOW);
                            AlreadyComplained = 1;
                        }
                        ch = Cg->currentInput->getch(Cg->currentInput);
                    } while ((ch >= '0' && ch <= '9') ||
                             (ch >= 'A' && ch <= 'F') ||
                             (ch >= 'a' && ch <= 'f'));
                } else {
                    SemanticError(Cg->tokenLoc, ERROR___ERROR_IN_HEX_CONSTANT);
                }
                Cg->currentInput->ungetch(Cg->currentInput, ch);
                yylval.sc_int = ival;
                return INTCONST_SY;

⌨️ 快捷键说明

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