Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2004-08-31
9
286 Views
Last Modified: 2010-04-05
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
0
Comment
Question by:johan777
9 Comments
 
LVL 27

Assisted Solution

by:kretzschmar
kretzschmar earned 200 total points
ID: 11940443
0
 
LVL 8

Expert Comment

by:BdLm
ID: 11940569
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.
0
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 11940703
or check the RO PascalScript : http://www.remobjects.com/page.asp?id={9A30A672-62C8-4131-BA89-EEBBE7E302E6}
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 13

Accepted Solution

by:
BlackTigerX earned 300 total points
ID: 11943234
what you need is a scripting engine, there are many available, you can use Delphi Scripting or any of the Windows Scripting engines like VBScript, JavaScript, etc... there are some that are free and some that are not (the best ones)

http://delphi.about.com/cs/toppicks/tp/aatpscripters.htm

I have used Dream Scripter (not free) and it works really nice; it has a something that allows you to create a unit (for the engine) out of your program units that get exposed to the Scripting Engine, so you can use code of your program from the Engine, like change properties of anything in your forms, etc
0
 
LVL 6

Expert Comment

by:Sergio_Hdez
ID: 11950011
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.
0
 

Author Comment

by:johan777
ID: 11961875
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
0
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 11961929
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
0
 
LVL 14

Expert Comment

by:DragonSlayer
ID: 11962451
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...
0
 

Author Comment

by:johan777
ID: 11979764
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
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi Dbf export problem to a Visual Foxpro application 6 207
code issue 8 154
When I am typing into a TEdit last word repeats 7 145
Delphi inherited method 6 76
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question