Evaluating expression return type

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Evaluating expression return type

Viliam Búr-2
Hi!


I have recently found BeanShell and I am very impressed by it. Easy to
learn, simple to use. I would like to use it in our project, but there
is one required functionality I do not know how to implement. I tried to
find the answer in the documentation, the example bsh scripts, mailing
list archives, google, but I am still not sure if something like this
can be done. Could you please help me?

The required functionality is to parse the expression and return the
result type, without evaluating it, with regard to variable types. In
our application, user can enter a custom expression (which will be later
evaluated), but if there is a mistake in the expression, program should
immediately return error like "this expression is not syntactically
correct" or "this expression does not return integer (or string, etc)
value". The first check is easy. But I do not know how to do the second one.

Note: It does not matter whether there is string Java mode or not, so
you can assume whatever setting provides the easier answer.


Example use:

Application has defined variables "int i" and "String s" (we know their
type, but not the value, because the value will be specified later), and
is asking about the correctness and return type of the following
expressions.

a) "i+1"  --  this expression returns integer
b) "s+1"  --  this expression returns string
c) "x+1"  --  this expression is wrong (there is no variable "x")
d) "+++"  --  this is not a valid expression


My first idea was to put random values to the variables, evaluate the
expression and see the results. But this is wrong for two reasons.
First, random values could produce random errors (for example "a/b"
could provide division-by-zero error for some values); second, calling
functions from the expression could have bad side effects.

Another idea was to use the Parser to parse the expression, and then
write my own methods to evaluate the return types of the expressions.
First, I am not sure if this is not duplicating some functionality
already existing in the library. Second, the Parser returns SimpleNode
object which is not public, so I probably should not use it. Also, there
are many BSH* classes, so I would like to avoid doing this, if possible.

Do you have any idea how to easily get the expression return type? If
not, do you think other Java interpreters may have such functionality?


Viliam

--
Viliam Bur | [hidden email]
Whitestein Technologies s.r.o. | www.whitestein.com
Panenska 28 | 811 03 Bratislava | Slovak Republic
Main +421 2 5443-5502 | Direct +421 2 593-007-21

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users
Reply | Threaded
Open this post in threaded view
|

Re: Evaluating expression return type

Geercken Uwe

What you want is a syntactical check of what was input, if I get it right. So can you not use regular expressions to match what was provided against your syntax?

Rgds,

uwe

-----Original Message-----
From: [hidden email] [mailto:[hidden email]] On Behalf Of Viliam Búr
Sent: Mittwoch, 12. September 2007 09:44
To: [hidden email]
Subject: [Beanshell-users] Evaluating expression return type

Hi!


I have recently found BeanShell and I am very impressed by it. Easy to learn, simple to use. I would like to use it in our project, but there is one required functionality I do not know how to implement. I tried to find the answer in the documentation, the example bsh scripts, mailing list archives, google, but I am still not sure if something like this can be done. Could you please help me?

The required functionality is to parse the expression and return the result type, without evaluating it, with regard to variable types. In our application, user can enter a custom expression (which will be later evaluated), but if there is a mistake in the expression, program should immediately return error like "this expression is not syntactically correct" or "this expression does not return integer (or string, etc) value". The first check is easy. But I do not know how to do the second one.

Note: It does not matter whether there is string Java mode or not, so you can assume whatever setting provides the easier answer.


Example use:

Application has defined variables "int i" and "String s" (we know their type, but not the value, because the value will be specified later), and is asking about the correctness and return type of the following expressions.

a) "i+1"  --  this expression returns integer
b) "s+1"  --  this expression returns string
c) "x+1"  --  this expression is wrong (there is no variable "x")
d) "+++"  --  this is not a valid expression


My first idea was to put random values to the variables, evaluate the expression and see the results. But this is wrong for two reasons.
First, random values could produce random errors (for example "a/b"
could provide division-by-zero error for some values); second, calling functions from the expression could have bad side effects.

Another idea was to use the Parser to parse the expression, and then write my own methods to evaluate the return types of the expressions.
First, I am not sure if this is not duplicating some functionality already existing in the library. Second, the Parser returns SimpleNode object which is not public, so I probably should not use it. Also, there are many BSH* classes, so I would like to avoid doing this, if possible.

