VB code dom expression evaluator

Published July 04, 2006
Advertisement
The following uses the codedom to compile and execute vb expressions on the fly. Thought I'd share.

Module Module1    Sub Main()        Dim Expressions() As String = {"2*args(0)+3*args(1) <= 20", "5*args(1)+7*args(0) < 10", "6*args(0)+2*args(1) < 50"}        Dim c As New ExpressionEvaluator        Dim AllTrue As Boolean = True        Dim X As Integer = 0        Dim Y As Integer = 1        For Each expression As String In Expressions            AllTrue = AllTrue And c.EvaluateExpression(expression, X, Y)        Next        If AllTrue Then            Console.WriteLine("All expressions are true")        End If    End SubEnd ModuleClass ExpressionEvaluator    Private Compiler As CodeDom.Compiler.ICodeCompiler    Sub New()        Dim c As New Microsoft.VisualBasic.VBCodeProvider        Compiler = c.CreateCompiler    End Sub    'evaluates an expression and returns the result    Public Function EvaluateExpression(ByVal Expression As String, ByVal ParamArray args() As Object) As Object        Dim results As CodeDom.Compiler.CompilerResults        results = Compiler.CompileAssemblyFromSource(CompilerParameters, CreateSource(Expression))        If results.Errors.Count > 0 Then            Dim ExMessage As String            For Each e As CodeDom.Compiler.CompilerError In results.Errors                ExMessage &= "Error " & e.ErrorNumber & " at " & e.Line & ": " & e.ErrorText & vbNewLine            Next            Throw New Exception("Errors were found while compiling: " & vbNewLine & ExMessage)        End If        If args Is Nothing Then            args = New Object() {}        End If        Return results.CompiledAssembly.GetType("Module1").InvokeMember("Foo", Reflection.BindingFlags.InvokeMethod, Nothing, Nothing, args, Nothing, Nothing, Nothing)    End Function    Private Function CreateSource(ByVal expression As String) As String        Return "Public Module Module1" & vbNewLine & _        "Function Foo(ByVal Paramarray args() as object) as object" & vbNewLine & _        "Return " & expression & vbNewLine & _        "End Function" & vbNewLine & _        "End Module"    End Function    Private Function CompilerParameters() As CodeDom.Compiler.CompilerParameters        Dim parameters As New CodeDom.Compiler.CompilerParameters        parameters.CompilerOptions = "/t:library"        parameters.GenerateInMemory = True        Return parameters    End FunctionEnd Class


In other news, having a cold on the fourth sucks.
Previous Entry Track Day!
Next Entry Track Day II
0 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement

Latest Entries

Brag on

1074 views

Guess where.

993 views

Eastsidekistan

964 views

Tobor the great

1162 views

Icy roads

1044 views

Trek Tri Island

1087 views

That's it, then

987 views

Track Day II

945 views

Track Day!

875 views
Advertisement