• Log in or sign up for an account here: https://www.sythe.org/login

[TUT] Making your own interpreted scripting language

Blupig

BEEF TOILET
Joined
Nov 23, 2006
Sythe Gold
1,609
Messages
7,145
Age
33
Location
Canada
[TUT] Making your own interpreted scripting language
What is an interpreted scripting language?
It's a language that allows you to perform instructions on a machine, though it does not compile. It also translates down to a different language, and most commonly uses one-line commands (and could miss various typical characteristics of a true programming language).

I just made up a small project that works with making your own scripting language, so it will not compile (unless you make it compile) and will translate down to VB. An example script that will run with the code I provide could be something like:
Code:
=start=
alert("YOOO");
alert("YOHOHOHOHOHO");
goTo(=nextMethod=);

=nextMethod=
alert("This is part of the second method");
gotoLine(3);

I won't be explaining each part individually, so instead I commented my source. Please don't steal my work, because I'll come at you with my fangs out :)

This is NOT a beginner's project. Best suited for intermediates and advanced programmers. Examples of current programs that use this method for its scripting are both my scriptable autotalker and autoclicker in the verified programs section.

Code:
Public Class Form1

    ' Run button event
    Private Sub cmdRun_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRun.Click

        ' This array will store each line of code
        ' You can merge the following two lines of code into one, it just depends on preference. I like to do it like this.
        Dim arr() As String = Nothing
        ' Split the code with the vbLf delimiter (line-feed)
        arr = Split(txtScript.Text, vbLf)

        ' Make a variable for the # of lines in the script
        Dim lines As Integer = arr.Length


        ' Run through loop for every line
        For i As Integer = 0 To arr.Length - 1

            ' Add extra delimiter onto each line, this is for parsing
            arr(i) = arr(i) & ");"

            ' If the line starts with "alert" then we have the alert command
            If arr(i).StartsWith("alert") Then
                ' Get the text the user wants to alert with GetBetween
                Dim str As String = GetBetween(arr(i), "alert(""", """);")
                ' Make alert
                MsgBox(str)
            End If

            ' If line starts with goTo (case sensitive) it is the goto command
            If arr(i).StartsWith("goTo") Then
                ' Get the method name to which the user wants to go to
                Dim str As String = GetBetween(arr(i), "goTo(=", "=);")
                ' Variable for the line where the method is
                Dim gotoline As Integer

                ' Go through script again to find the method
                For j As Integer = 0 To arr.Length - 1
                    If arr(j).StartsWith("=" & str) Then
                        ' The line # is the counter +1 because j starts at 0 and the line numbers start at 1
                        gotoline = j + 1
                    End If
                Next

                ' Change i so that we start the loop at the method in the goTo command
                i = gotoline - 1

            End If

            ' If line starts with gotoLine (case sensitive) then its the gotoLine command
            If arr(i).StartsWith("gotoLine") Then
                ' Get line # to go to
                Dim str As String = GetBetween(arr(i), "gotoLine(", ");")
                ' Int variable to store # in
                Dim lInt As Integer
                ' If it isn't numeric...
                If IsNumeric(str) = False Then
                    ' This should throw some kind of exception
                    lInt = 0

                    ' If it is numeric, then store it as a conversion
                Else
                    lInt = CInt(str)
                End If
                ' Change the counter so that we go to the specific line the user wants to go to
                i = lInt - 2
            End If

        Next

    End Sub

    ' GetBetween function, gets between strings start and finish in string source
    Private Function GetBetween(ByVal Source As String, ByVal Start As String, ByVal Finish As String) As String

        ' Get the starting index in the source string
        Dim index As Integer = InStr(Source, Start) + (Start.Length - 1)
        ' Get the ending index in the source string
        Dim fIndex As Integer = InStr(Source, Finish) - 1
        ' Determine the length of the string we got between Start and Finish
        Dim len As Integer = fIndex - index

        ' Return that string starting at the start index, and going for the length of characters it is
        Return Source.Substring(index, len)

    End Function

End Class
 
[TUT] Making your own interpreted scripting language
Isn't it better to just create your own API in a known language?
 
[TUT] Making your own interpreted scripting language
Isn't it better to just create your own API in a known language?

I've tried both. Combined, my scriptable bots have close to or over 2000 downloads (haven't checked recently) as where all the APIs I've made (combined downloads) reach maybe only 100. People are more interested in "programming" in something easier and far more simple to grasp than a real language, which is why I wanted to show how to do this.
 
[TUT] Making your own interpreted scripting language
I just glanced through looks pretty helpful for beginners. Might want to trim whitespace from left and right of each line before processing it to be a little more thorough.

Also, why is the end semi-colon needed?
 
[TUT] Making your own interpreted scripting language
I just glanced through looks pretty helpful for beginners. Might want to trim whitespace from left and right of each line before processing it to be a little more thorough.

Also, why is the end semi-colon needed?

In case there are brackets inside function arguments that would cause everything to go whacky...Better chance that an argument won't contain "); as opposed to ").
 
[TUT] Making your own interpreted scripting language
Oh right good point. In that case you should make your GetBetween function take regular expressions as the start and end arguments. That way the end argument could just be ")$" which is a bracket followed by the end of the string/line.
 
[TUT] Making your own interpreted scripting language
Oh right good point. In that case you should make your GetBetween function take regular expressions as the start and end arguments. That way the end argument could just be ")$" which is a bracket followed by the end of the string/line.

I actually reconstructed the whole thing to use regex, it's way easier now. It allows for different types of syntax (sort of like VB6), so things like this are all acceptable:
Code:
alert "text", "title";
alert "text" "title";
alert("text", "title");

And I changed the delimiter from vbLf to ";" & vbCrLf so that you can have single commands on different lines. Works much better now.
 
Runescape Gamblingrunescape gambling
Back
Top