Do you have any idea how to easily get the expression return type? If not, do you think other Java interpreters may have such functionality?


Viliam

--
Viliam Bur | [hidden email]
Whitestein Technologies s.r.o. | www.whitestein.com Panenska 28 | 811 03 Bratislava | Slovak Republic Main +421 2 5443-5502 | Direct +421 2 593-007-21

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users
Reply | Threaded
Open this post in threaded view
|

Java 6 ?

Mikael Aronsson
How is it going with the JSR-274 version of BeanShell ?

Mikael


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users
Reply | Threaded
Open this post in threaded view
|

Re: Evaluating expression return type

Viliam Búr-2
In reply to this post by Geercken Uwe
> What you want is a syntactical check of what was input, if I get it right.
> So can you not use regular expressions to match
> what was provided against your syntax?

This is what we already have in the project. Simple syntax, parser,
validator, interpreter. But there are always new requirements for new
syntax features... and what I like about BeanShell is that it would
allow us to use right now the *full Java syntax*, without having to
write our own parsers and interpreters. If I would write my own Java
parser, the biggest advantage of BeanShell for me would be lost.

Also parsing (with regular expressions or otherwise) cannot check
everything. The following code will parse correctly using bsh.Parser,
but it can never run correctly:

String s = "hello";
int i = s + 1;

When separated, each line is correct. The incorrectness of the second
line can be determined only by knowing that "s" is string. The
bsh.Parser will not complain about this code; only later the "eval"
function will return error. I need to know that the code is wrong
without calling the "eval" methods.

Perhaps I could write new methods "evaltype" which would only return the
type of the expression, without actually performing the functionality
(especially if it would mean calling some Java function; then I would
just use the reflection to find the returned type). -- I just hoped that
maybe such functionality already exists in the library.


Viliam

--
Viliam Bur | [hidden email]
Whitestein Technologies s.r.o. | www.whitestein.com
Panenska 28 | 811 03 Bratislava | Slovak Republic
Main +421 2 5443-5502 | Direct +421 2 593-007-21

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users
Reply | Threaded
Open this post in threaded view
|

Re: Evaluating expression return type

Stewart Cambridge
>From javadocs for class bsh.Interpreter:

"public void setStrictJava(boolean b)

    Set strict Java mode on or off. This mode attempts to make
BeanShell syntax behave as Java syntax, eliminating conveniences like
loose variables, etc. When enabled, variables are required to be
declared or initialized before use and method arguments are reqired to
have types.

    This mode will become more strict in a future release when classes
are interpreted and there is an alternative to scripting objects as
method closures. "


On 12/09/2007, Viliam Búr <[hidden email]> wrote:

> > What you want is a syntactical check of what was input, if I get it right.
> > So can you not use regular expressions to match
> > what was provided against your syntax?
>
> This is what we already have in the project. Simple syntax, parser,
> validator, interpreter. But there are always new requirements for new
> syntax features... and what I like about BeanShell is that it would
> allow us to use right now the *full Java syntax*, without having to
> write our own parsers and interpreters. If I would write my own Java
> parser, the biggest advantage of BeanShell for me would be lost.
>
> Also parsing (with regular expressions or otherwise) cannot check
> everything. The following code will parse correctly using bsh.Parser,
> but it can never run correctly:
>
> String s = "hello";
> int i = s + 1;
>
> When separated, each line is correct. The incorrectness of the second
> line can be determined only by knowing that "s" is string. The
> bsh.Parser will not complain about this code; only later the "eval"
> function will return error. I need to know that the code is wrong
> without calling the "eval" methods.
>
> Perhaps I could write new methods "evaltype" which would only return the
> type of the expression, without actually performing the functionality
> (especially if it would mean calling some Java function; then I would
> just use the reflection to find the returned type). -- I just hoped that
> maybe such functionality already exists in the library.
>
>
> Viliam
>
> --
> Viliam Bur | [hidden email]
> Whitestein Technologies s.r.o. | www.whitestein.com
> Panenska 28 | 811 03 Bratislava | Slovak Republic
> Main +421 2 5443-5502 | Direct +421 2 593-007-21
>
> -------------------------------------------------------------------------
> This SF.net email is sponsored by: Microsoft
> Defy all challenges. Microsoft(R) Visual Studio 2005.
> http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
> _______________________________________________
> Beanshell-users mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/beanshell-users
>

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users
Reply | Threaded
Open this post in threaded view
|

