eval() function

  • Thread starter Thread starter sunrise
  • Start date Start date
S

sunrise

Dynamic languages like Python and Ruby have eval() function which is
very handy when trying to evaluate a mathematical expression.

For example:

x = 90
puts eval(user_input)

imagine that user_input is "x+sin(x)" this will prints 91.

Is there something similar in C#, and if not, how hard would be to
implement it?


Thanks.
sunrise
 
Sunrise,

No, C# does not have anything like this.

In order to implement this, it would depend on whether or not you want
it to interpret C# code or not.

If you are looking to have it evaluate C# code, then you can get it to
work, but with a good deal of work. You would have to dynamically compile
assemblies and then load and execute them. However, the mechanism to do so
is in the framework.

If it is another language, or something custom, then you have to write
your own evaluator.

Hope this helps.
 
Nicholas said:
Sunrise,

No, C# does not have anything like this.

In order to implement this, it would depend on whether or not you want
it to interpret C# code or not.

If you are looking to have it evaluate C# code, then you can get it to
work, but with a good deal of work. You would have to dynamically compile
assemblies and then load and execute them. However, the mechanism to do so
is in the framework.

If it is another language, or something custom, then you have to write
your own evaluator.

Hi Nicholas,

thanks for your answer. I'm planning to develop some math tools, so I
have some flexibility on the input format (but likely to be similar to
the math notations). I'll need to write my own evaluator then. I
suppose this means parsing the expression and creating a tree with the
operators as nodes, and values as leafs. Right?

Thanks.
 
sunrise said:
Dynamic languages like Python and Ruby have eval() function which is
very handy when trying to evaluate a mathematical expression.

For example:

x = 90
puts eval(user_input)

imagine that user_input is "x+sin(x)" this will prints 91.

Is there something similar in C#, and if not, how hard would be to
implement it?

I've seen solutions posted here that essentially involve writing C#
code and compiling it at run time.

However, the precise example you gave cannot be achieved in C# by any
means, because unlike Python and Ruby, C# is a compiled language. There
is no guarantee that the variable "x" even exists at run time, and even
if it does the fact that it is called "x" is not embedded in the DLL
generated by the compiler. So, in effect, there is no "x" to include in
the equation.

Now, if the user types something like:

"int x = 90; x + sin(x)"

then that's a different matter.
 
Sunrise,

Yes, something like that. I would take a look on the web, I am sure
someone has done this work already.

If anything, I would interop with matlab or something like that.
 
In a conversation with one of the MS .NET team it was mentioned that full
..NET support of dynamic languages is a design goal of .NET 3.x (or later);
at that point you would have LISP.NET, Scheme.NET, Prolog.NET, etc as part
of .NET, not just simulated on top of it.

I can't wait.

Regards,
Raj
 
Have a look at Tommys math calculator. Nice little calc with a neat twist.
He will email you the source. I think he uses a math lib, not sure.
http://channel9.msdn.com/ShowPost.aspx?PostID=197792

--
William Stacey [MVP]

| Nicholas Paldino [.NET/C# MVP] wrote:
| > Sunrise,
| >
| > No, C# does not have anything like this.
| >
| > In order to implement this, it would depend on whether or not you
want
| > it to interpret C# code or not.
| >
| > If you are looking to have it evaluate C# code, then you can get it
to
| > work, but with a good deal of work. You would have to dynamically
compile
| > assemblies and then load and execute them. However, the mechanism to do
so
| > is in the framework.
| >
| > If it is another language, or something custom, then you have to
write
| > your own evaluator.
|
| Hi Nicholas,
|
| thanks for your answer. I'm planning to develop some math tools, so I
| have some flexibility on the input format (but likely to be similar to
| the math notations). I'll need to write my own evaluator then. I
| suppose this means parsing the expression and creating a tree with the
| operators as nodes, and values as leafs. Right?
|
| Thanks.
|
 
Back
Top