mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 23:04:08 +10:00
Implemented #27, easy coercion from int to float
This commit is contained in:
13
scripts/test/coercions.toy
Normal file
13
scripts/test/coercions.toy
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
//test int -> float coercion
|
||||||
|
{
|
||||||
|
var f: float = 0;
|
||||||
|
|
||||||
|
assert typeof f == float, "coercion on decl failed";
|
||||||
|
|
||||||
|
f = 42;
|
||||||
|
|
||||||
|
assert typeof f == float, "coercion on assign failed";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
print "All good";
|
||||||
@@ -544,6 +544,11 @@ static bool execVarDecl(Interpreter* interpreter, bool lng) {
|
|||||||
parseCompoundToPureValues(interpreter, &val);
|
parseCompoundToPureValues(interpreter, &val);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//BUGFIX: allow easy coercion on decl
|
||||||
|
if (AS_TYPE(type).typeOf == LITERAL_FLOAT && IS_INTEGER(val)) {
|
||||||
|
val = TO_FLOAT_LITERAL(AS_INTEGER(val));
|
||||||
|
}
|
||||||
|
|
||||||
if (!IS_NULL(val) && !setScopeVariable(interpreter->scope, identifier, val, false)) {
|
if (!IS_NULL(val) && !setScopeVariable(interpreter->scope, identifier, val, false)) {
|
||||||
interpreter->errorOutput("Incorrect type assigned to variable \"");
|
interpreter->errorOutput("Incorrect type assigned to variable \"");
|
||||||
printLiteralCustom(identifier, interpreter->errorOutput);
|
printLiteralCustom(identifier, interpreter->errorOutput);
|
||||||
@@ -635,6 +640,12 @@ static bool execVarAssign(Interpreter* interpreter) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//BUGFIX: allow easy coercion on assign
|
||||||
|
Literal type = getScopeType(interpreter->scope, lhs);
|
||||||
|
if (AS_TYPE(type).typeOf == LITERAL_FLOAT && IS_INTEGER(rhs)) {
|
||||||
|
rhs = TO_FLOAT_LITERAL(AS_INTEGER(rhs));
|
||||||
|
}
|
||||||
|
|
||||||
if (!setScopeVariable(interpreter->scope, lhs, rhs, true)) {
|
if (!setScopeVariable(interpreter->scope, lhs, rhs, true)) {
|
||||||
interpreter->errorOutput("Incorrect type assigned to variable \"");
|
interpreter->errorOutput("Incorrect type assigned to variable \"");
|
||||||
printLiteralCustom(lhs, interpreter->errorOutput);
|
printLiteralCustom(lhs, interpreter->errorOutput);
|
||||||
@@ -642,11 +653,13 @@ static bool execVarAssign(Interpreter* interpreter) {
|
|||||||
|
|
||||||
freeLiteral(lhs);
|
freeLiteral(lhs);
|
||||||
freeLiteral(rhs);
|
freeLiteral(rhs);
|
||||||
|
freeLiteral(type);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
freeLiteral(lhs);
|
freeLiteral(lhs);
|
||||||
freeLiteral(rhs);
|
freeLiteral(rhs);
|
||||||
|
freeLiteral(type);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -165,6 +165,7 @@ int main() {
|
|||||||
char* filenames[] = {
|
char* filenames[] = {
|
||||||
"arithmetic.toy",
|
"arithmetic.toy",
|
||||||
"casting.toy",
|
"casting.toy",
|
||||||
|
"coercions.toy",
|
||||||
"comparisons.toy",
|
"comparisons.toy",
|
||||||
"dot-and-matrix.toy",
|
"dot-and-matrix.toy",
|
||||||
"functions.toy",
|
"functions.toy",
|
||||||
|
|||||||
Reference in New Issue
Block a user