Recent

Author Topic: freepascal: Is it possible to convert string to function call?  (Read 2602 times)

OldPascal

  • New Member
  • *
  • Posts: 19
I have defined some function say cosinus().   In other routines I have strings with the text that have some math functions and my special math function cosinus(x).  Is it possible to convert text string "cosinus(x)" into a function call and that function cosinus() already has been defined before. Or at least assigning function to a variable.   I know that pascal has a library called symbolic.
see example code below,
Code: Pascal  [Select][+][-]
  1.  
  2. uses symbol;
  3. var
  4.  s :real;
  5. s:='cos(3.14159265359*45/180)';
  6.   writeln(quickevaluate(s,[],[]));
  7.  
This works fine.  But as you can see, this method does not allow array of x nor constant Pi because its library will generate execution error.  Therefore I have to start from scratch.  :o

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: freepascal: Is it possible to convert string to function call?
« Reply #1 on: April 13, 2021, 10:53:09 pm »
No. At least, not without invoking the compiler to create a library and then using dynamic linkage... this would be tricky, risky and the sort of damnfool thing only somebody like me would do.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

ASBzone

  • Hero Member
  • *****
  • Posts: 678
  • Automation leads to relaxation...
    • Free Console Utilities for Windows (and a few for Linux) from BrainWaveCC
Re: freepascal: Is it possible to convert string to function call?
« Reply #2 on: April 14, 2021, 12:10:28 am »
I have defined some function say cosinus().   In other routines I have strings with the text that have some math functions and my special math function cosinus(x).  Is it possible to convert text string "cosinus(x)" into a function call and that function cosinus() already has been defined before. Or at least assigning function to a variable.   I know that pascal has a library called symbolic.
see example code below,
Code: Pascal  [Select][+][-]
  1.  
  2. uses symbol;
  3. var
  4.  s :real;
  5. s:='cos(3.14159265359*45/180)';
  6.   writeln(quickevaluate(s,[],[]));
  7.  
This works fine.  But as you can see, this method does not allow array of x nor constant Pi because its library will generate execution error.  Therefore I have to start from scratch.  :o

There are a number of math expression parsers that you might use. Consider the following:

https://forum.lazarus.freepascal.org/index.php/topic,53116.msg392453.html#msg392453
https://forum.lazarus.freepascal.org/index.php/topic,41588.msg288875.html#msg288875



-ASB: https://www.BrainWaveCC.com/

Lazarus v2.2.7-ada7a90186 / FPC v3.2.3-706-gaadb53e72c
(Windows 64-bit install w/Win32 and Linux/Arm cross-compiles via FpcUpDeluxe on both instances)

My Systems: Windows 10/11 Pro x64 (Current)

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: freepascal: Is it possible to convert string to function call?
« Reply #3 on: April 14, 2021, 04:53:58 am »
But as you can see, this method does not allow array of x nor constant Pi because its library will generate execution error.

Unit fpexprpars has TFPExpressionParser which allows adding functions and variables. Functions are added using Identifiers.AddFunction

Check its example in:
 fcl-base\tests\testexprpars.pp

Specifically look for ExprAveOf which gives the average of unspecified number of arguments.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: freepascal: Is it possible to convert string to function call?
« Reply #4 on: April 14, 2021, 09:26:17 am »
This works fine.  But as you can see, this method does not allow array of x nor constant Pi because its library will generate execution error.  Therefore I have to start from scratch.  :o

What do you mean by array of x? You can pass pi etc using variables and constants in the symbolic library (see the other demo)

Code: [Select]
writeln(quickevaluate(s,['pi'],[pivalueasdouble]));


OldPascal

  • New Member
  • *
  • Posts: 19
Re: freepascal: Is it possible to convert string to function call?
« Reply #5 on: April 14, 2021, 10:14:53 pm »
Thanks everyone for clarity.  I guess I need glasses.  How could I miss it.  Thanks marcov.
« Last Edit: April 14, 2021, 10:16:32 pm by OldPascal »

OldPascal

  • New Member
  • *
  • Posts: 19
Re: freepascal: Is it possible to convert string to function call?
« Reply #6 on: April 14, 2021, 11:44:32 pm »
Again, I am confused here, %)
I added the corrected code here and still I am getting error that PI variable is not recognized even when I am doing exactly like in example.
Code: Pascal  [Select][+][-]
  1. program example4;
  2. uses symbolic;
  3. var
  4. s,t :AnsiString;
  5. pip :double; {it does not work even with extended}
  6. begin
  7. pip:=3.14159265359;
  8. s:='cos(3.14159265359*45/180)';
  9. t:='cos(PI*45/180)';
  10.   writeln(quickevaluate(s,[],[]));
  11.  
  12.   writeln(quickevaluate(t,['PI'],[pip]));
  13.   end.
  14.  

the error message is "....no value for parameter "PI" ....."

OldPascal

  • New Member
  • *
  • Posts: 19
Re: freepascal: Is it possible to convert string to function call?
« Reply #7 on: April 14, 2021, 11:58:37 pm »
I think it might be a bug.   I does not work as shown before, but when I added another variable it works.  Below is the modified code.
Code: Pascal  [Select][+][-]
  1. program example4;
  2.  
  3. uses symbolic;
  4. var
  5. s,t :AnsiString;
  6. pip :extended;
  7. begin
  8. pip:=3.14159265359;
  9. s:='cos(3.14159265359*45/180)';
  10. t:='cos(PI*A/180)';
  11.   writeln(quickevaluate(s,[],[]));
  12.  
  13.   writeln(QuickEvaluate(t,['PI','A'],[pip,45]));
  14.   end.
  15.  

Therefore, my problem is fixed   :).  Of course, this one variable call issue may cause problems later on.
Its a good library for pascal.  It is very handy.

 

TinyPortal © 2005-2018