all repos — hex @ 894b69111b3ffc91286e8c50f06b680afe9160c7

A tiny, minimalist, slightly-esoteric concatenative programming lannguage.

Added prefixes.
h3rald h3rald@h3rald.com
Mon, 18 Nov 2024 07:33:40 +0100
commit

894b69111b3ffc91286e8c50f06b680afe9160c7

parent

435d9dedb70a46f5e5db227debb7ebd3e457204f

2 files changed, 351 insertions(+), 311 deletions(-)

jump to
A .vscode/settings.json

@@ -0,0 +1,8 @@

+{ + "files.associations": { + "*.erd": "json", + "*.vuerd": "json", + "*.mjs": "javascript", + "string.h": "c" + } +}
M hex.chex.c

@@ -6,7 +6,8 @@ #include <signal.h>

#ifdef _WIN32 #include <windows.h> #include <io.h> -int isatty(int fd) { +int hex_isatty(int fd) +{ HANDLE h = (HANDLE)_get_osfhandle(fd); return (GetFileType(h) == FILE_TYPE_CHAR); }

@@ -14,102 +15,103 @@ #else

#include <unistd.h> #endif - // Enum to represent the type of stack elements typedef enum { - TYPE_INTEGER, - TYPE_STRING, - TYPE_QUOTATION, - TYPE_FUNCTION -} ElementType; + HEX_TYPE_INTEGER, + HEX_TYPE_STRING, + HEX_TYPE_QUOTATION, + HEX_TYPE_FUNCTION +} HEX_ElementType; // Unified Stack Element -typedef struct StackElement +typedef struct HEX_StackElement { - ElementType type; + HEX_ElementType type; union { - int intValue; - char *strValue; + int intValue; + char *strValue; void (*functionPointer)(); - struct StackElement **quotationValue; + struct HEX_StackElement **quotationValue; } data; - size_t quotationSize; // Size of the quotation (valid for TYPE_QUOTATION) -} StackElement; + size_t quotationSize; // Size of the quotation (valid for HEX_TYPE_QUOTATION) +} HEX_StackElement; // Dictionary Entry typedef struct { char *key; - StackElement value; -} DictionaryEntry; + HEX_StackElement value; +} HEX_DictionaryEntry; // Size of STDIN buffer (gets operator) -#define STDIN_BUFFER_SIZE 256 +#define HEX_STDIN_BUFFER_SIZE 256 // Global Dictionary for Variables -#define DICTIONARY_SIZE 1024 -DictionaryEntry dictionary[DICTIONARY_SIZE]; -int dictCount = 0; +#define HEX_DICTIONARY_SIZE 1024 +HEX_DictionaryEntry hex_dictionary[HEX_DICTIONARY_SIZE]; +int hex_dictCount = 0; -void free_element(StackElement element); +void hex_free_element(HEX_StackElement element); -void fail(char *message) +void hex_fail(char *message) { fprintf(stderr, "%s\n", message); } // Function to add a variable to the dictionary -int set_variable(const char *key, StackElement value) +int hex_set_variable(const char *key, HEX_StackElement value) { - for (int i = 0; i < dictCount; i++) + for (int i = 0; i < hex_dictCount; i++) { - if (strcmp(dictionary[i].key, key) == 0) + if (strcmp(hex_dictionary[i].key, key) == 0) { - if (dictionary[i].value.type == TYPE_FUNCTION){ + if (hex_dictionary[i].value.type == HEX_TYPE_FUNCTION) + { fprintf(stderr, "Cannot overwrite native symbol %s", key); return 0; } - free(dictionary[i].key); - free_element(dictionary[i].value); - dictionary[i].key = strdup(key); - dictionary[i].value = value; + free(hex_dictionary[i].key); + hex_free_element(hex_dictionary[i].value); + hex_dictionary[i].key = strdup(key); + hex_dictionary[i].value = value; return 0; } } - if (dictCount >= DICTIONARY_SIZE) + if (hex_dictCount >= HEX_DICTIONARY_SIZE) { - fail("Dictionary overflow"); + hex_fail("Dictionary overflow"); return 1; } - dictionary[dictCount].key = strdup(key); - dictionary[dictCount].value = value; - dictCount++; + hex_dictionary[hex_dictCount].key = strdup(key); + hex_dictionary[hex_dictCount].value = value; + hex_dictCount++; return 1; } -void register_symbol(const char *name, void (*func)()) +void hex_register_symbol(const char *name, void (*func)()) { - StackElement funcElement; - funcElement.type = TYPE_FUNCTION; + HEX_StackElement funcElement; + funcElement.type = HEX_TYPE_FUNCTION; funcElement.data.functionPointer = func; - if (!set_variable(name, funcElement)) { + if (!hex_set_variable(name, funcElement)) + { fprintf(stderr, "Error: Failed to register native symbol '%s'.\n", name); } } // Function to get a variable value from the dictionary -int get_variable(const char *key, StackElement *result) +int hex_get_variable(const char *key, HEX_StackElement *result) { - for (int i = 0; i < dictCount; i++) + for (int i = 0; i < hex_dictCount; i++) { - if (strcmp(dictionary[i].key, key) == 0) + if (strcmp(hex_dictionary[i].key, key) == 0) { - *result = dictionary[i].value; + *result = hex_dictionary[i].value; return 1; } }

