Expression evaluator

Share your advanced PureBasic knowledge/code with the community.
DarkDragon
Addict
Addict
Posts: 2344
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Post by DarkDragon »

utopiomania wrote:Daniel, it handles any number of variables up to #MAXVARS, and you can use long variable
names if you like.
But it just returns one result for 2 variables O_o which var is it then?
bye,
Daniel
User avatar
utopiomania
Addict
Addict
Posts: 1655
Joined: Tue May 10, 2005 10:00 pm
Location: Norway

Post by utopiomania »

It isn't case sensitive if that's what you mean ?, so O and o is the same variable, just like variables in PureBasic.
Hope I understood you right.
Justin
Addict
Addict
Posts: 948
Joined: Sat Apr 26, 2003 2:49 pm

Post by Justin »

Very cool. maybe this could be used for this?,

in an ADO recordset there is a filter property that accepts an expression, but the syntax is limited, from the docs:

Code: Select all

There is no precedence between AND and OR. Clauses can be grouped within parentheses. However, you cannot group clauses joined by an OR and then join the group to another clause with an AND, like this: 
(LastName = 'Smith' OR LastName = 'Jones') AND FirstName = 'John'

Instead, you would construct this filter as 
(LastName = 'Smith' AND FirstName = 'John') OR (LastName = 'Jones' AND FirstName = 'John')
so the function would take a complex expression like the first one and translate it to the second one, handling all nesting levels. How difficult would be?

Here's the complete description for the filter property,

Code: Select all

Use the Filter property to selectively screen out records in a Recordset object. The filtered Recordset becomes the current cursor. Other properties that return values based on the current cursor are affected, such as AbsolutePosition, AbsolutePage, RecordCount, and PageCount. This is because setting the Filter property to a specific value will move the current record to the first record that satisfies the new value.

The criteria string is made up of clauses in the form FieldName-Operator-Value (for example, "LastName = 'Smith'"). You can create compound clauses by concatenating individual clauses with AND (for example, "LastName = 'Smith' AND FirstName = 'John'") or OR (for example, "LastName = 'Smith' OR LastName = 'Jones'"). Use the following guidelines for criteria strings: 

FieldName must be a valid field name from the Recordset. If the field name contains spaces, you must enclose the name in square brackets. 
Operator must be one of the following: <, >, <=, >=, <>, =, or LIKE. 
Value is the value with which you will compare the field values (for example, 'Smith', #8/24/95#, 12.345, or $50.00). Use single quotes with strings and pound signs (#) with dates. For numbers, you can use decimal points, dollar signs, and scientific notation. If Operator is LIKE, Value can use wildcards. Only the asterisk (*) and percent sign (%) wild cards are allowed, and they must be the last character in the string. Value cannot be null. 
Note   To include single quotation marks (') in the filter Value, use two single quotation marks to represent one. For example, to filter on O'Malley, the criteria string should be "col1 = 'O''Malley'". To include single quotation marks at both the beginning and the end of the filter value, enclose the string with pound signs (#). For example, to filter on '1', the criteria string should be "col1 = #'1'#". 

There is no precedence between AND and OR. Clauses can be grouped within parentheses. However, you cannot group clauses joined by an OR and then join the group to another clause with an AND, like this: 
(LastName = 'Smith' OR LastName = 'Jones') AND FirstName = 'John'
Instead, you would construct this filter as 
(LastName = 'Smith' AND FirstName = 'John') OR (LastName = 'Jones' AND FirstName = 'John')
In a LIKE clause, you can use a wildcard at the beginning and end of the pattern (for example, LastName Like '*mit*'), or only at the end of the pattern (for example, LastName Like 'Smit*'). 
User avatar
utopiomania
Addict
Addict
Posts: 1655
Joined: Tue May 10, 2005 10:00 pm
Location: Norway

Post by utopiomania »

Justin, I'm shure it can be used for this. The simplest would probably be to write a
procedure to walk through the expression string and replace all FieldName-Op-Value's
with a 1 for True and 0 for a False, then let Eval() solve it:

"(LastName = 'Smith' OR LastName = 'Jones') AND FirstName = 'John'" is translated to:
"(1 OR 0) AND 1" which is then solved by Eval(). :)
Justin
Addict
Addict
Posts: 948
Joined: Sat Apr 26, 2003 2:49 pm

Post by Justin »

that should work but would require to filter the recordset programatically by me, i was thinking in an expression translator that accepts a string like,

(LastName = 'Smith' OR LastName = 'Jones') AND FirstName = 'John'

and returns a string,

(LastName = 'Smith' AND FirstName = 'John') OR (LastName = 'Jones' AND FirstName = 'John')

so it can be used by the filter property and lets the ado engine do the work, looks a bit complex.
DarkDragon
Addict
Addict
Posts: 2344
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Post by DarkDragon »

utopiomania wrote:It isn't case sensitive if that's what you mean ?, so O and o is the same variable, just like variables in PureBasic.
Hope I understood you right.
Noooo, here a short equation:

0=x^2+x-5

There it should give me 2 results:

x1/2 = -0.25+-sqr(0.25+5)
x1 = 2.04 ;Result 1
x2 = -2.54 ;Result 2

Or this one:

1) y=x+5
2) y=2x-5
---
2x-5 = x+5 | +5
2x = x+10 | - x
x = 10
---
y = 10+5
y = 15

