mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 23:04:08 +10:00
Fixed type variable evaluation, it now occurs at var definition
This commit is contained in:
@@ -1,10 +1,15 @@
|
|||||||
|
|
||||||
|
|
||||||
var t: type = typeas [[int]];
|
|
||||||
|
|
||||||
var a: t = [
|
|
||||||
[1, 2, 3]
|
|
||||||
];
|
|
||||||
|
|
||||||
print a;
|
var t: type = typeas [int];
|
||||||
print typeof a;
|
var u: type = typeas [t];
|
||||||
|
|
||||||
|
var a: u;
|
||||||
|
|
||||||
|
t = typeas [float]; //redefnition
|
||||||
|
|
||||||
|
var b: u;
|
||||||
|
|
||||||
|
print typeof a; //<[<int>]>
|
||||||
|
print typeof b; //<[<float>]>
|
||||||
@@ -418,6 +418,24 @@ static bool execArithmetic(Interpreter* interpreter, Opcode opcode) {
|
|||||||
return false;
|
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) {
|
static bool execVarDecl(Interpreter* interpreter, bool lng) {
|
||||||
//read the index in the cache
|
//read the index in the cache
|
||||||
int identifierIndex = 0;
|
int identifierIndex = 0;
|
||||||
@@ -433,14 +451,17 @@ static bool execVarDecl(Interpreter* interpreter, bool lng) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Literal identifier = interpreter->literalCache.literals[identifierIndex];
|
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)) {
|
if (IS_IDENTIFIER(type)) {
|
||||||
|
Literal orig = type;
|
||||||
parseIdentifierToValue(interpreter, &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)) {
|
if (!declareScopeVariable(interpreter->scope, identifier, type)) {
|
||||||
interpreter->errorOutput("Can't redefine the variable \"");
|
interpreter->errorOutput("Can't redefine the variable \"");
|
||||||
printLiteralCustom(identifier, interpreter->errorOutput);
|
printLiteralCustom(identifier, interpreter->errorOutput);
|
||||||
@@ -461,20 +482,14 @@ static bool execVarDecl(Interpreter* interpreter, bool lng) {
|
|||||||
printLiteralCustom(identifier, interpreter->errorOutput);
|
printLiteralCustom(identifier, interpreter->errorOutput);
|
||||||
interpreter->errorOutput("\"\n");
|
interpreter->errorOutput("\"\n");
|
||||||
|
|
||||||
if (freeType) {
|
freeLiteral(type);
|
||||||
freeLiteral(type);
|
|
||||||
}
|
|
||||||
|
|
||||||
freeLiteral(val);
|
freeLiteral(val);
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
freeLiteral(val);
|
freeLiteral(val);
|
||||||
|
freeLiteral(type);
|
||||||
if (freeType) {
|
|
||||||
freeLiteral(type);
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -385,11 +385,11 @@ int hashLiteral(Literal lit) {
|
|||||||
case LITERAL_IDENTIFIER:
|
case LITERAL_IDENTIFIER:
|
||||||
return HASH_I(lit); //pre-computed
|
return HASH_I(lit); //pre-computed
|
||||||
|
|
||||||
// case LITERAL_TYPE:
|
case LITERAL_TYPE:
|
||||||
// //not needed
|
return AS_TYPE(lit).typeOf; //nothing else I can do
|
||||||
|
|
||||||
// case LITERAL_ANY:
|
case LITERAL_ANY:
|
||||||
// //not needed
|
return -1;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
//should never bee seen
|
//should never bee seen
|
||||||
|
|||||||
Reference in New Issue
Block a user