2

So here is what I am trying to do:

I would like a parser that would evaluate equations and allow me to use custom functions. So, for example, an equation could be:

cos(5) * Patient:['lat1'] 

and it would let me have a function that does something if it finds Patient: in the expression.

The program I'm working on used a parser called MathParser (http://www.myart.bz/mathparser/), which worked fine in Delphi 2007, but has problems in Delphi XE (because of unicode). The MathParser site has a new version for XE, which I installed, but some functions have been changed around and I'm trying to get it to work right.

So, the problem seems to be with the custom function part, since everything else works fine.

The way I'm adding the functions is:

MathParser.AddFunction('Patient:', FPatFunction, fkMethod, FunctionMethod(PatFunction, 1),
False, False, vtDouble);
MathParser.AddFunction('Organ:', FOrgFunction, fkMethod, FunctionMethod(OrganFunction, 1),
False, False, vtDouble);

And then the functions themselves are

// patient function
function CEquatCalc.PatFunction(const AFunction: PFunction; const AType: PType;
 const ParameterArray: TParameterArray): TValue;
 begin
  if Assigned(FPatient) and Assigned(FOrgan) then
   AssignDouble(Result, (FPatient as CPatientItem).GetScoreVal((Trim(ParameterArray[0].Text)), (FOrgan as COrganItem)))
  else if Assigned(FPatient) then
   AssignDouble(Result,(FPatient as CPatientItem).GetScoreVal((Trim(ParameterArray[0].Text)), NIL));

end;

// organ function
function CEquatCalc.OrganFunction(const AFunction: PFunction; const AType: PType;
 const ParameterArray: TParameterArray): TValue;
 begin
  AssignDouble(Result, (FOrgan as COrganItem).GetScoreVal((Trim(ParameterArray[0].Text))));
 end;

The error that pops up says something like "Unknown element: lat1". Unfortunately since the component didn't come with Parser.pas (just the dcu file), I can't debug where it's happening.

Hopefully someone knows what the problem is, and if not, could you recommend another parser that would do what I want? (and is preferably free)

KingOfKong
  • 279
  • 7
  • 17
  • Sounds like a bug or a parser change in the component itself: without source, your best option would be to [contact the developer](http://www.myart.bz/mathparser/index.php?option=com_content&view=article&id=11&Itemid=13) and give them a small demo program that shows the problem (with a mockup of your math expression and the functions you need to call.) – David Aug 17 '11 at 00:23

4 Answers4

3

There are several nice free solutions for evaluating expressions at runtime now. Check out the answers to this question.

Community
  • 1
  • 1
Brian Frost
  • 13,334
  • 11
  • 80
  • 154
  • Those are good ideas, I have also used `JvInterpreter` for a lot of this kind of thing. It is free in JVCL. – Warren P Apr 06 '13 at 14:49
2

TbcParser does what you want. It is a math parser component for Delphi. It allows custom functions, and variables.

Mike
  • 21
  • 2
1

If you do have JVCL then it have an expression evaluating component too - JvInterpreter.

ain
  • 22,394
  • 3
  • 54
  • 74
0

Not sure if it does exactly what you want, but you may give Pegtop math components a try.

iamjoosy
  • 3,299
  • 20
  • 30