diff --git a/scripts/small.toy b/scripts/small.toy index d18ddcb..a2a7134 100644 --- a/scripts/small.toy +++ b/scripts/small.toy @@ -1,14 +1,50 @@ -import compound; +//test the standard library, under a number of different circumstances -fn p(x) { - print x; -}; +//test basic import +{ + import standard; -fn f(acc, k, v) { - acc(v); - return acc; + //this depends on external factors, so only check the length + assert clock().length() == 24, "import library failed"; } -var a = [1, 2, 3, 4]; -a.reduce(p, f); //prints "10" \ No newline at end of file +//test import within a function +{ + fn f() { + import standard; + + assert clock != null, "import library within function failed"; + + return clock; + } + + //invoke + assert f()().length() == 24, "import library within function and return failed"; +} + + +//test closing over standard library element +{ + import standard; + + fn f() { + assert clock != null, "import library outside function failed"; + + return clock; + } + + //invoke + assert f()().length() == 24, "import library outside function and return failed"; +} + + +//test importing as an alias +{ + import standard as std; + + assert std["clock"]().length() == 24, "import library as alias failed"; +} + + +print "All good"; diff --git a/source/toy_interpreter.c b/source/toy_interpreter.c index f62eb98..20b9ca6 100644 --- a/source/toy_interpreter.c +++ b/source/toy_interpreter.c @@ -1123,6 +1123,14 @@ static bool execFnCall(Toy_Interpreter* interpreter, bool looseFirstArgument) { //let's screw with the fn name, too if (looseFirstArgument) { + if (!TOY_IS_IDENTIFIER(identifier)) { + interpreter->errorOutput("Bad literal passed as a function identifier\n"); + Toy_freeLiteral(identifier); + Toy_freeLiteral(stackSize); + Toy_freeLiteralArray(&arguments); + return false; + } + int length = TOY_AS_IDENTIFIER(identifier)->length + 1; char buffer[TOY_MAX_STRING_LENGTH]; snprintf(buffer, TOY_MAX_STRING_LENGTH, "_%s", Toy_toCString(TOY_AS_IDENTIFIER(identifier))); //prepend an underscore @@ -1135,6 +1143,7 @@ static bool execFnCall(Toy_Interpreter* interpreter, bool looseFirstArgument) { if (!Toy_parseIdentifierToValue(interpreter, &func)) { Toy_freeLiteralArray(&arguments); + Toy_freeLiteral(stackSize); Toy_freeLiteral(identifier); return false; } @@ -1157,6 +1166,7 @@ static bool execFnCall(Toy_Interpreter* interpreter, bool looseFirstArgument) { TOY_AS_FUNCTION_NATIVE(func)(interpreter, &correct); Toy_freeLiteralArray(&correct); + Toy_freeLiteral(stackSize); Toy_freeLiteral(identifier); return true; } @@ -1167,6 +1177,7 @@ static bool execFnCall(Toy_Interpreter* interpreter, bool looseFirstArgument) { interpreter->errorOutput("\n"); Toy_freeLiteral(identifier); + Toy_freeLiteral(stackSize); Toy_freeLiteralArray(&arguments); return false; } @@ -1181,6 +1192,7 @@ static bool execFnCall(Toy_Interpreter* interpreter, bool looseFirstArgument) { Toy_freeLiteralArray(&arguments); Toy_freeLiteral(func); + Toy_freeLiteral(stackSize); Toy_freeLiteral(identifier); return ret; diff --git a/test/scripts/mustfail/bad-function-identifier.toy b/test/scripts/mustfail/bad-function-identifier.toy new file mode 100644 index 0000000..0d8c7c4 --- /dev/null +++ b/test/scripts/mustfail/bad-function-identifier.toy @@ -0,0 +1,2 @@ +1.type(); + diff --git a/test/scripts/mustfail/declare-types-array.toy b/test/scripts/mustfail/declare-types-array.toy index cdb50bb..16ebc0f 100644 --- a/test/scripts/mustfail/declare-types-array.toy +++ b/test/scripts/mustfail/declare-types-array.toy @@ -2,6 +2,3 @@ var t = astype [int]; var arr: t = [1, 2, 3.14]; } - - -print "All good"; diff --git a/test/scripts/mustfail/declare-types-dictionary-key.toy b/test/scripts/mustfail/declare-types-dictionary-key.toy index e200c6b..6266c4d 100644 --- a/test/scripts/mustfail/declare-types-dictionary-key.toy +++ b/test/scripts/mustfail/declare-types-dictionary-key.toy @@ -2,6 +2,3 @@ var t = astype [string:int]; var dict: t = ["one": 1, "two": 2, 3:4]; } - - -print "All good"; diff --git a/test/scripts/mustfail/declare-types-dictionary-value.toy b/test/scripts/mustfail/declare-types-dictionary-value.toy index 10fbd93..04ee8d8 100644 --- a/test/scripts/mustfail/declare-types-dictionary-value.toy +++ b/test/scripts/mustfail/declare-types-dictionary-value.toy @@ -2,6 +2,3 @@ var t = astype [string:int]; var dict: t = ["one": 1, "two": 2, "pi": 3.14]; } - - -print "All good"; diff --git a/test/scripts/mustfail/index-arrays-non-integer.toy b/test/scripts/mustfail/index-arrays-non-integer.toy index f55f53f..c6836c9 100644 --- a/test/scripts/mustfail/index-arrays-non-integer.toy +++ b/test/scripts/mustfail/index-arrays-non-integer.toy @@ -2,6 +2,3 @@ var a: [int] = [1, 2, 3]; print a[a]; } - - -print "All good"; diff --git a/test/scripts/mustfail/string-concat.toy b/test/scripts/mustfail/string-concat.toy index d89ef41..4da4489 100644 --- a/test/scripts/mustfail/string-concat.toy +++ b/test/scripts/mustfail/string-concat.toy @@ -1,5 +1,3 @@ { var s = "foo" - "bar"; } - -print "All good"; diff --git a/test/test_mustfail.c b/test/test_mustfail.c index a937c8c..d71e8c9 100644 --- a/test/test_mustfail.c +++ b/test/test_mustfail.c @@ -96,6 +96,7 @@ int main() { //run each file in tests/scripts/ char* filenames[] = { "access-parent-directory.toy", + "bad-function-identifier.toy", "declare-types-array.toy", "declare-types-dictionary-key.toy", "declare-types-dictionary-value.toy",