@@ -117,60 +119,60 @@ return 0;

} // Stack Definition -#define STACK_SIZE 100 -StackElement stack[STACK_SIZE]; -int top = -1; +#define HEX_STACK_SIZE 100 +HEX_StackElement hex_stack[HEX_STACK_SIZE]; +int hex_top = -1; // Push functions -void push(StackElement element) +void hex_push(HEX_StackElement element) { - if (top >= STACK_SIZE - 1) + if (hex_top >= HEX_STACK_SIZE - 1) { - fail("Stack overflow"); + hex_fail("Stack overflow"); } - stack[++top] = element; + hex_stack[++hex_top] = element; } -void push_int(int value) +void hex_push_int(int value) { - StackElement element = {.type = TYPE_INTEGER, .data.intValue = value}; - push(element); + HEX_StackElement element = {.type = HEX_TYPE_INTEGER, .data.intValue = value}; + hex_push(element); } -void push_string(const char *value) +void hex_push_string(const char *value) { - StackElement element = {.type = TYPE_STRING, .data.strValue = strdup(value)}; - push(element); + HEX_StackElement element = {.type = HEX_TYPE_STRING, .data.strValue = strdup(value)}; + hex_push(element); } -void push_quotation(StackElement **quotation, size_t size) +void hex_push_quotation(HEX_StackElement **quotation, size_t size) { - StackElement element = {.type = TYPE_QUOTATION, .data.quotationValue = quotation, .quotationSize = size}; - push(element); + HEX_StackElement element = {.type = HEX_TYPE_QUOTATION, .data.quotationValue = quotation, .quotationSize = size}; + hex_push(element); } // Pop function -StackElement pop() +HEX_StackElement hex_pop() { - if (top < 0) + if (hex_top < 0) { - fail("Stack underflow"); + hex_fail("Stack underflow"); } - return stack[top--]; + return hex_stack[hex_top--]; } // Free a stack element -void free_element(StackElement element) +void hex_free_element(HEX_StackElement element) { - if (element.type == TYPE_STRING) + if (element.type == HEX_TYPE_STRING) { free(element.data.strValue); } - else if (element.type == TYPE_QUOTATION) + else if (element.type == HEX_TYPE_QUOTATION) { for (size_t i = 0; i < element.quotationSize; i++) { - free_element(*element.data.quotationValue[i]); + hex_free_element(*element.data.quotationValue[i]); free(element.data.quotationValue[i]); } free(element.data.quotationValue);

@@ -180,21 +182,21 @@

