Convert codebase to C89 compatibility and update test scripts
This commit is contained in:
+65
-50
@@ -5,17 +5,17 @@
|
||||
#include <string.h>
|
||||
|
||||
struct TokenStream {
|
||||
char* filename;
|
||||
const char* code;
|
||||
size_t pos;
|
||||
int line;
|
||||
int column;
|
||||
const char* line_start;
|
||||
char* filename;
|
||||
const char* code;
|
||||
size_t pos;
|
||||
int line;
|
||||
int column;
|
||||
const char* line_start;
|
||||
|
||||
// End of last non-EOF token
|
||||
int last_line;
|
||||
int last_column_end;
|
||||
const char* last_line_start;
|
||||
/* End of last non-EOF token */
|
||||
int last_line;
|
||||
int last_column_end;
|
||||
const char* last_line_start;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -38,8 +38,9 @@ static const KeywordMap keywords[] = {
|
||||
* Returns TOKEN_IDENTIFIER if not found.
|
||||
*/
|
||||
static TokenType lookup_keyword(const char* str, size_t length) {
|
||||
int count = sizeof(keywords) / sizeof(keywords[0]);
|
||||
for (int i = 0; i < count; i++) {
|
||||
int count = sizeof(keywords) / sizeof(keywords[0]);
|
||||
int i;
|
||||
for (i = 0; i < count; i++) {
|
||||
if (strlen(keywords[i].keyword) == length &&
|
||||
strncmp(keywords[i].keyword, str, length) == 0) {
|
||||
return keywords[i].token;
|
||||
@@ -117,27 +118,31 @@ static Token create_token(TokenStream* ts, TokenType type, const char* text, siz
|
||||
}
|
||||
|
||||
TokenStream* tokenstream_open(const char* filename, const char* code) {
|
||||
if (code == NULL) return NULL;
|
||||
/* Declarations first for C89 */
|
||||
TokenStream* ts;
|
||||
const char* name_src;
|
||||
|
||||
TokenStream* ts = (TokenStream*)malloc(sizeof(struct TokenStream));
|
||||
if (ts == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
if (code == NULL) return NULL;
|
||||
|
||||
const char* name_src = filename ? filename : "unknown";
|
||||
ts = (TokenStream*)malloc(sizeof(struct TokenStream));
|
||||
if (ts == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
name_src = filename ? filename : "unknown";
|
||||
ts->filename = malloc(strlen(name_src) + 1);
|
||||
if (ts->filename) {
|
||||
memcpy(ts->filename, name_src, strlen(name_src) + 1);
|
||||
}
|
||||
ts->code = code;
|
||||
ts->pos = 0;
|
||||
ts->line = 1;
|
||||
ts->column = 1;
|
||||
ts->line_start = code;
|
||||
ts->last_line = 1;
|
||||
ts->last_column_end = 0;
|
||||
ts->last_line_start = code;
|
||||
return ts;
|
||||
ts->code = code;
|
||||
ts->pos = 0;
|
||||
ts->line = 1;
|
||||
ts->column = 1;
|
||||
ts->line_start = code;
|
||||
ts->last_line = 1;
|
||||
ts->last_column_end = 0;
|
||||
ts->last_line_start = code;
|
||||
return ts;
|
||||
}
|
||||
|
||||
void tokenstream_close(TokenStream* ts) {
|
||||
@@ -147,16 +152,22 @@ void tokenstream_close(TokenStream* ts) {
|
||||
}
|
||||
|
||||
Token tokenstream_next(TokenStream* ts) {
|
||||
if (ts == NULL) {
|
||||
Token t = {0};
|
||||
t.token = TOKEN_EOF;
|
||||
return t;
|
||||
}
|
||||
/* Declarations first for C89 */
|
||||
char c;
|
||||
int start_line;
|
||||
int start_column;
|
||||
const char* line_start;
|
||||
const char* start_text;
|
||||
Token t;
|
||||
|
||||
char c;
|
||||
if (ts == NULL) {
|
||||
Token t = {0};
|
||||
t.token = TOKEN_EOF;
|
||||
return t;
|
||||
}
|
||||
|
||||
/* Skip whitespace and comments */
|
||||
while ((c = peek_char(ts)) != '\0') {
|
||||
/* Skip whitespace and comments */
|
||||
while ((c = peek_char(ts)) != '\0') {
|
||||
if (isspace(c)) {
|
||||
read_char(ts);
|
||||
continue;
|
||||
@@ -194,12 +205,12 @@ Token tokenstream_next(TokenStream* ts) {
|
||||
return t;
|
||||
}
|
||||
|
||||
int start_line = ts->line;
|
||||
int start_column = ts->column;
|
||||
const char* line_start = ts->line_start;
|
||||
const char* start_text = &ts->code[ts->pos];
|
||||
start_line = ts->line;
|
||||
start_column = ts->column;
|
||||
line_start = ts->line_start;
|
||||
start_text = &ts->code[ts->pos];
|
||||
|
||||
c = read_char(ts);
|
||||
c = read_char(ts);
|
||||
|
||||
/* Single-character tokens */
|
||||
switch (c) {
|
||||
@@ -212,18 +223,22 @@ Token tokenstream_next(TokenStream* ts) {
|
||||
}
|
||||
|
||||
/* Keywords and identifiers */
|
||||
if (is_identifier_start(c)) {
|
||||
size_t length = 1;
|
||||
while (is_identifier_part(peek_char(ts))) {
|
||||
read_char(ts);
|
||||
length++;
|
||||
}
|
||||
TokenType type = lookup_keyword(start_text, length);
|
||||
return create_token(ts, type, start_text, length, start_line, start_column, line_start);
|
||||
}
|
||||
if (is_identifier_start(c)) {
|
||||
/* Declarations first for C89 */
|
||||
size_t length;
|
||||
TokenType type;
|
||||
|
||||
length = 1;
|
||||
while (is_identifier_part(peek_char(ts))) {
|
||||
read_char(ts);
|
||||
length++;
|
||||
}
|
||||
type = lookup_keyword(start_text, length);
|
||||
return create_token(ts, type, start_text, length, start_line, start_column, line_start);
|
||||
}
|
||||
|
||||
/* Unknown character */
|
||||
Token t = create_token(ts, TOKEN_UNKNOWN, start_text, 1, start_line, start_column, line_start);
|
||||
t = create_token(ts, TOKEN_UNKNOWN, start_text, 1, start_line, start_column, line_start);
|
||||
log_on_line(&t.location, t.location.column_end, "unexpected token '%c'", c);
|
||||
return t;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user