Refactoring string handling.
h3rald h3rald@h3rald.com
Fri, 28 Nov 2025 07:58:16 +0100
1 files changed,
39 insertions(+),
20 deletions(-)
jump to
M
xywasm.c
→
xywasm.c
@@ -54,10 +54,8 @@
typedef enum { TOKEN_END_EOF = 0, - TOKEN_END_EOS, TOKEN_END_EOL, TOKEN_END_SPACE, - TOKEN_END_COMMENT, TOKEN_END_INVALID } xyw_token_terminator;@@ -217,10 +215,14 @@ }
static int validate_single_directive_argument(xyw_context *ctx, xyw_token_terminator r) { - while (r != TOKEN_END_EOL && r != TOKEN_END_EOF && r != TOKEN_END_COMMENT) + while (r != TOKEN_END_EOL && r != TOKEN_END_EOF) { r = next_token(ctx); - if (r == TOKEN_END_EOL || r == TOKEN_END_EOF || r == TOKEN_END_COMMENT) + if (r == TOKEN_END_INVALID) + { + return -1; + } + if (r == TOKEN_END_EOL || r == TOKEN_END_EOF) { break; }@@ -309,6 +311,7 @@ char c;
// Process single-line comment while (fread(&c, 1, 1, ctx->fp) == 1) { + // Ignore all characters until end of line if (c == '\n') { ctx->line++;@@ -318,6 +321,7 @@ }
ctx->column++; } token_type = TOKEN_TYPE_COMMENT; + printf("Comment:\t%s\n", token); return 0; }@@ -535,6 +539,10 @@
static int process_directive_org(xyw_context *ctx) { xyw_token_terminator r = next_token(ctx); + if (r == TOKEN_END_INVALID) + { + return -1; + } if (process_number(ctx) != 0) { return -1;@@ -548,6 +556,10 @@
static int process_directive_label(xyw_context *ctx) { xyw_token_terminator r = next_token(ctx); + if (r == TOKEN_END_INVALID) + { + return -1; + } if (process_symbol(ctx) != 0) { return -1;@@ -560,9 +572,8 @@
static int process_directive_include(xyw_context *ctx) { xyw_token_terminator r = next_token(ctx); - if (r != TOKEN_END_EOS) + if (r == TOKEN_END_INVALID) { - token_error("Expected string after .include directive"); return -1; } process_string(ctx);@@ -574,9 +585,8 @@
static int process_directive_string(xyw_context *ctx) { xyw_token_terminator r = next_token(ctx); - if (r != TOKEN_END_EOS) + if (r == TOKEN_END_INVALID) { - token_error("Expected string after .string directive"); return -1; } process_string(ctx);@@ -597,6 +607,10 @@ printf("Directive:\t.byte\n");
while (1) { r = next_token(ctx); + if (r == TOKEN_END_INVALID) + { + return -1; + } if (process_number(ctx) != 0) { return -1;@@ -606,7 +620,7 @@ {
token_error("Byte value out of range (0-255)"); return -1; } - if (r == TOKEN_END_EOL || r == TOKEN_END_EOF || r == TOKEN_END_COMMENT) + if (r == TOKEN_END_EOL || r == TOKEN_END_EOF) { break; }@@ -617,7 +631,11 @@ }
static int process_directive_macro(xyw_context *ctx) { - next_token(ctx); + xyw_token_terminator r = next_token(ctx); + if (r == TOKEN_END_INVALID) + { + return -1; + } if (process_symbol(ctx) != 0) { return -1;@@ -748,19 +766,17 @@ token_length = 0;
token[0] = '\0'; while (fread(&c, 1, 1, ctx->fp) == 1) { - if (!in_string && c == '\n') + if (c == '\n') { + if (in_string) + { + token_error("Unterminated string literal"); + return TOKEN_END_INVALID; + } ctx->line++; ctx->column = 1; return TOKEN_END_EOL; } - else if (!in_string && token_length == 0 && c == ';') - { - ctx->column++; - token[token_length++] = (char)c; - token[token_length] = '\0'; - return TOKEN_END_COMMENT; - } else if (c == '"') { ctx->column++;@@ -773,9 +789,12 @@ token[token_length++] = (char)c;
token[token_length] = '\0'; if (in_string) { - return TOKEN_END_EOS; + in_string = 0; + } + else + { + in_string = 1; } - in_string = 1; } else if (in_string) {