There are already very good answers/solutions, but... anyway I wanted to show here my implementation of eval() which is more "clean", because it not uses CodeDom or lexers/parsers. Every needed bit of evaluation - is in code itself :-) Take a look :

[link:coding-experiments.blogspot.com]

By on 10/2/2009 1:14 PM ()

You could evaluate an expression fairly trivially using a .Net compiler for example VB from F#:

<SUP><SUB>open System.CodeDom.Compiler
open System.Reflection;
open Microsoft.VisualBasic;</SUB></SUP>

<SUP><SUB>let CompileExpression (expression:string) =
let source =
"Imports System.Math\r\n" ^
"Public Module Module1\r\n" ^
"\tPublic Function Eval(ByVal x As Double)\r\n" ^
"\tReturn " ^ expression ^ "\r\n" ^
"\tEnd Function\r\n" ^
"End Module"
let provider = new VBCodeProvider();
let parameters = new CompilerParameters()
parameters.GenerateInMemory <- true
provider.CompileAssemblyFromSource(parameters, [|source|])

let CreateMethod (expression:string) =
let results = CompileExpression expression
if not results.Errors.HasErrors then
let ass = results.CompiledAssembly
let t = ass.GetType("Module1")
t.GetMethod("Eval")
else
null

let InvokeMethod (mi:MethodInfo) (value:float) =
mi.Invoke(null, [|box value|]) :?> float</SUB></SUP>

You can see the technique used for a dynamic graph plot here: [link:www.trelford.com]

Hope this helps,

Phil

By on 5/3/2007 6:59 AM ()

A long time back (.NET 1.0 prelaunch) I experienced incredulity when I realised that the new languages had no version of eval.

I found a solution back then. The little publicised JScript.NET does work. I wrote a tiny JScript assembly that did the job. I passed in some objects and code (string). The code in this case sat in a database and gave me ways to construct objects on the fly without being limited by compilation.

Some issues are:

1) Code is in JScript.

2) JScript seems to lack a passionate evangelist like Don. I'm not sure what the future is, in fact I haven't used it for some time.

3) I got a slew of suggestions which involved a lot more work than the solution I picked. (Peter Torr and Eric Lippert spring to mind as potential sources of futher information, as does LSharp. I suspect the status of the latter is research finished, no or slow progress from here on.)

(I haven't yet seriously looked into doing that job with F#, but would be genuinely surprised, and disappointed, if a reformulation didn't offer a way.)

By on 5/3/2007 3:53 PM ()

Since all I really need is a way to do basic math and variable substitution, I wound up adapting one of the F# samples to lex/parse a very simple arithmetic language. It's pretty much BASIC without a "for" construct, and there are a couple of holes in it, but I got it to work without too much difficulty. I did it using fslex and fsyacc, but I suspect I could have done something with one of the compilers, as was suggested earlier.

DC

By on 5/3/2007 4:28 PM ()

You might want to try looking at this blog post:

[link:fsharpnews.blogspot.com]

If you read in your expressions into these kind of symbolic datastructures, they are pretty easy to evaluate.

Jurgen

By on 4/27/2007 2:22 PM ()

I like the way that works, but it still runs me up against the problem of decomposing a string into those types. It's trivial for me to break the string into a list, but it seems like I'd still need to be able to generate an expression, or a syntax tree, or something like that.

DC

By on 4/30/2007 9:44 AM ()

Also see the article on the front page

[link:cs.hubfs.net]

By on 4/27/2007 2:44 PM ()

I know that there isn't an eval as there is in Lisp-world. At least not yet.

I think it would be cool to have a compiler API that could turn text into a tree, and trees into bytecode using System.Reflection.Emit or whatever similar API the F# compiler uses.

Rob

By on 4/27/2007 1:16 PM ()

I know that there isn't an eval as there is in Lisp-world.

Judging by the discussion of lexers and parsers, I think doomchild is after something like BASIC's EVAL rather than Lisp's EVAL. The former lexes, parses and evaluates a string containing BASIC code whereas the latter evaluates an s-expr.

Cheers,
Jon.

By on 5/4/2007 8:34 PM ()

I know that there isn't an eval as there is in Lisp-world.

Judging by the discussion of lexers and parsers, I think doomchild is after something like BASIC's EVAL rather than Lisp's EVAL. The former lexes, parses and evaluates a string containing BASIC code whereas the latter evaluates an s-expr.

Cheers,
Jon.

Verily, he speaks the truth. What would be perfect would be the ability to eval a string containing F# code. For the time being, I've done my best to put together a little math language that'll do what I'm trying to do, but I'm pretty sure there's a cleaner way to go about things.

DC

By on 5/5/2007 10:29 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper