[SOLVED] TFPExpressionParser using Aggregate-function inside other functions lazarus / 31-LCL ---------------------------------------------------------------------- PetWey · 2020-05-16 17:40 Hello Forum, i found the great TFPExpressionParser and tested a little bit. There is a problem i found, if i use a aggregate-function inside an other function. The example makes no sense, but demonstrates the problem (i followed the hints in "How To Use TFPExpressionParser): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);begin  FParser.Free;end; procedure TForm1.Button1Click(Sender: TObject);begin  FParser.UpdateAggregate;  Label1.caption := FloatToStr(FParser.Evaluate.ResFloat);end; procedure TForm1.FormCreate(Sender: TObject);begin  FParser := TFPExpressionParser.Create(nil);  FParser.BuiltIns := [bcMath, bcAggregate];  FParser.Identifiers.AddFloatVariable('x', 0.5);  FParser.Expression := 'sin(sum(x))';  FParser.InitAggregate;end;   If i press the button first time the sum should get 0.5, but the sin gives me 0. If i press the button again, the result is also 0. I know there is a way to solve this problem by splitting the calculation in two steps. But is this the correct behaviour of the aggregat-functions? Thanks for your help ---------------------------------------------------------------------- wp · 2020-05-17 09:48 The issue seems to be fixed in fpc 3.2. ---------------------------------------------------------------------- PetWey · 2020-05-17 12:47 Hallo wp, thanks for your reply. I installed lazarus 2.0.6 with FPC 3.2 and tryied again. Same mistake as with my older lazarus 2.00 withh FPC 3.0.4! I made an additional Test with an user-function ... Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i FParser.Identifiers.AddFunction('ArrI','I','SI+',@BuiltInArrI); ... and i get an error-message (SErrInvalidResultCharacter) -> + is not a valid return type indicator. I'm using Win10 64Bit. Any other hints? ---------------------------------------------------------------------- julkas · 2020-05-17 16:14 Off topic question. From wiki - QuoteIn the next step, the expression string has to be passed to the parser:   FParser.Expression := 'sin(x)*cos(2*x)'; It is essential to call this after setting up of the variables because the parser needs to know the variables for analyzing the expression. Who knows why ? Parser can analyze variables in evaluate method. ---------------------------------------------------------------------- wp · 2020-05-17 17:47 @julkas When you assign the string to the Expression property the "scanner" builds a tree of all elements of the expression. Therefore, every element must be known to find its correct place in this tree, and so you must "declare" the variable "x"(by calling "AddFloatVariable()") before assigning the expression string. In the Evaluate method the parser simply calculates the result based on the tree structure set up by the scanner. @PetWey I just checked again: I downloaded the fpexprpars.pp from FPC 3.2, copied it into the folder with your demo project, rebuilt it - and it was working correctly, even with Laz 1.4.4/fpc 2.6.4. I also checked the new symbol '+' in the definition of user-defined formulas. It is working, too.  I often place a local copy of fpexprpars.pp in a project folder to simplify debugging of issues in this unit. This is possible because fpexprpars is not used by the major FCL and LCL units (it is used by TAChart, though). ---------------------------------------------------------------------- PetWey · 2020-05-17 18:26 Hallo wp, I'm sorry I got you off work. The mistake is mine. I previously saved and expanded fpexprpars locally because I had the problem with zero division in the arguments. For this reason I now had a current FPC 3.2.0 but still the old fpexprpars. I will now test with the current version. I think I owe you something. I want to add array functions and iterations. I already have variable assignments. And it is planned that not just one but several formula lines should be processed one after the other. Maybe you can do something with the finished code. It only takes a little bit because I won't be able to do that the next week. MfG, petwey ---------------------------------------------------------------------- wp · 2020-05-17 20:19 Quote from: PetWey on May 17, 2020, 08:26:13 pmI want to add array functions and iterations. I already have variable assignments. And it is planned that not just one but several formula lines should be processed one after the other. Maybe you can do something with the finished code. Sounds interesting. When you want to extend the expression parser post your code as a patch relative to the trunk version in the bug tracker (link in the forum sidebar). Then Michael Van Canneyt who is the author of the parser can have a look and decide whether he wants to include it in the official distribution or not. Since you are new to this community you maybe need some help in writing a bug report: you can take my own feature request for the variable argument count as an example (https://bugs.freepascal.org/view.php?id=36380). ---------------------------------------------------------------------- PetWey · 2020-05-18 08:26 Hallo wp, Now I have tested with the code you posted -> everything works as described by you. Compare my freshly downloaded version with yours -> yours is more current. Divide-by-zero test results in an error message as requested. BUT if I set up an IFF condition to intercept the division by NULL, I get the error message instead of the alternative value. Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i iff(true,3.0,3.0/0.0) It would be nice if the IFx only evaluated the branch that was given by the condition. For my previously mentioned extensions I will choose a workaround because I am not deep enough in the programming of fpexprpars. I will then provide you with my code. Maybe you will find a way to incorporate it cleanly. However, I would find it very useful to firmly integrate the remaining mathematical functions such as tangents (see your addons). MfG, petwey ---------------------------------------------------------------------- Thaddy · 2020-05-18 08:31 Afaik, the generic ifthen<> does what you want (otherwise I am more stupid than that little patch I provided....) 3.2.0 ---------------------------------------------------------------------- PascalDragon · 2020-05-18 08:42 Quote from: Thaddy on May 18, 2020, 10:31:50 amAfaik, the generic ifthen<> does what you want (otherwise I am more stupid than that little patch I provided....) 3.2.0 If I understood this correctly, the IIF is not about a Pascal if, but about the iif supported by the expression parser. But even if not you're wrong: Even for inlined functions all parameters are evaluated before the control is passed to the inlined code, thus for IfThen<> all “branches” will be evaluated beforehand as well. ---------------------------------------------------------------------- PetWey · 2020-05-18 11:58 Hallo Thaddy, hallo PascalDragon, IFF is an expression and all branches of the expressions are evaluated. If i know a expression leads to division to zero, i want to use a iff to handle it. For example: Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i IFF(i=0,0.0,1/i) The current version gives me an exception and stops the evaluation. My solution is a adapted version of: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Procedure TFPDivideOperation.GetNodeValue(var Result : TFPExpressionResult); Var  RRes : TFPExpressionResult; begin  Left.GetNodeValue(Result);  Right.GetNodeValue(RRes);  case Result.ResultType of    rtInteger  :      if RRes.ResInteger<>0 then      Result.ResFloat:=Result.ResInteger/RRes.ResInteger      else if Result.ResInteger > 0 then        Result.ResFloat := Infinity      else if Result.ResInteger < 0 then        Result.ResFloat := NegInfinity      else        Result.ResFloat := NaN;      //else      //  RaiseParserError(SErrDivisionByZero, [ResultTypeName(rtInteger)]);    rtFloat    :      if RRes.ResFloat<>0 then        Result.ResFloat:=Result.ResFloat/RRes.ResFloat      else if Result.ResFloat > 0 then        Result.ResFloat := Infinity      else if Result.ResFloat < 0 then        Result.ResFloat := NegInfinity      else        Result.ResFloat := NaN;      //else      //  RaiseParserError(SErrDivisionByZero, [ResultTypeName(rtInteger)]);    rtCurrency :        if NodeType=rtCurrency then           if RRes.ResCurrency <> 0 then           Result.ResCurrency:=Result.ResCurrency/RRes.ResCurrency         else if Result.ResCurrency > 0 then             Result.ResCurrency := NaN // Infinity           else if Result.ResCurrency < 0 then           Result.ResCurrency := NaN // NegInfinity           else             Result.ResCurrency := NaN;           //else           //  RaiseParserError(SErrDivisionByZero, [ResultTypeName(rtCurrency)])      else          if RRes.ResFloat<> 0 then            Result.ResFloat:=Result.ResFloat/RRes.ResFloat          else if Result.ResFloat > 0 then            Result.ResFloat := Infinity        else if Result.ResFloat < 0 then            Result.ResFloat := NegInfinity          else            Result.ResFloat := NaN;          //else          //  RaiseParserError(SErrDivisionByZero, [ResultTypeName(rtFloat)]);  end;  Result.ResultType:=NodeType;end;  MfG, petwey ---------------------------------------------------------------------- wp · 2020-05-18 12:57 Please file a bug report, I do not have write access to the fpc units. I don't know whether Michael will accept this change because you can always catch the exception to handle the error. Your code kind of breaks this concept. Of course, the IFF function becomes useless then for this particular application. A better solution in my eyes would be to write something for the IFF function alone. But this may become quite difficult because all the functions get an argument list for which they calculate the results first. Then these results are passed to the function itself: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Procedure TFPFunctionCallBack.GetNodeValue(var Result : TFPExpressionResult);begin  If Length(FArgumentParams)>0 then      CalcParams;                     // <--- Calculate values of arguments   FCallBack(Result,FArgumentParams);           // <--- Calculate functionm  Result.ResultType:=NodeType;end;This function callback is very general, it does not know anything about the special situation in the IFF function, i.e. it does not know that it should calculate the first argument at first and then decide on calculation of the other arguments. Quite tough... ---------------------------------------------------------------------- PetWey · 2020-05-18 16:51 Hallo wp, thanks for the hint. I know this problem is not easy to solve. I filled a bug report: https://bugs.freepascal.org/view.php?id=37106 But at this time my solution is to use a local copy of fpexprpars with my changes, till this problem is solved. Many thanks. MfG, petwey ---------------------------------------------------------------------- wp · 2020-05-18 17:01 Thanks for the report. You could help the reviewer by adding a demonstration project; for FPC-related issues you should use console demos. Maybe something like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Project1; uses  fpexprpars; var  parser: TFPExpressionParser;  res: TFPExpressionResult; begin  parser := TFPExpressionParser.Create(nil);  try    parser.BuiltIns := [bcBoolean];    parser.Expression := 'iff(true, 999.999, 1.0/0.0)';  res := parser.Evaluate;    WriteLn(res.ResFloat:0:3);  finally    parser.Free;  end;     WriteLn('Press ENTER to close.');  ReadLn; end. ---------------------------------------------------------------------- PetWey · 2020-05-18 17:22 Hallo wp, I thought the shorter the better. I'm still looking for the source from which you get your FPC. SourceForge offers a stable version 3.0.4. Hence my problems with outdated code. Where do you get the current FPC from? MfG, petwey ---------------------------------------------------------------------- wp · 2020-05-18 18:26 Yes, "short" is always good, but having a demo is even better. Because this way the reviewer can absolutely reproduce the issue. Just following the description often leads to misunderstands, and these require discussions until everything is understood. And when there is a demo the developer can begin immediately to work, otherwise he must create a project on his own - which again may lead to misunderstandings. The easiest way to build a trunk fpc is by using fpcupdeluxe (https://wiki.freepascal.org/fpcupdeluxe). If you only need a few files you can also have a look at https://svn.freepascal.org/cgi-bin/viewvc.cgi/trunk/ for trunk, or https://svn.freepascal.org/cgi-bin/viewvc.cgi/tags/release_3_2_0/ for v3.2.0. You can also build without fpcupdeluxe: get yourself SVN (for windows: TortoiseSVN) and look for instructions in the forum or in the wiki. Using SVN is almost indispensable when you want to contribute to Lazarus or FPC since you can very easily keep your installations up to date with it (much faster than the initial installation), and you can create patch files with the changes that you made to fix issues or to add new features. ---------------------------------------------------------------------- PetWey · 2020-05-20 18:25 Hello forum, First of all I have to emphasize again that I have the highest respect for the work in fpexprpars. It is really a great job. I was looking for something for a special application! I searched the internet and found wp's HowTo and was thrilled. While searching for the skills of the parser I found fpexprpars.txt, but what I found there was already in wp's HowTo. Well, then I have to put together the few functions and put together my own instructions for the users of the program that I am writing. - Then I stumbled across the problem that aggregate functions always returned 0 within expressions -> solved by FPC >= 3.2 - Then I tested a variable number of parameters, did not work -> there was a more current version of FPC 3.2 than what I had -> wp helped me. - Then I wanted to intercept a division by zero using an IFF construct, but since both branches are evaluated, execution stopped here. So local copy of fpexprpars drawn and renamed to avoid conflicts and reworked the problematic passages. That worked but as wp rightly noticed, it violated the concept of exceptions. So I wrote a bug report. Unfortunately I only discovered Michael Van Canneyt's answer 2 days later -> "IFF" are old obsolete constructs, if you use "if" everything is fine. I think you will notice what this boils down to: - We have a great job with fpexprpars here, I'm really serious. - But you have to have the most current version of it, which is not so easy, because the search for stable FPC versions still leads to FPC 3.0.4 - And now the big request to ALL developers: Please give a reasonably up-to-date documentation on your works, otherwise they will remain in the drawer and rot. - Please keep in mind that not only cracks like you program with Lazarus, but also normal people like me, who are happy to have a clean installation of Lazarus, want to avoid a fast version change and also struggle with the pitfalls of Win10! By document I mean: - complete listing of operators - minimal brief description of the included commands such as sin, cos, tan, etc. - Information on which functions should no longer be used because they are out of date. - Nesting depth of the functions (How many pairs of brackets are allowed?). - A good place for this documentary could be the HowTo or fpexprpars.txt mentioned above. - ... I have attached the source files of my project here. It contains an extended fpexprpars, a workaround for array functions and the execution of several expressions in succession. Also an incomplete compilation of the commands I found (the focus here is on mathematics). I would be happy if you it useful. Oh yes, ... ... creative criticism is expressly welcome. ... I don't like Variants very much, but they were the possibility to make the classes for the arrays simple. ... I don't really like the GetElemI, GelElemF and similar functions. I would prefer some kind of overloaded function GetElem that can handle all array types. MfG, petwey ---------------------------------------------------------------------- wp · 2020-05-20 21:40 Your project is missing the lpr file. Quote from: PetWey on May 20, 2020, 08:25:15 pmBut you have to have the most current version of it.No, this statement is too general. I could achieve everything with an fpexprpars version from the fpc 2.x series.It just depends on what is needed. You were a bit unlucky because you needed a rarely used and recently added function and feature, and by chance you came across a bug. On the other hand you were lucky because the bug already had been fixed, otherwise you'd have to report it and wait for a few days (often not even that). This is much better in open source software than in commercial software. I remember that I had reported Delphi 7 bugs, recently they closed their bug tracker, and my reports were still open... Quote from: PetWey on May 20, 2020, 08:25:15 pmbecause the search for stable FPC versions still leads to FPC 3.0.4That's the way it is, FPC 3.0.4 IS the stable release, v3.2 has not yet been released, it is still in testing phase. The problem that you see is that the release cylcle of FPC is very long, I would say too long. Quote from: PetWey on May 20, 2020, 08:25:15 pmAnd now the big request to ALL developers: Please give a reasonably up-to-date documentation on your works, otherwise they will remain in the drawer and rot.No, this is too idealistic. It will not happen. Documentation will never be "up-to-date". Or we will drawn the internet with lots of outdated information: because there will also be users of older versions the old documentation must be kept. Look at our wiki and you'll have a great collection of outdated articles. Yes, "somebody" should take care of it. Who is this? And even if somebody would take responsibility I am rather sure that he would not dare to delete a large number of articles. Deleting is much more difficult than writing. I think the first thing to learn by a user of some library is its basic functionality and usage. This is why I write "how-to" and "first steps" articles sometimes. Hopefully the basic usage will survice the version cycles of the library. The next step: you must navigate the source code of the library to find what you need. I wrote is several times here in the forum how you can find a certain identifier, or the parameters of a function, within a fraction of a second. The source code is the most up-to-date documentation. The final step is to play with the library. Write short projects to test a feature and to see what happens. Do not do this inside your project. Quote from: PetWey on May 20, 2020, 08:25:15 pmAlso an incomplete compilation of the commands I found (the focus here is on mathematics).The collection seems to be a list of (some) functions provided by the math unit, you can see all in the help file (https://www.freepascal.org/docs-html/rtl/math/index-5.html). This has nothing to do with fpexprpars per se, because the built-in functions are only those of the system unit. Of course you can extend the parser by any function (for example even by Bessel functions of NumLib) - thaT's one of the most attractive feature of this parser. If you feel something is missing in the wiki article, do not publish it as an attachment of a forum post, using your own words: it "will remain in the drawer and rot". Simply modify the article, or write one of your own. Everybody can write an article in the wiki, no privileges needed. Simply register and you can begin typing. Being registered you'll see an "Edit" button at the top of the article, study the contents previously written, and you'll immediately understand the syntax. ---------------------------------------------------------------------- PetWey · 2020-05-21 07:10 Hello wp, QuoteYour project is missing the lpr file. Sorry, here is the missing lpr. MfG, petwey ---------------------------------------------------------------------- PetWey · 2020-05-21 08:22 Hello wp I won't let you off the hook so easily. I am not the cleanest programmer myself and always get angry with myself when I find undocumented code and have to familiarize myself with it again. I had downloaded lazarus-2.0.6-62131-fpc-3.2.0-beta-43271-win64.exe (approx. 1/2 year old). The aggregate functions worked, but the flexible number of parameters did not. Then I compared fpexprpars with the fpexprpars you added using WinMerge and lo and behold, the flexible number of parameters was possible here. You may have an fpc 2.x that did it all, but surely fpexprpars was brand new. The question arises here if we are talking about fpc 3.2.0, is that the whole package fpc or parts? I can't find a version number in fpexprpars! There may be a lack of conceptual clarity in the use of "fpc xxx". Even if it's idealistic, I think that a rudimentary document belongs to a piece of software. In this case a listing of the functions and / or a reference to existing documentation as you attached it. But surely there are such special functions as "if" and "case" to be described in this case! Even if that's a really stupid comparison (and I'm a mechanical engineer): I drive my car, fill up with fuel and oil, but I no longer understand in detail how the engine works. I don't want that at all, because the car is a more or less well functioning black box. And that's exactly what fpc is for me, it's far too big and powerful, and I don't have enough time to go into every detail. Small documentaries of the individual packages must help here. I do not make great demands here, a simple ASCII file is completely sufficient and it should come from or from the environment of the respective programmer, only he knows his code well enough. Furthermore, I would like to thank you again, and especially wp, for your help. If I look at the title of my question, it is more than answered, so I set the topic to solved MfG, petwey. ---------------------------------------------------------------------- PascalDragon · 2020-05-21 08:44 Quote from: PetWey on May 21, 2020, 10:22:49 amYou may have an fpc 2.x that did it all, but surely fpexprpars was brand new. The question arises here if we are talking about fpc 3.2.0, is that the whole package fpc or parts? I can't find a version number in fpexprpars! There may be a lack of conceptual clarity in the use of "fpc xxx". fpExprPars is shipped in the FPC distribution, thus it's version number is the same as that of the whole of FPC. Also the version can be seen in the fpmake.pp file of the package that the unit fpExprPars belongs to.