Re: Evaluating expression return type

David Lee
In reply to this post by Viliam Búr-2
There is no way to do this in BeanShell to my knowledge.
I wish there was.

There are 2 pieces to this puzzle.


Syntax Checking:

To my mind there is no method of BSH to do syntax checking without
execution.
However I get around this by wrapping everything within a function that I
dont want executed immedeatly,
and do my own validity checking by parsing the script.

e.g.

func()
{
   some_code();
}


By parsing the above the function "func" is defined and some basic syntax
checking is done.  If there are horrible failures,
these are caught immedeatly.  Such as if I had


func()
{
       bad_call(  ;  // no closing (
}


That would be caught.


Then ("of course") to execute the code I call "func".
This works OK.   although not all errors are caught, but atleast basic
syntax errors are caught.



Type evaluation.
This is impossible in BSH because the type of expressions can vary at
runtime.
Example

func()
{
    if( random() == 0 ) return 1;
     else
          return "foo";
}

or even the more general

func( x ) { return x ; }



These fuctions are of dynamic type.  Their actual return type cannot be
determined until they are run.
Different runs may produce differnt types.








> Hi!
>
>
> I have recently found BeanShell and I am very impressed by it. Easy to
> learn, simple to use. I would like to use it in our project, but there
> is one required functionality I do not know how to implement. I tried to
> find the answer in the documentation, the example bsh scripts, mailing
> list archives, google, but I am still not sure if something like this
> can be done. Could you please help me?
>
> The required functionality is to parse the expression and return the
> result type, without evaluating it, with regard to variable types. In
> our application, user can enter a custom expression (which will be later
> evaluated), but if there is a mistake in the expression, program should
> immediately return error like "this expression is not syntactically
> correct" or "this expression does not return integer (or string, etc)
> value". The first check is easy. But I do not know how to do the second
> one.
>
> Note: It does not matter whether there is string Java mode or not, so
> you can assume whatever setting provides the easier answer.
>
>
> Example use:
>
> Application has defined variables "int i" and "String s" (we know their
> type, but not the value, because the value will be specified later), and
> is asking about the correctness and return type of the following
> expressions.
>
> a) "i+1"  --  this expression returns integer
> b) "s+1"  --  this expression returns string
> c) "x+1"  --  this expression is wrong (there is no variable "x")
> d) "+++"  --  this is not a valid expression
>
>
> My first idea was to put random values to the variables, evaluate the
> expression and see the results. But this is wrong for two reasons.
> First, random values could produce random errors (for example "a/b"
> could provide division-by-zero error for some values); second, calling
> functions from the expression could have bad side effects.
>
> Another idea was to use the Parser to parse the expression, and then
> write my own methods to evaluate the return types of the expressions.
> First, I am not sure if this is not duplicating some functionality
> already existing in the library. Second, the Parser returns SimpleNode
> object which is not public, so I probably should not use it. Also, there
> are many BSH* classes, so I would like to avoid doing this, if possible.
>
> Do you have any idea how to easily get the expression return type? If
> not, do you think other Java interpreters may have such functionality?
>
>
> Viliam
>
> --
> Viliam Bur | [hidden email]
> Whitestein Technologies s.r.o. | www.whitestein.com
> Panenska 28 | 811 03 Bratislava | Slovak Republic
> Main +421 2 5443-5502 | Direct +421 2 593-007-21
>
> -------------------------------------------------------------------------
> This SF.net email is sponsored by: Microsoft
> Defy all challenges. Microsoft(R) Visual Studio 2005.
> http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
> _______________________________________________
> Beanshell-users mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/beanshell-users
>


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users
Reply | Threaded
Open this post in threaded view
|

Re: Java 6 ?

Luis Montes-2
In reply to this post by Mikael Aronsson
Mikael wrote:

> How is it going with the JSR-274 version of BeanShell ?
>
> Mikael
>
>
> -------------------------------------------------------------------------
> This SF.net email is sponsored by: Microsoft
> Defy all challenges. Microsoft(R) Visual Studio 2005.
> http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
> _______________________________________________
> Beanshell-users mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/beanshell-users
>
>  
Any follow up?
It's been a year.



-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users