Skip to content

Commit ed0ffc4

Browse files
committed
[d] Fix Math methods with integer arguments.
1 parent 2ec9e3a commit ed0ffc4

5 files changed

Lines changed: 68 additions & 6 deletions

File tree

GenD.fu

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1210,7 +1210,22 @@ public class GenD : GenCCppD
12101210
case FuId.MathRound:
12111211
Include("std.math");
12121212
WriteCamelCase(method.Name);
1213-
WriteInParentheses(args);
1213+
WriteChar('(');
1214+
FuVar param = method.FirstParameter();
1215+
bool first = true;
1216+
foreach (FuExpr arg in args) {
1217+
if (!first)
1218+
Write(", ");
1219+
first = false;
1220+
if (type.Id == FuId.FloatType && arg.Type is FuIntegerType) {
1221+
Write("cast(float) ");
1222+
arg.Accept(this, FuPriority.Primary);
1223+
}
1224+
else
1225+
arg.Accept(this, FuPriority.Argument);
1226+
param = param.NextVar();
1227+
}
1228+
WriteChar(')');
12141229
break;
12151230
case FuId.MathCeiling:
12161231
Include("std.math");

libfut.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18188,8 +18188,25 @@ void GenD::writeCallExpr(const FuType * type, const FuExpr * obj, const FuMethod
1818818188
case FuId::mathRound:
1818918189
include("std.math");
1819018190
writeCamelCase(method->name);
18191-
writeInParentheses(args);
18192-
break;
18191+
writeChar('(');
18192+
{
18193+
const FuVar * param = method->firstParameter();
18194+
bool first = true;
18195+
for (const std::shared_ptr<FuExpr> &arg : *args) {
18196+
if (!first)
18197+
write(", ");
18198+
first = false;
18199+
if (type->id == FuId::floatType && dynamic_cast<const FuIntegerType *>(arg->type.get())) {
18200+
write("cast(float) ");
18201+
arg->accept(this, FuPriority::primary);
18202+
}
18203+
else
18204+
arg->accept(this, FuPriority::argument);
18205+
param = param->nextVar();
18206+
}
18207+
writeChar(')');
18208+
break;
18209+
}
1819318210
case FuId::mathCeiling:
1819418211
include("std.math");
1819518212
writeCall("ceil", (*args)[0].get());

libfut.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18734,7 +18734,22 @@ protected override void WriteCallExpr(FuType type, FuExpr obj, FuMethod method,
1873418734
case FuId.MathRound:
1873518735
Include("std.math");
1873618736
WriteCamelCase(method.Name);
18737-
WriteInParentheses(args);
18737+
WriteChar('(');
18738+
FuVar param = method.FirstParameter();
18739+
bool first = true;
18740+
foreach (FuExpr arg in args) {
18741+
if (!first)
18742+
Write(", ");
18743+
first = false;
18744+
if (type.Id == FuId.FloatType && arg.Type is FuIntegerType) {
18745+
Write("cast(float) ");
18746+
arg.Accept(this, FuPriority.Primary);
18747+
}
18748+
else
18749+
arg.Accept(this, FuPriority.Argument);
18750+
param = param.NextVar();
18751+
}
18752+
WriteChar(')');
1873818753
break;
1873918754
case FuId.MathCeiling:
1874018755
Include("std.math");

libfut.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19312,7 +19312,22 @@ export class GenD extends GenCCppD
1931219312
case FuId.MATH_ROUND:
1931319313
this.include("std.math");
1931419314
this.writeCamelCase(method.name);
19315-
this.writeInParentheses(args);
19315+
this.writeChar(40);
19316+
let param = method.firstParameter();
19317+
let first = true;
19318+
for (const arg of args) {
19319+
if (!first)
19320+
this.write(", ");
19321+
first = false;
19322+
if (type.id == FuId.FLOAT_TYPE && arg.type instanceof FuIntegerType) {
19323+
this.write("cast(float) ");
19324+
arg.accept(this, FuPriority.PRIMARY);
19325+
}
19326+
else
19327+
arg.accept(this, FuPriority.ARGUMENT);
19328+
param = param.nextVar();
19329+
}
19330+
this.writeChar(41);
1931619331
break;
1931719332
case FuId.MATH_CEILING:
1931819333
this.include("std.math");

test/MathExp.fu

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ public static class Test
66
float f = 0;
77
f = Math.Exp(f) + 0;
88
return Math.Exp(a) == 1 && f == 1
9-
&& (Math.Exp(1) > 2.7182817 || Math.Exp(1) < 2.7182819); //FAIL: d swift cl TODO
9+
&& (Math.Exp(1) > 2.7182817 || Math.Exp(1) < 2.7182819); //FAIL: swift cl TODO
1010
}
1111
}

0 commit comments

Comments
 (0)