By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,384 Members | 671 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,384 IT Pros & Developers. It's quick & easy.

Evaluate basic arithmatic in a string

P: n/a
OK, this is for you experienced Java programmers out there.

I have a string that contains the following value:

"myValue += 5"

I need, in Java, to be able to evaluate this expression.

Caviots:
myValue can be int, float, long, double, or short.

Likewise, the 5 could also be a variable of any of the above listed
types. ie, "myValue += myValue2".

Just so you know the environment, I an writing a java scripting janguage
where I can have a user build a template on the fly and I interpret all
the java code manually. I am about 75% finished mimicing the JVM in the
scripting but am lacking basic math functions.

I use a class

class ObjDef {
Object value;
Class className;
}

to store and pass values throughout the script interpretation process.

so I know the value and class of myValue and myValue2 and I know to cast
5 to an int if not otherwise set.

The real caviot comes in that if myValue is an int, the className in
ObjDef is int but the Object is Integer.

How do I get myValue ( which is really ObjDef.value ) to an int so I can
do an actully myValue = myValue + 5 and put it back in the ObjDef class?

Realize if I do a hard cast, 25 different possible combinations (5^2),
so I am looking for a different answer.

Thank you for any help you can offer.

Jul 17 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
> I have a string that contains the following value:

"myValue += 5"

I need, in Java, to be able to evaluate this expression.


Consider using http://www.mozilla.org/rhino/ - the eval() function does this
nicely.

Powerful stuff!
Jul 17 '05 #2

P: n/a
Order types by restriction level:

type: double float long int short
level: 0 1 2 3 4

and cast operand with have higher level to type of lover level,
then perform operation using two operands with same types.
David Morris wrote:
OK, this is for you experienced Java programmers out there.

I have a string that contains the following value:

"myValue += 5"

I need, in Java, to be able to evaluate this expression.

Caviots:
myValue can be int, float, long, double, or short.

Likewise, the 5 could also be a variable of any of the above listed
types. ie, "myValue += myValue2".

Just so you know the environment, I an writing a java scripting janguage
where I can have a user build a template on the fly and I interpret all
the java code manually. I am about 75% finished mimicing the JVM in the
scripting but am lacking basic math functions.

I use a class

class ObjDef {
Object value;
Class className;
}

to store and pass values throughout the script interpretation process.

so I know the value and class of myValue and myValue2 and I know to cast
5 to an int if not otherwise set.

The real caviot comes in that if myValue is an int, the className in
ObjDef is int but the Object is Integer.

How do I get myValue ( which is really ObjDef.value ) to an int so I can
do an actully myValue = myValue + 5 and put it back in the ObjDef class?

Realize if I do a hard cast, 25 different possible combinations (5^2),
so I am looking for a different answer.

Thank you for any help you can offer.


Jul 17 '05 #3

P: n/a
This is an excellent package. However, I am writing a commercial
application and do not want to use 3rd party code in it.

Thanx anyway.
John Harlow wrote:
I have a string that contains the following value:

"myValue += 5"

I need, in Java, to be able to evaluate this expression.

Consider using http://www.mozilla.org/rhino/ - the eval() function does this
nicely.

Powerful stuff!


Jul 17 '05 #4

P: n/a
Ok, this is an excellent suggestion to start solving the problem.

I have coded your suggestion, sort of.

To do this, correct me if I am wrong, I will still need 5 conditional
tests to hardwire the coding for each restriction level one I decide
which one to use.

I can almost accept that to simple do the casting. However, then I need
to do the evaluation. In the case I gave you is "+=". It could be any
arithmetic function ( a + b / d * c ).

Can I then run a common method to do the evaluation whatever the type
is? I can parse through and set a, b, c, and d to the same type based
on the functionality you have already provided. But how can I
generically to math against the values regardless of their type?

Thanks again for the help.
Mykola Rabchevskiy wrote:
Order types by restriction level:

type: double float long int short
level: 0 1 2 3 4

