Refactoring.
h3rald h3rald@h3rald.com
Wed, 26 Nov 2025 17:47:00 +0100
1 files changed,
100 insertions(+),
80 deletions(-)
jump to
M
xywasm.c
→
xywasm.c
@@ -339,23 +339,18 @@ token_type = TOKEN_TYPE_SYMBOL;
return 0; } +// Returns: 1 token processed, 0 EOF, -1 error static int next_token(xyw_context *ctx); static int process_directive_label(xyw_context *ctx) { - (void)ctx; - /* - if (next_token(ctx) != 0) - { - return -1; - } - if (token_type != TOKEN_TYPE_SYMBOL) + int r = next_token(ctx); + if (r != 1 || token_type != TOKEN_TYPE_SYMBOL) { token_error("Expected symbol after .label directive"); return -1; } - */ - // TODO: store label in token. + // TODO: store label info return 0; }@@ -424,103 +419,131 @@
static int next_token(xyw_context *ctx) { unsigned char c; + token_length = 0; + token[0] = '\0'; + int in_string = 0; + while (fread(&c, 1, 1, ctx->fp) == 1) { + if (!in_string && c == '\n') + { + if (token_length > 0) + { + if (process_token(ctx) != 0) + return -1; + token_length = 0; + token[0] = '\0'; + ctx->line++; + ctx->column = 1; + reset_directive(); + return 1; + } + ctx->line++; + ctx->column = 1; + reset_directive(); + continue; + } + if (!in_string && token_length == 0 && c == ';') + { + token[token_length++] = (char)c; + token[token_length] = '\0'; + ctx->column++; + if (process_token(ctx) != 0) + return -1; + token_length = 0; + token[0] = '\0'; + return 1; + } if (c == '"') { - // Special handling for string tokens - if (token_length < STRING_MAX_LENGTH - 1) + ctx->column++; + if (!in_string) { - token[token_length++] = (char)c; - token[token_length] = '\0'; - ctx->column++; - while (fread(&c, 1, 1, ctx->fp) == 1) + in_string = 1; + if (token_length < STRING_MAX_LENGTH - 1) { - ctx->column++; - if (token_length < STRING_MAX_LENGTH - 1) - { - token[token_length++] = (char)c; - token[token_length] = '\0'; - } - else - { - token_error("Token too long"); - return -1; - } - if (c == '"') - { - break; - } + token[token_length++] = (char)c; + token[token_length] = '\0'; } - // Process string token - if (process_token(ctx) != 0) + else { + token_error("Token too long"); return -1; } - token_length = 0; - token[0] = '\0'; + continue; } else { - token_error("Token too long"); - return -1; + if (token_length < STRING_MAX_LENGTH - 1) + { + token[token_length++] = (char)c; + token[token_length] = '\0'; + } + else + { + token_error("Token too long"); + return -1; + } + if (process_token(ctx) != 0) + return -1; + token_length = 0; + token[0] = '\0'; + in_string = 0; + return 1; } - continue; } - if (c > 0x20 && c != 0x7f) // printable non-space + else if (in_string) { - if (token_length < TOKEN_MAX_LENGTH - 1) + ctx->column++; + if (token_length < STRING_MAX_LENGTH - 1) { token[token_length++] = (char)c; token[token_length] = '\0'; - ctx->column++; - // Save directive data if in a directive - if (directive_length > 0) - { - if (directive_data_length >= DIRECTIVE_CONTENT_MAX_LENGTH - 1) - { - token_error("Directive data too long"); - return -1; - } - directive_data[directive_data_length++] = (char)c; - directive_data[directive_data_length] = '\0'; - } } else { token_error("Token too long"); return -1; } + continue; } else if (c == ' ' || c == '\t') { - // whitespace separator: finalize token if present if (token_length > 0) { if (process_token(ctx) != 0) - { return -1; - } token_length = 0; token[0] = '\0'; + ctx->column++; + return 1; } ctx->column++; + continue; } - else if (c == '\n') + else if (c > 0x20 && c != 0x7f) { - if (token_length > 0) + ctx->column++; + if (token_length < TOKEN_MAX_LENGTH - 1) { - if (process_token(ctx) != 0) + token[token_length++] = (char)c; + token[token_length] = '\0'; + if (directive_length > 0) { - return -1; + if (directive_data_length >= DIRECTIVE_CONTENT_MAX_LENGTH - 1) + { + token_error("Directive data too long"); + return -1; + } + directive_data[directive_data_length++] = (char)c; + directive_data[directive_data_length] = '\0'; } - token_length = 0; - token[0] = '\0'; + } + else + { + token_error("Token too long"); + return -1; } - ctx->line++; - ctx->column = 1; - // Directives are line-based, so reset directive data - reset_directive(); } else {@@ -528,7 +551,15 @@ token_error("Invalid character encountered");
return -1; } } - return 0; + if (token_length > 0) + { + if (process_token(ctx) != 0) + return -1; + token_length = 0; + token[0] = '\0'; + return 1; // last token at EOF + } + return 0; // EOF no token } // Read a file and process its tokens@@ -541,24 +572,13 @@ error("Could not open file");
return -1; } - if (next_token(ctx) != 0) - { - fclose(ctx->fp); - return -1; - } - - // Final token at EOF - if (token_length > 0) + int r; + while ((r = next_token(ctx)) == 1) { - if (process_token(ctx) != 0) - { - fclose(ctx->fp); - return -1; - } + // tokens processed inline } - fclose(ctx->fp); - return 0; + return r; } ///// Public API