title: Math Parser in Delphi
body: <p>So here is what I am trying to do:</p>
  
  <p>I would like a parser that would evaluate equations and allow me to use custom functions. So, for example, an equation could be: </p>
  
  <pre><code>cos(5) * Patient:['lat1'] 
  </code></pre>
  
  <p>and it would let me have a function that does something if it finds Patient: in the expression. </p>
  
  <p>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.</p>
  
  <p>So, the problem seems to be with the custom function part, since everything else works fine.</p>
  
  <p>The way I'm adding the functions is:</p>
  
  <pre><code>MathParser.AddFunction('Patient:', FPatFunction, fkMethod, FunctionMethod(PatFunction, 1),
  False, False, vtDouble);
  MathParser.AddFunction('Organ:', FOrgFunction, fkMethod, FunctionMethod(OrganFunction, 1),
  False, False, vtDouble);
  </code></pre>
  
  <p>And then the functions themselves are</p>
  
  <pre><code>// 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));
  </code></pre>
  
  <p>end;</p>
  
  <pre><code>// 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;
  </code></pre>
  
  <p>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.</p>
  
  <p>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)</p>