### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

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

#### OldPascal

• New Member
• Posts: 19
##### freepascal: Is it possible to convert string to function call?
« on: April 13, 2021, 10:21:58 pm »
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.

#### MarkMLl

• Hero Member
• Posts: 2499
##### 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
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

#### jamie

• Hero Member
• Posts: 4445
##### Re: freepascal: Is it possible to convert string to function call?
« Reply #2 on: April 14, 2021, 12:09:24 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.
You can do that if  QuickeValuate is a function that knows how to parse the string and lookup a real function of that type or maybe even find another like it.

Its called scripting which means you need to parse the strings and lookup a function ready made..

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.
The only true wisdom is knowing you know nothing

#### ASBzone

• Hero Member
• Posts: 615
• Automation leads to relaxation...
##### Re: freepascal: Is it possible to convert string to function call?
« Reply #3 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.

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.0.13 r64843 / FPC v3.2.1-r49055 (via FpcUpDeluxe) -- Windows 64-bit install w/Win32 and Linux/Arm cross-compiles
Primary System: Windows 10 Pro x64, Version 2009 (Build 19042)
Other Systems: Windows 10 Pro x64, Version 2009 (Build 19042) or greater

#### engkin

• Hero Member
• Posts: 2692
##### Re: freepascal: Is it possible to convert string to function call?
« Reply #4 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

• Global Moderator
• Hero Member
• Posts: 9268
• FPC developer.
##### Re: freepascal: Is it possible to convert string to function call?
« Reply #5 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.

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 #6 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 #7 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 #8 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.