From 6c71a16e3e3c63e329b97aa532d4fd9ee25bc5f6 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Wed, 7 Sep 2022 14:21:40 +0100 Subject: [PATCH] Fixed type variable evaluation, it now occurs at var definition --- scripts/small.toy | 17 +++++++++++------ source/interpreter.c | 37 ++++++++++++++++++++++++++----------- source/literal.c | 8 ++++---- 3 files changed, 41 insertions(+), 21 deletions(-) diff --git a/scripts/small.toy b/scripts/small.toy index ae96b43..f1a3d08 100644 --- a/scripts/small.toy +++ b/scripts/small.toy @@ -1,10 +1,15 @@ -var t: type = typeas [[int]]; -var a: t = [ - [1, 2, 3] -]; -print a; -print typeof a; +var t: type = typeas [int]; +var u: type = typeas [t]; + +var a: u; + +t = typeas [float]; //redefnition + +var b: u; + +print typeof a; //<[]> +print typeof b; //<[]> \ No newline at end of file diff --git a/source/interpreter.c b/source/interpreter.c index 26af053..4b8b6f2 100644 --- a/source/interpreter.c +++ b/source/interpreter.c @@ -418,6 +418,24 @@ static bool execArithmetic(Interpreter* interpreter, Opcode opcode) { return false; } +static Literal parseTypeToValue(Interpreter* interpreter, Literal type) { + //if an identifier is embedded in the type, figure out what it iss + if (IS_IDENTIFIER(type)) { + Literal idn = type; + parseIdentifierToValue(interpreter, &type); + freeLiteral(idn); + } + + //if this is an array or dictionary, continue to the subtypes + if (IS_TYPE(type) && (AS_TYPE(type).typeOf == LITERAL_ARRAY || AS_TYPE(type).typeOf == LITERAL_DICTIONARY)) { + for (int i = 0; i < AS_TYPE(type).count; i++) { + ((Literal*)(AS_TYPE(type).subtypes))[i] = parseTypeToValue(interpreter, ((Literal*)(AS_TYPE(type).subtypes))[i]); + } + } + + return type; +} + static bool execVarDecl(Interpreter* interpreter, bool lng) { //read the index in the cache int identifierIndex = 0; @@ -433,14 +451,17 @@ static bool execVarDecl(Interpreter* interpreter, bool lng) { } Literal identifier = interpreter->literalCache.literals[identifierIndex]; - Literal type = interpreter->literalCache.literals[typeIndex]; + Literal type = copyLiteral(interpreter->literalCache.literals[typeIndex]); - bool freeType = false; if (IS_IDENTIFIER(type)) { + Literal orig = type; parseIdentifierToValue(interpreter, &type); - freeType = true; + freeLiteral(orig); } + //BUGFIX: because identifiers are getting embedded in type definitions + type = parseTypeToValue(interpreter, type); + if (!declareScopeVariable(interpreter->scope, identifier, type)) { interpreter->errorOutput("Can't redefine the variable \""); printLiteralCustom(identifier, interpreter->errorOutput); @@ -461,20 +482,14 @@ static bool execVarDecl(Interpreter* interpreter, bool lng) { printLiteralCustom(identifier, interpreter->errorOutput); interpreter->errorOutput("\"\n"); - if (freeType) { - freeLiteral(type); - } - + freeLiteral(type); freeLiteral(val); return false; } freeLiteral(val); - - if (freeType) { - freeLiteral(type); - } + freeLiteral(type); return true; } diff --git a/source/literal.c b/source/literal.c index 36afc85..de251b0 100644 --- a/source/literal.c +++ b/source/literal.c @@ -385,11 +385,11 @@ int hashLiteral(Literal lit) { case LITERAL_IDENTIFIER: return HASH_I(lit); //pre-computed - // case LITERAL_TYPE: - // //not needed + case LITERAL_TYPE: + return AS_TYPE(lit).typeOf; //nothing else I can do - // case LITERAL_ANY: - // //not needed + case LITERAL_ANY: + return -1; default: //should never bee seen