This commit is contained in:
Yaossg 2024-11-16 08:24:55 +08:00
parent 1b144230d4
commit 6c8e15ba66
2 changed files with 20 additions and 16 deletions

View File

@ -1,12 +1,9 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
// std // std
int printf(const char* format, ...); int printf(const char* format, ...);
int getchar(); int getchar();
int putchar(int ch);
int strcmp(const char* s1, const char* s2);
void exit(int status); void exit(int status);
// ext // ext

33
boot.c
View File

@ -103,6 +103,13 @@ int get_escaped_char() {
return ch; return ch;
} }
int streq(const char* s1, const char* s2) {
while (*s1 && *s2 && *s1 == *s2) {
s1++;
s2++;
}
return *s1 == *s2;
}
char string_table[65536]; char string_table[65536];
int string_offset; int string_offset;
@ -148,7 +155,7 @@ void dedup_id() {
char* latest = &id_table[id_lut[id_lut_size - 1]]; char* latest = &id_table[id_lut[id_lut_size - 1]];
for (int i = 0; i < id_lut_size - 1; i++) { for (int i = 0; i < id_lut_size - 1; i++) {
char* candidate = &id_table[id_lut[i]]; char* candidate = &id_table[id_lut[i]];
if (!strcmp(candidate, latest)) { if (streq(candidate, latest)) {
rewind_id(i); rewind_id(i);
return; return;
} }
@ -159,27 +166,27 @@ void parse_id_like(int ch) {
token_type = TOKEN_ID; token_type = TOKEN_ID;
token_data = parse_id(ch); token_data = parse_id(ch);
char* id = &id_table[id_lut[token_data]]; char* id = &id_table[id_lut[token_data]];
if (!strcmp(id, "int")) { if (streq(id, "int")) {
token_type = TOKEN_INT; token_type = TOKEN_INT;
} else if (!strcmp(id, "if")) { } else if (streq(id, "if")) {
token_type = TOKEN_IF; token_type = TOKEN_IF;
} else if (!strcmp(id, "else")) { } else if (streq(id, "else")) {
token_type = TOKEN_ELSE; token_type = TOKEN_ELSE;
} else if (!strcmp(id, "while")) { } else if (streq(id, "while")) {
token_type = TOKEN_WHILE; token_type = TOKEN_WHILE;
} else if (!strcmp(id, "break")) { } else if (streq(id, "break")) {
token_type = TOKEN_BREAK; token_type = TOKEN_BREAK;
} else if (!strcmp(id, "continue")) { } else if (streq(id, "continue")) {
token_type = TOKEN_CONTINUE; token_type = TOKEN_CONTINUE;
} else if (!strcmp(id, "return")) { } else if (streq(id, "return")) {
token_type = TOKEN_RETURN; token_type = TOKEN_RETURN;
} else if (!strcmp(id, "void")) { } else if (streq(id, "void")) {
token_type = TOKEN_VOID; token_type = TOKEN_VOID;
} else if (!strcmp(id, "const")) { } else if (streq(id, "const")) {
token_type = TOKEN_CONST; token_type = TOKEN_CONST;
} else if (!strcmp(id, "char")) { } else if (streq(id, "char")) {
token_type = TOKEN_CHAR; token_type = TOKEN_CHAR;
} else if (!strcmp(id, "for")) { } else if (streq(id, "for")) {
token_type = TOKEN_FOR; token_type = TOKEN_FOR;
} }
if (token_type != TOKEN_ID) { if (token_type != TOKEN_ID) {
@ -1295,7 +1302,7 @@ void dump_string_table() {
} else if (ch == '\"') { } else if (ch == '\"') {
printf("\\\""); printf("\\\"");
} else { } else {
putchar(ch); printf("%c", ch);
} }
offset++; offset++;
} }