[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2004-08-31
9
Medium Priority
?
295 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 600 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 13

Accepted Solution

by:
BlackTigerX earned 900 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

810 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