// Token Types typedef enum { - TOKEN_NUMBER, - TOKEN_STRING, - TOKEN_OPERATOR, - TOKEN_QUOTATION_START, - TOKEN_QUOTATION_END -} TokenType; + HEX_TOKEN_NUMBER, + HEX_TOKEN_STRING, + HEX_TOKEN_OPERATOR, + HEX_TOKEN_QUOTATION_START, + HEX_TOKEN_QUOTATION_END +} HEX_TokenType; typedef struct { - TokenType type; + HEX_TokenType type; char *value; -} Token; +} HEX_Token; // Tokenizer Implementation -Token *next_token(const char **input) +HEX_Token *hex_next_token(const char **input) { const char *ptr = *input;

@@ -216,7 +218,7 @@ {

return NULL; // End of input } - Token *token = (Token *)malloc(sizeof(Token)); + HEX_Token *token = (HEX_Token *)malloc(sizeof(HEX_Token)); token->value = NULL; if (*ptr == '"')

@@ -246,7 +248,7 @@ }

if (*ptr != '"') { - fail("Unterminated string"); + hex_fail("Unterminated string"); } token->value = (char *)malloc(len + 1);

@@ -267,7 +269,7 @@ }

} *dst = '\0'; ptr++; - token->type = TOKEN_STRING; + token->type = HEX_TOKEN_STRING; } else if (strncmp(ptr, "0x", 2) == 0 || strncmp(ptr, "0X", 2) == 0) {

@@ -282,16 +284,16 @@ size_t len = ptr - start;

token->value = (char *)malloc(len + 1); strncpy(token->value, start, len); token->value[len] = '\0'; - token->type = TOKEN_NUMBER; + token->type = HEX_TOKEN_NUMBER; } else if (*ptr == '(') { - token->type = TOKEN_QUOTATION_START; + token->type = HEX_TOKEN_QUOTATION_START; ptr++; } else if (*ptr == ')') { - token->type = TOKEN_QUOTATION_END; + token->type = HEX_TOKEN_QUOTATION_END; ptr++; } else

@@ -305,7 +307,7 @@ size_t len = ptr - start;

token->value = (char *)malloc(len + 1); strncpy(token->value, start, len); token->value[len] = '\0'; - token->type = TOKEN_OPERATOR; + token->type = HEX_TOKEN_OPERATOR; } *input = ptr;

@@ -313,7 +315,7 @@ return token;

} // Free a token -void free_token(Token *token) +void hex_free_token(HEX_Token *token) { if (token) {

@@ -323,67 +325,67 @@ }

} // Recursive quotation parsing -StackElement **parse_quotation(const char **input, size_t *size) +HEX_StackElement **hex_parse_quotation(const char **input, size_t *size) { - StackElement **quotation = NULL; + HEX_StackElement **quotation = NULL; size_t capacity = 2; *size = 0; - quotation = (StackElement **)malloc(capacity * sizeof(StackElement *)); + quotation = (HEX_StackElement **)malloc(capacity * sizeof(HEX_StackElement *)); if (!quotation) { - fail("Memory allocation failed"); + hex_fail("Memory allocation failed"); } - Token *token; - while ((token = next_token(input)) != NULL) + HEX_Token *token; + while ((token = hex_next_token(input)) != NULL) { - if (token->type == TOKEN_QUOTATION_END) + if (token->type == HEX_TOKEN_QUOTATION_END) { - free_token(token); + hex_free_token(token); break; } if (*size >= capacity) { capacity *= 2; - quotation = (StackElement **)realloc(quotation, capacity * sizeof(StackElement *)); + quotation = (HEX_StackElement **)realloc(quotation, capacity * sizeof(HEX_StackElement *)); if (!quotation) { - fail("Memory allocation failed"); + hex_fail("Memory allocation failed"); } } - StackElement *element = (StackElement *)malloc(sizeof(StackElement)); - if (token->type == TOKEN_NUMBER) + HEX_StackElement *element = (HEX_StackElement *)malloc(sizeof(HEX_StackElement)); + if (token->type == HEX_TOKEN_NUMBER) { - element->type = TYPE_INTEGER; + element->type = HEX_TYPE_INTEGER; element->data.intValue = (int)strtol(token->value, NULL, 16); } - else if (token->type == TOKEN_STRING) + else if (token->type == HEX_TOKEN_STRING) { - element->type = TYPE_STRING; + element->type = HEX_TYPE_STRING; element->data.strValue = strdup(token->value); } - else if (token->type == TOKEN_QUOTATION_START) + else if (token->type == HEX_TOKEN_QUOTATION_START) { - element->type = TYPE_QUOTATION; - element->data.quotationValue = parse_quotation(input, &element->quotationSize); + element->type = HEX_TYPE_QUOTATION; + element->data.quotationValue = hex_parse_quotation(input, &element->quotationSize); } else { - fail("Unexpected token in quotation"); + hex_fail("Unexpected token in quotation"); } quotation[*size] = element; (*size)++; - free_token(token); + hex_free_token(token); } return quotation; } -char *itoa(int num) +char *hex_itoa(int num) { static char str[20]; int i = 0;

