Solved

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

Posted on 2004-08-31
9
284 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

867 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

16 Experts available now in Live!

Get 1:1 Help Now