0

Is it possible?

Example: ComboBox1 has two items (or, and). ComboBox2 has two items (<, >).

Var
  Int1, Int2: Integer;

Begin
  if (Int1 ComboBox2.Text 10) ComboBox1.Text (Int2 ComboBox2.Text 12) then bla bla;
Mike Kinny
  • 21
  • 5
  • 2
    The short answer to your q is no, Delphi is a compiler not an interpreter. But you can get scripting engines that you can compile into your app and use to execute Delphi-like script. – MartynA Jul 05 '14 at 16:02

2 Answers2

1

You essentially want to build up a Pascal statement from the contents of your comboboxes (I assume user chosen), then execute it.

Any of the Pascal scripting tools mentioned in this Stackoverflow post can work for that.

But for your very simple example you don't need that, you can just check what ComboBoxx.Text or better ComboBox.ItemIndex return and then write Delphi code at design time.

Community
  • 1
  • 1
Jan Doggen
  • 8,799
  • 13
  • 70
  • 144
1

The first thing that you need to understand is how Delphi expressions are processed by the compiler. They key point is that expressions are parsed at compile time. By parsing we mean the act of breaking apart an expression into its component parts: operations and the associated operands.

Now, the compile time parsing determines the operations definitively. They must be known at compile time. The operands may be variables, and so only known at run time.

For example, consider this expression:

x + 1

Here the operation is addition. The two operands are the variable x and the constant 1. Everything is known at compile time apart from the value of x.

The act of parsing breaks down an expression into a form where it can be evaluated without further structural analysis. All that is needed are the values of the unknown variables.

You are hoping to write expressions where the operations are not known until run time. That is not compatible with compile time parsing of expressions. The Delphi compiler, because it parses expressions at compile time, therefore cannot do what you hope.

Instead you need run time parsing of expressions. You could write your own parser/evaluator. You could use Delphi's LiveBindings expression evaluator. You could use a third party expression evaluator, for instance the JCL library.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490