需要帮助对JCL的TEvaluator进行有趣的调用

X-R*_*Ray 3 delphi jedi-code-library

我正在使用JCL的表达评估员TEvaluator(由barry kelly捐赠的奇妙创作).(谢谢你!)

背景

我使用了AddFunc方法.

function MyFunc:double;
begin
  // calculations here
  Result:=1;  
end;
Run Code Online (Sandbox Code Playgroud)

您可以使用AddFunc方法使该函数可用:

  AddFunc('MyFunc', MyFunc);
Run Code Online (Sandbox Code Playgroud)

这是问题......

我需要在一个对象上调用一个方法而不是一个独立的例程.

原因是我有一个提供值的对象列表.

说我们有一个车辆对象列表.每个对象都有一个权重函数.我希望能够使每个对象的重量可用于公式中.

一个愚蠢的例子,但很容易解释:

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;
Run Code Online (Sandbox Code Playgroud)

我的选择:

  1. AddVar()或AddConst() - 但这不是很好,因为如果值不可用,我需要能够引发异常.

  2. 具有独立函数的AddFunc().不能这样做,因为变量的名称(和数量)在运行时才是未知的.

  3. 如果找不到变量,则修改对象以添加回调.我实际上已经完成了这个,但需要编辑源代码的副本以回复它以实现此目的.

  4. 创建一个能够使用方法函数的AddFunc().

选项#3实际上已构建,但额外的AddFunc会更好.麻烦的是我不知道提供什么方法​​原型.我认为TMethod会是方式,但我的知识在这里太有限了...这是我的尝试不成功但我仍然得到"E2250在eval.AddFunc中没有可以使用这些参数调用'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;
Run Code Online (Sandbox Code Playgroud)

谢谢您的帮助!

熔点

X-R*_*Ray 5

弄清楚了...

TFloat64MethodFunc = function:对象的TFloat;