Merge branch 'master' of github.com:h3rald/hex
@@ -173,6 +173,7 @@ HEX_RegistryEntry HEX_REGISTRY[HEX_REGISTRY_SIZE];
int HEX_REGISTRY_COUNT = 0; void hex_free_element(HEX_StackElement element); +void hex_free_token(HEX_Token *token); int hex_valid_user_symbol(const char *symbol) {@@ -216,8 +217,6 @@ hex_error("Cannot overwrite native symbol %s", key);
return 1; } free(HEX_REGISTRY[i].key); - //hex_free_element(HEX_REGISTRY[i].value); - value.symbolName = strdup(key); HEX_REGISTRY[i].key = strdup(key); HEX_REGISTRY[i].value = value; return 0;@@ -227,7 +226,7 @@
if (HEX_REGISTRY_COUNT >= HEX_REGISTRY_SIZE) { hex_error("Registry overflow"); - free(value.symbolName); + hex_free_token(value.token); return 1; }@@ -243,7 +242,6 @@ {
HEX_StackElement funcElement; funcElement.type = HEX_TYPE_NATIVE_SYMBOL; funcElement.data.functionPointer = func; - funcElement.symbolName = strdup(name); if (hex_set_symbol(name, funcElement, 1) != 0) {@@ -291,13 +289,13 @@ int result = 0;
if (element.type == HEX_TYPE_USER_SYMBOL) { HEX_StackElement value; - if (hex_get_symbol(element.symbolName, &value)) + if (hex_get_symbol(element.token->value, &value)) { result = hex_push(value); } else { - hex_error("Undefined user symbol: %s", element.symbolName); + hex_error("Undefined user symbol: %s", element.token->value); hex_free_element(value); result = 1; }@@ -456,19 +454,13 @@ }
free(element.data.quotationValue); element.data.quotationValue = NULL; } - else if (element.type == HEX_TYPE_NATIVE_SYMBOL && element.symbolName != NULL) + else if (element.type == HEX_TYPE_NATIVE_SYMBOL && element.token->value != NULL) { - free(element.symbolName); - element.symbolName = NULL; hex_free_token(element.token); - element.token = NULL; } - else if (element.type == HEX_TYPE_USER_SYMBOL && element.symbolName != NULL) + else if (element.type == HEX_TYPE_USER_SYMBOL && element.token->value != NULL) { - free(element.symbolName); - element.symbolName = NULL; hex_free_token(element.token); - element.token = NULL; } }@@ -956,7 +948,7 @@ fprintf(stream, "%s", element.data.strValue);
break; case HEX_TYPE_USER_SYMBOL: case HEX_TYPE_NATIVE_SYMBOL: - fprintf(stream, "%s", element.symbolName); + fprintf(stream, "%s", element.token->value); break; case HEX_TYPE_QUOTATION: fprintf(stream, "(");@@ -1036,7 +1028,7 @@ break;
case HEX_TYPE_USER_SYMBOL: case HEX_TYPE_NATIVE_SYMBOL: - fprintf(stream, "%s", element.symbolName); + fprintf(stream, "%s", element.token->value); break; case HEX_TYPE_QUOTATION:@@ -1080,23 +1072,22 @@ {
hex_free_element(name); return 1; } - int result = 0; HEX_StackElement value = hex_pop(); if (value.type == HEX_TYPE_INVALID) { - result = 1; + return 1; } - else if (name.type != HEX_TYPE_STRING) + if (name.type != HEX_TYPE_STRING) { hex_error("Symbol name must be a string"); - result = 1; + return 1; } - else if (hex_set_symbol(name.data.strValue, value, 0) != 0) + if (hex_set_symbol(name.data.strValue, value, 0) != 0) { hex_error("Failed to store variable"); - result = 1; + return 1; } - return result; + return 0; } int hex_symbol_free()@@ -1155,21 +1146,19 @@ {
hex_free_element(element); return 1; } - int result = 0; if (element.type != HEX_TYPE_QUOTATION) { hex_error("'i' symbol requires a quotation"); - result = 1; + return 1; } for (int i = 0; i < element.quotationSize; i++) { if (hex_push(*element.data.quotationValue[i]) != 0) { - result = 1; - break; + return 1; } } - return result; + return 0; } int hex_interpret(const char *code, const char *filename, int line, int column);@@ -1183,14 +1172,12 @@ {
hex_free_element(element); return 1; } - int result = 0; if (element.type != HEX_TYPE_STRING) { hex_error("'eval' symbol requires a string"); - result = 1; + return 1; } - result = hex_interpret(element.data.strValue, "<eval>", 1, 1); - return result; + return hex_interpret(element.data.strValue, "<eval>", 1, 1); } // IO Symbols@@ -1268,17 +1255,12 @@ hex_free_element(a);
hex_free_element(b); return 1; } - int result = 0; if (a.type == HEX_TYPE_INTEGER && b.type == HEX_TYPE_INTEGER) { - result = hex_push_int(a.data.intValue + b.data.intValue); + return hex_push_int(a.data.intValue + b.data.intValue); } - else - { - hex_error("'+' symbol requires two integers"); - result = 1; - } - return result; + hex_error("'+' symbol requires two integers"); + return 1; } int hex_symbol_subtract()@@ -1296,17 +1278,12 @@ hex_free_element(a);
hex_free_element(b); return 1; } - int result = 0; if (a.type == HEX_TYPE_INTEGER && b.type == HEX_TYPE_INTEGER) { - result = hex_push_int(a.data.intValue - b.data.intValue); + return hex_push_int(a.data.intValue - b.data.intValue); } - else - { - hex_error("'-' symbol requires two integers"); - result = 1; - } - return result; + hex_error("'-' symbol requires two integers"); + return 1; } int hex_symbol_multiply()@@ -1324,16 +1301,12 @@ hex_free_element(a);
hex_free_element(b); return 1; } - int result = 0; if (a.type == HEX_TYPE_INTEGER && b.type == HEX_TYPE_INTEGER) { - result = hex_push_int(a.data.intValue * b.data.intValue); - } - else - { - hex_error("'*' symbol requires two integers"); + return hex_push_int(a.data.intValue * b.data.intValue); } - return result; + hex_error("'*' symbol requires two integers"); + return 1; } int hex_symbol_divide()@@ -1351,7 +1324,6 @@ hex_free_element(a);
hex_free_element(b); return 1; } - int result = 0; if (a.type == HEX_TYPE_INTEGER && b.type == HEX_TYPE_INTEGER) { if (b.data.intValue == 0)@@ -1359,14 +1331,10 @@ {
hex_error("Division by zero"); return 1; } - result = hex_push_int(a.data.intValue / b.data.intValue); + return hex_push_int(a.data.intValue / b.data.intValue); } - else - { - hex_error("'/' symbol requires two integers"); - result = 1; - } - return result; + hex_error("'/' symbol requires two integers"); + return 1; } int hex_symbol_modulo()@@ -1384,21 +1352,16 @@ hex_free_element(a);
hex_free_element(b); return 1; } - int result = 0; if (a.type == HEX_TYPE_INTEGER && b.type == HEX_TYPE_INTEGER) { if (b.data.intValue == 0) { hex_error("Division by zero"); } - result = hex_push_int(a.data.intValue % b.data.intValue); + return hex_push_int(a.data.intValue % b.data.intValue); } - else - { - hex_error("'%%' symbol requires two integers"); - result = 1; - } - return result; + hex_error("'%%' symbol requires two integers"); + return 1; } // Bit symbols@@ -1418,18 +1381,12 @@ hex_free_element(left);
hex_free_element(right); return 1; } - int result = 0; if (left.type == HEX_TYPE_INTEGER && right.type == HEX_TYPE_INTEGER) { - int value = left.data.intValue & right.data.intValue; - result = hex_push_int(value); + return hex_push_int(left.data.intValue & right.data.intValue); } - else - { - hex_error("Bitwise AND requires two integers"); - result = 1; - } - return result; + hex_error("Bitwise AND requires two integers"); + return 1; } int hex_symbol_bitor()@@ -1447,18 +1404,12 @@ hex_free_element(left);
hex_free_element(right); return 1; } - int result = 0; if (left.type == HEX_TYPE_INTEGER && right.type == HEX_TYPE_INTEGER) { - int value = left.data.intValue | right.data.intValue; - result = hex_push_int(value); - } - else - { - hex_error("Bitwise OR requires two integers"); - result = 1; + return hex_push_int(left.data.intValue | right.data.intValue); } - return result; + hex_error("Bitwise OR requires two integers"); + return 1; } int hex_symbol_bitxor()@@ -1476,18 +1427,12 @@ hex_free_element(left);
hex_free_element(right); return 1; } - int result = 0; if (left.type == HEX_TYPE_INTEGER && right.type == HEX_TYPE_INTEGER) { - int value = left.data.intValue ^ right.data.intValue; - result = hex_push_int(value); - } - else - { - hex_error("Bitwise XOR requires two integers"); - result = 1; + return hex_push_int(left.data.intValue ^ right.data.intValue); } - return result; + hex_error("Bitwise XOR requires two integers"); + return 1; } int hex_symbol_shiftleft()@@ -1505,18 +1450,12 @@ hex_free_element(left);
hex_free_element(right); return 1; } - int result = 0; if (left.type == HEX_TYPE_INTEGER && right.type == HEX_TYPE_INTEGER) { - int value = left.data.intValue << right.data.intValue; - result = hex_push_int(value); + return hex_push_int(left.data.intValue << right.data.intValue); } - else - { - hex_error("Left shift requires two integers"); - result = 1; - } - return result; + hex_error("Left shift requires two integers"); + return 1; } int hex_symbol_shiftright()@@ -1534,18 +1473,12 @@ hex_free_element(left);
hex_free_element(right); return 1; } - int result = 0; if (left.type == HEX_TYPE_INTEGER && right.type == HEX_TYPE_INTEGER) { - int value = left.data.intValue >> right.data.intValue; - result = hex_push_int(value); + return hex_push_int(left.data.intValue >> right.data.intValue); } - else - { - hex_error("Right shift requires two integers"); - result = 1; - } - return result; + hex_error("Right shift requires two integers"); + return 1; } int hex_symbol_bitnot()@@ -1556,18 +1489,12 @@ {
hex_free_element(element); return 1; } - int result = 0; if (element.type == HEX_TYPE_INTEGER) { - int value = ~element.data.intValue; - result = hex_push_int(value); - } - else - { - hex_error("Bitwise NOT requires one integer"); - result = 1; + return hex_push_int(~element.data.intValue); } - return result; + hex_error("Bitwise NOT requires one integer"); + return 1; } // Conversion symbols@@ -1580,21 +1507,21 @@ {
hex_free_element(a); return 1; } - int result = 0; if (a.type == HEX_TYPE_QUOTATION) { hex_error("Cannot convert a quotation to an integer"); - result = 1; + return 1; } - else if (a.type == HEX_TYPE_INTEGER) + if (a.type == HEX_TYPE_INTEGER) { - result = hex_push_int(a.data.intValue); + return hex_push_int(a.data.intValue); } - else if (a.type == HEX_TYPE_STRING) + if (a.type == HEX_TYPE_STRING) { - result = hex_push_int(strtol(a.data.strValue, NULL, 16)); + return hex_push_int(strtol(a.data.strValue, NULL, 16)); } - return result; + hex_error("Unsupported data type: %s", hex_type(a.type)); + return 1; } int hex_symbol_str()@@ -1605,21 +1532,21 @@ {
hex_free_element(a); return 1; } - int result = 0; if (a.type == HEX_TYPE_QUOTATION) { hex_error("Cannot convert a quotation to a string"); - result = 1; + return 1; } - else if (a.type == HEX_TYPE_INTEGER) + if (a.type == HEX_TYPE_INTEGER) { - result = hex_push_string(hex_itoa_hex(a.data.intValue)); + return hex_push_string(hex_itoa_hex(a.data.intValue)); } - else if (a.type == HEX_TYPE_STRING) + if (a.type == HEX_TYPE_STRING) { - result = hex_push_string(a.data.strValue); + return hex_push_string(a.data.strValue); } - return result; + hex_error("Unsupported data type: %s", hex_type(a.type)); + return 1; } int hex_symbol_dec()@@ -1630,17 +1557,12 @@ {
hex_free_element(a); return 1; } - int result = 0; if (a.type == HEX_TYPE_INTEGER) { - result = hex_push_string(hex_itoa_dec(a.data.intValue)); - } - else - { - hex_error("An integer is required"); - result = 1; + return hex_push_string(hex_itoa_dec(a.data.intValue)); } - return result; + hex_error("An integer is required"); + return 1; } int hex_symbol_hex()@@ -1651,18 +1573,12 @@ {
hex_free_element(element); return 1; } - int result = 0; if (element.type == HEX_TYPE_STRING) { - int value = strtol(element.data.strValue, NULL, 10); - result = hex_push_int(value); + return hex_push_int(strtol(element.data.strValue, NULL, 10)); } - else - { - hex_error("'hex' symbol requires a string representing a decimal integer"); - result = 1; - } - return result; + hex_error("'hex' symbol requires a string representing a decimal integer"); + return 1; } // Comparison symbols@@ -1715,17 +1631,12 @@ hex_free_element(a);
hex_free_element(b); return 1; } - int result = 0; if ((a.type == HEX_TYPE_INTEGER && b.type == HEX_TYPE_INTEGER) || (a.type == HEX_TYPE_STRING && b.type == HEX_TYPE_STRING) || (a.type == HEX_TYPE_QUOTATION && b.type == HEX_TYPE_QUOTATION)) { - result = hex_push_int(hex_equal(a, b)); + return hex_push_int(hex_equal(a, b)); } - else - { - hex_error("'==' symbol requires two integers, two strings, or two quotations"); - result = 1; - } - return result; + hex_error("'==' symbol requires two integers, two strings, or two quotations"); + return 1; } int hex_symbol_notequal()@@ -1743,17 +1654,12 @@ hex_free_element(a);
hex_free_element(b); return 1; } - int result = 0; if ((a.type == HEX_TYPE_INTEGER && b.type == HEX_TYPE_INTEGER) || (a.type == HEX_TYPE_STRING && b.type == HEX_TYPE_STRING) || (a.type == HEX_TYPE_QUOTATION && b.type == HEX_TYPE_QUOTATION)) { - result = hex_push_int(!hex_equal(a, b)); + return hex_push_int(!hex_equal(a, b)); } - else - { - hex_error("'!=' symbol requires two integers, two strings, or two quotations"); - result = 1; - } - return result; + hex_error("'!=' symbol requires two integers, two strings, or two quotations"); + return 1; } int hex_symbol_greater()@@ -1771,21 +1677,16 @@ hex_free_element(a);
hex_free_element(b); return 1; } - int result = 0; if (a.type == HEX_TYPE_INTEGER && b.type == HEX_TYPE_INTEGER) { - result = hex_push_int(a.data.intValue > b.data.intValue); + return hex_push_int(a.data.intValue > b.data.intValue); } else if (a.type == HEX_TYPE_STRING && b.type == HEX_TYPE_STRING) { - result = hex_push_int(strcmp(a.data.strValue, b.data.strValue) > 0); + return hex_push_int(strcmp(a.data.strValue, b.data.strValue) > 0); } - else - { - hex_error("'>' symbol requires two integers or two strings"); - result = 1; - } - return result; + hex_error("'>' symbol requires two integers or two strings"); + return 1; } int hex_symbol_less()@@ -1803,21 +1704,16 @@ hex_free_element(a);
hex_free_element(b); return 1; } - int result = 0; if (a.type == HEX_TYPE_INTEGER && b.type == HEX_TYPE_INTEGER) { - result = hex_push_int(a.data.intValue < b.data.intValue); + return hex_push_int(a.data.intValue < b.data.intValue); } - else if (a.type == HEX_TYPE_STRING && b.type == HEX_TYPE_STRING) + if (a.type == HEX_TYPE_STRING && b.type == HEX_TYPE_STRING) { - result = hex_push_int(strcmp(a.data.strValue, b.data.strValue) < 0); + return hex_push_int(strcmp(a.data.strValue, b.data.strValue) < 0); } - else - { - hex_error("'<' symbol requires two integers or two strings"); - result = 1; - } - return result; + hex_error("'<' symbol requires two integers or two strings"); + return 1; } int hex_symbol_greaterequal()@@ -1835,21 +1731,16 @@ hex_free_element(a);
hex_free_element(b); return 1; } - int result = 0; if (a.type == HEX_TYPE_INTEGER && b.type == HEX_TYPE_INTEGER) { - result = hex_push_int(a.data.intValue >= b.data.intValue); + return hex_push_int(a.data.intValue >= b.data.intValue); } - else if (a.type == HEX_TYPE_STRING && b.type == HEX_TYPE_STRING) + if (a.type == HEX_TYPE_STRING && b.type == HEX_TYPE_STRING) { - result = hex_push_int(strcmp(a.data.strValue, b.data.strValue) >= 0); + return hex_push_int(strcmp(a.data.strValue, b.data.strValue) >= 0); } - else - { - hex_error("'>=' symbol requires two integers or two strings"); - result = 1; - } - return result; + hex_error("'>=' symbol requires two integers or two strings"); + return 1; } int hex_symbol_lessequal()@@ -1867,21 +1758,16 @@ hex_free_element(a);
hex_free_element(b); return 1; } - int result = 0; if (a.type == HEX_TYPE_INTEGER && b.type == HEX_TYPE_INTEGER) { - result = hex_push_int(a.data.intValue <= b.data.intValue); + return hex_push_int(a.data.intValue <= b.data.intValue); } - else if (a.type == HEX_TYPE_STRING && b.type == HEX_TYPE_STRING) + if (a.type == HEX_TYPE_STRING && b.type == HEX_TYPE_STRING) { - result = hex_push_int(strcmp(a.data.strValue, b.data.strValue) <= 0); + return hex_push_int(strcmp(a.data.strValue, b.data.strValue) <= 0); } - else - { - hex_error("'<=' symbol requires two integers or two strings"); - result = 1; - } - return result; + hex_error("'<=' symbol requires two integers or two strings"); + return 1; } // Boolean symbols@@ -1901,17 +1787,12 @@ hex_free_element(a);
hex_free_element(b); return 1; } - int result = 0; if (a.type == HEX_TYPE_INTEGER && b.type == HEX_TYPE_INTEGER) { - result = hex_push_int(a.data.intValue && b.data.intValue); + return hex_push_int(a.data.intValue && b.data.intValue); } - else - { - hex_error("'and' symbol requires two integers"); - result = 1; - } - return result; + hex_error("'and' symbol requires two integers"); + return 1; } int hex_symbol_or()@@ -1929,17 +1810,12 @@ hex_free_element(a);
hex_free_element(b); return 1; } - int result = 0; if (a.type == HEX_TYPE_INTEGER && b.type == HEX_TYPE_INTEGER) { - result = hex_push_int(a.data.intValue || b.data.intValue); - } - else - { - hex_error("'or' symbol requires two integers"); - result = 1; + return hex_push_int(a.data.intValue || b.data.intValue); } - return result; + hex_error("'or' symbol requires two integers"); + return 1; } int hex_symbol_not()@@ -1950,18 +1826,13 @@ {
hex_free_element(a); return 1; } - int result = 0; if (a.type == HEX_TYPE_INTEGER) { - result = hex_push_int(!a.data.intValue); + return hex_push_int(!a.data.intValue); } - else - { - hex_error("'not' symbol requires an integer"); - result = 1; - } + hex_error("'not' symbol requires an integer"); hex_free_element(a); - return result; + return 1; } int hex_symbol_xor()@@ -1979,17 +1850,12 @@ hex_free_element(a);
hex_free_element(b); return 1; } - int result = 0; if (a.type == HEX_TYPE_INTEGER && b.type == HEX_TYPE_INTEGER) { - result = hex_push_int(a.data.intValue ^ b.data.intValue); + return hex_push_int(a.data.intValue ^ b.data.intValue); } - else - { - hex_error("'xor' symbol requires two integers"); - result = 1; - } - return result; + hex_error("'xor' symbol requires two integers"); + return 1; } ////////////////////////////////////////@@ -2162,6 +2028,12 @@ {
hex_error("Symbol 'slice' requires a quotation or a string"); result = 1; } + if (result != 0) + { + hex_free_element(list); + hex_free_element(start); + hex_free_element(end); + } return result; }@@ -2186,6 +2058,10 @@ else
{ hex_error("Symbol 'len' requires a quotation or a string"); result = 1; + } + if (result != 0) + { + hex_free_element(element); } return result; }