Results 1 to 5 of 5

Thread: Example of a script parser and interpreter

  1. #1

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    57

    Red face 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.



    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:




    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: Nameco.zip (repacked, all binaries removed)

    - Barret
    Last edited by TheBarret; Apr 15th, 2015 at 01:06 PM.

  2. #2

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    57

    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...

  3. #3
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Example of a script parser and interpreter

    Quote Originally Posted by TheBarret View Post
    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

  4. #4

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    57

    Re: Example of a script parser and interpreter

    Quote Originally Posted by ident View Post
    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

  5. #5

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    57

    Re: Example of a script parser and interpreter

    Repacked the zip , i double checked it no binaries are in there.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width