fpexprpars.txt 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. The fpexprpars unit contains an expression parser.
  2. The parser compiles the expression into a node tree, which is
  3. type checked after it was compiled.
  4. The expression parser handles the following types:
  5. Integer (64-bit)
  6. Float (TExprFloat, normally Double)
  7. TDateTime
  8. Boolean
  9. The following operations are allowed:
  10. + - / *
  11. not and or xor
  12. ( )
  13. The binary operations can also be done on integer values, in which
  14. case they act on the bits of the integer.
  15. In the case of strings addition results in concatenation of the strings.
  16. Operator precedence is observed. In case of equal precedence, evaluation
  17. order is left-to-right.
  18. Normally, both operands of binary operations must have the same type.
  19. There are 2 exceptions: The engine will convert integers to float or
  20. TDateTime if it detects that one of the nodes is a float or datetime.
  21. The engine can be extended with variables and functions. There are over
  22. 60 built-in functions, which can be enabled by setting the Builtins property
  23. of the expression parser to a set of the following values:
  24. bcStrings: Various string routines
  25. length copy delete pos lowercase uppercase stringreplace comparetext
  26. bcDateTime: Various datetime routines
  27. date time now dayofweek extractyear extractmonth extractday extracthour
  28. extractmin extractsec extractmsec encodedate encodetime encodedatetime
  29. shortdayname shortmonthname longdayname longmonthname formatdatetime
  30. bcMath: Various mathematical routines
  31. pi cos sin arctan abs sqr sqrt exp ln log frac int round trunc
  32. bcBoolean: Various boolean routines
  33. shl shr IFS IFF IFD IFI
  34. bcConversion : Conversion routines
  35. inttostr strtoint strtointdef floattostr strtofloat strtofloatdef
  36. booltostr strtobool strtobooldef datetostr timetostr strtodate strtodatedef
  37. strtotime strtotimedef strtodatetime strtodatetimedef
  38. Additional functions/variables can be added to the Identifiers collection:
  39. FP : TFPexpressionParser;
  40. The following will define a TODAY variable which has value equal to the date
  41. at the moment is is defined:
  42. FP.Identifiers.AddDateTimeVariable('TODAY',Date);
  43. The following will define a function echodate:
  44. Procedure EchoDate(Var Result : TFPExpressionResult; Const Args : TExprParameterArray);
  45. begin
  46. Result.resDateTime:=Args[0].resDateTime;
  47. end;
  48. FP.Identifiers.AddFunction('EchoDate','D','D',@EchoDate);
  49. The arguments are:
  50. Name : Name of the function
  51. Result type : Character with result type:
  52. I : integer
  53. S :
  54. F : FLoat
  55. D : TDateTime
  56. B : Boolean
  57. Argument types : A string with each character the type of argument at that position.
  58. Callback : executed when the function is called. This can be a procedural
  59. variable or an event (procedure of object).
  60. Result and arguments are type-checked.
  61. The engine knows 2 built-in functions which are handled specially:
  62. IF(Expr,Res1,Res1)
  63. Will return Res1 if expr evaluates to True, or Res2 if expr evaluates to False.
  64. The types of Res1 and Res2 must be the same, and expr must be a boolean
  65. expression.
  66. CASE(Tag,Def,Label1,Value1,Label2,Value2,...)
  67. Case will examine the value of Tag and compare it with Label1, Label2 etc.
  68. till a match is found. It will return Value1, Value2 etc. depending on the
  69. match. If no match is found, Def will be returned. From this it follows that
  70. 1) The number of arguments is always even and is at least 4.
  71. 2) The types of Tag, label1, label2 must be the same;
  72. 3) The types of Def, Value1, Value2 must be the same;
  73. As soon as the expression is set, it is compiled and checked. Thus
  74. FP.Expression:='1*2';
  75. will work.
  76. On the other hand
  77. FP.Expression:=' 1 * ''a string''';
  78. will result in an exception because 1 and ''a string'' do not have the same
  79. type.
  80. Getting the result is quite simple
  81. FP.Expression:='1*2';
  82. Writeln(FP.AsInteger);
  83. This will raise an exception if the type of the result is not integer.
  84. In case the expression result type is unknown, it can be examined using
  85. the ResultType function, as in :
  86. FP.Expression:='Some user-provided expression';
  87. Case FP.ResultType of
  88. rtString : Writeln(FP.Evaluate.ResString);
  89. rtInteger : Writeln(FP.Evaluate.ResInteger);
  90. rtFloat : Writeln(FP.Evaluate.ResFloat);
  91. rtBoolean : Writeln(FP.Evaluate.ResBoolean);
  92. rtDateTime : Writeln(FormatDateTime('cccc',FP.Evaluate.ResDateTime));
  93. end;
  94. Which is equivalent to
  95. FP.Expression:='Some user-provided expression';
  96. Case FP.ResultType of
  97. rtString : Writeln(FP.AsString);
  98. rtInteger : Writeln(FP.AsInteger);
  99. rtFloat : Writeln(FP.AsFloat);
  100. rtBoolean : Writeln(FP.AsBoolean);
  101. rtDateTime : Writeln(FormatDateTime('cccc',FP.AsDateTime));
  102. end;