Updates.
h3rald h3rald@h3rald.com
Tue, 26 Nov 2024 10:32:27 +0100
M
hex.c
→
hex.c
@@ -568,7 +568,7 @@ int len = ptr - start;
token->value = (char *)malloc(len + 1); strncpy(token->value, start, len); token->value[len] = '\0'; - token->type = HEX_TOKEN_NUMBER; + token->type = HEX_TOKEN_INTEGER; } else if (*ptr == '(') {@@ -634,6 +634,15 @@ }
return 0; } +int32_t hex_parse_integer(const char *hex_str) +{ + // Parse the hexadecimal string as an unsigned 32-bit integer + uint32_t unsigned_value = (uint32_t)strtoul(hex_str, NULL, 16); + + // Cast the unsigned value to a signed 32-bit integer + return (int32_t)unsigned_value; +} + int hex_parse_quotation(const char **input, hex_item_t *result, const char *filename, int *line, int *column) { hex_item_t **quotation = NULL;@@ -669,10 +678,10 @@ }
} hex_item_t *element = (hex_item_t *)malloc(sizeof(hex_item_t)); - if (token->type == HEX_TOKEN_NUMBER) + if (token->type == HEX_TOKEN_INTEGER) { element->type = HEX_TYPE_INTEGER; - element->data.intValue = (int)strtol(token->value, NULL, 16); + element->data.intValue = hex_parse_integer(token->value); } else if (token->type == HEX_TOKEN_STRING) {@@ -3539,9 +3548,9 @@
while (token != NULL && token->type != HEX_TOKEN_INVALID) { int result = 0; - if (token->type == HEX_TOKEN_NUMBER) + if (token->type == HEX_TOKEN_INTEGER) { - result = hex_push_int((int)strtol(token->value, NULL, 16)); + result = hex_push_int(hex_parse_integer(token->value)); } else if (token->type == HEX_TOKEN_STRING) {
M
hex.h
→
hex.h
@@ -8,6 +8,7 @@ #include <string.h>
#include <ctype.h> #include <signal.h> #include <stdarg.h> +#include <stdint.h> #ifdef _WIN32 #include <windows.h>@@ -38,7 +39,7 @@ } hex_item_type_t;
typedef enum hex_token_type_t { - HEX_TOKEN_NUMBER, + HEX_TOKEN_INTEGER, HEX_TOKEN_STRING, HEX_TOKEN_SYMBOL, HEX_TOKEN_QUOTATION_START,@@ -61,7 +62,7 @@ {
hex_item_type_t type; union { - int intValue; + int32_t intValue; char *strValue; int (*functionPointer)(); struct hex_item_t **quotationValue;@@ -229,6 +230,7 @@ void hex_process_stdin();
void hex_handle_sigint(int sig); char *hex_read_file(const char *filename); +int32_t hex_parse_integer(const char *hex_str); int hex_parse_quotation(const char **input, hex_item_t *result, const char *filename, int *line, int *column); int hex_interpret(const char *code, const char *filename, int line, int column);
M
tests.hex
→
tests.hex
@@ -20,7 +20,7 @@ if
) ( ; Store error - errors " - Test #" test-count str cat ": " cat error cat q cat "errors" store + errors " - Test #" test-count dec cat ": " cat error cat q cat "errors" store "x" print failures 0x1 + "failures" store )@@ -29,8 +29,27 @@ ) "test" store
; --- Tests + (0x1 "a" store a 0x1 ==) test i ("a" free 0x1) test i +("aaa" type "string" == 0x1 type "integer" == () type "quotation" == and and) test i +((0x1 0x2 +) i 0x3 ==) test i +("0x2 0x2 -" eval 0x0 ==) test i + +(0x4 0x5 + 0x9 ==) test i +(0x5 0x3 - 0x2 ==) test i +(0x5 0x2 * 0xa ==) test i +(0x5 0x2 / 0x2 ==) test i +(0x4 0x2 % 0x0 ==) test i + +(0x10101 0x01010 & 0x0 ==) test i +(0x10111 0x01000 | 0x11111 ==) test i +(0x1 0x1 ^ 0x0 ==) test i +(0x1 ~ 0xfffffffe ==) test i +(0x1 0x4 << 0x10 ==) test i + +(0x10 0x4 >> 0x1 ==) test i + ; --- Report "\nSuccessful Tests: " print successes dec print "/" print successes failures + dec puts