diff --git a/docs/TODO.txt b/docs/TODO.txt index 1a309b4..fb2525d 100644 --- a/docs/TODO.txt +++ b/docs/TODO.txt @@ -27,6 +27,7 @@ DONE: native functions DONE: global functions _get, _set, _push, _pop, _length, clear available +TODO: change comma to colon in dictionary definition? TODO: slice and dot notation around the _index function TODO: ternary operator TODO: Nullish types diff --git a/source/literal_util.c b/source/literal_util.c index b2c77d6..82c4175 100644 --- a/source/literal_util.c +++ b/source/literal_util.c @@ -194,8 +194,8 @@ int hashLiteral(Literal lit) { return hash(res); } - // case LITERAL_FUNCTION: - // // + case LITERAL_FUNCTION: + return 0; case LITERAL_IDENTIFIER: return HASH_I(lit); //pre-computed diff --git a/source/literal_util.h b/source/literal_util.h index 950d329..520c2e0 100644 --- a/source/literal_util.h +++ b/source/literal_util.h @@ -8,3 +8,5 @@ int hashLiteral(Literal lit); void printLiteral(Literal literal); void printLiteralCustom(Literal literal, void (printFn)(const char*)); + +//TODO: copy literal (to be used in dictionaries and arrays, as well) diff --git a/test/test_literal.c b/test/test_literal.c index 31838b9..6eccd2d 100644 --- a/test/test_literal.c +++ b/test/test_literal.c @@ -10,7 +10,6 @@ int main() { //test a single null literal Literal literal = TO_NULL_LITERAL; - // if (!IS_NULL(literal)) { fprintf(stderr, ERROR "ERROR: null literal failed\n" RESET); return -1; @@ -22,7 +21,6 @@ int main() { Literal t = TO_BOOLEAN_LITERAL(true); Literal f = TO_BOOLEAN_LITERAL(false); - // if (!IS_TRUTHY(t) || IS_TRUTHY(f)) { fprintf(stderr, ERROR "ERROR: boolean literal failed\n" RESET); return -1; @@ -30,6 +28,7 @@ int main() { } { + //test string literals char* buffer = ALLOCATE(char, 128); snprintf(buffer, 128, "Hello world"); @@ -39,6 +38,17 @@ int main() { freeLiteral(literal); } + { + //test identifier literals + char* buffer = ALLOCATE(char, 128); + + snprintf(buffer, 128, "foobar"); + + Literal literal = TO_IDENTIFIER_LITERAL(buffer, 128); + + freeLiteral(literal); + } + //check allocated memory if (getAllocatedMemoryCount() != 0) { fprintf(stderr, ERROR "ERROR: Dangling memory detected: %d byes\n" RESET, getAllocatedMemoryCount()); diff --git a/test/test_literal_array.c b/test/test_literal_array.c new file mode 100644 index 0000000..c375911 --- /dev/null +++ b/test/test_literal_array.c @@ -0,0 +1,78 @@ +#include "literal_array.h" + +#include "memory.h" +#include "console_colors.h" + +#include + +int main() { + { + //test init & cleanup + LiteralArray array; + initLiteralArray(&array); + freeLiteralArray(&array); + } + + { + //test pushing and pulling + LiteralArray array; + initLiteralArray(&array); + + for (int i = 0; i < 100; i++) { + pushLiteralArray(&array, TO_INTEGER_LITERAL(i)); + } + + for (int i = 0; i < 90; i++) { + Literal lit = popLiteralArray(&array); + + freeLiteral(lit); + } + + if (array.count != 10) { + fprintf(stderr, ERROR "ERROR: Array didn't clear the correct number of literal integers\n" RESET); + freeLiteralArray(&array); + return -1; + } + + freeLiteralArray(&array); + } + + { + //check string, identifier and compound type behaviours + LiteralArray array; + initLiteralArray(&array); + + //raw + char* str_raw = "hello world"; + char* idn_raw = "foobar"; + + Literal string = TO_STRING_LITERAL(copyString(str_raw, strlen(str_raw)), strlen(str_raw)); + Literal identifier = TO_IDENTIFIER_LITERAL(copyString(idn_raw, strlen(idn_raw)), strlen(idn_raw)); + + //[string, string] + Literal type = TO_TYPE_LITERAL(LITERAL_DICTIONARY, false); + TYPE_PUSH_SUBTYPE(&type, TO_TYPE_LITERAL(LITERAL_STRING, false)); + TYPE_PUSH_SUBTYPE(&type, TO_TYPE_LITERAL(LITERAL_STRING, false)); + + //push + pushLiteralArray(&array, string); + pushLiteralArray(&array, identifier); + pushLiteralArray(&array, type); + + //free the local literals + freeLiteral(string); + freeLiteral(identifier); + freeLiteral(type); + + freeLiteralArray(&array); + } + + //check allocated memory + if (getAllocatedMemoryCount() != 0) { + fprintf(stderr, ERROR "ERROR: Dangling memory detected: %d byes\n" RESET, getAllocatedMemoryCount()); + return -1; + } + + printf(NOTICE "All good\n" RESET); + return 0; +} \ No newline at end of file