int getop(char s[]) { int i, c, next; while ((s[0] = c = get_ch()) == ' ' || c == '\t') ; /* 我写代码时,还检查了回车,但是不应该检查回车,其是有效字符 */ s[1] = '\0'; if (is_loweralpha(c)) /* 判断是否是变量,变量名只有一个小写字母 */ return 'a'; if (!is_digit(c) && c != '.' && c != '-') return c; i = 0; if (c == '-') { /* collect negative sign */ if (!is_digit(next = get_ch()) && next != '.') { unget_ch(next); return c; } s[++i] = c = next; } if (is_digit(c)) while (is_digit(s[++i] = c = get_ch())) ; if (c == '.') /* collect fraction part */ while (is_digit(s[++i] = c = get_ch())) ; s[i] = '\0'; if (c != EOF) unget_ch(c); return '0'; }
void unget_str(char s[]) { int len = strlen(s); while (len > 0) { len--; unget_ch(s[len]); } }
static void devide_line( char * out /* 'out' is 'output' in actual */ ) /* * Devide a too long line into output lines shorter than NWORK. * This routine is called from putout(). */ { FILEINFO * file; char * save; char * wp; int c; file = unget_string( out, NULL); /* To re-read the line */ wp = out_ptr = out; while ((c = get_ch()), file == infile) { if (char_type[ c] & HSP) { if (keep_spaces || out == out_ptr || (char_type[ *(out_ptr - 1) & UCHARMAX] & HSP)) { *out_ptr++ = c; wp++; } continue; } scan_token( c, &wp, out_wend); /* Read a token */ if (NWORK-2 < wp - out_ptr) { /* Too long a token */ cfatal( "Too long token %s", out_ptr, 0L, NULL); /* _F_ */ } else if (out_end <= wp) { /* Too long line */ if (mcpp_debug & MACRO_CALL) { /* -K option */ /* Other than GCC or Visual C */ /* scan_token() scans a comment as sequence of some */ /* tokens such as '/', '*', ..., '*', '/', since it */ /* does not expect comment. */ save = out_ptr; while ((save = strrchr( save, '/')) != NULL) { if (*(save - 1) == '*') { /* '*' '/' sequence */ out_ptr = save + 1; /* Devide at the end*/ break; /* of a comment*/ } } } save = save_string( out_ptr); /* Save the token */ *out_ptr++ = '\n'; /* Append newline */ *out_ptr = EOS; put_a_line( out); /* Putout the former tokens */ wp = out_ptr = stpcpy( out, save); /* Restore the token */ free( save); } else { /* Still in size */ out_ptr = wp; /* Advance the pointer */ } } unget_ch(); /* Push back the source character */ put_a_line( out); /* Putout the last tokens */ sharp( NULL, 0); /* Correct line number */ }
static void put_seq( char * begin, /* Sequence already in buffer */ char * seq /* Sequence to be read */ ) /* * Put out the failed sequence as it is. */ { FILEINFO * file = infile; int c; cerror( "Operand of _Pragma() is not a string literal" /* _E_ */ , NULL, 0L, NULL); while (c = get_ch(), file == infile) *seq++ = c; unget_ch(); out_ptr = seq; putout( begin); }
int get_op(char s[]) { int i, c; while ((s[0] = c = get_ch()) == ' ' || c == '\t') ; s[1] = '\0'; if (!isdigit(c) && c != '.' && c != '-') return c; i = 0; if (isdigit(c) || c == '-') while (isdigit(s[++i] = c = get_ch())) ; if (c == '.') while (isdigit(s[++i] = c = get_ch())) ; s[i] = '\0'; if (c != EOF) unget_ch(c); return NUMBER; }
static void do_pragma_op( void) /* * Execute the _Pragma() operator contained in an expanded macro. * Note: _Pragma() operator is also implemented as a special macro. Therefore * it is always searched as a macro. * There might be more than one _Pragma() in a expanded macro and those may be * surrounded by other token sequences. * Since all the macros have been expanded completely, any name identical to * macro should not be re-expanded. * However, a macro in the string argument of _Pragma() may be expanded by * do_pragma() after de_stringize(), if EXPAND_PRAGMA == TRUE. */ { FILEINFO * file; DEFBUF * defp; int prev = output < out_ptr; /* There is a previous sequence */ int token_type; char * cp1, * cp2; int c; file = unget_string( out_ptr, NULL); while (c = get_ch(), file == infile) { if (char_type[ c] & HSP) { *out_ptr++ = c; continue; } if (scan_token( c, (cp1 = out_ptr, &cp1), out_wend) == NAM && (defp = is_macro( &cp1)) != NULL && defp->nargs == DEF_PRAGMA) { /* _Pragma() operator */ if (prev) { putout( output); /* Putout the previous sequence */ cp1 = stpcpy( output, "pragma "); /* From top of buffer */ } /* is_macro() already read over possible spaces after _Pragma */ *cp1++ = get_ch(); /* '(' */ while (char_type[ c = get_ch()] & HSP) *cp1++ = c; if (((token_type = scan_token( c, (cp2 = cp1, &cp1), out_wend)) != STR && token_type != WSTR)) { /* Not a string literal */ put_seq( output, cp1); return; } workp = de_stringize( cp2, work_buf); while (char_type[ c = get_ch()] & HSP) *cp1++ = c; if (c != ')') { /* More than a string literal */ unget_ch(); put_seq( output, cp1); return; } strcpy( workp, "\n"); /* Terminate with <newline> */ unget_string( work_buf, NULL); do_pragma(); /* Do the #pragma "line" */ infile->bptr += strlen( infile->bptr); /* Clear sequence */ cp1 = out_ptr = output; /* From the top of buffer */ prev = FALSE; } else { /* Not pragma sequence */ out_ptr = cp1; prev = TRUE; } } unget_ch(); if (prev) putout( output); }
static void mcpp_main( void) /* * Main process for mcpp -- copies tokens from the current input stream * (main file or included file) to the output file. */ { int c; /* Current character */ char * wp; /* Temporary pointer */ DEFBUF * defp; /* Macro definition */ int line_top; /* Is in the line top, possibly spaces */ LINE_COL line_col; /* Location of macro call in source */ keep_comments = option_flags.c && !no_output; keep_spaces = option_flags.k; /* Will be turned off if !compiling */ line_col.col = line_col.line = 0L; /* * This loop is started "from the top" at the beginning of each line. * 'wrong_line' is set TRUE in many places if it is necessary to write * a #line record. (But we don't write them when expanding macros.) * * 'newlines' variable counts the number of blank lines that have been * skipped over. These are then either output via #line records or * by outputting explicit blank lines. * 'newlines' will be cleared on end of an included file by get_ch(). */ while (1) { /* For the whole input */ newlines = 0; /* Count empty lines */ while (1) { /* For each line, ... */ out_ptr = output; /* Top of the line buf */ c = get_ch(); if (src_col) break; /* There is a residual tokens on the line */ while (char_type[ c] & HSP) { /* ' ' or '\t' */ if (c != COM_SEP) *out_ptr++ = c; /* Retain line top white spaces */ /* Else skip 0-length comment */ c = get_ch(); } if (c == '#') { /* Is 1st non-space '#' */ directive(); /* Do a #directive */ } else if (mcpp_mode == STD && option_flags.dig && c == '%') { /* In POST_STD digraphs are already converted */ if (get_ch() == ':') { /* '%:' i.e. '#' */ directive(); /* Do a #directive */ } else { unget_ch(); if (! compiling) { skip_nl(); newlines++; } else { break; } } } else if (c == CHAR_EOF) { /* End of input */ break; } else if (! compiling) { /* #ifdef false? */ skip_nl(); /* Skip to newline */ newlines++; /* Count it, too. */ } else if (in_asm && ! no_output) { /* In #asm block */ put_asm(); /* Put out as it is */ } else if (c == '\n') { /* Blank line */ if (keep_comments) mcpp_fputc( '\n', OUT); /* May flush comments */ else newlines++; /* Wait for a token */ } else { break; /* Actual token */ } } if (c == CHAR_EOF) /* Exit process at */ break; /* end of input */ /* * If the loop didn't terminate because of end of file, we * know there is a token to compile. First, clean up after * absorbing newlines. newlines has the number we skipped. */ if (no_output) { wrong_line = FALSE; } else { if (wrong_line || newlines > 10) { sharp( NULL, 0); /* Output # line number */ if (keep_spaces && src_col) { while (src_col--) /* Adjust columns */ mcpp_fputc( ' ', OUT); src_col = 0; } } else { /* If just a few, stuff */ while (newlines-- > 0) /* them out ourselves */ mcpp_fputc('\n', OUT); } } /* * Process each token on this line. */ line_top = TRUE; while (c != '\n' && c != CHAR_EOF) { /* For the whole line */ /* * has_pragma is set to TRUE so as to execute _Pragma() operator * when the psuedo macro _Pragma() is found. */ int has_pragma; if ((mcpp_debug & MACRO_CALL) && ! in_directive) { line_col.line = src_line; /* Location in source */ line_col.col = infile->bptr - infile->buffer - 1; } if (scan_token( c, (wp = out_ptr, &wp), out_wend) == NAM && (defp = is_macro( &wp)) != NULL) { /* A macro */ wp = expand_macro( defp, out_ptr, out_wend, line_col , & has_pragma); /* Expand it completely */ if (line_top) { /* The first token is a macro */ char * tp = out_ptr; while (char_type[ *tp & UCHARMAX] & HSP) tp++; /* Remove excessive spaces */ memmove( out_ptr, tp, strlen( tp) + 1); wp -= (tp - out_ptr); } if (has_pragma) { /* Found _Pramga() */ do_pragma_op(); /* Do _Pragma() operator*/ out_ptr = output; /* Do the rest of line */ wrong_line = TRUE; /* Line-num out of sync */ } else { out_ptr = wp; } if (keep_spaces && wrong_line && infile && *(infile->bptr) != '\n' && *(infile->bptr) != EOS) { src_col = infile->bptr - infile->buffer; /* Remember the current colums */ break; /* Do sharp() now */ } } else { /* Not a macro call */ out_ptr = wp; /* Advance the place */ if (wrong_line) /* is_macro() swallowed */ break; /* the newline */ } while (char_type[ c = get_ch()] & HSP) { /* Horizontal space */ if (c != COM_SEP) /* Skip 0-length comment*/ *out_ptr++ = c; } line_top = FALSE; /* Read over some token */ } /* Loop for line */ putout( output); /* Output the line */ } /* Continue until EOF */ }