Connecting Tech Pros Worldwide Forums | Help | Site Map

Need Boolean Expression Evaluation Routine - Reward

Newbie
 
Join Date: Apr 2007
Posts: 20
#1: Apr 7 '07
I am looking for a routine that can evaluate boolean expressions like:

IF/ELSE-IF (X1=X2 AND (Y=1 OR Z1=Z2))

IF/ELSE-IF (A$='Y' AND B=1) OR (C=1 AND D>E)

etc.

I can handle determining if the individual terms (X1=X2, etc) are TRUE/FALSE at runtime. I just need the routine that determines if the entire expression is TRUE/FALSE taking into account ANDs, ORs, NOTs and parens.

I would like the routine to be written in BASIC.

If possible, the routine should determine at which point the expression is known TRUE or FALSE and short-circuit for performance.

I'm guessing the routine to do this will need to convert the expression into
reverse polish notation before evaluating it?

If I use your routine I will send a small reward to the Paypal account of your choice.

Moderator
 
Join Date: Oct 2006
Location: Australia
Posts: 7,748
#2: Apr 8 '07

re: Need Boolean Expression Evaluation Routine - Reward


Does this have to be VB code? What about, for example, a DLL routine you could call?

I ask because this seems a fascinating problem which I'd like to throw to the wider community at TheScripts, but if they comes up with a lovely simple solution in C++, for example, it may not help you at all.

If we get some decent code, chances are we can convert it to VB. But I seem to recall that some languages/tools include parsers for this kind of thing, and would require very little if any coding to produce a result.
Newbie
 
Join Date: Apr 2007
Posts: 20
#3: Apr 8 '07

re: Need Boolean Expression Evaluation Routine - Reward


The routine will have to be converted into a 4GL that runs on the mainframe. I asked for it in BASIC because I used to code in BASIC years ago and I could probably rewrite the routine if it's written in BASIC but I'll take it in whatever language I can get it in.
Newbie
 
Join Date: Apr 2007
Posts: 20
#4: Apr 8 '07

re: Need Boolean Expression Evaluation Routine - Reward


More...

DLL won't work:) This routine will end up being converted into a 4GL that runs on an mainframe.

I asked for it in BASIC because I used to code in BASIC years ago and I could probably rewrite the routine if it's written in BASIC but I'll take it in whatever language I can get it in.

The routine is needed for a business rules engine I'm working on. It consists of an end-user scripting "language", a "compiler", and a "processor" (The black box that actually executes the "script".

Currently, the language does not support parens in boolean expressions
(compiler will flag them). Expressions are evaluated left to right at runtime.
ANDs are evaluated when the expression is TRUE and can change the
expression to FALSE. ORs are evaluated when the expression is FALSE
and can change the expression to TRUE.

To get around not being able to code parens users write an expression like:

IF (A=1 AND B=1) OR (A=2 AND B=2)
Do Something
END-IF

As...

IF A=1 AND B=1
Do Something
ELSE-IF A=2 AND B=2
Do Something
END-IF

And an expression like:

IF A=1 AND (B=1 OR C=1)

as:

IF A=1
IF B=1 OR C=1
Do Something
END-IF
END-IF

The compiler is not really a compiler in the traditional sense. It puts the
offset (address) on the next executable statement if a condition is FALSE
on each line and the offset on the next END-IF on each line. This allows
the processor to "skip" thru the scripts at runtime by branching around FALSE
code. Because the "object" code can run on any platform without being recompiled given a processor exists that platform.

Variables are actually lists that can contain one or more values. References to a variable without a subscript actually apply to any value in the list. So

IF X=1
AND X=3
Do Something
END-IF

Can be TRUE in this language if the variable X contains two values (1 AND 3).

Treating all variables as lists eliminates the need for end-users to write routines
to loop thru occurences...
Moderator
 
Join Date: Oct 2006
Location: Australia
Posts: 7,748
#5: Apr 9 '07

re: Need Boolean Expression Evaluation Routine - Reward


Interesting - what language is this? Or is it top secret? :)

If you think you've got problems, you ought to try setting up complex logic in the scripting language in an M/Text document. Not only is it very awkward to write, but if you have nested IFs, and End If closes all of them - argh!

I've posted a message in the Experts forum (most users can't see it) to see whether anyone there can help. Will also post in the miscellaneous discussions area for everyone to see, in the hope someone can help out.
Moderator
 
Join Date: Oct 2006
Location: Australia
Posts: 7,748
#6: Apr 9 '07

re: Need Boolean Expression Evaluation Routine - Reward


You may be interested to see this thread which I started in the Software Development forum. So far it has drawn a couple of responses.
Reply