Skip to content

Commit

Permalink
[math] long and float overloads of Abs, Clamp, Max, Min.
Browse files Browse the repository at this point in the history
  • Loading branch information
pfusik committed Aug 6, 2024
1 parent ff192d9 commit e250dbc
Show file tree
Hide file tree
Showing 16 changed files with 316 additions and 354 deletions.
28 changes: 10 additions & 18 deletions AST.fu
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public enum FuId
DoubleType,
FloatIntType,
FloatingType,
NumericType,
BoolType,
StringClass,
StringPtrType,
Expand Down Expand Up @@ -223,10 +224,8 @@ public enum FuId
MathIsInfinity,
MathIsNaN,
MathLog2,
MathMaxInt,
MathMaxDouble,
MathMinInt,
MathMinDouble,
MathMax,
MathMin,
MathRound,
MathTruncate
}
Expand Down Expand Up @@ -1094,7 +1093,7 @@ public class FuType : FuScope
}
}

abstract class FuNumericType : FuType
class FuNumericType : FuType
{
}

Expand Down Expand Up @@ -1828,21 +1827,18 @@ public class FuSystem : FuScope
jsonElementClass.Add(FuMethod.New(null, FuVisibility.Public, FuCallType.Normal, BoolType, FuId.JsonElementGetBoolean, "GetBoolean", false));
Add(jsonElementClass);

