Fix in RTL-Core

This commit is contained in:
Michael Schimmel
2026-01-06 16:50:48 +01:00
parent 74a5c30ae0
commit 51d0b8d9b7
2 changed files with 93 additions and 52 deletions
+1
View File
@@ -216,6 +216,7 @@ begin
+ '### 2. Constraints & Rules' + '### 2. Constraints & Rules'
+ sLineBreak + sLineBreak
+ GenerateGenerationRules; + GenerateGenerationRules;
//TODO add 3. RTL
end; end;
end. end.
+92 -52
View File
@@ -7,92 +7,145 @@ uses
Myc.Utils, Myc.Utils,
Myc.Data.Scalar, Myc.Data.Scalar,
Myc.Data.Value, Myc.Data.Value,
Myc.Ast.RTL; Myc.Ast.RTL,
Myc.Ast.Attributes;
type type
// Contains the "pure" native implementations. // Contains the "pure" native implementations.
TRtlFunctions = record TRtlFunctions = record
public public
// (* --- Dynamic Fallbacks (Interpreter) --- *)
// Arithmetic // Arithmetic
[TRtlExport('+', Pure)] [TRtlExport('+', Pure)]
[AstDoc('Adds two numbers or concatenates two strings.')]
class function Add(const Args: TArray<TDataValue>): TDataValue; overload; static; class function Add(const Args: TArray<TDataValue>): TDataValue; overload; static;
[TRtlExport('-', Pure)] [TRtlExport('-', Pure)]
[AstDoc('Subtracts B from A, or negates A if called with one argument.')]
class function Subtract(const Args: TArray<TDataValue>): TDataValue; static; class function Subtract(const Args: TArray<TDataValue>): TDataValue; static;
[TRtlExport('*', Pure)] [TRtlExport('*', Pure)]
[AstDoc('Multiplies two numbers.')]
class function Multiply(const Args: TArray<TDataValue>): TDataValue; static; class function Multiply(const Args: TArray<TDataValue>): TDataValue; static;
[TRtlExport('/', Pure)] [TRtlExport('/', Pure)]
[AstDoc('Divides A by B. Returns a Float.')]
class function Divide(const Args: TArray<TDataValue>): TDataValue; static; class function Divide(const Args: TArray<TDataValue>): TDataValue; static;
[TRtlExport('div', Pure)] [TRtlExport('div', Pure)]
[AstDoc('Integer division.')]
class function IntDivide(const Args: TArray<TDataValue>): TDataValue; static; class function IntDivide(const Args: TArray<TDataValue>): TDataValue; static;
[TRtlExport('mod', Pure)] [TRtlExport('mod', Pure)]
[AstDoc('Remainder of integer division.')]
class function Modulus(const Args: TArray<TDataValue>): TDataValue; static; class function Modulus(const Args: TArray<TDataValue>): TDataValue; static;
// Comparison // Comparison
[TRtlExport('=', Impure)] [TRtlExport('=', Pure)]
[AstDoc('Returns true if A equals B.')]
class function Equal(const Args: TArray<TDataValue>): TDataValue; static; class function Equal(const Args: TArray<TDataValue>): TDataValue; static;
[TRtlExport('<>', Impure)]
[TRtlExport('<>', Pure)]
[AstDoc('Returns true if A is not equal to B.')]
class function NotEqual(const Args: TArray<TDataValue>): TDataValue; static; class function NotEqual(const Args: TArray<TDataValue>): TDataValue; static;
[TRtlExport('<', Impure)]
[TRtlExport('<', Pure)]
[AstDoc('Returns true if A is less than B.')]
class function LessThan(const Args: TArray<TDataValue>): TDataValue; static; class function LessThan(const Args: TArray<TDataValue>): TDataValue; static;
[TRtlExport('<=', Impure)]
[TRtlExport('<=', Pure)]
[AstDoc('Returns true if A is less than or equal to B.')]
class function LessThanOrEqual(const Args: TArray<TDataValue>): TDataValue; static; class function LessThanOrEqual(const Args: TArray<TDataValue>): TDataValue; static;
[TRtlExport('>', Impure)]
[TRtlExport('>', Pure)]
[AstDoc('Returns true if A is greater than B.')]
class function GreaterThan(const Args: TArray<TDataValue>): TDataValue; static; class function GreaterThan(const Args: TArray<TDataValue>): TDataValue; static;
[TRtlExport('>=', Impure)]
[TRtlExport('>=', Pure)]
[AstDoc('Returns true if A is greater than or equal to B.')]
class function GreaterThanOrEqual(const Args: TArray<TDataValue>): TDataValue; static; class function GreaterThanOrEqual(const Args: TArray<TDataValue>): TDataValue; static;
// Logic / Bitwise // Logic / Bitwise
[TRtlExport('not', Impure)] [TRtlExport('not', Pure)]
[AstDoc('Logical or bitwise NOT.')]
class function LogicalNot(const Args: TArray<TDataValue>): TDataValue; static; class function LogicalNot(const Args: TArray<TDataValue>): TDataValue; static;
[TRtlExport('and', Impure)]
[TRtlExport('and', Pure)]
[AstDoc('Logical or bitwise AND.')]
class function BitwiseAnd(const Args: TArray<TDataValue>): TDataValue; static; class function BitwiseAnd(const Args: TArray<TDataValue>): TDataValue; static;
[TRtlExport('or', Impure)]
[TRtlExport('or', Pure)]
[AstDoc('Logical or bitwise OR.')]
class function BitwiseOr(const Args: TArray<TDataValue>): TDataValue; static; class function BitwiseOr(const Args: TArray<TDataValue>): TDataValue; static;
[TRtlExport('xor', Impure)]
[TRtlExport('xor', Pure)]
[AstDoc('Logical or bitwise XOR.')]
class function BitwiseXor(const Args: TArray<TDataValue>): TDataValue; static; class function BitwiseXor(const Args: TArray<TDataValue>): TDataValue; static;
[TRtlExport('shl', Impure)]
[TRtlExport('shl', Pure)]
[AstDoc('Bitwise shift left.')]
class function LeftShift(const Args: TArray<TDataValue>): TDataValue; static; class function LeftShift(const Args: TArray<TDataValue>): TDataValue; static;
[TRtlExport('shr', Impure)]
[TRtlExport('shr', Pure)]
[AstDoc('Bitwise shift right.')]
class function RightShift(const Args: TArray<TDataValue>): TDataValue; static; class function RightShift(const Args: TArray<TDataValue>): TDataValue; static;
// (* Dynamic fallbacks for TScalar input *) // Math Functions
[TRtlExport('Abs', Impure)] [TRtlExport('Abs', Pure)]
[AstDoc('Returns the absolute value.')]
class function Abs(const Arg: TScalar): TScalar; static; class function Abs(const Arg: TScalar): TScalar; static;
[TRtlExport('Round', Impure)]
[TRtlExport('Round', Pure)]
[AstDoc('Rounds a float to the nearest integer.')]
class function Round(const Arg: TScalar): TScalar; static; class function Round(const Arg: TScalar): TScalar; static;
[TRtlExport('Trunc', Impure)]
[TRtlExport('Trunc', Pure)]
[AstDoc('Truncates a float to an integer.')]
class function Trunc(const Arg: TScalar): TScalar; static; class function Trunc(const Arg: TScalar): TScalar; static;
[TRtlExport('Ceil', Impure)]
[TRtlExport('Ceil', Pure)]
[AstDoc('Returns the smallest integer greater than or equal to the argument.')]
class function Ceil(const Arg: TScalar): TScalar; static; class function Ceil(const Arg: TScalar): TScalar; static;
[TRtlExport('Floor', Impure)]
[TRtlExport('Floor', Pure)]
[AstDoc('Returns the largest integer less than or equal to the argument.')]
class function Floor(const Arg: TScalar): TScalar; static; class function Floor(const Arg: TScalar): TScalar; static;
[TRtlExport('Sign', Impure)]
[TRtlExport('Sign', Pure)]
[AstDoc('Returns -1 for negative numbers, 1 for positive numbers, and 0 for zero.')]
class function Sign(const Arg: TScalar): TScalar; static; class function Sign(const Arg: TScalar): TScalar; static;
// (* DateTime Constructors *) // DateTime
[TRtlExport('Now', Impure)] [TRtlExport('Now', Impure)]
[AstDoc('Returns the current system date and time.')]
class function Now(const Args: TArray<TDataValue>): TDataValue; static; class function Now(const Args: TArray<TDataValue>): TDataValue; static;
[TRtlExport('Date', Impure)] [TRtlExport('Date', Impure)]
[AstDoc('Returns the current date or constructs a date from (Year, Month, Day).')]
class function Date(const Args: TArray<TDataValue>): TDataValue; static; class function Date(const Args: TArray<TDataValue>): TDataValue; static;
// (* Other dynamic functions *) // High-Order / Series
[TRtlExport('Memoize', Impure)] [TRtlExport('Memoize', Pure)]
[AstDoc('Returns a memoized version of the provided function.')]
class function Memoize(const Args: TArray<TDataValue>): TDataValue; static; class function Memoize(const Args: TArray<TDataValue>): TDataValue; static;
[TRtlExport('Map', Impure)]
[TRtlExport('Map', Pure)]
[AstDoc('Maps a function over a series.')]
class function Map(const Args: TArray<TDataValue>): TDataValue; static; class function Map(const Args: TArray<TDataValue>): TDataValue; static;
[TRtlExport('Reduce', Impure)]
[TRtlExport('Reduce', Pure)]
[AstDoc('Reduces a series using a combining function and an accumulator.')]
class function Reduce(const Args: TArray<TDataValue>): TDataValue; static; class function Reduce(const Args: TArray<TDataValue>): TDataValue; static;
[TRtlExport('Where', Impure)]
[TRtlExport('Where', Pure)]
[AstDoc('Filters a series using a predicate function.')]
class function Where(const Args: TArray<TDataValue>): TDataValue; static; class function Where(const Args: TArray<TDataValue>): TDataValue; static;
[TRtlExport('Any', Impure)]
[TRtlExport('Any', Pure)]
[AstDoc('Returns true if any element in the series satisfies the predicate.')]
class function Any(const Args: TArray<TDataValue>): TDataValue; static; class function Any(const Args: TArray<TDataValue>): TDataValue; static;
// (* --- Static Specializations (Monomorphization Targets) --- *) // --- Static Specializations (Picked up by TRtlRegistry via attributes) ---
// Add
[TRtlExport('+', Pure)] [TRtlExport('+', Pure)]
class function Add_Ordinal_Ordinal_Ordinal(A, B: Int64): Int64; static; class function Add_Ordinal_Ordinal_Ordinal(A, B: Int64): Int64; static;
[TRtlExport('+', Pure)] [TRtlExport('+', Pure)]
@@ -104,7 +157,6 @@ type
[TRtlExport('+', Pure)] [TRtlExport('+', Pure)]
class function Add_Text_Text_Text(const A: String; const B: String): String; static; class function Add_Text_Text_Text(const A: String; const B: String): String; static;
// Subtract
[TRtlExport('-', Pure)] [TRtlExport('-', Pure)]
class function Subtract_Ordinal_Ordinal_Ordinal(A, B: Int64): Int64; static; class function Subtract_Ordinal_Ordinal_Ordinal(A, B: Int64): Int64; static;
[TRtlExport('-', Pure)] [TRtlExport('-', Pure)]
@@ -114,7 +166,6 @@ type
[TRtlExport('-', Pure)] [TRtlExport('-', Pure)]
class function Subtract_Float_Ordinal_Float(A: Double; B: Int64): Double; static; class function Subtract_Float_Ordinal_Float(A: Double; B: Int64): Double; static;
// Multiply
[TRtlExport('*', Pure)] [TRtlExport('*', Pure)]
class function Multiply_Ordinal_Ordinal_Ordinal(A, B: Int64): Int64; static; class function Multiply_Ordinal_Ordinal_Ordinal(A, B: Int64): Int64; static;
[TRtlExport('*', Pure)] [TRtlExport('*', Pure)]
@@ -124,23 +175,20 @@ type
[TRtlExport('*', Pure)] [TRtlExport('*', Pure)]
class function Multiply_Float_Ordinal_Float(A: Double; B: Int64): Double; static; class function Multiply_Float_Ordinal_Float(A: Double; B: Int64): Double; static;
// Divide (Impure due to EDivByZero potential) [TRtlExport('/', Pure)]
[TRtlExport('/', Impure)]
class function Divide_Ordinal_Ordinal_Float(A, B: Int64): Double; static; class function Divide_Ordinal_Ordinal_Float(A, B: Int64): Double; static;
[TRtlExport('/', Impure)] [TRtlExport('/', Pure)]
class function Divide_Float_Float_Float(A, B: Double): Double; static; class function Divide_Float_Float_Float(A, B: Double): Double; static;
[TRtlExport('/', Impure)] [TRtlExport('/', Pure)]
class function Divide_Ordinal_Float_Float(A: Int64; B: Double): Double; static; class function Divide_Ordinal_Float_Float(A: Int64; B: Double): Double; static;
[TRtlExport('/', Impure)] [TRtlExport('/', Pure)]
class function Divide_Float_Ordinal_Float(A: Double; B: Int64): Double; static; class function Divide_Float_Ordinal_Float(A: Double; B: Int64): Double; static;
// Integer Math (Impure due to EDivByZero) [TRtlExport('div', Pure)]
[TRtlExport('div', Impure)]
class function Div_Ordinal_Ordinal_Ordinal(A, B: Int64): Int64; static; class function Div_Ordinal_Ordinal_Ordinal(A, B: Int64): Int64; static;
[TRtlExport('mod', Impure)] [TRtlExport('mod', Pure)]
class function Mod_Ordinal_Ordinal_Ordinal(A, B: Int64): Int64; static; class function Mod_Ordinal_Ordinal_Ordinal(A, B: Int64): Int64; static;
// Comparisons
[TRtlExport('=', Pure)] [TRtlExport('=', Pure)]
class function Equal_Ordinal_Ordinal_Ordinal(A, B: Int64): Int64; static; class function Equal_Ordinal_Ordinal_Ordinal(A, B: Int64): Int64; static;
[TRtlExport('=', Pure)] [TRtlExport('=', Pure)]
@@ -199,7 +247,6 @@ type
[TRtlExport('>=', Pure)] [TRtlExport('>=', Pure)]
class function GreaterOrEqual_Float_Ordinal_Ordinal(A: Double; B: Int64): Int64; static; class function GreaterOrEqual_Float_Ordinal_Ordinal(A: Double; B: Int64): Int64; static;
// Bitwise Static
[TRtlExport('and', Pure)] [TRtlExport('and', Pure)]
class function And_Ordinal_Ordinal_Ordinal(A, B: Int64): Int64; static; class function And_Ordinal_Ordinal_Ordinal(A, B: Int64): Int64; static;
[TRtlExport('or', Pure)] [TRtlExport('or', Pure)]
@@ -211,26 +258,21 @@ type
[TRtlExport('shr', Pure)] [TRtlExport('shr', Pure)]
class function Shr_Ordinal_Ordinal_Ordinal(A, B: Int64): Int64; static; class function Shr_Ordinal_Ordinal_Ordinal(A, B: Int64): Int64; static;
// Unary
[TRtlExport('-', Pure)] [TRtlExport('-', Pure)]
class function Negate_Ordinal_Ordinal(A: Int64): Int64; static; class function Negate_Ordinal_Ordinal(A: Int64): Int64; static;
[TRtlExport('-', Pure)] [TRtlExport('-', Pure)]
class function Negate_Float_Float(A: Double): Double; static; class function Negate_Float_Float(A: Double): Double; static;
[TRtlExport('not', Pure)] [TRtlExport('not', Pure)]
class function Not_Ordinal_Ordinal(A: Int64): Int64; static; class function Not_Ordinal_Ordinal(A: Int64): Int64; static;
// Standard functions
[TRtlExport('Abs', Pure)] [TRtlExport('Abs', Pure)]
class function Abs_Ordinal_Ordinal(A: Int64): Int64; static; class function Abs_Ordinal_Ordinal(A: Int64): Int64; static;
[TRtlExport('Abs', Pure)] [TRtlExport('Abs', Pure)]
class function Abs_Float_Float(A: Double): Double; static; class function Abs_Float_Float(A: Double): Double; static;
[TRtlExport('Round', Pure)] [TRtlExport('Round', Pure)]
class function Round_Ordinal_Ordinal(A: Int64): Int64; static; class function Round_Ordinal_Ordinal(A: Int64): Int64; static;
[TRtlExport('Round', Pure)] [TRtlExport('Round', Pure)]
class function Round_Float_Ordinal(A: Double): Int64; static; class function Round_Float_Ordinal(A: Double): Int64; static;
[TRtlExport('Trunc', Pure)] [TRtlExport('Trunc', Pure)]
class function Trunc_Ordinal_Ordinal(A: Int64): Int64; static; class function Trunc_Ordinal_Ordinal(A: Int64): Int64; static;
[TRtlExport('Trunc', Pure)] [TRtlExport('Trunc', Pure)]
@@ -278,7 +320,6 @@ class function TRtlFunctions.Divide(const Args: TArray<TDataValue>): TDataValue;
begin begin
if Length(Args) <> 2 then if Length(Args) <> 2 then
raise EArgumentException.Create('Operator / requires 2 arguments.'); raise EArgumentException.Create('Operator / requires 2 arguments.');
// Explicit check is handled in TScalar.Divide
Result := Args[0].AsScalar / Args[1].AsScalar; Result := Args[0].AsScalar / Args[1].AsScalar;
end; end;
@@ -609,7 +650,6 @@ begin
item := sourceSeries.Items[i]; item := sourceSeries.Items[i];
predicateResult := predicateFunc([TDataValue(item)]); predicateResult := predicateFunc([TDataValue(item)]);
// Use the implicit Boolean operator of TScalar
if (predicateResult.Kind = vkScalar) and (Boolean(predicateResult.AsScalar)) then if (predicateResult.Kind = vkScalar) and (Boolean(predicateResult.AsScalar)) then
matchingIndices.Add(i); matchingIndices.Add(i);
end; end;
@@ -754,7 +794,7 @@ end;
class function TRtlFunctions.Div_Ordinal_Ordinal_Ordinal(A, B: Int64): Int64; class function TRtlFunctions.Div_Ordinal_Ordinal_Ordinal(A, B: Int64): Int64;
begin begin
Result := A div B; Result := A div B;
end; // Delphi runtime handles EDivByZero for integers end;
class function TRtlFunctions.Mod_Ordinal_Ordinal_Ordinal(A, B: Int64): Int64; class function TRtlFunctions.Mod_Ordinal_Ordinal_Ordinal(A, B: Int64): Int64;
begin begin
Result := A mod B; Result := A mod B;
@@ -906,7 +946,7 @@ end;
class function TRtlFunctions.Not_Ordinal_Ordinal(A: Int64): Int64; class function TRtlFunctions.Not_Ordinal_Ordinal(A: Int64): Int64;
begin begin
Result := Ord(A = 0); Result := Ord(A = 0);
end; // Logical NOT for Int64 end;
// Standard // Standard
class function TRtlFunctions.Abs_Ordinal_Ordinal(A: Int64): Int64; class function TRtlFunctions.Abs_Ordinal_Ordinal(A: Int64): Int64;