Need help with an interesting call to JCL TEvaluator

I am using the TEvaluator JCL expression analyzer (a wonderful creation donated by barry kelly). (THANKS TO BE!)

background

I used the AddFunc method.

function MyFunc:double;
begin
  // calculations here
  Result:=1;  
end;

you can use the AddFunc method to make this function available:

  AddFunc('MyFunc', MyFunc);

here is the problem ...

I need to call a method on an object instead of a separate procedure.

the reason is that I have a list of objects that provide values.

let's say we have a list of vehicle objects. each object has a weight function. I want to be able to make every object weight available for use in the formula.

silly example, but easy to explain:

type
  TVehicle=class
  private
  public
    function Weight:double;
  end;

function StrangeCalculation:double;
var
  vehicle:TVehicle;
begin
  for iVehicle = 0 to Count - 1 do
    begin
      vehicle:=GetVehicle(iVehicle);
      // E2250 There is no overloaded version of 'AddFunc' that can be called with these arguments
      eval.AddFunc(vehicle.Name, vehicle.Weight);
    end;

  Result:=eval.Evaluate('JeepTJWeight + FordF150Weight * 2');
end;

my parameters:

  • AddVar () or AddConst () - but this is not so cool because I need to throw an exception if the value is not available.

  • AddFunc() . , ( ) .

  • , , . , , , .

  • AddFunc(), .

№ 3 , AddFunc . , , . , TMethod , ... , "E2250. " AddFunc ", " eval.AddFunc(), .

TFloat64MethodFunc = function(c:pointer): TFloat64;

procedure TEasyEvaluator.AddFunc(const AName: string; AFunc: TFloat64MethodFunc);
begin
  FOwnContext.Add(TExprFloat64MethodFuncSym.Create(AName, AFunc));
end;

TExprFloat64MethodFuncSym = class(TExprAbstractFuncSym)
private
  FFunc: TFloat64MethodFunc;
public
  constructor Create(const AIdent: string; AFunc: TFloat64MethodFunc);
  function Evaluate: TFloat; override;
// not using   function Compile: TExprNode; override;
end;

!

+3
2

...

TFloat64MethodFunc = function: TFloat ;

+5

- (2004), . Turbo Power SysTools, .

0

Source: https://habr.com/ru/post/1705486/


All Articles