Result = (10|15)
bye,
Daniel
User avatar
utopiomania
Addict
Addict
Posts: 1655
Joined: Tue May 10, 2005 10:00 pm
Location: Norway

Post by utopiomania »

Oh, I see. The parser solves expressions, not equations.
Justin
Addict
Addict
Posts: 948
Joined: Sat Apr 26, 2003 2:49 pm

Post by Justin »

utopiomania, can you add string support? i'll try to do the ado filter, making a LIKE command should be easy and it could even have a regexp command REG, there is a com object that evaluates regular expressions, and a NULL keyword for values to match empty fields.
User avatar
utopiomania
Addict
Addict
Posts: 1655
Joined: Tue May 10, 2005 10:00 pm
Location: Norway

Post by utopiomania »

Justin, I don't know if I can add string support, but I have to tell you I have too much
work to do on my own petty projects already, so I can't say yes or no.

Sorry for my late answer, but I'm on holiday now, and just managed to log on to the net
using my laptop and my Moto V600, at 9.6kbits a sec... Sloooow and expensive, so I have
to keep it short for the next week and a half. :)
Justin
Addict
Addict
Posts: 948
Joined: Sat Apr 26, 2003 2:49 pm

Post by Justin »

i have already added string support and turned it into a custom eval object where you set the ops to use etc.. needs still a little more work, then i'll post it
ricardo
Addict
Addict
Posts: 2438
Joined: Fri Apr 25, 2003 7:06 pm
Location: Argentina

Post by ricardo »

Justin wrote:i have already added string support and turned it into a custom eval object where you set the ops to use etc.. needs still a little more work, then i'll post it
Great!!

Any posibility to see the code? :D
ARGENTINA WORLD CHAMPION
thefool
Always Here
Always Here
Posts: 5875
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post by thefool »

very nice code!
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

even if there are some improvements to do, it's a nice & clean parser, thanx.

@utopiomania:
Eval("(23).56")
Eval("23___56")

...should return ERR_SYNTAX, no ?
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
User avatar
utopiomania
Addict
Addict
Posts: 1655
Joined: Tue May 10, 2005 10:00 pm
Location: Norway

Post by utopiomania »

thefool and Flype, Thanks for your comments. :)
...should return ERR_SYNTAX, no ?
Of course it should have. :wink:
User avatar
utopiomania
Addict
Addict
Posts: 1655
Joined: Tue May 10, 2005 10:00 pm
Location: Norway

Post by utopiomania »

I was about to include this in a program compiled to unicode, so I've just updated it to PB4.00.

The original console mode demo didn't work in unicode, so I removed it for the post. The evaluator
itself works in unicode though.
Post Reply