all repos — xyw @ 5cda32bb76c493a9f84ec48fb6f4c1243682f3dd

A minimal virtual machine and assembler for terminals.

Refactoring.
h3rald h3rald@h3rald.com
Wed, 26 Nov 2025 17:37:58 +0100
commit

5cda32bb76c493a9f84ec48fb6f4c1243682f3dd

parent

e668d5b0501c46f75c9640811be7aeba3319922a

1 files changed, 48 insertions(+), 22 deletions(-)

jump to
M xywasm.cxywasm.c

@@ -339,6 +339,26 @@ token_type = TOKEN_TYPE_SYMBOL;

return 0; } +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) + { + token_error("Expected symbol after .label directive"); + return -1; + } + */ + // TODO: store label in token. + return 0; +} + static int process_directive(xyw_context *ctx) { if (validate_symbol(ctx, token + 1, token_length - 1) != 0)

@@ -355,11 +375,15 @@ // TODO: Remove

printf("Directive:\t%s\n", token); token_type = TOKEN_TYPE_DIRECTIVE; // TODO: Process directive + if (strcmpn(directive, ".label", 6) == 0) + { + return process_directive_label(ctx); + } return 0; } // Parse tokens -static int parse_token(xyw_context *ctx) +static int process_token(xyw_context *ctx) { if (token[0] == ';') {

@@ -397,16 +421,8 @@ token_error("Unrecognized token");

return -1; } -// Read a file and process its tokens -static int process_file(xyw_context *ctx) +static int next_token(xyw_context *ctx) { - ctx->fp = fopen(ctx->file, "r"); - if (!ctx->fp) - { - error("Could not open file"); - return -1; - } - unsigned char c; while (fread(&c, 1, 1, ctx->fp) == 1) {

@@ -429,7 +445,6 @@ }

else { token_error("Token too long"); - fclose(ctx->fp); return -1; } if (c == '"')

@@ -438,9 +453,8 @@ break;

} } // Process string token - if (parse_token(ctx) != 0) + if (process_token(ctx) != 0) { - fclose(ctx->fp); return -1; } token_length = 0;

@@ -449,7 +463,6 @@ }

else { token_error("Token too long"); - fclose(ctx->fp); return -1; } continue;

@@ -467,7 +480,6 @@ {

if (directive_data_length >= DIRECTIVE_CONTENT_MAX_LENGTH - 1) { token_error("Directive data too long"); - fclose(ctx->fp); return -1; } directive_data[directive_data_length++] = (char)c;

@@ -477,7 +489,6 @@ }

else { token_error("Token too long"); - fclose(ctx->fp); return -1; } }

@@ -486,9 +497,8 @@ {

// whitespace separator: finalize token if present if (token_length > 0) { - if (parse_token(ctx) != 0) + if (process_token(ctx) != 0) { - fclose(ctx->fp); return -1; } token_length = 0;

@@ -500,9 +510,8 @@ else if (c == '\n')

{ if (token_length > 0) { - if (parse_token(ctx) != 0) + if (process_token(ctx) != 0) { - fclose(ctx->fp); return -1; } token_length = 0;

@@ -516,15 +525,32 @@ }

else { token_error("Invalid character encountered"); - fclose(ctx->fp); return -1; } } + return 0; +} + +// Read a file and process its tokens +static int process_file(xyw_context *ctx) +{ + ctx->fp = fopen(ctx->file, "r"); + if (!ctx->fp) + { + 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) { - if (parse_token(ctx) != 0) + if (process_token(ctx) != 0) { fclose(ctx->fp); return -1;