From 17f1dc86479e33d067687e5014156a26a7a56cb5 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Tue, 6 Sep 2022 09:22:50 +0100 Subject: [PATCH] cleaning up tests --- scripts/small.toy | 23 ----------------------- scripts/test/index-arrays.toy | 29 +++++++++++++++++++++++++++++ scripts/test/index-dictionaries.toy | 21 +++++++++++++++++++++ scripts/test/index-strings.toy | 21 +++++++++++++++++++++ source/compiler.c | 7 ++++++- source/interpreter.c | 8 ++++++++ source/lib_builtin.c | 5 +++-- source/opcodes.h | 2 +- source/parser.c | 27 +++++++++++++++++++++------ test/test_interpreter.c | 9 ++++++--- 10 files changed, 116 insertions(+), 36 deletions(-) delete mode 100644 scripts/small.toy create mode 100644 scripts/test/index-arrays.toy create mode 100644 scripts/test/index-dictionaries.toy create mode 100644 scripts/test/index-strings.toy diff --git a/scripts/small.toy b/scripts/small.toy deleted file mode 100644 index 7b7d436..0000000 --- a/scripts/small.toy +++ /dev/null @@ -1,23 +0,0 @@ -var week = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"]; - -week[::-2] = ["first", "second", "third"]; - -print week; - - -var str = "0123456789"; - -str[3:5:-2] = "abc"; - -print str; - - - -str = "Hello world"; - -print str; //Hello world -print str[::2]; //Hlowrd -print str[::-2]; //drwolH - - - diff --git a/scripts/test/index-arrays.toy b/scripts/test/index-arrays.toy new file mode 100644 index 0000000..860e089 --- /dev/null +++ b/scripts/test/index-arrays.toy @@ -0,0 +1,29 @@ +//test basic replacement +{ + var week = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"]; + + week[3] = "Holiday"; + + assert week == ["monday", "tuesday", "wednesday", "Holiday", "friday", "saturday", "sunday"], "basic replacement failed"; +} + + +//test backwards +{ + var week = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"]; + + assert week[::-1] == ["sunday", "saturday", "friday", "thursday", "wednesday", "tuesday", "monday"], "backwards failed"; +} + + +//test array weird manipulation +{ + var week = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"]; + + week[::-2] = ["first", "second", "third"]; + + assert week == ["monday", "tuesday", "third", "thursday", "second", "saturday", "first"], "array weird manipulation failed"; +} + + +print "All good"; diff --git a/scripts/test/index-dictionaries.toy b/scripts/test/index-dictionaries.toy new file mode 100644 index 0000000..1626dc8 --- /dev/null +++ b/scripts/test/index-dictionaries.toy @@ -0,0 +1,21 @@ +//test basic insertion +{ + var d = [:]; + + d["foo"] = "bar"; + + assert d == ["foo":"bar"], "basic insertion failed"; +} + + +//test dot insertion +{ + var d = [:]; + + d.foo = "bar"; + + assert d == ["foo":"bar"], "dot insertion failed"; +} + + +print "All good"; \ No newline at end of file diff --git a/scripts/test/index-strings.toy b/scripts/test/index-strings.toy new file mode 100644 index 0000000..7aba228 --- /dev/null +++ b/scripts/test/index-strings.toy @@ -0,0 +1,21 @@ +//test basic replacement +var greeting: string = "hello world"; + +greeting[0:4] = "goodnight"; + +assert greeting == "goodnight world", "basic replacement failed"; + + +//test backwards string +assert greeting[::-1] == "dlrow thgindoog", "backwards string failed"; + + +//test string weird manipulation +var numbers = "0123456789"; + +numbers[::-2] = "abc"; + +assert numbers == "01234c6b8a", "string weird manipulation failed"; + + +print "All good"; diff --git a/source/compiler.c b/source/compiler.c index 6ba36af..04460a4 100644 --- a/source/compiler.c +++ b/source/compiler.c @@ -306,7 +306,7 @@ static Opcode writeCompilerWithJumps(Compiler* compiler, Node* node, void* break //pass to the child nodes, then embed the binary command (math, etc.) Opcode override = writeCompilerWithJumps(compiler, node->binary.left, breakAddressesPtr, continueAddressesPtr, jumpOffsets); - //special case for when indexing + //special case for when indexing and assigning if (override != OP_EOF && node->binary.opcode >= OP_VAR_ASSIGN && node->binary.opcode <= OP_VAR_MODULO_ASSIGN) { writeCompilerWithJumps(compiler, node->binary.right, breakAddressesPtr, continueAddressesPtr, jumpOffsets); compiler->bytecode[compiler->count++] = (unsigned char)override + 2; //1 byte WARNING: enum arithmetic @@ -314,6 +314,11 @@ static Opcode writeCompilerWithJumps(Compiler* compiler, Node* node, void* break return OP_EOF; } + //compensate for... yikes + if (override != OP_EOF) { + compiler->bytecode[compiler->count++] = (unsigned char)override; //1 byte + } + //return from the index-binary Opcode ret = writeCompilerWithJumps(compiler, node->binary.right, breakAddressesPtr, continueAddressesPtr, jumpOffsets); diff --git a/source/interpreter.c b/source/interpreter.c index d971bee..c0923ec 100644 --- a/source/interpreter.c +++ b/source/interpreter.c @@ -1355,6 +1355,8 @@ static bool execIndex(Interpreter* interpreter) { if (!IS_IDENTIFIER(compound)) { interpreter->errorOutput("Unknown literal found in indexing notation\n"); + printLiteralCustom(compound, interpreter->errorOutput); + interpreter->errorOutput("\n"); freeLiteral(third); freeLiteral(second); freeLiteral(first); @@ -1434,6 +1436,8 @@ static bool execDot(Interpreter* interpreter) { if (!IS_IDENTIFIER(compound)) { interpreter->errorOutput("Unknown literal found in dot notation\n"); + printLiteralCustom(compound, interpreter->errorOutput); + interpreter->errorOutput("\n"); freeLiteral(first); freeLiteral(compound); return false; @@ -1500,6 +1504,8 @@ static bool execIndexAssign(Interpreter* interpreter) { if (!IS_IDENTIFIER(compound)) { interpreter->errorOutput("Unknown literal found in index assigning notation\n"); + printLiteralCustom(compound, interpreter->errorOutput); + interpreter->errorOutput("\n"); freeLiteral(assign); freeLiteral(third); freeLiteral(second); @@ -1669,6 +1675,8 @@ static bool execDotAssign(Interpreter* interpreter) { if (!IS_IDENTIFIER(compound)) { interpreter->errorOutput("Unknown literal found in dot assigning notation\n"); + printLiteralCustom(compound, interpreter->errorOutput); + interpreter->errorOutput("\n"); freeLiteral(assign); freeLiteral(first); freeLiteral(compound); diff --git a/source/lib_builtin.c b/source/lib_builtin.c index eeb4ef9..9a779fd 100644 --- a/source/lib_builtin.c +++ b/source/lib_builtin.c @@ -41,7 +41,6 @@ int _index(Interpreter* interpreter, LiteralArray* arguments) { if (IS_DICTIONARY(compound)) { value = getLiteralDictionary(AS_DICTIONARY(compound), first); - //dictionary //dictionary if (IS_NULL(op)) { pushLiteralArray(&interpreter->stack, value); @@ -525,6 +524,8 @@ int _index(Interpreter* interpreter, LiteralArray* arguments) { for (int i = AS_INTEGER(second) + 1; i < strlen(AS_STRING(compound)); i++) { result[ resultIndex++ ] = AS_STRING(compound)[ i ]; } + + result[ resultIndex++ ] = '\0'; } //else override elements of the array instead @@ -532,7 +533,7 @@ int _index(Interpreter* interpreter, LiteralArray* arguments) { //copy compound to result snprintf(result, MAX_STRING_LENGTH, AS_STRING(compound)); - int min = AS_INTEGER(third) > 0 ? AS_INTEGER(first) : AS_INTEGER(second); + int min = AS_INTEGER(third) > 0 ? AS_INTEGER(first) : AS_INTEGER(second) - 1; int assignIndex = 0; for (int i = min; i >= AS_INTEGER(first) && i <= AS_INTEGER(second) && assignIndex < strlen(AS_STRING(assign)); i += AS_INTEGER(third)) { diff --git a/source/opcodes.h b/source/opcodes.h index 9e77116..1e647e4 100644 --- a/source/opcodes.h +++ b/source/opcodes.h @@ -55,7 +55,7 @@ typedef enum Opcode { OP_INDEX_ASSIGN, OP_DOT_ASSIGN, - //comparion of values + //comparison of values OP_COMPARE_EQUAL, OP_COMPARE_NOT_EQUAL, OP_COMPARE_LESS, diff --git a/source/parser.c b/source/parser.c index 9ad30ca..d052159 100644 --- a/source/parser.c +++ b/source/parser.c @@ -530,6 +530,11 @@ static Opcode identifier(Parser* parser, Node** nodeHandle) { //make a copy of the string Token identifierToken = parser->previous; + if (identifierToken.type != TOKEN_IDENTIFIER) { + error(parser, parser->previous, "Expected identifier"); + return OP_EOF; + } + int length = identifierToken.length; //for safety @@ -739,10 +744,10 @@ static Opcode indexAccess(Parser* parser, Node** nodeHandle) { } if (match(parser, TOKEN_BRACKET_RIGHT)) { - // freeNode(second); - // freeNode(third); - // second = NULL; - // third = NULL; + freeNode(second); + freeNode(third); + second = NULL; + third = NULL; emitNodeIndex(nodeHandle, first, second, third); return OP_INDEX; @@ -756,8 +761,8 @@ static Opcode indexAccess(Parser* parser, Node** nodeHandle) { } if (match(parser, TOKEN_BRACKET_RIGHT)) { - // freeNode(third); - // third = NULL; + freeNode(third); + third = NULL; emitNodeIndex(nodeHandle, first, second, third); return OP_INDEX; } @@ -1267,6 +1272,11 @@ static void importStmt(Parser* parser, Node** nodeHandle) { Node* node = NULL; advance(parser); identifier(parser, &node); + + if (node == NULL) { + return; + } + Literal idn = copyLiteral(node->atomic.literal); freeNode(node); @@ -1292,6 +1302,11 @@ static void exportStmt(Parser* parser, Node** nodeHandle) { Node* node = NULL; advance(parser); identifier(parser, &node); + + if (node == NULL) { + return; + } + Literal idn = copyLiteral(node->atomic.literal); freeNode(node); diff --git a/test/test_interpreter.c b/test/test_interpreter.c index 8b9fcb3..9d434f6 100644 --- a/test/test_interpreter.c +++ b/test/test_interpreter.c @@ -162,7 +162,6 @@ int main() { { //run each file in ../scripts/test/ - int count = 15; char* filenames[] = { "arithmetic.toy", "casting.toy", @@ -170,6 +169,9 @@ int main() { "dot-and-matrix.toy", "functions.toy", "imports-and-exports.toy", + "index-arrays.toy", + "index-dictionaries.toy", + "index-strings.toy", "jumps.toy", "jumps-in-functions.toy", "logicals.toy", @@ -178,10 +180,11 @@ int main() { "long-literals.toy", "native-functions.toy", "panic-within-functions.toy", - "types.toy" + "types.toy", + NULL }; - for (int i = 0; i < count; i++) { + for (int i = 0; filenames[i]; i++) { printf("Running %s\n", filenames[i]); char buffer[128];