[TUT] Making your own interpreted scripting language

Discussion in 'Programming General' started by Blupig, Jan 16, 2012.

[TUT] Making your own interpreted scripting language
  1. Unread #1 - Jan 16, 2012 at 3:51 PM
  2. Blupig
    Joined:
    Nov 23, 2006
    Posts:
    7,145
    Referrals:
    16
    Sythe Gold:
    1,609
    Discord Unique ID:
    178533992981594112
    Valentine's Singing Competition Winner Member of the Month Winner MushyMuncher Gohan has AIDS Extreme Homosex World War 3 I'm LAAAAAAAME
    Off Topic Participant

    Blupig BEEF TOILET
    $5 USD Donor

    [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
    
     
  3. Unread #2 - Jan 16, 2012 at 4:45 PM
  4. iJava
    Joined:
    Nov 21, 2011
    Posts:
    1,184
    Referrals:
    9
    Sythe Gold:
    464
    Discord Unique ID:
    220055593568829441

    iJava .Previously known as RSGoldRush
    $200 USD Donor New

    [TUT] Making your own interpreted scripting language

    Isn't it better to just create your own API in a known language?
     
  5. Unread #3 - Jan 16, 2012 at 10:25 PM
  6. Blupig
    Joined:
    Nov 23, 2006
    Posts:
    7,145
    Referrals:
    16
    Sythe Gold:
    1,609
    Discord Unique ID:
    178533992981594112
    Valentine's Singing Competition Winner Member of the Month Winner MushyMuncher Gohan has AIDS Extreme Homosex World War 3 I'm LAAAAAAAME
    Off Topic Participant

    Blupig BEEF TOILET
    $5 USD Donor

    [TUT] Making your own interpreted scripting 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.
     
  7. Unread #4 - Jan 17, 2012 at 1:14 AM
  8. Nullware
    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0

    Nullware Guru

    [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?
     
  9. Unread #5 - Jan 17, 2012 at 6:18 AM
  10. Blupig
    Joined:
    Nov 23, 2006
    Posts:
    7,145
    Referrals:
    16
    Sythe Gold:
    1,609
    Discord Unique ID:
    178533992981594112
    Valentine's Singing Competition Winner Member of the Month Winner MushyMuncher Gohan has AIDS Extreme Homosex World War 3 I'm LAAAAAAAME
    Off Topic Participant

    Blupig BEEF TOILET
    $5 USD Donor

    [TUT] Making your own interpreted scripting language

    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 ").
     
  11. Unread #6 - Jan 17, 2012 at 6:25 PM
  12. Nullware
    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0

    Nullware Guru

    [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.
     
  13. Unread #7 - Jan 17, 2012 at 9:49 PM
  14. Blupig
    Joined:
    Nov 23, 2006
    Posts:
    7,145
    Referrals:
    16
    Sythe Gold:
    1,609
    Discord Unique ID:
    178533992981594112
    Valentine's Singing Competition Winner Member of the Month Winner MushyMuncher Gohan has AIDS Extreme Homosex World War 3 I'm LAAAAAAAME
    Off Topic Participant

    Blupig BEEF TOILET
    $5 USD Donor

    [TUT] Making your own interpreted scripting language

    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.
     
< Paying for java help. | Java internal mouse? >

Users viewing this thread
1 guest


 
 
Adblock breaks this site