@@ -422,81 +424,83 @@

return str; } -void print_element(FILE *stream, StackElement element) +void hex_print_element(FILE *stream, HEX_StackElement element) { switch (element.type) { - case TYPE_INTEGER: + case HEX_TYPE_INTEGER: fprintf(stream, "0x%x", element.data.intValue); break; - case TYPE_STRING: + case HEX_TYPE_STRING: fprintf(stream, "\"%s\"", element.data.strValue); break; - case TYPE_FUNCTION: + case HEX_TYPE_FUNCTION: fprintf(stream, "<native>"); break; - case TYPE_QUOTATION: + case HEX_TYPE_QUOTATION: { fprintf(stream, "("); for (size_t i = 0; i < element.quotationSize; i++) { if (i > 0) fprintf(stream, " "); // Add space between elements - print_element(stream, *element.data.quotationValue[i]); + hex_print_element(stream, *element.data.quotationValue[i]); } fprintf(stream, ")"); break; } default: - fail("Unknown element type"); + hex_fail("Unknown element type"); } } -int is_operator(Token *token, char *value) +int hex_is_operator(HEX_Token *token, char *value) { return strcmp(token->value, value) == 0; } -void operator_define() +void hex_operator_define() { - StackElement name = pop(); - StackElement value = pop(); - if (name.type != TYPE_STRING) + HEX_StackElement name = hex_pop(); + HEX_StackElement value = hex_pop(); + if (name.type != HEX_TYPE_STRING) { - fail("Variable name must be a string"); + hex_fail("Variable name must be a string"); } - if (set_variable(name.data.strValue, value)) {} - free_element(name); + if (hex_set_variable(name.data.strValue, value)) + { + } + hex_free_element(name); } // IO Operators -void operator_puts() +void hex_operator_puts() { - StackElement element = pop(); - print_element(stdout, element); + HEX_StackElement element = hex_pop(); + hex_print_element(stdout, element); printf("\n"); - free_element(element); + hex_free_element(element); } -void operator_warn() +void hex_operator_warn() { - StackElement element = pop(); - print_element(stderr, element); + HEX_StackElement element = hex_pop(); + hex_print_element(stderr, element); printf("\n"); - free_element(element); + hex_free_element(element); } -void operator_print() +void hex_operator_print() { - StackElement element = pop(); - print_element(stdout, element); - free_element(element); + HEX_StackElement element = hex_pop(); + hex_print_element(stdout, element); + hex_free_element(element); } -void operator_gets() +void hex_operator_gets() { - char input[STDIN_BUFFER_SIZE]; // Buffer to store the input (adjust size if needed) + char input[HEX_STDIN_BUFFER_SIZE]; // Buffer to store the input (adjust size if needed) if (fgets(input, sizeof(input), stdin) != NULL) {

@@ -504,276 +508,293 @@ // Strip the newline character at the end of the string

input[strcspn(input, "\n")] = '\0'; // Push the input string onto the stack - push_string(input); + hex_push_string(input); } else { - fail("Failed to read input"); + hex_fail("Failed to read input"); } } // Mathematical operators -void operator_add() +void hex_operator_add() { - StackElement b = pop(); - StackElement a = pop(); - if (a.type == TYPE_INTEGER && b.type == TYPE_INTEGER) + HEX_StackElement b = hex_pop(); + HEX_StackElement a = hex_pop(); + if (a.type == HEX_TYPE_INTEGER && b.type == HEX_TYPE_INTEGER) { - push_int(a.data.intValue + b.data.intValue); + hex_push_int(a.data.intValue + b.data.intValue); } else { - fail("'+' operator requires two integers"); + hex_fail("'+' operator requires two integers"); } - free_element(a); - free_element(b); + hex_free_element(a); + hex_free_element(b); } -void operator_subtract() +void hex_operator_subtract() { - StackElement b = pop(); - StackElement a = pop(); - if (a.type == TYPE_INTEGER && b.type == TYPE_INTEGER) + HEX_StackElement b = hex_pop(); + HEX_StackElement a = hex_pop(); + if (a.type == HEX_TYPE_INTEGER && b.type == HEX_TYPE_INTEGER) { - push_int(a.data.intValue - b.data.intValue); + hex_push_int(a.data.intValue - b.data.intValue); } else { - fail("'-' operator requires two integers"); + hex_fail("'-' operator requires two integers"); } - free_element(a); - free_element(b); + hex_free_element(a); + hex_free_element(b); } -void operator_multiply() +void hex_operator_multiply() { - StackElement b = pop(); - StackElement a = pop(); - if (a.type == TYPE_INTEGER && b.type == TYPE_INTEGER) + HEX_StackElement b = hex_pop(); + HEX_StackElement a = hex_pop(); + if (a.type == HEX_TYPE_INTEGER && b.type == HEX_TYPE_INTEGER) { - push_int(a.data.intValue * b.data.intValue); + hex_push_int(a.data.intValue * b.data.intValue); } else { - fail("'*' operator requires two integers"); + hex_fail("'*' operator requires two integers"); } - free_element(a); - free_element(b); + hex_free_element(a); + hex_free_element(b); } -void operator_divide() +void hex_operator_divide() { - StackElement b = pop(); - StackElement a = pop(); - if (a.type == TYPE_INTEGER && b.type == TYPE_INTEGER) + HEX_StackElement b = hex_pop(); + HEX_StackElement a = hex_pop(); + if (a.type == HEX_TYPE_INTEGER && b.type == HEX_TYPE_INTEGER) { if (b.data.intValue == 0) { - fail("Division by zero"); + hex_fail("Division by zero"); } - push_int(a.data.intValue / b.data.intValue); + hex_push_int(a.data.intValue / b.data.intValue); } else { - fail("'/' operator requires two integers"); + hex_fail("'/' operator requires two integers"); } - free_element(a); - free_element(b); + hex_free_element(a); + hex_free_element(b); } // Bit operators -void operator_bitand() +void hex_operator_bitand() { - StackElement right = pop(); // Pop the second operand - StackElement left = pop(); // Pop the first operand - if (left.type == TYPE_INTEGER && right.type == TYPE_INTEGER) { - int result = left.data.intValue & right.data.intValue; // Bitwise AND - push_int(result); // Push the result back onto the stack - } else { - fail("Bitwise AND requires two integers"); + HEX_StackElement right = hex_pop(); // Pop the second operand + HEX_StackElement left = hex_pop(); // Pop the first operand + if (left.type == HEX_TYPE_INTEGER && right.type == HEX_TYPE_INTEGER) + { + int result = left.data.intValue & right.data.intValue; // Bitwise AND + hex_push_int(result); // Push the result back onto the stack + } + else + { + hex_fail("Bitwise AND requires two integers"); } } -void operator_bitor() +void hex_operator_bitor() { - StackElement right = pop(); - StackElement left = pop(); - if (left.type == TYPE_INTEGER && right.type == TYPE_INTEGER) { - int result = left.data.intValue | right.data.intValue; // Bitwise OR - push_int(result); - } else { - fail("Bitwise OR requires two integers"); + HEX_StackElement right = hex_pop(); + HEX_StackElement left = hex_pop(); + if (left.type == HEX_TYPE_INTEGER && right.type == HEX_TYPE_INTEGER) + { + int result = left.data.intValue | right.data.intValue; // Bitwise OR + hex_push_int(result); + } + else + { + hex_fail("Bitwise OR requires two integers"); } } -void operator_bitxor() +void hex_operator_bitxor() { - StackElement right = pop(); - StackElement left = pop(); - if (left.type == TYPE_INTEGER && right.type == TYPE_INTEGER) { - int result = left.data.intValue ^ right.data.intValue; // Bitwise XOR - push_int(result); - } else { - fail("Bitwise XOR requires two integers"); + HEX_StackElement right = hex_pop(); + HEX_StackElement left = hex_pop(); + if (left.type == HEX_TYPE_INTEGER && right.type == HEX_TYPE_INTEGER) + { + int result = left.data.intValue ^ right.data.intValue; // Bitwise XOR + hex_push_int(result); + } + else + { + hex_fail("Bitwise XOR requires two integers"); } } -void operator_shiftleft() +void hex_operator_shiftleft() { - StackElement right = pop(); // The number of positions to shift - StackElement left = pop(); // The value to shift - if (left.type == TYPE_INTEGER && right.type == TYPE_INTEGER) { - int result = left.data.intValue << right.data.intValue; // Left shift - push_int(result); - } else { - fail("Left shift requires two integers"); + HEX_StackElement right = hex_pop(); // The number of positions to shift + HEX_StackElement left = hex_pop(); // The value to shift + if (left.type == HEX_TYPE_INTEGER && right.type == HEX_TYPE_INTEGER) + { + int result = left.data.intValue << right.data.intValue; // Left shift + hex_push_int(result); + } + else + { + hex_fail("Left shift requires two integers"); } } -void operator_shiftright() +void hex_operator_shiftright() { - StackElement right = pop(); // The number of positions to shift - StackElement left = pop(); // The value to shift - if (left.type == TYPE_INTEGER && right.type == TYPE_INTEGER) { - int result = left.data.intValue >> right.data.intValue; // Right shift - push_int(result); - } else { - fail("Right shift requires two integers"); + HEX_StackElement right = hex_pop(); // The number of positions to shift + HEX_StackElement left = hex_pop(); // The value to shift + if (left.type == HEX_TYPE_INTEGER && right.type == HEX_TYPE_INTEGER) + { + int result = left.data.intValue >> right.data.intValue; // Right shift + hex_push_int(result); + } + else + { + hex_fail("Right shift requires two integers"); } } -void operator_bitnot() +void hex_operator_bitnot() { - StackElement element = pop(); // Only one operand for bitwise NOT - if (element.type == TYPE_INTEGER) { - int result = ~element.data.intValue; // Bitwise NOT (complement) - push_int(result); - } else { - fail("Bitwise NOT requires one integer"); + HEX_StackElement element = hex_pop(); // Only one operand for bitwise NOT + if (element.type == HEX_TYPE_INTEGER) + { + int result = ~element.data.intValue; // Bitwise NOT (complement) + hex_push_int(result); + } + else + { + hex_fail("Bitwise NOT requires one integer"); } } // Converter operators -void operator_int() +void hex_operator_int() { - StackElement a = pop(); - if (a.type == TYPE_QUOTATION) + HEX_StackElement a = hex_pop(); + if (a.type == HEX_TYPE_QUOTATION) { - fail("Cannot convert a quotation to an integer"); + hex_fail("Cannot convert a quotation to an integer"); } - else if (a.type == TYPE_INTEGER) + else if (a.type == HEX_TYPE_INTEGER) { - push_int(a.data.intValue); + hex_push_int(a.data.intValue); } - else if (a.type == TYPE_STRING) + else if (a.type == HEX_TYPE_STRING) { - push_int(strtol(a.data.strValue, NULL, 16)); + hex_push_int(strtol(a.data.strValue, NULL, 16)); } } -void operator_str() +void hex_operator_str() { - StackElement a = pop(); - if (a.type == TYPE_QUOTATION) + HEX_StackElement a = hex_pop(); + if (a.type == HEX_TYPE_QUOTATION) { - fail("Cannot convert a quotation to a string"); + hex_fail("Cannot convert a quotation to a string"); } - else if (a.type == TYPE_INTEGER) + else if (a.type == HEX_TYPE_INTEGER) { - push_string(itoa(a.data.intValue)); + hex_push_string(hex_itoa(a.data.intValue)); } - else if (a.type == TYPE_STRING) + else if (a.type == HEX_TYPE_STRING) { - push_string(a.data.strValue); + hex_push_string(a.data.strValue); } } -// Process code -void process(const char *code) +void hex_process(const char *code) { const char *input = code; - Token *token; + HEX_Token *token; - while ((token = next_token(&input)) != NULL) + while ((token = hex_next_token(&input)) != NULL) { - if (token->type == TOKEN_NUMBER) + if (token->type == HEX_TOKEN_NUMBER) { - push_int((int)strtol(token->value, NULL, 16)); + hex_push_int((int)strtol(token->value, NULL, 16)); } - else if (token->type == TOKEN_STRING) + else if (token->type == HEX_TOKEN_STRING) { - StackElement variableValue; - if (get_variable(token->value, &variableValue)) + HEX_StackElement variableValue; + if (hex_get_variable(token->value, &variableValue)) { - push(variableValue); + hex_push(variableValue); } else { - push_string(token->value); + hex_push_string(token->value); } } - else if (token->type == TOKEN_OPERATOR) + else if (token->type == HEX_TOKEN_OPERATOR) { if (strcmp(token->value, "define") == 0) { - operator_define(); + hex_operator_define(); } - else if (is_operator(token, "+")) + else if (hex_is_operator(token, "+")) { - operator_add(); + hex_operator_add(); } - else if (is_operator(token, "puts")) + else if (hex_is_operator(token, "puts")) { - operator_puts(); + hex_operator_puts(); } - else if (is_operator(token, "gets")) + else if (hex_is_operator(token, "gets")) { - operator_gets(); + hex_operator_gets(); } - else if (is_operator(token, "&")) + else if (hex_is_operator(token, "&")) { - operator_bitand(); + hex_operator_bitand(); } - else if (is_operator(token, "|")) + else if (hex_is_operator(token, "|")) { - operator_bitor(); + hex_operator_bitor(); } - else if (is_operator(token, "^")) + else if (hex_is_operator(token, "^")) { - operator_bitxor(); + hex_operator_bitxor(); } - else if (is_operator(token, "~")) + else if (hex_is_operator(token, "~")) { - operator_bitnot(); + hex_operator_bitnot(); } - else if (is_operator(token, "<<")) + else if (hex_is_operator(token, "<<")) { - operator_shiftleft(); + hex_operator_shiftleft(); } - else if (is_operator(token, ">>")) + else if (hex_is_operator(token, ">>")) { - operator_shiftright(); + hex_operator_shiftright(); } - else if (is_operator(token, "-")) + else if (hex_is_operator(token, "-")) { - operator_subtract(); + hex_operator_subtract(); } - else if (is_operator(token, "*")) + else if (hex_is_operator(token, "*")) { - operator_multiply(); + hex_operator_multiply(); } - else if (is_operator(token, "/")) + else if (hex_is_operator(token, "/")) { - operator_divide(); + hex_operator_divide(); } - else if (is_operator(token, "int")) + else if (hex_is_operator(token, "int")) { - operator_int(); + hex_operator_int(); } - else if (is_operator(token, "str")) + else if (hex_is_operator(token, "str")) { - operator_str(); + hex_operator_str(); } else {

@@ -781,18 +802,18 @@ fprintf(stderr, "Unknown operator: %s\n", token->value);

exit(EXIT_FAILURE); } } - else if (token->type == TOKEN_QUOTATION_START) + else if (token->type == HEX_TOKEN_QUOTATION_START) { size_t quotationSize; - StackElement **quotation = parse_quotation(&input, &quotationSize); - push_quotation(quotation, quotationSize); + HEX_StackElement **quotation = hex_parse_quotation(&input, &quotationSize); + hex_push_quotation(quotation, quotationSize); } - free_token(token); + hex_free_token(token); } } // Main Function -char *read_file(const char *filename) +char *hex_read_file(const char *filename) { FILE *file = fopen(filename, "r"); if (!file)

