From aeb008c6845fa5b791fd302a366a99adc3406880 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Fri, 10 Feb 2023 18:38:25 +0000 Subject: [PATCH] Fixed unary negation bug, removed newline from print --- scripts/example-entity.toy | 14 +++---- scripts/fib-memo.toy | 2 +- scripts/fib.toy | 2 +- scripts/level.toy | 61 +++++++++++++++++++++++++++++ scripts/rule110.toy | 4 +- scripts/small.toy | 14 ++++--- source/toy_interpreter.c | 2 +- source/toy_parser.c | 6 +-- test/scripts/or-chaining-bugfix.toy | 7 ++++ test/test_call_from_host.c | 8 ++-- test/test_interpreter.c | 1 + 11 files changed, 96 insertions(+), 25 deletions(-) create mode 100644 scripts/level.toy create mode 100644 test/scripts/or-chaining-bugfix.toy diff --git a/scripts/example-entity.toy b/scripts/example-entity.toy index dcc514e..4206550 100644 --- a/scripts/example-entity.toy +++ b/scripts/example-entity.toy @@ -24,7 +24,7 @@ fn getY(node: opaque) { //lifecycle functions fn onInit(node: opaque) { - print "render.toy:onInit() called"; + print "render.toy:onInit() called\n"; node.loadTexture("sprites:/character.png"); parent = node.getNodeParent(); @@ -36,13 +36,13 @@ fn onStep(node: opaque) { } fn onFree(node: opaque) { - print "render.toy:onFree() called"; + print "render.toy:onFree() called\n"; node.freeTexture(); } fn onDraw(node: opaque) { -// print "render.toy:onDraw() called"; +// print "render.toy:onDraw() called\n"; var px = parent.callNode("getX"); var py = parent.callNode("getY"); @@ -104,11 +104,11 @@ fn onKeyUp(node: opaque, event: string) { } fn onMouseMotion(node: opaque, x: int, y: int, xrel: int, yrel: int) { - print "entity.toy:onMouseMotion(" + string x + ", " + string y + ", " + string xrel + ", " + string yrel + ")"; + // print "entity.toy:onMouseMotion(" + string x + ", " + string y + ", " + string xrel + ", " + string yrel + ")\n"; } fn onMouseButtonDown(node: opaque, x: int, y: int, button: string) { - print "entity.toy:onMouseButtonDown(" + string x + ", " + string y + ", " + button + ")"; + // print "entity.toy:onMouseButtonDown(" + string x + ", " + string y + ", " + button + ")\n"; //jump to pos posX = x - WIDTH / 2; @@ -116,10 +116,10 @@ fn onMouseButtonDown(node: opaque, x: int, y: int, button: string) { } fn onMouseButtonUp(node: opaque, x: int, y: int, button: string) { - print "entity.toy:onMouseButtonUp(" + string x + ", " + string y + ", " + button + ")"; + // print "entity.toy:onMouseButtonUp(" + string x + ", " + string y + ", " + button + ")\n"; } fn onMouseWheel(node: opaque, xrel: int, yrel: int) { - print "entity.toy:onMouseWheel(" + string xrel + ", " + string yrel + ")"; + // print "entity.toy:onMouseWheel(" + string xrel + ", " + string yrel + ")\n"; } diff --git a/scripts/fib-memo.toy b/scripts/fib-memo.toy index b41a5ae..2cd3c35 100644 --- a/scripts/fib-memo.toy +++ b/scripts/fib-memo.toy @@ -17,5 +17,5 @@ fn fib(n : int) { for (var i = 0; i < 40; i++) { var res = fib(i); - print string i + ": " + string res; + print string i + ": " + string res + "\n"; } \ No newline at end of file diff --git a/scripts/fib.toy b/scripts/fib.toy index ef59c88..7e17967 100644 --- a/scripts/fib.toy +++ b/scripts/fib.toy @@ -5,5 +5,5 @@ fn fib(n : int) { for (var i = 0; i < 20; i++) { var res = fib(i); - print string i + ": " + string res; + print string i + ": " + string res + "\n"; } \ No newline at end of file diff --git a/scripts/level.toy b/scripts/level.toy new file mode 100644 index 0000000..5fd6bbd --- /dev/null +++ b/scripts/level.toy @@ -0,0 +1,61 @@ +//constants +var WIDTH: int const = 10; +var HEIGHT: int const = 10; + +//WIDTH * HEIGHT in size +var tiles: [[int]] const = [ + [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], + [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], + [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], + [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], + [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], + [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], + [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], + [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], + [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], + [1, 1, 1, 1, 1, 1, 1, 1, 1, 1] +]; + +var tileset: [int: string] const = [ + 0: " ", + 1: " X " +]; + +//variables +var posX: int = 5; +var posY: int = 5; + +//functions +fn draw() { + for (var j: int = 0; j < HEIGHT; j++) { + for (var i: int = 0; i < WIDTH; i++) { + //draw the player pos + if (i == posX && j == posY) { + print " O "; + continue; + } + + print tileset[ tiles[i][j] ]; + } + print "\n"; + } + print "\n"; +} + +fn move(xrel: int, yrel: int) { + if (xrel > 1 || xrel < -1 || yrel > 1 || yrel < -1 || (xrel != 0 && yrel != 0)) { + print "too fast!\n"; + return; + } + + if (tiles[posX + xrel][posY + yrel] > 0) { + print "Can't move that way\n"; + return; + } + + posX += xrel; + posY += yrel; + + draw(); +} + diff --git a/scripts/rule110.toy b/scripts/rule110.toy index d7e8419..97cbaa3 100644 --- a/scripts/rule110.toy +++ b/scripts/rule110.toy @@ -29,7 +29,7 @@ for (var i = 0; i < SIZE -1; i++) { prev += " "; } prev += "*"; //initial -print prev; +print prev + "\n"; //run for (var iteration = 0; iteration < SIZE -1; iteration++) { @@ -44,6 +44,6 @@ for (var iteration = 0; iteration < SIZE -1; iteration++) { //right output += (lookup[prev[SIZE-2]][prev[SIZE-1]][" "]); - print output; + print output + "\n"; prev = output; } diff --git a/scripts/small.toy b/scripts/small.toy index 602d440..69e2c61 100644 --- a/scripts/small.toy +++ b/scripts/small.toy @@ -1,8 +1,10 @@ -import compound; -var a = [false, false, false]; -var d = ["one": false, "two": false]; - -print d.containsKey("one"); -print d.containsKey("three"); +var xrel: int = 0; +var yrel: int = 0; +if (xrel > 1 || xrel < -1 || yrel > 1 || yrel < -1) { + print "outside"; +} +else { + print "inside"; +} \ No newline at end of file diff --git a/source/toy_interpreter.c b/source/toy_interpreter.c index d5b28c9..3994b32 100644 --- a/source/toy_interpreter.c +++ b/source/toy_interpreter.c @@ -13,7 +13,7 @@ static void printWrapper(const char* output) { printf("%s", output); - printf("\n"); //default new line + // printf("\n"); //default new line } static void assertWrapper(const char* output) { diff --git a/source/toy_parser.c b/source/toy_parser.c index b6aa691..23bf72f 100644 --- a/source/toy_parser.c +++ b/source/toy_parser.c @@ -428,12 +428,12 @@ static Toy_Opcode binary(Toy_Parser* parser, Toy_ASTNode** nodeHandle) { } case TOY_TOKEN_AND: { - parsePrecedence(parser, nodeHandle, PREC_COMPARISON); + parsePrecedence(parser, nodeHandle, PREC_AND); return TOY_OP_AND; } case TOY_TOKEN_OR: { - parsePrecedence(parser, nodeHandle, PREC_COMPARISON); + parsePrecedence(parser, nodeHandle, PREC_OR); return TOY_OP_OR; } @@ -448,7 +448,7 @@ static Toy_Opcode unary(Toy_Parser* parser, Toy_ASTNode** nodeHandle) { if (parser->previous.type == TOY_TOKEN_MINUS) { //temp handle to potentially negate values - parsePrecedence(parser, &tmpNode, PREC_TERNARY); //can be a literal + parsePrecedence(parser, &tmpNode, PREC_TERM); //can be a literal //optimisation: check for negative literals if (tmpNode != NULL && tmpNode->type == TOY_AST_NODE_LITERAL && (TOY_IS_INTEGER(tmpNode->atomic.literal) || TOY_IS_FLOAT(tmpNode->atomic.literal))) { diff --git a/test/scripts/or-chaining-bugfix.toy b/test/scripts/or-chaining-bugfix.toy new file mode 100644 index 0000000..c319247 --- /dev/null +++ b/test/scripts/or-chaining-bugfix.toy @@ -0,0 +1,7 @@ +//This is just a check to ensure that unary minus doesn't screw up the AST + +var xrel: int = 0; +var yrel: int = 0; + +assert (xrel > 1 || xrel < -1 || yrel > 1 || yrel < -1) == false, "or-chaining bugfix failed"; + diff --git a/test/test_call_from_host.c b/test/test_call_from_host.c index 8bb1aba..9cc1ae5 100644 --- a/test/test_call_from_host.c +++ b/test/test_call_from_host.c @@ -41,7 +41,7 @@ int main() { //test answer { - interpreter.printOutput("Testing answer"); + interpreter.printOutput("Testing answer\n"); Toy_LiteralArray arguments; Toy_initLiteralArray(&arguments); @@ -69,7 +69,7 @@ int main() { //test identity { - interpreter.printOutput("Testing identity"); + interpreter.printOutput("Testing identity\n"); Toy_LiteralArray arguments; Toy_initLiteralArray(&arguments); @@ -104,7 +104,7 @@ int main() { //test makeCounter (closures) { - interpreter.printOutput("Testing makeCounter (closures)"); + interpreter.printOutput("Testing makeCounter (closures)\n"); Toy_LiteralArray arguments; Toy_initLiteralArray(&arguments); @@ -209,7 +209,7 @@ int main() { //test assertion failure { - interpreter.printOutput("Testing assertion failure"); + interpreter.printOutput("Testing assertion failure\n"); Toy_setInterpreterAssert(&interpreter, noPrintFn); diff --git a/test/test_interpreter.c b/test/test_interpreter.c index b5a2ffe..1c15c86 100644 --- a/test/test_interpreter.c +++ b/test/test_interpreter.c @@ -126,6 +126,7 @@ int main() { "long-dictionary.toy", "long-literals.toy", "native-functions.toy", + "or-chaining-bugfix.toy", "panic-within-functions.toy", "ternary-expressions.toy", "types.toy",