Scalar operators

This commit is contained in:
Michael Schimmel
2025-09-15 17:13:45 +02:00
parent a83d3bf9fc
commit f5c7121e26
3 changed files with 98 additions and 51 deletions
+15 -37
View File
@@ -395,7 +395,6 @@ end;
function TEvaluatorVisitor.VisitBinaryExpression(const Node: IBinaryExpressionNode): TDataValue;
var
leftValue, rightValue: TDataValue;
leftScalar, rightScalar: TScalar;
begin
leftValue := Node.Left.Accept(Self);
rightValue := Node.Right.Accept(Self);
@@ -403,51 +402,30 @@ begin
if (leftValue.Kind <> vkScalar) or (rightValue.Kind <> vkScalar) then
raise ENotSupportedException.Create('Binary operations are only supported for scalar types.');
leftScalar := leftValue.AsScalar;
rightScalar := rightValue.AsScalar;
if not TScalar.IsBinaryOperatorSupported(Node.Operator, leftScalar.Kind, rightScalar.Kind) then
raise ENotSupportedException.Create('Binary operation not supported for scalar type.');
case Node.Operator of
boAdd: Result := leftScalar + rightScalar;
boSubtract: Result := leftScalar - rightScalar;
boMultiply: Result := leftScalar * rightScalar;
boDivide: Result := leftScalar / rightScalar;
boEqual: Result := TScalar.FromBoolean(leftScalar = rightScalar);
boNotEqual: Result := TScalar.FromBoolean(leftScalar <> rightScalar);
boLess: Result := TScalar.FromBoolean(leftScalar < rightScalar);
boGreater: Result := TScalar.FromBoolean(leftScalar > rightScalar);
boLessOrEqual: Result := TScalar.FromBoolean(leftScalar <= rightScalar);
boGreaterOrEqual: Result := TScalar.FromBoolean(leftScalar >= rightScalar);
else
raise ENotSupportedException.Create('Unsupported binary operator.');
end;
var res: TScalar;
if not TScalar.TryBinaryOperation(Node.Operator, leftValue.AsScalar, rightValue.AsScalar, res) then
raise ENotSupportedException.Create(
'Binary operation not supported for scalar types '
+ leftValue.AsScalar.Kind.ToString
+ ' and '
+ rightValue.AsScalar.Kind.ToString
+ ' .');
Result := res;
end;
function TEvaluatorVisitor.VisitUnaryExpression(const Node: IUnaryExpressionNode): TDataValue;
var
rightValue: TDataValue;
rightScalar: TScalar;
begin
rightValue := Node.Right.Accept(Self);
if not TScalar.IsUnaryOperatorSupported(Node.Operator, rightScalar.Kind) then
raise ENotSupportedException.Create('Unary operation not supported for scalar type.');
if rightValue.Kind <> vkScalar then
raise ENotSupportedException.Create('Unary operations are only supported for scalar types.');
case Node.Operator of
uoNegate:
begin
rightScalar := rightValue.AsScalar;
Result := -rightScalar;
end;
uoNot:
begin
Result := TScalar.FromBoolean(not IsTruthy(rightValue));
end;
else
raise ENotSupportedException.Create('Unary operator not supported');
end;
var res: TScalar;
if not TScalar.TryUnaryOperation(Node.Operator, rightValue.AsScalar, res) then
raise ENotSupportedException.Create('Unary operation not supported for scalar type' + rightValue.AsScalar.Kind.ToString + '.');
Result := res;
end;
function TEvaluatorVisitor.VisitIfExpression(const Node: IIfExpressionNode): TDataValue;