1 Attachment(s)
Example of a script parser and interpreter
I showed not long ago a basis of how to parse a script using the abstract tree format.
I've been busy to make an interpreter and reseached some examples i found floating on the internet.
I must admit im not a user friendly coder and i also admit my code might not be well written as desired by some of you (*cough* ident), however this is the best i think i can produce to show you a way to create a script interpreter done in vb.net.
My script supports the following types:
- String
- Integer
- Float
- Boolean
- Collection (list)
- Stream
- Function
- Delegate
- Null
Exposing external modules and custom functions
I found a suitable idea from John Leitch (Link)
Code:
<ScriptFunction("Path", ValueType.Function, False, False)> _
Public Shared Function AssemblyAbsolutePath(Name As String) As String
Return Path.Combine(Helpers.AssemblyDirectory, Name)
End Function
Once the evaluater has located the class that you want to utilize in your script it archives all the methods to a delegate class, this class acts as middle men that invokes it.
Some of those are global functions and some are an extension to a type.
Those are set on the global scope of the script like "__string.name", once you would call a member function of a string type then it would invoke that static method.
http://i.imgur.com/QxxwGJE.png
The creation of a static method he wrote was as followed (and a bit modified by my needs)
Code:
Public Shared Function CreateDelegate(Method As MethodInfo) As System.[Delegate]
If (Method IsNot Nothing) AndAlso (Method.IsStatic) AndAlso (Not Method.IsGenericMethod) Then
Dim ExprParams() As LinqExpressions.ParameterExpression = Method.GetParameters().Select(Function(p) LinqExpressions.Expression.Parameter(p.ParameterType, p.Name)).ToArray()
Dim ExprCall As LinqExpressions.MethodCallExpression = LinqExpressions.Expression.Call(Nothing, Method, ExprParams)
Return LinqExpressions.Expression.Lambda(ExprCall, ExprParams).Compile()
End If
Throw New Exception(String.Format(My.Settings.InvalidFunctionForDelegate, Method.ToString))
End Function
Serializing scripts
Further on i was aimed to use the google's approach for serializing, called proto-buf.
Simply because my idea later on is that i want to send scripts from a remote location to a client.
And protobuf produces very small binary footprints, a script of 300 bytes would result in 400 bytes.
Example of using a query and file stream usage script wise:
http://i.imgur.com/lBacnel.png
Well i guess you just need to download the source and see how you can utilize it for your own desire.
Any suggestions or qeustions, feel free to bombard this thread ;)
Nameco Source: Attachment 125747 (repacked, all binaries removed)
- Barret
Re: Example of a script parser and interpreter
Oh darn it, just realized i posted this in the general vb.net instead of Codebank -.- im so sorry...
Re: Example of a script parser and interpreter
Quote:
Originally Posted by
TheBarret
Oh darn it, just realized i posted this in the general vb.net instead of Codebank -.- im so sorry...
Not to mention contains multiple exe files that is not allowed on this forum ;)
Re: Example of a script parser and interpreter
Quote:
Originally Posted by
ident
Not to mention contains multiple exe files that is not allowed on this forum ;)
I thought i removed all the binary except the proto-buf library. Hmmz im sorry folks , scan it anyway :S
Re: Example of a script parser and interpreter
Repacked the zip , i double checked it no binaries are in there.