FuFloatingType# floatIntType = new FuFloatingType { Id = FuId.FloatIntType, Name = "float" };
FuNumericType# numericType = new FuNumericType { Id = FuId.NumericType, Name = "numeric" };
FuFloatingType# floatingType = new FuFloatingType { Id = FuId.FloatingType, Name = "float" };
FuFloatingType# floatIntType = new FuFloatingType { Id = FuId.FloatIntType, Name = "float" };
FuClass# mathClass = FuClass.New(FuCallType.Static, FuId.None, "Math");
mathClass.Add(FuMethodGroup.New(
FuMethod.New(null, FuVisibility.Public, FuCallType.Static, IntType, FuId.MathAbs, "Abs", false, FuVar.New(LongType, "a")),
FuMethod.New(null, FuVisibility.Public, FuCallType.Static, FloatType, FuId.MathAbs, "Abs", false, FuVar.New(DoubleType, "a"))));
mathClass.AddStaticMethod(numericType, FuId.MathAbs, "Abs", FuVar.New(DoubleType, "a"));
mathClass.AddStaticMethod(floatingType, FuId.MathMethod, "Acos", FuVar.New(DoubleType, "a"));
mathClass.AddStaticMethod(floatingType, FuId.MathMethod, "Asin", FuVar.New(DoubleType, "a"));
mathClass.AddStaticMethod(floatingType, FuId.MathMethod, "Atan", FuVar.New(DoubleType, "a"));
mathClass.AddStaticMethod(floatingType, FuId.MathMethod, "Atan2", FuVar.New(DoubleType, "y"), FuVar.New(DoubleType, "x"));
mathClass.AddStaticMethod(floatingType, FuId.MathMethod, "Cbrt", FuVar.New(DoubleType, "a"));
mathClass.AddStaticMethod(floatIntType, FuId.MathCeiling, "Ceiling", FuVar.New(DoubleType, "a"));
mathClass.Add(FuMethodGroup.New(
FuMethod.New(null, FuVisibility.Public, FuCallType.Static, IntType, FuId.MathClamp, "Clamp", false, FuVar.New(LongType, "value"), FuVar.New(LongType, "min"), FuVar.New(LongType, "max")),
FuMethod.New(null, FuVisibility.Public, FuCallType.Static, FloatType, FuId.MathClamp, "Clamp", false, FuVar.New(DoubleType, "value"), FuVar.New(DoubleType, "min"), FuVar.New(DoubleType, "max"))));
mathClass.AddStaticMethod(numericType, FuId.MathClamp, "Clamp", FuVar.New(DoubleType, "value"), FuVar.New(DoubleType, "min"), FuVar.New(DoubleType, "max"));
mathClass.AddStaticMethod(floatingType, FuId.MathMethod, "Cos", FuVar.New(DoubleType, "a"));
mathClass.AddStaticMethod(floatingType, FuId.MathMethod, "Cosh", FuVar.New(DoubleType, "a"));
mathClass.Add(NewConstDouble("E", Math.E));
Expand All @@ -1855,12 +1851,8 @@ public class FuSystem : FuScope
mathClass.AddStaticMethod(floatingType, FuId.MathMethod, "Log", FuVar.New(DoubleType, "a"));
mathClass.AddStaticMethod(floatingType, FuId.MathLog2, "Log2", FuVar.New(DoubleType, "a"));
mathClass.AddStaticMethod(floatingType, FuId.MathMethod, "Log10", FuVar.New(DoubleType, "a"));
mathClass.Add(FuMethodGroup.New(
FuMethod.New(null, FuVisibility.Public, FuCallType.Static, IntType, FuId.MathMaxInt, "Max", false, FuVar.New(LongType, "a"), FuVar.New(LongType, "b")),
FuMethod.New(null, FuVisibility.Public, FuCallType.Static, FloatType, FuId.MathMaxDouble, "Max", false, FuVar.New(DoubleType, "a"), FuVar.New(DoubleType, "b"))));
mathClass.Add(FuMethodGroup.New(
FuMethod.New(null, FuVisibility.Public, FuCallType.Static, IntType, FuId.MathMinInt, "Min", false, FuVar.New(LongType, "a"), FuVar.New(LongType, "b")),
FuMethod.New(null, FuVisibility.Public, FuCallType.Static, FloatType, FuId.MathMinDouble, "Min", false, FuVar.New(DoubleType, "a"), FuVar.New(DoubleType, "b"))));
mathClass.AddStaticMethod(numericType, FuId.MathMax, "Max", FuVar.New(DoubleType, "a"), FuVar.New(DoubleType, "b"));
mathClass.AddStaticMethod(numericType, FuId.MathMin, "Min", FuVar.New(DoubleType, "a"), FuVar.New(DoubleType, "b"));
mathClass.Add(FuStaticProperty.New(FloatType, FuId.MathNaN, "NaN"));
mathClass.Add(FuStaticProperty.New(FloatType, FuId.MathNegativeInfinity, "NegativeInfinity"));
mathClass.Add(NewConstDouble("PI", Math.PI));
Expand Down
4 changes: 2 additions & 2 deletions GenC.fu
Original file line number Diff line number Diff line change
Expand Up @@ -2467,10 +2467,10 @@ public class GenC : GenCCpp
IncludeMath();
WriteCall("isnan", args[0]);
break;
case FuId.MathMaxDouble:
case FuId.MathMax:
WriteMathFloating("fmax", args);
break;
case FuId.MathMinDouble:
case FuId.MathMin:
WriteMathFloating("fmin", args);
break;
case FuId.MathRound:
Expand Down
14 changes: 8 additions & 6 deletions GenCl.fu
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,6 @@ public class GenCl : GenC
case FuId.MathIsFinite:
case FuId.MathIsNaN:
case FuId.MathLog2:
case FuId.MathMaxInt:
case FuId.MathMinInt:
case FuId.MathRound:
WriteLowercase(method.Name);
WriteInParentheses(args);
Expand All @@ -267,11 +265,15 @@ public class GenCl : GenC
case FuId.MathIsInfinity:
WriteCall("isinf", args[0]);
break;
case FuId.MathMaxDouble:
WriteCall("fmax", args[0], args[1]);
case FuId.MathMax:
if (args[0].Type is FuFloatingType || args[1].Type is FuFloatingType)
WriteChar('f');
WriteCall("max", args[0], args[1]);
break;
case FuId.MathMinDouble:
WriteCall("fmin", args[0], args[1]);
case FuId.MathMin:
if (args[0].Type is FuFloatingType || args[1].Type is FuFloatingType)
WriteChar('f');
WriteCall("min", args[0], args[1]);
break;
case FuId.MathTruncate:
WriteCall("trunc", args[0]);
Expand Down
6 changes: 2 additions & 4 deletions GenCpp.fu
Original file line number Diff line number Diff line change
Expand Up @@ -1218,13 +1218,11 @@ public class GenCpp : GenCCpp
IncludeMath();
WriteCall("std::isinf", args[0]);
break;
case FuId.MathMaxInt:
case FuId.MathMaxDouble: // TODO: <cmath> fmax ?
case FuId.MathMax: // TODO: <cmath> fmax ?
Include("algorithm");
WriteCall("(std::max)", args[0], args[1]);
break;
case FuId.MathMinInt:
case FuId.MathMinDouble: // TODO: <cmath> fmin ?
case FuId.MathMin: // TODO: <cmath> fmin ?
Include("algorithm");
WriteCall("(std::min)", args[0], args[1]);
break;
Expand Down
6 changes: 2 additions & 4 deletions GenCs.fu
Original file line number Diff line number Diff line change
Expand Up @@ -729,10 +729,8 @@ public class GenCs : GenTyped
break;
case FuId.MathAbs:
case FuId.MathClamp:
case FuId.MathMaxInt:
case FuId.MathMaxDouble:
case FuId.MathMinInt:
case FuId.MathMinDouble:
case FuId.MathMax:
case FuId.MathMin:
Include("System");
Write("Math.");
Write(method.Name);
Expand Down
6 changes: 2 additions & 4 deletions GenD.fu
Original file line number Diff line number Diff line change
Expand Up @@ -1173,10 +1173,8 @@ public class GenD : GenCCppD
WriteCall("ceil", args[0]);
break;
case FuId.MathClamp:
case FuId.MathMaxInt:
case FuId.MathMaxDouble:
case FuId.MathMinInt:
case FuId.MathMinDouble:
case FuId.MathMax:
case FuId.MathMin:
Include("std.algorithm");
WriteLowercase(method.Name);
WriteInParentheses(args);
Expand Down
6 changes: 2 additions & 4 deletions GenJava.fu
Original file line number Diff line number Diff line change
Expand Up @@ -628,10 +628,8 @@ public class GenJava : GenTyped
case FuId.StringWriterToString:
case FuId.MathMethod:
case FuId.MathAbs:
case FuId.MathMaxInt:
case FuId.MathMaxDouble:
case FuId.MathMinInt:
case FuId.MathMinDouble:
case FuId.MathMax:
case FuId.MathMin:
if (obj != null) {
if (IsReferenceTo(obj, FuId.BasePtr))
Write("super");
Expand Down
12 changes: 5 additions & 7 deletions GenJs.fu
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ public class GenJsNoModule : GenBase
Write("((x, y) => x ");
WriteChar(op);
Write(" y ? x : y)");
WriteCoercedArgsInParentheses(method, args);
WriteInParentheses(args);
}
else
WriteCall(name, args[0], args[1]);
Expand Down Expand Up @@ -548,8 +548,6 @@ public class GenJsNoModule : GenBase
case FuId.StringWriterToString:
case FuId.MathMethod:
case FuId.MathLog2:
case FuId.MathMaxDouble:
case FuId.MathMinDouble:
case FuId.MathRound:
if (obj == null)
WriteLocalName(method, FuPriority.Primary);
Expand Down Expand Up @@ -900,9 +898,9 @@ public class GenJsNoModule : GenBase
WriteCall("Math.ceil", args[0]);
break;
case FuId.MathClamp:
if (method.Type.Id == FuId.IntType && HasLong(args)) {
if (HasLong(args)) {
Write("((x, min, max) => x < min ? min : x > max ? max : x)");
WriteCoercedArgsInParentheses(method, args);
WriteInParentheses(args);
}
else {
Write("Math.min(Math.max(");
Expand Down Expand Up @@ -933,10 +931,10 @@ public class GenJsNoModule : GenBase
if (parent > FuPriority.Equality)
WriteChar(')');
break;
case FuId.MathMaxInt:
case FuId.MathMax:
WriteMathMaxMin(method, "Math.max", '>', args);
break;
case FuId.MathMinInt:
case FuId.MathMin:
WriteMathMaxMin(method, "Math.min", '<', args);
break;
case FuId.MathTruncate:
Expand Down
6 changes: 2 additions & 4 deletions GenPy.fu
Original file line number Diff line number Diff line change
Expand Up @@ -1137,12 +1137,10 @@ public class GenPy : GenPySwift
Include("math");
WriteCall("math.isinf", args[0]);
break;
case FuId.MathMaxInt:
case FuId.MathMaxDouble:
case FuId.MathMax:
WriteCall("max", args[0], args[1]);
break;
case FuId.MathMinInt:
case FuId.MathMinDouble:
case FuId.MathMin:
WriteCall("min", args[0], args[1]);
break;
case FuId.MathRound:
Expand Down
6 changes: 2 additions & 4 deletions GenSwift.fu
Original file line number Diff line number Diff line change
Expand Up @@ -789,10 +789,8 @@ public class GenSwift : GenPySwift
WriteInParentheses(args);
break;
case FuId.MathAbs:
case FuId.MathMaxInt:
case FuId.MathMaxDouble:
case FuId.MathMinInt:
case FuId.MathMinDouble:
case FuId.MathMax:
case FuId.MathMin:
WriteCamelCase(method.Name);
WriteInParentheses(args);
break;
Expand Down
6 changes: 6 additions & 0 deletions Sema.fu
Original file line number Diff line number Diff line change
Expand Up @@ -1397,6 +1397,12 @@ public class FuSema
FuType# type = method.Type;
if (type.Id == FuId.FloatingType)
type = arguments.Any(arg => arg.Type.Id == FuId.DoubleType) ? this.Host.Program.System.DoubleType : this.Host.Program.System.FloatType;
else if (type.Id == FuId.NumericType) {
type = arguments.Any(arg => arg.Type.Id == FuId.DoubleType) ? this.Host.Program.System.DoubleType
: arguments.Any(arg => arg.Type.Id == FuId.FloatType) ? this.Host.Program.System.FloatType
: arguments.Any(arg => arg.Type.Id == FuId.LongType) ? this.Host.Program.System.LongType
: this.Host.Program.System.IntType;
}
else if (symbol.Left != null && symbol.Left.Type is FuClassType generic)
type = EvalType(generic, type);
expr.Type = type;
Expand Down
Loading

0 comments on commit e250dbc

Please sign in to comment.