rename to next and unget

This commit is contained in:
Yaossg 2025-04-11 18:20:52 +08:00
parent 098a36427e
commit 04a58f8dec

98
boot.c
View file

@ -37,7 +37,7 @@ enum {
char echo_buffer[ECHO_BUFFER_SIZE]; char echo_buffer[ECHO_BUFFER_SIZE];
int echo_size; int echo_size;
int readchar() { int next_char() {
int ch = getchar(); int ch = getchar();
if (ch == '\n' || ch == -1) { if (ch == '\n' || ch == -1) {
echo_buffer[echo_size++] = 0; echo_buffer[echo_size++] = 0;
@ -49,7 +49,7 @@ int readchar() {
return ch; return ch;
} }
void ungetchar(int ch) { void unget_char(int ch) {
--echo_size; --echo_size;
ungetc(ch, stdin); ungetc(ch, stdin);
} }
@ -239,16 +239,16 @@ int is_id_cont(int ch) {
int parse_int(int ch) { int parse_int(int ch) {
int num = ch - '0'; int num = ch - '0';
while (is_digit(ch = readchar())) { while (is_digit(ch = next_char())) {
num *= 10; num *= 10;
num += ch - '0'; num += ch - '0';
} }
ungetchar(ch); unget_char(ch);
return num; return num;
} }
int get_escaped_char() { int get_escaped_char() {
int ch = readchar(); int ch = next_char();
if (ch == 'n') return '\n'; if (ch == 'n') return '\n';
if (ch == 't') return '\t'; if (ch == 't') return '\t';
if (ch == 'r') return '\r'; if (ch == 'r') return '\r';
@ -260,7 +260,7 @@ int get_escaped_char() {
exit(1); exit(1);
} }
int token_state; int token_unget;
int token_type; int token_type;
int token_data; int token_data;
@ -271,7 +271,7 @@ int string_lut_size;
int parse_string() { int parse_string() {
int offset = string_offset; int offset = string_offset;
int ch; int ch;
while ((ch = readchar()) != '"') { while ((ch = next_char()) != '"') {
if (ch == -1 || ch == '\n') { if (ch == -1 || ch == '\n') {
fprintf(stderr, "expecting '\"'\n"); fprintf(stderr, "expecting '\"'\n");
exit(1); exit(1);
@ -293,10 +293,10 @@ int id_lut_size;
int parse_id(int ch) { int parse_id(int ch) {
int offset = id_offset; int offset = id_offset;
id_table[id_offset++] = ch; id_table[id_offset++] = ch;
while (is_id_cont(ch = readchar())) { while (is_id_cont(ch = next_char())) {
id_table[id_offset++] = ch; id_table[id_offset++] = ch;
} }
ungetchar(ch); unget_char(ch);
id_table[id_offset++] = 0; id_table[id_offset++] = 0;
id_lut[id_lut_size] = offset; id_lut[id_lut_size] = offset;
return id_lut_size++; return id_lut_size++;
@ -363,17 +363,17 @@ void parse_id_like(int ch) {
} }
void unget_token() { void unget_token() {
token_state = 1; token_unget = 1;
} }
void next_token() { void next_token() {
if (token_state) { if (token_unget) {
token_state = 0; token_unget = 0;
return; return;
} }
int ch = readchar(); int ch = next_char();
while (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n') { while (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n') {
ch = readchar(); ch = next_char();
} }
if (ch == -1) { if (ch == -1) {
token_type = TOKEN_EOF; token_type = TOKEN_EOF;
@ -390,50 +390,50 @@ void next_token() {
} else if (ch == '}') { } else if (ch == '}') {
token_type = TOKEN_BRACE_RIGHT; token_type = TOKEN_BRACE_RIGHT;
} else if (ch == '+') { } else if (ch == '+') {
int ch2 = readchar(); int ch2 = next_char();
if (ch2 == '+') { if (ch2 == '+') {
token_type = TOKEN_INC; token_type = TOKEN_INC;
} else if (ch2 == '=') { } else if (ch2 == '=') {
token_type = TOKEN_ADD_ASSIGN; token_type = TOKEN_ADD_ASSIGN;
} else { } else {
ungetchar(ch2); unget_char(ch2);
token_type = TOKEN_ADD; token_type = TOKEN_ADD;
} }
} else if (ch == '-') { } else if (ch == '-') {
int ch2 = readchar(); int ch2 = next_char();
if (ch2 == '-') { if (ch2 == '-') {
token_type = TOKEN_DEC; token_type = TOKEN_DEC;
} else if (ch2 == '=') { } else if (ch2 == '=') {
token_type = TOKEN_SUB_ASSIGN; token_type = TOKEN_SUB_ASSIGN;
} else { } else {
ungetchar(ch2); unget_char(ch2);
token_type = TOKEN_SUB; token_type = TOKEN_SUB;
} }
} else if (ch == '*') { } else if (ch == '*') {
int ch2 = readchar(); int ch2 = next_char();
if (ch2 == '=') { if (ch2 == '=') {
token_type = TOKEN_MUL_ASSIGN; token_type = TOKEN_MUL_ASSIGN;
} else { } else {
ungetchar(ch2); unget_char(ch2);
token_type = TOKEN_MUL; token_type = TOKEN_MUL;
} }
} else if (ch == '/') { } else if (ch == '/') {
int ch2 = readchar(); int ch2 = next_char();
if (ch2 == '=') { if (ch2 == '=') {
token_type = TOKEN_DIV_ASSIGN; token_type = TOKEN_DIV_ASSIGN;
} else if (ch2 == '/') { } else if (ch2 == '/') {
do ch = readchar(); while (ch != -1 && ch != '\n'); do ch = next_char(); while (ch != -1 && ch != '\n');
next_token(); next_token();
return; return;
} else if (ch2 == '*') { } else if (ch2 == '*') {
while (1) { while (1) {
ch = readchar(); ch = next_char();
if (ch == -1) { if (ch == -1) {
fprintf(stderr, "expecting '*/'\n"); fprintf(stderr, "expecting '*/'\n");
exit(1); exit(1);
} }
if (ch == '*') { if (ch == '*') {
ch = readchar(); ch = next_char();
if (ch == '/') { if (ch == '/') {
break; break;
} }
@ -442,15 +442,15 @@ void next_token() {
next_token(); next_token();
return; return;
} else { } else {
ungetchar(ch2); unget_char(ch2);
token_type = TOKEN_DIV; token_type = TOKEN_DIV;
} }
} else if (ch == '%') { } else if (ch == '%') {
int ch2 = readchar(); int ch2 = next_char();
if (ch2 == '=') { if (ch2 == '=') {
token_type = TOKEN_REM_ASSIGN; token_type = TOKEN_REM_ASSIGN;
} else { } else {
ungetchar(ch2); unget_char(ch2);
token_type = TOKEN_REM; token_type = TOKEN_REM;
} }
} else if (ch == ';') { } else if (ch == ';') {
@ -462,90 +462,90 @@ void next_token() {
} else if (ch == ',') { } else if (ch == ',') {
token_type = TOKEN_COMMA; token_type = TOKEN_COMMA;
} else if (ch == '<') { } else if (ch == '<') {
int ch2 = readchar(); int ch2 = next_char();
if (ch2 == '=') { if (ch2 == '=') {
token_type = TOKEN_LE; token_type = TOKEN_LE;
} else if (ch2 == '<') { } else if (ch2 == '<') {
int ch3 = readchar(); int ch3 = next_char();
if (ch3 == '=') { if (ch3 == '=') {
token_type = TOKEN_LSHIFT_ASSIGN; token_type = TOKEN_LSHIFT_ASSIGN;
} else { } else {
ungetchar(ch3); unget_char(ch3);
token_type = TOKEN_LSHIFT; token_type = TOKEN_LSHIFT;
} }
} else { } else {
ungetchar(ch2); unget_char(ch2);
token_type = TOKEN_LT; token_type = TOKEN_LT;
} }
} else if (ch == '>') { } else if (ch == '>') {
int ch2 = readchar(); int ch2 = next_char();
if (ch2 == '=') { if (ch2 == '=') {
token_type = TOKEN_GE; token_type = TOKEN_GE;
} else if (ch2 == '>') { } else if (ch2 == '>') {
int ch3 = readchar(); int ch3 = next_char();
if (ch3 == '=') { if (ch3 == '=') {
token_type = TOKEN_RSHIFT_ASSIGN; token_type = TOKEN_RSHIFT_ASSIGN;
} else { } else {
ungetchar(ch3); unget_char(ch3);
token_type = TOKEN_RSHIFT; token_type = TOKEN_RSHIFT;
} }
} else { } else {
ungetchar(ch2); unget_char(ch2);
token_type = TOKEN_GT; token_type = TOKEN_GT;
} }
} else if (ch == '=') { } else if (ch == '=') {
int ch2 = readchar(); int ch2 = next_char();
if (ch2 == '=') { if (ch2 == '=') {
token_type = TOKEN_EQ; token_type = TOKEN_EQ;
} else { } else {
ungetchar(ch2); unget_char(ch2);
token_type = TOKEN_ASSIGN; token_type = TOKEN_ASSIGN;
} }
} else if (ch == '!') { } else if (ch == '!') {
int ch2 = readchar(); int ch2 = next_char();
if (ch2 == '=') { if (ch2 == '=') {
token_type = TOKEN_NE; token_type = TOKEN_NE;
} else { } else {
ungetchar(ch2); unget_char(ch2);
token_type = TOKEN_NOT; token_type = TOKEN_NOT;
} }
} else if (ch == '&') { } else if (ch == '&') {
int ch2 = readchar(); int ch2 = next_char();
if (ch2 == '=') { if (ch2 == '=') {
token_type = TOKEN_AND_ASSIGN; token_type = TOKEN_AND_ASSIGN;
} else if (ch2 == '&') { } else if (ch2 == '&') {
token_type = TOKEN_LAND; token_type = TOKEN_LAND;
} else { } else {
ungetchar(ch2); unget_char(ch2);
token_type = TOKEN_AND; token_type = TOKEN_AND;
} }
} else if (ch == '|') { } else if (ch == '|') {
int ch2 = readchar(); int ch2 = next_char();
if (ch2 == '=') { if (ch2 == '=') {
token_type = TOKEN_OR_ASSIGN; token_type = TOKEN_OR_ASSIGN;
} else if (ch2 == '|') { } else if (ch2 == '|') {
token_type = TOKEN_LOR; token_type = TOKEN_LOR;
} else { } else {
ungetchar(ch2); unget_char(ch2);
token_type = TOKEN_OR; token_type = TOKEN_OR;
} }
} else if (ch == '^') { } else if (ch == '^') {
int ch2 = readchar(); int ch2 = next_char();
if (ch2 == '=') { if (ch2 == '=') {
token_type = TOKEN_XOR_ASSIGN; token_type = TOKEN_XOR_ASSIGN;
} else { } else {
ungetchar(ch2); unget_char(ch2);
token_type = TOKEN_XOR; token_type = TOKEN_XOR;
} }
} else if (ch == '~') { } else if (ch == '~') {
token_type = TOKEN_INV; token_type = TOKEN_INV;
} else if (ch == '\'') { } else if (ch == '\'') {
token_type = TOKEN_NUMBER; token_type = TOKEN_NUMBER;
token_data = readchar(); token_data = next_char();
if (token_data == '\\') { if (token_data == '\\') {
token_data = get_escaped_char(); token_data = get_escaped_char();
} }
if (readchar() != '\'') { if (next_char() != '\'') {
fprintf(stderr, "expecting '\n"); fprintf(stderr, "expecting '\n");
exit(1); exit(1);
} }
@ -555,8 +555,8 @@ void next_token() {
dedup_string(); dedup_string();
} else if (ch == '.') { } else if (ch == '.') {
token_type = 0; token_type = 0;
if (readchar() == '.') { if (next_char() == '.') {
if (readchar() == '.') { if (next_char() == '.') {
token_type = TOKEN_ELLIPSIS; token_type = TOKEN_ELLIPSIS;
} }
} }