Solved

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

Posted on 2004-08-31
9
283 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

760 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now