For Programmers: Free Programming Magazines  


Home > Archive > Tcl > April 2005 > Expression builder









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Expression builder
Mona

2005-03-31, 8:58 pm

Hi all,

I am trying to create an expression builder dialog, which can handle
logical, mathematical as well as relational operator queries.

There is an entry widget where the user can enter the query/expression.
Does anyone know of any easy way to do this, instead of parsing the
string and doing push and pop stack procedure to calculate.

The expression builder needs to handle +, -, *, /, <, >, <>, !, &&, ||,
like, (, ).

Any help is papreciated.

Thanks in advance.

Gerald W. Lester

2005-04-01, 4:00 am

Mona wrote:
> Hi all,
>
> I am trying to create an expression builder dialog, which can handle
> logical, mathematical as well as relational operator queries.
>
> There is an entry widget where the user can enter the query/expression.
> Does anyone know of any easy way to do this, instead of parsing the
> string and doing push and pop stack procedure to calculate.
>
> The expression builder needs to handle +, -, *, /, <, >, <>, !, &&, ||,
> like, (, ).


How about :
set result [expr $enteredExpression]

--
+--------------------------------+---------------------------------------+
| Gerald W. Lester | "The man who fights for his ideals is |
| Gerald.Lester@cox.net | the man who is alive." -- Cervantes |
+--------------------------------+---------------------------------------+
Roy Terry

2005-04-01, 4:00 am

"Gerald W. Lester" <Gerald.Lester@cox.net> wrote in message
news:th13e.49364$Az.38522@lakeread02...
> Mona wrote:
>
> How about :
> set result [expr $enteredExpression]

Seems Mona is looking for a parse tree rather than the
answer.
Looks like there's a good start in Tcl code
on the wiki.
http://mini.net/tcl/3498

>
> --
> +--------------------------------+---------------------------------------+
> | Gerald W. Lester | "The man who fights for his ideals is |
> | Gerald.Lester@cox.net | the man who is alive." -- Cervantes |
> +--------------------------------+---------------------------------------+



Mona

2005-04-01, 4:01 pm

Hi all,

Thanks for the reply.

I would like to compare a variable with the expression builder string
and perform some operations. My problem is how do I insert the
variable in the expression string?
So, the expresion need to be something like
[("my_variable" > "somevalue") && ("my_variable" < "othervalue")].

Something like a database query.

Mona.




Arjen Markus wrote:
> Roy Terry wrote:
handle[color=darkred]
query/expression.[color=darkred]
parsing the[color=darkred]
&&, ||,[color=darkred]
>
> Yet another one, which builds on the parsing technique used by [expr]
> itself
> is here: http://wiki.tcl.tk/13399
>
> Regards,
>
> Arjen


Donald Arseneau

2005-04-01, 8:58 pm

"Mona" <impm01@yahoo.com> writes:

> I would like to compare a variable with the expression builder string
> and perform some operations. My problem is how do I insert the
> variable in the expression string?
> [("my_variable" > "somevalue") && ("my_variable" < "othervalue")].


If I read you right, $my_variable is an expression...

Instead of:

if { ($my_variable > $somevalue) && ($my_variable < $othervalue) } {...

use:

if " ($my_variable > $somevalue) && ($my_variable < $othervalue) " {...

That will pre-expand the variables before expr looks at them.

Or were you talking about string comparisons?

--
Donald Arseneau asnd@triumf.ca
Arjen Markus

2005-04-06, 12:26 pm

Mona wrote:
>
> Hi all,
>
> Thanks for the reply.
>
> I would like to compare a variable with the expression builder string
> and perform some operations. My problem is how do I insert the
> variable in the expression string?
> So, the expresion need to be something like
> [("my_variable" > "somevalue") && ("my_variable" < "othervalue")].
>
> Something like a database query.
>


If I understand you correctly, then you want to add a $ to every
variable name?

Well, the simplest way would be:
1. Maintain a list of variable names (or assume anything text that is
not quoted is a variable name)
2. Exploit [regexp] or [string map] to convert the raw but
non-Tcl-programmer-
friendly string into a proper Tcl [expr] expression.

If you want an example that may be beyond your needs, then have a look
at:
http://wiki.tcl.tk/11379 or http://wiki.tcl.tk/2822.

Otherwise:

set raw_expr {v > "a" && v < "m"}
regsub -all {([^"]|^)([a-zA-Z]+)} $raw_expr {$\2} converted_expr
set v "b"
expr $converted_expr

(Note: the regular expression is too simple - it fails with " m " for
instance :()

Regards,

Arjen
Mona

2005-04-27, 8:59 pm

Thank you all for your replies.
The wiki tcl pages were a great help.
Thanks again.
Mona.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com