Fixed type variable evaluation, it now occurs at var definition

This commit is contained in:
2022-09-07 14:21:40 +01:00
parent 6511d652f2
commit 6c71a16e3e
3 changed files with 41 additions and 21 deletions

View File

@@ -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; //<[<int>]>
print typeof b; //<[<float>]>

View File

@@ -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;
}

View File

@@ -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