all repos — xyw @ 492a40825569405ca3d40290c10b59833eee4799

A minimal virtual machine and assembler for terminals.

Implemented contextual parsing (tokens within directives)
h3rald h3rald@h3rald.com
Wed, 26 Nov 2025 14:04:02 +0100
commit

492a40825569405ca3d40290c10b59833eee4799

parent

fbe98fda1e632359375b317afaf37f9eae30bf04

1 files changed, 33 insertions(+), 44 deletions(-)

jump to
M xywasm.cxywasm.c

@@ -36,6 +36,9 @@ // The token being processed

static char token[TOKEN_MAX_LENGTH]; static unsigned int token_length = 0; +static char directive[TOKEN_MAX_LENGTH]; +static unsigned int directive_length = 0; + // Directive data static char directive_data[DIRECTIVE_CONTENT_MAX_LENGTH]; static unsigned int directive_data_length = 0;

@@ -88,15 +91,12 @@ }

return 0; } -//// Directive handlers - -static int directive_label(xyw_context *ctx) +static void reset_directive() { - if (validate_symbol(ctx, directive_data, 1, directive_data_length) != 0) - { - return -1; - } - return 0; + directive_length = 0; + directive[0] = '\0'; + directive_data_length = 0; + directive_data[0] = '\0'; } //// Parsing functions

@@ -106,6 +106,7 @@ {

char c; char comment_data[COMMENT_CONTENT_MAX_LENGTH]; unsigned int comment_length = 0; + reset_directive(); // Process single-line comment while (fread(&c, 1, 1, ctx->fp) == 1) {

@@ -159,7 +160,7 @@ return -1;

} } // TODO: Remove - printf("Hex number:\t%s\t(%04X)\n", token, value); + printf("Hex number:\t%s\t(%04X) [directive: %s]\n", token, value, directive_length > 0 ? directive : "N/A"); // TODO: Store value return 0; }

@@ -187,7 +188,7 @@ return -1;

} } // TODO: Remove - printf("Binary number:\t%s\t(%04X)\n", token, value); + printf("Binary number:\t%s\t(%04X) [directive: %s]\n", token, value, directive_length > 0 ? directive : "N/A"); // TODO: Store value return 0; }

@@ -221,7 +222,7 @@ {

value = -value; } // TODO: Remove - printf("Decimal number:\t%s\t(%04X)\n", token, value); + printf("Decimal number:\t%s\t(%04X) [directive: %s]\n", token, value, directive_length > 0 ? directive : "N/A"); // TODO: Store value return 0; }

@@ -267,7 +268,7 @@ }

// TODO: Remove char bits[9]; byte_to_binary(opcode, bits); - printf("Instruction:\t%s\t(%02X - %s)\n", token, opcode, bits); + printf("Instruction:\t%s\t(%02X - %s) [directive: %s]\n", token, opcode, bits, directive_length > 0 ? directive : "N/A"); // TODO: Store opcode return 0; }

@@ -282,7 +283,7 @@ {

return -1; } // TODO: Remove - printf("Symbol:\t\t%s\n", token); + printf("Symbol:\t\t%s [directive: %s]\n", token, directive_length > 0 ? directive : "N/A"); // TODO: Process symbol return 0; }

@@ -293,41 +294,15 @@ if (validate_symbol(ctx, token, 1, token_length) != 0)

{ return -1; } - // Parse labels starting with . until end of line - char c; - while (fread(&c, 1, 1, ctx->fp) == 1) + directive_length = token_length; + for (unsigned int i = 0; i < directive_length; i++) { - if (c == '\n') - { - break; - } - ctx->column++; - if (directive_data_length < DIRECTIVE_CONTENT_MAX_LENGTH - 1) - { - directive_data[directive_data_length++] = c; - directive_data[directive_data_length] = '\0'; - } - else - { - token_error("Directive too long"); - return -1; - } + directive[i] = token[i]; } // TODO: Remove - printf("Directive:\t%s\t(%s)\n", token, directive_data); + printf("Directive:\t%s\n", token); // TODO: Process directive - int result = 0; - if (strcmpn(token, ".label", 6) == 0) - { - result = directive_label(ctx); - } - // Advance to next line - ctx->line++; - ctx->column = 0; - // Reset directive data - directive_data_length = 0; - directive_data[0] = '\0'; - return result; + return 0; } static int parse_token(xyw_context *ctx)

@@ -384,6 +359,18 @@ {

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"); + fclose(ctx->fp); + return -1; + } + directive_data[directive_data_length++] = (char)c; + directive_data[directive_data_length] = '\0'; + } } else {

@@ -421,6 +408,8 @@ token[0] = '\0';

} ctx->line++; ctx->column = 1; + // Directives are line-based, so reset directive data + reset_directive(); } else {