From 9c995830e267c384752519c660459cf2d8d8d653 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sun, 21 Aug 2022 00:51:28 +0100 Subject: [PATCH] Resolved #14 --- scripts/example.toy | 2 +- source/compiler.c | 9 ++++++++- source/literal.c | 1 + 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/scripts/example.toy b/scripts/example.toy index 53af2cb..1efe9b4 100644 --- a/scripts/example.toy +++ b/scripts/example.toy @@ -46,7 +46,7 @@ var a = 1; print a; //test scope will shadow higher scope on redefine -var b = 3; +var b: int = 3; { var b = 4; print b; diff --git a/source/compiler.c b/source/compiler.c index 1ff8d3b..65fcce9 100644 --- a/source/compiler.c +++ b/source/compiler.c @@ -144,7 +144,14 @@ static int writeLiteralTypeToCache(LiteralArray* literalCache, Literal literal) //push the store to the cache, tweaking the type Literal lit = TO_ARRAY_LITERAL(store); lit.type = LITERAL_TYPE_INTERMEDIATE; //NOTE: tweaking the type usually isn't a good idea - return pushLiteralArray(literalCache, lit); + + //BUGFIX: check if exactly this literal array exists + int index = findLiteralIndex(literalCache, lit); + if (index < 0) { + index = pushLiteralArray(literalCache, lit); + } + + return index; } static int writeLiteralToCompiler(Compiler* compiler, Literal literal) { diff --git a/source/literal.c b/source/literal.c index 8826e7f..06bb197 100644 --- a/source/literal.c +++ b/source/literal.c @@ -400,6 +400,7 @@ bool literalsAreEqual(Literal lhs, Literal rhs) { return !strncmp(AS_STRING(lhs), AS_STRING(rhs), STRLEN(lhs)); case LITERAL_ARRAY: + case LITERAL_TYPE_INTERMEDIATE: //BUGFIX: used for storing types as an array //mismatched sizes if (AS_ARRAY(lhs)->count != AS_ARRAY(rhs)->count) { return false;