@@ -808,7 +829,7 @@

char *buffer = (char *)malloc(length + 1); if (!buffer) { - fail("Memory allocation failed"); + hex_fail("Memory allocation failed"); } fread(buffer, 1, length, file);

@@ -817,16 +838,19 @@ fclose(file);

return buffer; } -volatile sig_atomic_t keep_running = 1; +volatile sig_atomic_t hex_keep_running = 1; -void repl() { +void hex_repl() +{ char line[1024]; - printf("hex Interactive REPL. Type 'exit' to quit or press Ctrl+C.\n"); + printf("Hex Interactive REPL. Type 'exit' to quit or press Ctrl+C.\n"); - while (keep_running) { - printf("> "); // Prompt - if (fgets(line, sizeof(line), stdin) == NULL) { - printf("\n"); // Handle EOF (Ctrl+D) + while (hex_keep_running) + { + printf("> "); // Prompt + if (fgets(line, sizeof(line), stdin) == NULL) + { + printf("\n"); // Handle EOF (Ctrl+D) break; }

@@ -834,57 +858,65 @@ // Normalize line endings (remove trailing \r\n or \n)

line[strcspn(line, "\r\n")] = '\0'; // Exit command - if (strcmp(line, "exit") == 0) { + if (strcmp(line, "exit") == 0) + { break; } // Tokenize and process the input - process(line); + hex_process(line); } } - -void handle_sigint(int sig) { - (void)sig; // Suppress unused warning - keep_running = 0; - printf("\nExiting hex REPL. Goodbye!\n"); +void hex_handle_sigint(int sig) +{ + (void)sig; // Suppress unused warning + hex_keep_running = 0; + printf("\nExiting Hex REPL. Goodbye!\n"); } -void process_stdin() { - char buffer[8192]; // Adjust buffer size as needed +void hex_process_stdin() +{ + char buffer[8192]; // Adjust buffer size as needed size_t bytesRead = fread(buffer, 1, sizeof(buffer) - 1, stdin); - if (bytesRead == 0) { + if (bytesRead == 0) + { fprintf(stderr, "Error: No input provided via stdin.\n"); return; } - buffer[bytesRead] = '\0'; // Null-terminate the input - process(buffer); + buffer[bytesRead] = '\0'; // Null-terminate the input + hex_process(buffer); } -int main(int argc, char *argv[]) { +int main(int argc, char *argv[]) +{ // Register SIGINT (Ctrl+C) signal handler - signal(SIGINT, handle_sigint); + signal(SIGINT, hex_handle_sigint); - if (argc == 2) { + if (argc == 2) + { // Process a file const char *filename = argv[1]; - char *fileContent = read_file(filename); - if (!fileContent) { + char *fileContent = hex_read_file(filename); + if (!fileContent) + { return EXIT_FAILURE; } - process(fileContent); - free(fileContent); // Free the allocated memory - } else if (!isatty(fileno(stdin))) { + hex_process(fileContent); + free(fileContent); // Free the allocated memory + } + else if (!hex_isatty(fileno(stdin))) + { // Process piped input from stdin - process_stdin(); - } else { + hex_process_stdin(); + } + else + { // Start REPL - repl(); + hex_repl(); } return EXIT_SUCCESS; } - -