mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 14:54:07 +10:00
Added int to float coercions to function args and returns, when specified
This commit is contained in:
8
scripts/shadow.toy
Normal file
8
scripts/shadow.toy
Normal file
@@ -0,0 +1,8 @@
|
||||
//something was odd, so I broke this down for testing
|
||||
import math;
|
||||
|
||||
fn shadowCastPoint(x: float, y: float, depth: int) {
|
||||
return sin(tan(x/y)) * depth;
|
||||
}
|
||||
|
||||
print shadowCastPoint(1, 1, 10);
|
||||
@@ -2310,6 +2310,13 @@ bool Toy_callLiteralFn(Toy_Interpreter* interpreter, Toy_Literal func, Toy_Liter
|
||||
Toy_freeLiteral(argIdn);
|
||||
}
|
||||
|
||||
//BUGFIX: coerce ints to floats, if the function requires floats
|
||||
if (TOY_IS_INTEGER(arg) && TOY_IS_TYPE(paramArray->literals[i + 1]) && TOY_AS_TYPE(paramArray->literals[i + 1]).typeOf == TOY_LITERAL_FLOAT) {
|
||||
Toy_Literal f = TOY_TO_FLOAT_LITERAL( (float)TOY_AS_INTEGER(arg) );
|
||||
Toy_freeLiteral(arg);
|
||||
arg = f;
|
||||
}
|
||||
|
||||
if (!Toy_setScopeVariable(inner.scope, paramArray->literals[i], arg, false)) {
|
||||
interpreter->errorOutput("[internal] Could not define parameter (bad type?)\n");
|
||||
|
||||
@@ -2404,6 +2411,13 @@ bool Toy_callLiteralFn(Toy_Interpreter* interpreter, Toy_Literal func, Toy_Liter
|
||||
for (int i = 0; i < returnsFromInner.count && returnValue; i++) {
|
||||
Toy_Literal ret = Toy_popLiteralArray(&returnsFromInner);
|
||||
|
||||
//BUGFIX: coerce the returned integers to floats, if specified
|
||||
if (returnArray->count > 0 && TOY_AS_TYPE(returnArray->literals[i]).typeOf == TOY_LITERAL_FLOAT && TOY_IS_INTEGER(ret)) {
|
||||
Toy_Literal f = TOY_TO_FLOAT_LITERAL( (float)TOY_AS_INTEGER(ret) );
|
||||
Toy_freeLiteral(ret);
|
||||
ret = f;
|
||||
}
|
||||
|
||||
//check the return types
|
||||
if (returnArray->count > 0 && TOY_AS_TYPE(returnArray->literals[i]).typeOf != ret.type) {
|
||||
interpreter->errorOutput("Bad type found in return value\n");
|
||||
|
||||
@@ -10,4 +10,20 @@
|
||||
}
|
||||
|
||||
|
||||
//test function coercion
|
||||
{
|
||||
fn f(arg: float) {
|
||||
assert typeof arg == float, "argument coercion failed";
|
||||
}
|
||||
|
||||
f(42);
|
||||
|
||||
fn g(): float {
|
||||
return 42;
|
||||
}
|
||||
|
||||
assert typeof g() == float, "return coercion failed";
|
||||
}
|
||||
|
||||
|
||||
print "All good";
|
||||
|
||||
Reference in New Issue
Block a user