and cast operand with have higher level to type of lover level,
then perform operation using two operands with same types.
David Morris wrote:
OK, this is for you experienced Java programmers out there.

I have a string that contains the following value:

"myValue += 5"

I need, in Java, to be able to evaluate this expression.

Caviots:
myValue can be int, float, long, double, or short.

Likewise, the 5 could also be a variable of any of the above listed
types. ie, "myValue += myValue2".

Just so you know the environment, I an writing a java scripting
janguage where I can have a user build a template on the fly and I
interpret all the java code manually. I am about 75% finished
mimicing the JVM in the scripting but am lacking basic math functions.

I use a class

class ObjDef {
Object value;
Class className;
}

to store and pass values throughout the script interpretation process.

so I know the value and class of myValue and myValue2 and I know to
cast 5 to an int if not otherwise set.

The real caviot comes in that if myValue is an int, the className in
ObjDef is int but the Object is Integer.

How do I get myValue ( which is really ObjDef.value ) to an int so I
can do an actully myValue = myValue + 5 and put it back in the ObjDef
class?

Realize if I do a hard cast, 25 different possible combinations (5^2),
so I am looking for a different answer.

Thank you for any help you can offer.


Jul 17 '05 #5

P: n/a
David Morris wrote:
OK, this is for you experienced Java programmers out there.

I have a string that contains the following value:

"myValue += 5"

I need, in Java, to be able to evaluate this expression.


This is pretty easy. You have to build a tokenized state machine to be
able to do this. In C we used Flex and Bison as an engine for that, but
you don't have to use those (you can build your own).

You hvae to build an lexical analyzer engine and then a grammar engine
for this. This is a lot like what we did in our Compiler Design course.
I guess trying to find a java alternative to Flex and Bison is what
you need, or simply do it yourself using a lexical analyzer and a state
machine for your grammar.

Jul 17 '05 #6

P: n/a
Yoyoma_2 <Yoyoma_2@[at-]Hotmail.com> wrote in message news:<FGgbc.12539$oR5.7778@pd7tw3no>...
David Morris wrote:
OK, this is for you experienced Java programmers out there.

I have a string that contains the following value:

"myValue += 5"

I need, in Java, to be able to evaluate this expression.


This is pretty easy. You have to build a tokenized state machine to be
able to do this. In C we used Flex and Bison as an engine for that, but
you don't have to use those (you can build your own).

You hvae to build an lexical analyzer engine and then a grammar engine
for this. This is a lot like what we did in our Compiler Design course.
I guess trying to find a java alternative to Flex and Bison is what
you need, or simply do it yourself using a lexical analyzer and a state
machine for your grammar.


JavaCC
https://javacc.dev.java.net/
Jul 17 '05 #7

P: n/a
Also, JFlex and CUP (http://www.cs.princeton.edu/~appel/modern/java/CUP/)

Mike
HG******@nifty.ne.jp (hiwa) wrote in message news:<68*************************@posting.google.c om>...
Yoyoma_2 <Yoyoma_2@[at-]Hotmail.com> wrote in message news:<FGgbc.12539$oR5.7778@pd7tw3no>...
David Morris wrote:
OK, this is for you experienced Java programmers out there.

I have a string that contains the following value:

"myValue += 5"

I need, in Java, to be able to evaluate this expression.


This is pretty easy. You have to build a tokenized state machine to be
able to do this. In C we used Flex and Bison as an engine for that, but
you don't have to use those (you can build your own).

You hvae to build an lexical analyzer engine and then a grammar engine
for this. This is a lot like what we did in our Compiler Design course.
I guess trying to find a java alternative to Flex and Bison is what
you need, or simply do it yourself using a lexical analyzer and a state
machine for your grammar.


JavaCC
https://javacc.dev.java.net/

Jul 17 '05 #8

P: n/a
On 12 Apr 2004 19:33:57 -0700, no*****************@scovetta.com
(Michael Scovetta) wrote or quoted :
> > I need, in Java, to be able to evaluate this expression.

see http://mindprod.com/jgloss/eval.html

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Jul 17 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.