diff --git a/repl/main.c b/repl/main.c index e484e94..2280249 100644 --- a/repl/main.c +++ b/repl/main.c @@ -111,12 +111,21 @@ static void printCallback(const char* msg) { } static void errorAndExitCallback(const char* msg) { - fprintf(stderr, "%s\n", msg); + fprintf(stderr, "Error: %s\n", msg); exit(-1); } static void errorAndContinueCallback(const char* msg) { - fprintf(stderr, "%s\n", msg); + fprintf(stderr, "Error: %s\n", msg); +} + +static void assertFailureAndExitCallback(const char* msg) { + fprintf(stderr, "Assert Failure: %s\n", msg); + exit(-1); +} + +static void assertFailureAndContinueCallback(const char* msg) { + fprintf(stderr, "Assert Failure: %s\n", msg); } static void noOpCallback(const char* msg) { @@ -263,7 +272,7 @@ int repl(const char* filepath) { //output options Toy_setPrintCallback(printCallback); Toy_setErrorCallback(errorAndContinueCallback); - Toy_setAssertFailureCallback(errorAndContinueCallback); + Toy_setAssertFailureCallback(assertFailureAndContinueCallback); //vars to use char prompt[256]; @@ -472,7 +481,7 @@ static void debugScopePrint(Toy_Scope* scope, int depth) { int main(int argc, const char* argv[]) { Toy_setPrintCallback(printCallback); Toy_setErrorCallback(errorAndExitCallback); - Toy_setAssertFailureCallback(errorAndExitCallback); + Toy_setAssertFailureCallback(assertFailureAndExitCallback); //if there's args, process them CmdLine cmd = parseCmdLine(argc, argv); diff --git a/scripts/a.toy b/scripts/a.toy deleted file mode 100644 index b5fe55e..0000000 --- a/scripts/a.toy +++ /dev/null @@ -1,5 +0,0 @@ - - -var a = 0; -var b = a = a + 1, 6; - diff --git a/scripts/big_if_true.toy b/scripts/big_if_true.toy deleted file mode 100644 index 641fe73..0000000 --- a/scripts/big_if_true.toy +++ /dev/null @@ -1,10 +0,0 @@ -if (true) print "Correct"; else print "Error"; -if (true) print "Correct"; else print "Error"; -if (true) print "Correct"; else print "Error"; -if (true) print "Correct"; else print "Error"; -if (true) print "Correct"; else print "Error"; -if (true) print "Correct"; else print "Error"; -if (true) print "Correct"; else print "Error"; -if (true) print "Correct"; else print "Error"; -if (true) print "Correct"; else print "Error"; -if (true) print "Correct"; else print "Error"; diff --git a/scripts/brace_yourself.toy b/scripts/brace_yourself.toy deleted file mode 100644 index 145ddd3..0000000 --- a/scripts/brace_yourself.toy +++ /dev/null @@ -1,11 +0,0 @@ - -if (true) { print "Correct"; } else { print "Error"; } -if (true) { print "Correct"; } else { print "Error"; } -if (true) { print "Correct"; } else { print "Error"; } -if (true) { print "Correct"; } else { print "Error"; } -if (true) { print "Correct"; } else { print "Error"; } -if (true) { print "Correct"; } else { print "Error"; } -if (true) { print "Correct"; } else { print "Error"; } -if (false) { print "Correct"; } else { print "Error"; } -if (true) { print "Correct"; } else { print "Error"; } -if (true) { print "Correct"; } else { print "Error"; } diff --git a/scripts/concat.toy b/scripts/concat.toy deleted file mode 100644 index 6e88700..0000000 --- a/scripts/concat.toy +++ /dev/null @@ -1,10 +0,0 @@ -print "foo" .. "bar"; -print "foo" .. "bar"; -print "foo" .. "bar"; -print "foo" .. "bar"; -print "foo" .. "bar"; -print "foo" .. "bar"; -print "foo" .. "bar"; -print "foo" .. "bar"; -print "foo" .. "bar"; -print "foo" .. "bar"; diff --git a/scripts/cond.toy b/scripts/cond.toy deleted file mode 100644 index 4fe0207..0000000 --- a/scripts/cond.toy +++ /dev/null @@ -1,67 +0,0 @@ - -//literals -if (true) { - print "Success 1"; -} -else { - print "Failure 1"; -} - -//false literals -if (false) { - print "Failure 2"; -} -else { - print "Success 2"; -} - -//conditionals -if (1 < 2) { - print "Success 3"; -} -if (1 > 2) { - print "Failure 3"; -} - - -//variables -var a = 42; - -if (a) { - print "Success 4"; -} -else { - print "Failure 4"; -} - - -if (a == 42) { - print "Success 5"; -} -else { - print "Failure 5"; -} - -//concatenated strings -if ("foo" .. "bar" == "foobar") { - print "Success 6"; -} -else { - print "Failure 6"; -} - - -if ("foobar" == "foo" .. "bar") { - print "Success 7"; -} -else { - print "Failure 7"; -} - -if ("fizz" .. "le" == "fi" .. "zzle") { - print "Success 8"; -} -else { - print "Failure 8"; -} - diff --git a/scripts/count.toy b/scripts/count.toy deleted file mode 100644 index 7678152..0000000 --- a/scripts/count.toy +++ /dev/null @@ -1,11 +0,0 @@ -//if and while works -var count = 1; -while (count <= 10) { - if (count % 2 == 0) { - print "even"; - } - else { - print "odd"; - } - count += 1; -} diff --git a/scripts/fizzbuzz.toy b/scripts/fizzbuzz.toy index d399804..c608c2c 100644 --- a/scripts/fizzbuzz.toy +++ b/scripts/fizzbuzz.toy @@ -1,6 +1,4 @@ - - -//moment of truth +//standard example var counter: int = 1; while (counter <= 100) { @@ -15,11 +13,11 @@ while (counter <= 100) { } //finally - if (result == "") { - print counter; + if (result != "") { + print result; } else { - print result; + print counter; } counter += 1; diff --git a/scripts/looping.toy b/scripts/looping.toy deleted file mode 100644 index 39117ec..0000000 --- a/scripts/looping.toy +++ /dev/null @@ -1,9 +0,0 @@ -var a: int = 0; - -while(a < 10) { - print a; - a += 1; -} - -print "Finished"; - diff --git a/scripts/odd_and_even.toy b/scripts/odd_and_even.toy new file mode 100644 index 0000000..e1a7517 --- /dev/null +++ b/scripts/odd_and_even.toy @@ -0,0 +1,13 @@ +{ + //if and while work together + var count = 1; + while (count <= 10) { + if (count % 2 == 0) { + print "even"; + } + else { + print "odd"; + } + count += 1; + } +} diff --git a/source/toy_array.h b/source/toy_array.h index f93575a..876a655 100644 --- a/source/toy_array.h +++ b/source/toy_array.h @@ -28,7 +28,7 @@ TOY_API Toy_Array* Toy_resizeArray(Toy_Array* array, unsigned int capacity); //quick free #ifndef TOY_ARRAY_FREE -#define TOY_ARRAY_FREE(array) Toy_resizeArray(array, 0) +#define TOY_ARRAY_FREE(array) (array = Toy_resizeArray(array, 0)) #endif //one line to expand the array @@ -38,7 +38,7 @@ TOY_API Toy_Array* Toy_resizeArray(Toy_Array* array, unsigned int capacity); //quick push back #ifndef TOY_ARRAY_PUSHBACK -#define TOY_ARRAY_PUSHBACK(array, value) (TOY_ARRAY_EXPAND(array),(array)->data[(array)->count++] = (value)) +#define TOY_ARRAY_PUSHBACK(array, value) (TOY_ARRAY_EXPAND(array), (array)->data[(array)->count++] = (value)) #endif //URGENT: get array length in scripts (dot operator?) diff --git a/source/toy_print.c b/source/toy_print.c index 1a75c78..1cc3b52 100644 --- a/source/toy_print.c +++ b/source/toy_print.c @@ -10,9 +10,13 @@ static void errDefault(const char* msg) { fprintf(stderr, "%s", msg); } +static void assertDefault(const char* msg) { + fprintf(stderr, "%s", msg); +} + static Toy_callbackType printCallback = outDefault; static Toy_callbackType errorCallback = errDefault; -static Toy_callbackType assertCallback = errDefault; +static Toy_callbackType assertCallback = assertDefault; void Toy_print(const char* msg) { printCallback(msg); @@ -47,5 +51,5 @@ void Toy_resetErrorCallback() { } void Toy_resetAssertFailureCallback() { - assertCallback = errDefault; + assertCallback = assertDefault; } diff --git a/source/toy_value.c b/source/toy_value.c index e54b653..49b6650 100644 --- a/source/toy_value.c +++ b/source/toy_value.c @@ -226,7 +226,7 @@ bool Toy_checkValuesAreEqual(Toy_Value left, Toy_Value right) { for (unsigned int i = 0; i < leftArray->count; i++) { //any mismatch is an easy difference - if (Toy_checkValuesAreEqual(leftArray->data[i], rightArray->data[i])) { + if (Toy_checkValuesAreEqual(leftArray->data[i], rightArray->data[i]) != true) { return false; } } diff --git a/tests/cases/test_array.c b/tests/cases/test_array.c index 7d9398d..f3b0f55 100644 --- a/tests/cases/test_array.c +++ b/tests/cases/test_array.c @@ -6,30 +6,30 @@ int test_array() { //test allocation and free { - Toy_Array* array = Toy_resizeArray(NULL, 1); - array = Toy_resizeArray(array, 0); + Toy_Array* array = TOY_ARRAY_ALLOCATE(); + TOY_ARRAY_FREE(array); } //test initial data { - Toy_Array* array = Toy_resizeArray(NULL, 10); + Toy_Array* array = TOY_ARRAY_ALLOCATE(); //check you can access the memory array->data[1] = TOY_VALUE_FROM_INTEGER(42); - Toy_resizeArray(array, 0); + TOY_ARRAY_FREE(array); } //test multiple arrays (no overlaps or conflicts) { - Toy_Array* array1 = Toy_resizeArray(NULL, 10); - Toy_Array* array2 = Toy_resizeArray(NULL, 10); + Toy_Array* array1 = TOY_ARRAY_ALLOCATE(); + Toy_Array* array2 = TOY_ARRAY_ALLOCATE(); array1->data[1] = TOY_VALUE_FROM_INTEGER(42); array2->data[1] = TOY_VALUE_FROM_INTEGER(42); - Toy_resizeArray(array1, 0); - Toy_resizeArray(array2, 0); + TOY_ARRAY_FREE(array1); + TOY_ARRAY_FREE(array2); } return 0; diff --git a/tests/cases/test_value.c b/tests/cases/test_value.c index 5888adc..d3e239d 100644 --- a/tests/cases/test_value.c +++ b/tests/cases/test_value.c @@ -6,6 +6,7 @@ #include "toy_array.h" #include +#include #include int test_value_creation() { @@ -166,6 +167,8 @@ int test_value_copying() { Toy_freeValue(result); } + //arrays can't be compared + return 0; } @@ -253,6 +256,37 @@ int test_value_equality() { Toy_freeBucket(&bucket); } + //again with arrays + { + //setup + Toy_Array* array1 = TOY_ARRAY_ALLOCATE(); + TOY_ARRAY_PUSHBACK(array1, TOY_VALUE_FROM_INTEGER(42)); + TOY_ARRAY_PUSHBACK(array1, TOY_VALUE_FROM_INTEGER(69)); + TOY_ARRAY_PUSHBACK(array1, TOY_VALUE_FROM_INTEGER(8891)); + + Toy_Value value1 = TOY_VALUE_FROM_ARRAY(array1); + + Toy_Array* array2 = TOY_ARRAY_ALLOCATE(); + TOY_ARRAY_PUSHBACK(array2, TOY_VALUE_FROM_INTEGER(42)); + TOY_ARRAY_PUSHBACK(array2, TOY_VALUE_FROM_INTEGER(69)); + TOY_ARRAY_PUSHBACK(array2, TOY_VALUE_FROM_INTEGER(8891)); + + Toy_Value value2 = TOY_VALUE_FROM_ARRAY(array2); + + + if (Toy_checkValuesAreEqual(value1, value2) != true) + { + fprintf(stderr, TOY_CC_ERROR "ERROR: array values are not equal\n" TOY_CC_RESET); + Toy_freeValue(value1); + Toy_freeValue(value2); + return -1; + } + + //cleanup + Toy_freeValue(value1); + Toy_freeValue(value2); + } + return 0; } @@ -314,6 +348,8 @@ int test_value_comparison() { Toy_freeBucket(&bucket); } + //arrays can't be compared + return 0; } @@ -448,7 +484,57 @@ int test_value_stringify() { Toy_freeBucket(&bucket); } -//URGENT: test stringify and string & array + //stringify strings + { + //setup + Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_SMALL); + + Toy_Value value = TOY_VALUE_FROM_STRING(Toy_createString(&bucket, "Hello world!")); + Toy_String* string = Toy_stringifyValue(&bucket, value); + char* buffer = Toy_getStringRawBuffer(string); + + if (buffer == NULL || strcmp(buffer, "Hello world!") != 0) + { + fprintf(stderr, TOY_CC_ERROR "ERROR: stringify string 'Hello world!' failed\n" TOY_CC_RESET); + free(buffer); + Toy_freeBucket(&bucket); + return -1; + } + + //cleanup + free(buffer); + Toy_freeBucket(&bucket); + } + + //stringify array + { + //setup + Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_SMALL); + + //setup + Toy_Array* array = TOY_ARRAY_ALLOCATE(); + TOY_ARRAY_PUSHBACK(array, TOY_VALUE_FROM_INTEGER(42)); + TOY_ARRAY_PUSHBACK(array, TOY_VALUE_FROM_INTEGER(69)); + TOY_ARRAY_PUSHBACK(array, TOY_VALUE_FROM_INTEGER(8891)); + + Toy_Value value = TOY_VALUE_FROM_ARRAY(array); + Toy_String* string = Toy_stringifyValue(&bucket, value); + char* buffer = Toy_getStringRawBuffer(string); + + if (buffer == NULL || strcmp(buffer, "[42,69,8891]") != 0) + { + fprintf(stderr, TOY_CC_ERROR "ERROR: stringify array '[42,69,8891]' failed\n" TOY_CC_RESET); + free(buffer); + TOY_ARRAY_FREE(array); + Toy_freeBucket(&bucket); + return -1; + } + + //cleanup + free(buffer); + TOY_ARRAY_FREE(array); + Toy_freeBucket(&bucket); + } return 0; } @@ -505,7 +591,7 @@ int main() { total += res; } - //URGENT: arrays + //TODO: references return total; } diff --git a/scripts/compound.toy b/tests/integrations/test_arrays.toy similarity index 56% rename from scripts/compound.toy rename to tests/integrations/test_arrays.toy index e70206d..b174f09 100644 --- a/scripts/compound.toy +++ b/tests/integrations/test_arrays.toy @@ -1,11 +1,8 @@ - - //1-D array var arr = [1, 2, 3]; arr[1] = 6; -print arr; - +assert arr == [1, 6, 3], "1-D array failed"; //we need to go deeper var barr = [ @@ -16,4 +13,5 @@ var barr = [ barr[1][1] = 99; -print barr; \ No newline at end of file +assert barr == [[1, 2, 3],[4,99,6],[7,8,9]], "2-D array failed"; + diff --git a/tests/integrations/test_keyword_while.toy b/tests/integrations/test_keyword_while.toy new file mode 100644 index 0000000..93e2365 --- /dev/null +++ b/tests/integrations/test_keyword_while.toy @@ -0,0 +1,25 @@ +//TODO: test keyword 'while', 'break', 'continue' + +{ + //iteration + var iteration = 0; + while(iteration < 10) { + print iteration; + iteration += 1; + } +} + + +{ + //if and while work together + var count = 1; + while (count <= 10) { + if (count % 2 == 0) { + print "even"; + } + else { + print "odd"; + } + count += 1; + } +}