From 1095e1a8850b520669cba770e9b60adbeeea35ac Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Fri, 17 Mar 2023 20:57:47 +1100 Subject: [PATCH] Added type casting a grouping bugfix, resolved #76 --- scripts/example.toy | 29 +++------------------ source/toy_parser.c | 21 +++++++++++++++ test/scripts/casting-parentheses-bugfix.toy | 7 +++++ test/test_interpreter.c | 1 + 4 files changed, 32 insertions(+), 26 deletions(-) create mode 100644 test/scripts/casting-parentheses-bugfix.toy diff --git a/scripts/example.toy b/scripts/example.toy index 7649981..8937ede 100644 --- a/scripts/example.toy +++ b/scripts/example.toy @@ -1,27 +1,4 @@ -var colors = [ - "red", - "orange", - "yellow", - "green", - "blue", - "violet", - "indigo" -]; +var s = "42"; +var t = "69"; -fn getRainbowColor(index) { - return colors[index]; -} - - -import standard; -import random; - -var rng: opaque = createRandomGenerator(hash(clock())); - - - -print getRainbowColor(rng.generateRandomNumber() % colors.length()); -print getRainbowColor(rng.generateRandomNumber() % colors.length()); -print getRainbowColor(rng.generateRandomNumber() % colors.length()); -print getRainbowColor(rng.generateRandomNumber() % colors.length()); -print getRainbowColor(rng.generateRandomNumber() % colors.length()); +print int (s + t) - 1; \ No newline at end of file diff --git a/source/toy_parser.c b/source/toy_parser.c index 0057823..a1a7d77 100644 --- a/source/toy_parser.c +++ b/source/toy_parser.c @@ -739,6 +739,27 @@ static Toy_Opcode decrementInfix(Toy_Parser* parser, Toy_ASTNode** nodeHandle) { } static Toy_Opcode fnCall(Toy_Parser* parser, Toy_ASTNode** nodeHandle) { + //wait - is the previous token a type? this should be casting instead + if (parser->previous.type >= TOY_TOKEN_NULL && parser->previous.type <= TOY_TOKEN_ANY) { + //casting type + Toy_ASTNode* lhsNode = NULL; + castingPrefix(parser, &lhsNode); + advance(parser); + + //casting value + Toy_ASTNode* rhsNode = NULL; + grouping(parser, &rhsNode); + + //emit the cast node + + Toy_emitASTNodeBinary(&lhsNode, rhsNode, TOY_OP_TYPE_CAST); + + //pass it off to the caller + *nodeHandle = lhsNode; + + return TOY_OP_GROUPING_BEGIN; //dummy value + } + advance(parser); //skip the left paren //binary() is an infix rule - so only get the RHS of the operator diff --git a/test/scripts/casting-parentheses-bugfix.toy b/test/scripts/casting-parentheses-bugfix.toy new file mode 100644 index 0000000..905b8c9 --- /dev/null +++ b/test/scripts/casting-parentheses-bugfix.toy @@ -0,0 +1,7 @@ +var s = "42"; +var t = "69"; + +assert int (s + t) - 1 == 4268, "casting parentheses failed"; + + +print "All good"; diff --git a/test/test_interpreter.c b/test/test_interpreter.c index e2be6ab..739cb84 100644 --- a/test/test_interpreter.c +++ b/test/test_interpreter.c @@ -108,6 +108,7 @@ int main() { //run each file in tests/scripts/ const char* filenames[] = { "arithmetic.toy", + "casting-parentheses-bugfix.toy", "casting.toy", "coercions.toy", "comparisons.toy",