2
0

easyevalexample.pp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. Program EasyEvalExample;
  2. {
  3. Copyright (c) 2011 by Marco van de Voort([email protected])
  4. member of the Free Pascal development team
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright. (LGPL-with-static-linking-exception)
  7. Rock bottom example of new evaluator helper function.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. }
  12. {$mode delphi}
  13. Uses Symbolic,Classes,sysutils;
  14. var s : AnsiString;
  15. a : extended;
  16. b : integer;
  17. begin
  18. // quickevaluate('expression',[],[]); evaluates a constant expression to an
  19. // extended result
  20. s:='(5+5+10)*2';
  21. writeln(s,'=',QuickEvaluate(s,[],[]):10:1);
  22. // ... but still allows variables:
  23. a:=2.0;
  24. b:=3;
  25. s:='(5+A+10)*B';
  26. // variable names are case sensitive!
  27. writeln(s,'=',QuickEvaluate(s,['A','B'],[a,b]):10:1,' with A=',a:0:1,' and B=',b);
  28. // now let's do that again, but add a symbol (C) that we don't define:
  29. try
  30. a:=2.0;
  31. b:=3;
  32. s:='(5+A+10)*B+C';
  33. // variable names are case sensitive!
  34. writeln(s,'=',QuickEvaluate(s,['A','B'],[a,b]):10:1,' with A=',a:0:1,' and B=',b);
  35. except
  36. on E:Exception do
  37. Writeln('An exception occurred: ',e.message);
  38. end;
  39. end.