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