Refactoring number processing.
h3rald h3rald@h3rald.com
Fri, 28 Nov 2025 15:56:48 +0100
1 files changed,
67 insertions(+),
109 deletions(-)
jump to
M
xywasm.c
→
xywasm.c
@@ -252,7 +252,7 @@ {
if (val > 0xff) { data[addr] = val & 0xFF; - data[addr] = (val >> 8) & 0xFF; + data[addr+1] = (val >> 8) & 0xFF; return; } data[addr] = (xyw_byte)val;@@ -336,103 +336,81 @@ dbg("Comment:\t%s\n", token);
return 0; } -static int process_hex_number(xyw_context *ctx) +static int process_number(xyw_context *ctx) { - // Token is a hex number starting with $ xyw_word value = 0; - for (unsigned int i = 1; i < token_length; i++) + unsigned int i = 0; + int negative = 0; + if (token[0] == '$') { - char c = token[i]; - value <<= 4; - if (c >= '0' && c <= '9') + for (i = 1; i < token_length; i++) { - value |= (c - '0'); + char c = token[i]; + value <<= 4; + if (c >= '0' && c <= '9') + { + value |= (c - '0'); + } + else if (c >= 'A' && c <= 'F') + { + value |= (c - 'A' + 10); + } + else if (c >= 'a' && c <= 'f') + { + value |= (c - 'a' + 10); + } + else + { + token_error("Invalid hex digit"); + return -1; + } } - else if (c >= 'A' && c <= 'F') - { - value |= (c - 'A' + 10); - } - else if (c >= 'a' && c <= 'f') - { - value |= (c - 'a' + 10); - } - else - { - token_error("Invalid hex digit"); - return -1; - } + dbg("Hex number:\t%s\t(%04X)\n", token, value); } - dbg("Hex number:\t%s\t(%04X)\n", token, value); - token_type = TOKEN_TYPE_NUMBER; - token_value = value; - if (!directive) - { - push(value); - } - return 0; -} - -static int process_binary_number(xyw_context *ctx) -{ - // Token is a binary number starting with % - xyw_word value = 0; - for (unsigned int i = 1; i < token_length; i++) + else if (token[0] == '%') { - char c = token[i]; - value <<= 1; - if (c == '0') + for (i = 1; i < token_length; i++) { - // do nothing - } - else if (c == '1') - { - value |= 1; - } - else - { - token_error("Invalid binary digit"); - return -1; + char c = token[i]; + value <<= 1; + if (c == '1') + { + value |= 1; + } + else if (c != '0') + { + token_error("Invalid binary digit"); + return -1; + } } - } - dbg("Binary number:\t%s\t(%04X)\n", token, value); - token_type = TOKEN_TYPE_NUMBER; - token_value = value; - if (!directive) - { - push(value); - } - return 0; -} - -static int process_decimal_number(xyw_context *ctx) -{ - // Token is a decimal number (may or may not start with -) - xyw_word value = 0; - unsigned int start_index = 0; - int is_negative = 0; - if (token[0] == '-') - { - is_negative = 1; - start_index = 1; + dbg("Binary number:\t%s\t(%04X)\n", token, value); } - for (unsigned int i = start_index; i < token_length; i++) + else { - char c = token[i]; - if (c >= '0' && c <= '9') + if (token[0] == '-') + { + negative = 1; + i = 1; + } + for (; i < token_length; i++) { - value = value * 10 + (c - '0'); + char c = token[i]; + if (c >= '0' && c <= '9') + { + value = value * 10 + (c - '0'); + } + else + { + token_error("Invalid decimal digit"); + return -1; + } } - else + if (negative) { - token_error("Invalid decimal digit"); - return -1; + value = -value; } + dbg("Decimal number:\t%s\t(%04X)\n", token, value); } - if (is_negative) - { - value = -value; - } - dbg("Decimal number:\t%s\t(%04X)\n", token, value); token_type = TOKEN_TYPE_NUMBER; token_value = value; if (!directive)@@ -440,22 +418,6 @@ {
push(value); } return 0; -} - -static int process_number(xyw_context *ctx) -{ - if (token[0] == '$') - { - return process_hex_number(ctx); - } - else if (token[0] == '%') - { - return process_binary_number(ctx); - } - else - { - return process_decimal_number(ctx); - } } static int process_instruction(xyw_context *ctx)@@ -790,21 +752,17 @@ else if (token[0] == '.')
{ return process_directive(ctx); } - else if (token[0] == '$') - { - return process_hex_number(ctx); - } - else if (token[0] == '%') - { - return process_binary_number(ctx); - } - else if (token[0] == '\"') + else if (token[0] == '$' || token[0] == '%' || token[0] == '"') { - return process_string(ctx); + if (token[0] == '"') + { + return process_string(ctx); + } + return process_number(ctx); } else if ((token[0] >= '0' && token[0] <= '9') || token[0] == '-') { - return process_decimal_number(ctx); + return process_number(ctx); } else if ((token[0] >= 'A' && token[0] <= 'Z') || (token[0] >= 'a' && token[0] <= 'z') || token[0] == '_') {