Fixed assembler parsing.
h3rald h3rald@h3rald.com
Tue, 25 Nov 2025 14:27:09 +0100
1 files changed,
57 insertions(+),
53 deletions(-)
jump to
M
xywasm.c
→
xywasm.c
@@ -9,8 +9,8 @@ #define MODE_X 0x20
#define MODE_Y 0x40 #define MODE_W 0x80 -#define error(msg) fprintf(stderr, "Error - %s [File: %s]\n", msg, ctx->file) -#define token_error(msg) fprintf(stderr, "Error - %s: %s [File: %s, Line: %d, Column %d: ]\n", token, msg, ctx->file, ctx->line, ctx->column) +#define error(msg) fprintf(stderr, "Error - %s [%s]\n", msg, ctx->file) +#define token_error(msg) fprintf(stderr, "Error - %s: %s [%s - Ln %d, Col %d]\n", token, msg, ctx->file, ctx->line, ctx->column) typedef struct {@@ -100,41 +100,6 @@ printf("Comment:\t%s%s\n", token, comment_data);
return 0; } -static int parse_directive(xyw_context *ctx) -{ - // Parse labels starting with . until end of line - char c; - char directive_data[DIRECTIVE_MAX_LENGTH]; - unsigned int directive_length = 0; - while (fread(&c, 1, 1, ctx->fp) == 1) - { - if (c == '\n') - { - ctx->line++; - ctx->column = 1; - break; - } - else - { - if (directive_length < DIRECTIVE_MAX_LENGTH - 1) - { - directive_data[directive_length++] = c; - directive_data[directive_length] = '\0'; - } - else - { - token_error("Directive too long"); - return -1; - } - } - ctx->column++; - } - // TODO: Remove - printf("Directive:\t%s %s\n", token, directive_data); - // TODO: Process directive - return 0; -} - static int parse_hex_number(xyw_context *ctx) { // Token is a hex number starting with $@@ -142,7 +107,6 @@ xyw_short value = 0;
for (unsigned int i = 1; i < token_length; i++) { char c = token[i]; - ctx->column++; value <<= 4; if (c >= '0' && c <= '9') {@@ -163,7 +127,7 @@ return -1;
} } // TODO: Remove - printf("Hex number:\t%s (%04X)\n", token, value); + printf("Hex number:\t%s\t(%04X)\n", token, value); // TODO: Store value return 0; }@@ -175,7 +139,6 @@ xyw_short value = 0;
for (unsigned int i = 1; i < token_length; i++) { char c = token[i]; - ctx->column++; value <<= 1; if (c == '0') {@@ -192,7 +155,7 @@ return -1;
} } // TODO: Remove - printf("Binary number:\t%s (%04X)\n", token, value); + printf("Binary number:\t%s\t(%04X)\n", token, value); // TODO: Store value return 0; }@@ -211,7 +174,6 @@ }
for (unsigned int i = start_index; i < token_length; i++) { char c = token[i]; - ctx->column++; if (c >= '0' && c <= '9') { value = value * 10 + (c - '0');@@ -227,13 +189,14 @@ {
value = -value; } // TODO: Remove - printf("Decimal number:\t%s (%04X)\n", token, value); + printf("Decimal number:\t%s\t(%04X)\n", token, value); // TODO: Store value return 0; } static int parse_instruction(xyw_context *ctx) { + (void)ctx; xyw_byte opcode = 0; // Match token against instruction mnemonics for (unsigned int i = 0; i < sizeof(instructions) / sizeof(instructions[0]); i++)@@ -241,14 +204,12 @@ {
if (strcmpn(token, instructions[i], 3) == 0) { opcode = (xyw_byte)i; - ctx->column += 3; // There could be 0 to 3 modes (x y z) after the mnemonic in any order if (token_length > 3) { for (unsigned int j = 3; j < token_length; j++) { char mode_char = token[j]; - ctx->column++; int mode_found = 0; if (mode_char == 'x' || mode_char == 'X') {@@ -267,7 +228,6 @@ mode_found = 1;
} if (!mode_found) { - token_error("Invalid mode character"); return -1; } }@@ -283,28 +243,72 @@ }
return -1; } -static int parse_symbol(xyw_context *ctx) +static int validate_identifier(xyw_context *ctx, unsigned int start_index) { - // Symbols must start with a letter or underscore, + // Identifiers must start with a letter or underscore, // and they can contain letters, numbers, underscores, dashes, or dots. - if (!((token[0] >= 'A' && token[0] <= 'Z') || (token[0] >= 'a' && token[0] <= 'z') || token[0] == '_')) + if (!((token[start_index] >= 'A' && token[start_index] <= 'Z') || (token[start_index] >= 'a' && token[start_index] <= 'z') || token[start_index] == '_')) { - token_error("Invalid symbol start character"); + token_error("Invalid start character for identifier"); return -1; } - for (unsigned int i = 1; i < token_length; i++) + for (unsigned int i = start_index + 1; i < token_length; i++) { char c = token[i]; - ctx->column++; if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '_' || c == '-' || c == '.')) { - token_error("Invalid symbol character"); + token_error("Invalid contains invalid characters"); return -1; } } + return 0; +} + +static int parse_symbol(xyw_context *ctx) +{ + if (validate_identifier(ctx, 0) != 0) + { + return -1; + } // TODO: Remove printf("Symbol:\t\t%s\n", token); // TODO: Process symbol + return 0; +} + +static int parse_directive(xyw_context *ctx) +{ + if (validate_identifier(ctx, 1) != 0) + { + return -1; + } + // Parse labels starting with . until end of line + char c; + char directive_data[DIRECTIVE_MAX_LENGTH]; + unsigned int directive_length = 0; + while (fread(&c, 1, 1, ctx->fp) == 1) + { + if (c == '\n') + { + ctx->line++; + ctx->column = 1; + break; + } + ctx->column++; + if (directive_length < DIRECTIVE_MAX_LENGTH - 1) + { + directive_data[directive_length++] = c; + directive_data[directive_length] = '\0'; + } + else + { + token_error("Directive too long"); + return -1; + } + } + // TODO: Remove + printf("Directive:\t%s\t(%s)\n", token, directive_data); + // TODO: Process directive return 0; }