Started implementing token types.
h3rald h3rald@h3rald.com
Wed, 26 Nov 2025 15:59:04 +0100
1 files changed,
45 insertions(+),
5 deletions(-)
jump to
M
xywasm.c
→
xywasm.c
@@ -14,6 +14,8 @@ #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) #define symbol_error(symbol, msg) fprintf(stderr, "Error - %s: %s [%s - Ln %d, Col %d]\n", symbol, msg, ctx->file, ctx->line, ctx->column) +#define PSH 0x01 + typedef struct { int line;@@ -21,6 +23,17 @@ int column;
char *file; FILE *fp; } xyw_context; + +typedef enum +{ + TOKEN_TYPE_UNKNOWN, + TOKEN_TYPE_COMMENT, + TOKEN_TYPE_DIRECTIVE, + TOKEN_TYPE_NUMBER, + TOKEN_TYPE_INSTRUCTION, + TOKEN_TYPE_STRING, + TOKEN_TYPE_SYMBOL +} xyw_token_type; // Instruction mnemonics static char instructions[][4] = {@@ -36,6 +49,7 @@
// The token being processed static char token[TOKEN_MAX_LENGTH]; static unsigned int token_length = 0; +static xyw_token_type token_type = TOKEN_TYPE_UNKNOWN; // Directive identifier static char directive[TOKEN_MAX_LENGTH];@@ -47,6 +61,9 @@ static unsigned int directive_data_length = 0;
// Bytecode to be written xyw_byte data[0xffff - 0x0100]; + +// Current write pointer +xyw_short ptr = 0x0100; //// Utilities@@ -101,6 +118,25 @@ directive_data_length = 0;
directive_data[0] = '\0'; } +// Write raw bytecode data +static inline void write(xyw_short val) +{ + if (val > 0xff) + { + data[ptr++] = val & 0xFF; + data[ptr++] = (val >> 8) & 0xFF; + return; + } + data[ptr++] = (xyw_byte)val; +} + +// Encode a push instruction +static inline void push(xyw_short val) +{ + (val > 0xff) ? write(PSH | MODE_W) : write(PSH); + write(val); +} + //// Processing functions static int process_comment(xyw_context *ctx)@@ -132,6 +168,7 @@ }
} // TODO: Remove printf("Comment:\t%s%s\n", token, comment_data); + token_type = TOKEN_TYPE_COMMENT; return 0; }@@ -163,7 +200,7 @@ }
} // TODO: Remove printf("Hex number:\t%s\t(%04X) [directive: %s]\n", token, value, directive_length > 0 ? directive : "N/A"); - // TODO: Store value + token_type = TOKEN_TYPE_NUMBER; return 0; }@@ -191,7 +228,7 @@ }
} // TODO: Remove printf("Binary number:\t%s\t(%04X) [directive: %s]\n", token, value, directive_length > 0 ? directive : "N/A"); - // TODO: Store value + token_type = TOKEN_TYPE_NUMBER; return 0; }@@ -225,7 +262,7 @@ value = -value;
} // TODO: Remove printf("Decimal number:\t%s\t(%04X) [directive: %s]\n", token, value, directive_length > 0 ? directive : "N/A"); - // TODO: Store value + token_type = TOKEN_TYPE_NUMBER; return 0; }@@ -271,7 +308,8 @@ // 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"); - // TODO: Store opcode + write(opcode); + token_type = TOKEN_TYPE_INSTRUCTION; return 0; } }@@ -285,6 +323,7 @@ // 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"); // TODO: Store string data + token_type = TOKEN_TYPE_STRING; return 0; }@@ -296,7 +335,7 @@ return -1;
} // TODO: Remove printf("Symbol:\t\t%s [directive: %s]\n", token, directive_length > 0 ? directive : "N/A"); - // TODO: Process symbol + token_type = TOKEN_TYPE_SYMBOL; return 0; }@@ -314,6 +353,7 @@ }
directive[directive_length] = '\0'; // TODO: Remove printf("Directive:\t%s\n", token); + token_type = TOKEN_TYPE_DIRECTIVE; // TODO: Process directive return 0; }