Now able to parse all directives; refactoring.
h3rald h3rald@h3rald.com
Thu, 27 Nov 2025 14:43:14 +0100
1 files changed,
54 insertions(+),
67 deletions(-)
jump to
M
xywasm.c
→
xywasm.c
@@ -62,14 +62,6 @@ static unsigned int token_length = 0;
static xyw_token_type token_type = TOKEN_TYPE_UNKNOWN; static xyw_short token_value = 0; -// Directive identifier -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; - // Bytecode to be written xyw_byte data[0xffff - 0x0100];@@ -120,14 +112,6 @@ return -1;
} } return 0; -} - -static void reset_directive() -{ - directive_length = 0; - directive[0] = '\0'; - directive_data_length = 0; - directive_data[0] = '\0'; } static int validate_single_directive_argument(xyw_context *ctx, xyw_token_terminator r)@@ -171,7 +155,6 @@ {
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) {@@ -226,7 +209,7 @@ return -1;
} } // TODO: Remove - printf("Hex number:\t%s\t(%04X) [directive: %s]\n", token, value, directive_length > 0 ? directive : "N/A"); + printf("Hex number:\t%s\t(%04X)\n", token, value); token_type = TOKEN_TYPE_NUMBER; token_value = value; return 0;@@ -255,7 +238,7 @@ return -1;
} } // TODO: Remove - printf("Binary number:\t%s\t(%04X) [directive: %s]\n", token, value, directive_length > 0 ? directive : "N/A"); + printf("Binary number:\t%s\t(%04X)\n", token, value); token_type = TOKEN_TYPE_NUMBER; token_value = value; return 0;@@ -290,7 +273,7 @@ {
value = -value; } // TODO: Remove - printf("Decimal number:\t%s\t(%04X) [directive: %s]\n", token, value, directive_length > 0 ? directive : "N/A"); + printf("Decimal number:\t%s\t(%04X)\n", token, value); token_type = TOKEN_TYPE_NUMBER; token_value = value; return 0;@@ -353,7 +336,7 @@ }
// TODO: Remove char bits[9]; byte_to_binary(opcode, bits); - printf("Instruction:\t%s\t(%02X - %s) [directive: %s]\n", token, opcode, bits, directive_length > 0 ? directive : "N/A"); + printf("Instruction:\t%s\t(%02X - %s)\n", token, opcode, bits); write(opcode); token_type = TOKEN_TYPE_INSTRUCTION; token_value = opcode;@@ -368,7 +351,7 @@ {
(void)ctx; // Token is a string enclosed in double quotes // TODO: Remove - printf("String:\t\t%s [directive: %s]\n", token, directive_length > 0 ? directive : "N/A"); + printf("String:\t\t%s\n", token); // TODO: Store string data token_type = TOKEN_TYPE_STRING; return 0;@@ -381,7 +364,7 @@ {
return -1; } // TODO: Remove - printf("Symbol:\t\t%s [directive: %s]\n", token, directive_length > 0 ? directive : "N/A"); + printf("Symbol:\t\t%s\n", token); token_type = TOKEN_TYPE_SYMBOL; return 0; }@@ -460,18 +443,40 @@ }
static int process_directive_macro(xyw_context *ctx) { - xyw_token_terminator r; - printf("Directive:\t.macro\n"); - while (1) + next_token(ctx); + if (process_symbol(ctx) != 0) + { + return -1; + } + printf("Directive:\t.macro %s\n", token); + char c; + char contents[DIRECTIVE_CONTENT_MAX_LENGTH]; + int length = 0; + while (fread(&c, 1, 1, ctx->fp)) { - r = next_token(ctx); - // TODO: Store macro contents - if (r == TOKEN_END_EOL || r == TOKEN_END_EOF || r == TOKEN_END_COMMENT) + if (c == '\n') { + ctx->line++; + ctx->column = 1; break; } - write((xyw_byte)token_value); + if (c == ';') + { + break; + } + if (length > DIRECTIVE_CONTENT_MAX_LENGTH) + { + + token_error("Macro contents too long"); + return -1; + } + ctx->column++; + contents[length++] = c; + contents[length] = '\0'; } + // TODO: remove + printf("Macro contents:\t%s\n", contents); + // TODO: process macro return 0; }@@ -481,45 +486,38 @@ if (validate_symbol(ctx, token + 1, token_length - 1) != 0)
{ return -1; } - directive_length = token_length; - for (unsigned int i = 0; i < directive_length; i++) - { - directive[i] = token[i]; - } - directive[directive_length] = '\0'; token_type = TOKEN_TYPE_DIRECTIVE; - // TODO: Process directive - if (strcmpn(directive, ".label", 6) == 0) + int r; + if (strcmpn(token, ".label", 6) == 0) { - return process_directive_label(ctx); + r = process_directive_label(ctx); } - else if (strcmpn(directive, ".include", 8) == 0) + else if (strcmpn(token, ".include", 8) == 0) { - return process_directive_include(ctx); + r = process_directive_include(ctx); } - else if (strcmpn(directive, ".string", 7) == 0) + else if (strcmpn(token, ".string", 7) == 0) { - return process_directive_string(ctx); + r = process_directive_string(ctx); } - else if (strcmpn(directive, ".org", 4) == 0) + else if (strcmpn(token, ".org", 4) == 0) { - return process_directive_org(ctx); + r = process_directive_org(ctx); } - else if (strcmpn(directive, ".byte", 5) == 0) + else if (strcmpn(token, ".byte", 5) == 0) { - return process_directive_byte(ctx); + r = process_directive_byte(ctx); } - else if (strcmpn(directive, ".macro", 6) == 0) + else if (strcmpn(token, ".macro", 6) == 0) { - return process_directive_macro(ctx); + r = process_directive_macro(ctx); } else { token_error("Unknown directive"); - return -1; + r = -1; } - reset_directive(); - return 0; + return r; } // Process and validate tokens@@ -567,11 +565,10 @@ }
static xyw_token_terminator next_token(xyw_context *ctx) { - unsigned char c; + char c; + int in_string = 0; token_length = 0; token[0] = '\0'; - int in_string = 0; - while (fread(&c, 1, 1, ctx->fp) == 1) { if (!in_string && c == '\n')@@ -582,9 +579,9 @@ return TOKEN_END_EOL;
} else if (!in_string && token_length == 0 && c == ';') { + ctx->column++; token[token_length++] = (char)c; token[token_length] = '\0'; - ctx->column++; return TOKEN_END_COMMENT; } else if (c == '"')@@ -634,16 +631,6 @@ return TOKEN_END_INVALID;
} token[token_length++] = (char)c; token[token_length] = '\0'; - if (directive_length > 0) - { - if (directive_data_length >= DIRECTIVE_CONTENT_MAX_LENGTH - 1) - { - token_error("Directive data too long"); - return TOKEN_END_INVALID; - } - directive_data[directive_data_length++] = (char)c; - directive_data[directive_data_length] = '\0'; - } } else {@@ -651,7 +638,7 @@ token_error("Invalid character encountered");
return TOKEN_END_INVALID; } } - return TOKEN_END_EOF; // EOF no token + return TOKEN_END_EOF; } // Read a file and process its tokens