Link to home
Start Free TrialLog in
Avatar of johan777
johan777

asked on

How can I get Delphi to execute a string as a piece of code?

Hi all,

I would like to use Delphi to execute the value of a string.

Say I have the following string values:

'((3 + 4) * cos(12)) / (9 div 2)) mod 4)'

'if (3 < 20) then begin (12 * 7) end else begin (12 * 8) end'

The bigger picture is that i allow the user to capture a lot of variables which has either a value or a formula.

For instance:

BoxHeight = 3
BoxWidth  = 3
BoxLength = 5
BoxVolume = (BoxHeight * BoxWidth * BoxLength)

It is quite easy to replace all the variables with their respective values thus:

BoxHeight = 3
BoxWidth  = 3
BoxLength = 5
BoxVolume = (3 * 3 * 5)

The question is how do I get Delphi to evaluate the string BoxVolume?
As you can see from my first set of examples, the formulae can get quite tricky and must make provision for condition statements, loops,  lookups etc.

I have been investigating the use of XML to store the strings in, but because of the lack of standard functions, I don't think this is such a good idea anymore.

What about VB Script? Can I get Delphi to pass the string to the VB Script parser to execute and return a result?

Maybe I'm on the wrong track. If anyone has any suggestions as to how I should approach this, please speak up!

I would like the 'language' that must be used to define the formulae to be either a well known langauge like VB or XML, or it needs to be extremely easy to learn.

Thanx for you help so far all!

Regards,
Johan Swart
SOLUTION
Avatar of kretzschmar
kretzschmar
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
a parser will do a interpreation of your string. Do will loose some performance.

.NET technology will enable you to compiler your string at run-time, if you need speed .NET will help in this case.
Avatar of Lee_Nover
Lee_Nover

or check the RO PascalScript : http://www.remobjects.com/page.asp?id={9A30A672-62C8-4131-BA89-EEBBE7E302E6}
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I have been doing this for long using "Delphi Web Script" (look for dws in google), it is a pascal/delphi "just-in-time" compiler, something like this:

dws.text:=  
  'BoxHeight = 3;'+
  'BoxWidth  = 3;'+
  'BoxLength = 5;'+
  'BoxVolume = (BoxHeight * BoxWidth * BoxLength)';
dws.compile;
dws.execute;
result:= dws.var('BoxVolume').asFloat;

I use an old version of dws in my projects, as it was first a "simple pascal interpreter" but later it get more and more complete (object oriented and so forth), but i didn't need all this, so i am stuck with this old version.

My apps use it to execute all kind of code, with lot of "user defined functions" that allows me to interact with delphi objects (make a script read-change properties of real delphi objects).

Drop me a line if you find it interesting and need this old version or some code snippet.
Avatar of johan777

ASKER

Hi all,

Thanx for all the valuable comments so far! I'm overwhellemed.

I will be spending some time going through all your answers in two days' time, please do not give up om me :-)

In the meantime, could you please include an example to such a parser. As I said earlier, I need to have the script langauge be as common as possible, so I leaning towards the vbcsript parser.

Also, is the vbscript parser free for distribution or not?

Regards,
Johan Swart
the RO PascalScript is delphi like .. it also supports objects and all that .. also includes some common VCL component implementations
delphi and script code can interact
and it's FREE with all the sources included .. (and supported by a very good company)
vbscript parser is a system component .. no source ofcourse
or download SysTool http://sourceforge.net/projects/tpsystools/ and use their TstExpression which parses all those and include variable resolution as well.

However, it doesn't support scripting such as if ... then ... else in the formula...
Hi all,

Thanx for all the help. I think I found my answers.

1) I will need a parser to do what I wanted.

2) If I'm going to use VBScript, I will need to use a third-party component to access the VBScript parser. (I liked the one called 'Windows Scripting Host Control ')

I would have liked to give you all points for all your comments, but i have decided to split it between kretzschmar and BlackTigerX.

Regards,
Johan Swart