The String Separator iinterpreter

lets take a look on this iinterpreter

Page 1 of 1

9 Replies - 1213 Views - Last Post: 15 August 2010 - 08:37 AM

#1 Cookiesliyr  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 136
  • Joined: 16-May 09

The String Separator iinterpreter

Posted 14 March 2010 - 08:57 AM

hello everyone, how ya doing ?

my name is Cookiesliyr and i would like to share one of my projects with you guys and i want to improve it some how, i hope it will be helpful for everyone, when i was in collage i was only write codes in VB6 and Java (i learned VB6 by myself but the university though me Java) but because i was way much better in VB6 i used it in one of my projects, it was the final project of ICS 410 Concepts of Programming language, the project was simple enough i guess : build a common lisp interpreter.

the time was short and we have more then one class and lab and project at once but i was exited, i liked to build something special, everyone followed the regular way which the book use but i liked to build my on code that's why i build the "String Separator", it was kinda similar to their job but a bit more complicated, to be frank i couldn't finish it, it should save variables in memory and recall them, and some functions weren't finish in time, i tried to complete it once but i was damn lazy to be frank because there is no grade on it anymore.

around 3 weeks ago i decided to finish my old job ... i wanna rebuild this code in more efficient way just like what i told my professor in his office in that day before i start working on it, i wanna build a common interpreter which can be use to make your own language by it and we can use it to give our projects more control in real time this time i wanna rewrite it in C#.

when i opened my old project i found it more complex then i though XD any way i can simply rewrite it but this is not how pro work, i wanna take the idea and improve it but to be frank i always work alone and i don't know what is a good idea and what is not that's why i was looking for brain storming guild or if i can make my own or if i can see some DIC members who have such a guild but allot of members told me "but we have such a thing !" i asked them " Where ?" they simply said "here ! Share your project forum" i said "but ... what ... i mean ... but my work is not complete !" but it is look like this forum has been made for this purpose after all.

now should i tell you my project idea first or should i show you the code ? i guess i should show you the idea first, but that well be written in another post.


===================================================================


the idea is simple i guess, the class well act in three phases

before we use get in details we need to initialize an object of String Separator (we will call it SS for short for now) to use it to interpret the code, then we send the code to it here it start the phase one.

1) First Phase : the code which we have sent will be separated, first we will check the space as you know the code might have extra space or sometimes we need to put a space but the user doesn't (yes the user is the programmer now) for example he might write a code like this "if value1 = 10 then" as you can see he put more then one space after "if" and sometimes he doesn't put space especially after operations symbols like "(1+2)" the SS will see this code as one line that's why we need the space dealer which will remove unwanted space and add them after symbols and make the last code like this " ( 1 + 2 ) "

then we will take each string blocks (yes everything here well be in String format) and save it in a string array and then we will pass it to a function called GetValue to see what is this block represent and save its type in another string array.

so far we have two arrays one save the code blocks and the other the meaning of the block

2) Second Phase : here we will call Body Body ! ahem i mean the Body function, this guy job is simple he will check for us if the body instruction is correct, then he will save some numbers to represent where is the start and the end for each body in the code by using the start of body position and it's length, this code will work recursively , you can see the code might be very simple like " 1 + 2 " or might be damn complex " ( 1 + ( 1 + 2 - ( 3 * 2 ) + ( 4 \ 5) ) + 1 ) - ( 2 * 4 ) + 3 " and don't worry it can be worst you don't know what the craze user will input in the SS ! so we should be ready for him and see his disappointing face after trying to dig a bug in the code and failed,

3) Third Phase : in here we will finally send our code to the Syntax Analyzer which well determine what will it do, this guy is a function which will return a string value, if something messed up it will return its value as an Error message, and if it is correct it will return the result that is needed or nothing if the code need to save a value in memory or whatever so it will return nothing, in any case this guy might work in recursive way too !

the Syntax Analyzer accept a number inside it which is the pointer to the code and it will be used in the string array which keep our code blocks meaning, then it pass it to switch command (it called Select case in VB6) to see what was the keyword in our code if it is "if-statement" then teda teda teda do the work of the If statement, if it is operation (because in common lisp they write the operation first for example ( 1 + 2 ) in lisp is written as (+ 1 2) and so on ) we will do the operation and see the result, if it is a bracket "(" we will call the function again !

when the Analyzer know what is the function it will check if the types are right and the code is in the right instruction so if it is an operation it expect to see two numbers after the operation, so first of all it will call another function called (Isit) function and check if the value of this code function well represent the right keywords and values for example if the code was like " 5 + ( 4 + 3 ) " the Analyzer well be called twice once for the body and the second time for the operation which is outside the body, when the first time is called it will calculated " ( 4 + 3 ) " and return the value of the Analyzer to the other one, the other one will check if it has two numbers again for the operation before it try the operation out , that's why the function return a string it is actually a keyword to itself when it call itself to see if what the body return what we need it or not.

the idea might look simple, it is just like if a normal programmer read his own code or something but when you get to work you will see it is not that easy to make such idea works, now we can take a look on the code, but that's well be in anew post.




===================================================================



[
Public SST(101) As String   ' type of key words of SSV , number zero represent the expected syntax
Public SSL(100) As Byte     ' lenthg of a keyword , number zero represnt how many ssv is there
Public SSB(50) As Byte      ' this show how many key word in bodys in a line , we have 50 body max in one line
Public SBL(50) As Byte      ' this shiw the body sequance in a command

Dim BN As Byte              ' Number of bodys found in syntaxana
Dim Syn As Byte             ' Number of how many syntax has been called as nasted
Dim i As Integer            ' for some loop it jumbs from one to other while we need to continue the looping , thats why they have common value
Dim SyntaxValue As Variant  ' it saves the value of the last syntaxana() value
Dim TempP As Integer        ' common temprore pointer , it is used by budy + syntax ( cdr )
Dim TempB As Boolean        ' common temprore boolean value
Public DebugMode As Boolean ' to give all the results or not
Public xNN As Integer       ' to save values of SSV instade of sending them , it will be later save the total number of key words so it can replace the array length to it !

Dim MyMemory(2, 20) As String ' this array will save the Varibles names , values and type



' This function get a word and return it its type ( key word ) , it is mainly
' used by syntaxana () and IsIt () functions
Function GetValue(WhatIsIT As Variant) As String
Dim c1 As Boolean, c2 As Boolean
Dim v As Variant
c1 = False
c2 = False
WhatIsIT = Trim(WhatIsIT)
WhatIsIT = LCase(WhatIsIT)
v = LCase(WhatIsIT)

'v = Trim(v) not needed
Select Case v
    Case "(":
        GetValue = "Open-L"
    Case ")":
        GetValue = "Open-R"
    Case "+", "-", "*", "\":
        GetValue = "Operation"
    Case "'":
        GetValue = "Quate"
    Case "#t", "#f":
        GetValue = "Boolean"
    Case ">", "<", ">=", "<=", "=", "<>":
        GetValue = "Relational-Operation"
    Case "if":
        GetValue = "If-Stat"
    Case "then":
        GetValue = "Then-Part"
    Case "else":
        GetValue = "Else-Part"
    Case "define":
        GetValue = "Define"
    Case "and", "or", "not":
        GetValue = "Boolean-Operation"
    Case "set":
        GetValue = "Set"
    Case "car":
        GetValue = "F-car"
    Case "cdr":
        GetValue = "F-cdr"
    Case "list":
        GetValue = "List-Function"
    Case "null?":
        GetValue = "Null?"
    Case "equal?":
        GetValue = "Equal?"
    Case "length":
        GetValue = "Length"
    Case "display":
        GetValue = "Display"
    Case "ismember":
        GetValue = "IsMember"
    Case "member":
        GetValue = "Member"
    Case "cons":
        GetValue = "Cons"
    Case "append":
        GetValue = "Append"
    Case "let":
        GetValue = "Let-Function"
    Case Else:
        If isNumber(v) Then
            GetValue = "Number"
        ElseIf Left(v, 1) = """" And Right(v, 1) = """" Then
            GetValue = "String"
        Else
            GetValue = "Variable"
        End If

End Select

End Function



' This sub will take the entered code and make several checks
' and spaces places , then it will take the words and send
' them to the getvalue function which will return its meaning , then it will save them
' in two arrays , one for the meaning and the other as the value it self
' by the way , this is the main function , the program start with it

Public Sub SSeprater(ST1 As Variant, Optional b As Boolean)
Dim ST2 As String
xNN = 1
SSL(0) = 0

For i = 1 To 100
SSV(i) = ""
SST(i) = ""
Next i

If ST1 = "" Then Exit Sub:

'-------------- Space Dealer
For j = 1 To Len(ST1)
    Select Case Mid(ST1, j, 1)
        Case "-", "+", "*", "\", "(", ")", "'":
            If j <= 1 Then GoTo 10:
            If Mid(ST1, j - 1, 1) = " " And Mid(ST1, j + 1, 1) = " " Then
            GoTo 11
            ElseIf Not Mid(ST1, j - 1, 1) = " " And Mid(ST1, j + 1, 1) = " " Then
            ST2 = ST2 + " " + Mid(ST1, j, 1)
            ElseIf Mid(ST1, j - 1, 1) = " " And Not Mid(ST1, j + 1, 1) = " " Then
            ST2 = ST2 + Mid(ST1, j, 1) + " "
            ElseIf Not Mid(ST1, j - 1, 1) = " " And Not Mid(ST1, j + 1, 1) = " " Then
            ST2 = ST2 + " " + Mid(ST1, j, 1) + " "
            End If
            
            If 1 = 2 Then
10:              If Mid(ST1, j + 1, 1) = " " Then
                    ST2 = ST2 + Mid(ST1, j, 1)
                 Else
                    ST2 = ST2 + " " + Mid(ST1, j, 1)
                 End If
            End If
        
        Case " ":
            If Mid(ST1, j + 1, 1) = " " Then
            
            Else
                ST2 = ST2 + " "
            End If
        Case Else
11:         ST2 = ST2 + Mid(ST1, j, 1)
    End Select
Next j

'-------------- String seprater
SSV(0) = CStr(ST2 + " ;")
For i = 1 To 100
    If Len(SSV(0)) <= 2 Then GoTo en:
   
    For j = 1 To Len(SSV(0))
      If j = 1 And Mid(SSV(0), 1, 1) = " " Then
        SSV(0) = Right(SSV(0), Len(SSV(0)) - 1)
      ElseIf Mid(SSV(0), j, 1) = " " Then
        
        SSV(xNN) = Trim(Left(SSV(0), j))
        SST(xNN) = GetValue(SSV(xNN))
        SSL(i) = Len(SSV(xNN))
        SSV(0) = Right(SSV(0), Len(SSV(0)) - (SSL(i)))
        xNN = xNN + 1
        j = 0
        ElseIf SSV(0) = ";" Then GoTo en:
        
      End If
    Next j
Next i
en:
'-------------- calling the rest methods
For j = 0 To 50
SBL(j) = 0
SSB(j) = 0
Next j

SSL(0) = xNN
TempB = True
TempP = 0
BN = 0
Body 0
If Not BN = 0 Then
Form1.Sheet.Text = Form1.Sheet.Text + vbNewLine + "Error : error in the Brackets format "
Exit Sub
End If
TempP = 0

Syn = 0
If SyntaxAna(1) = "error" Then MsgBox "Error : unknow error happen", vbOKOnly + vbCritical, "error"
End Sub



' This my friend is the body body ! he makes the bodys structuer for us
' it save recusivly in SSB array the  body length and SBL array the sequance of the
' body
Public Function Body(NOB As Byte)
If TempB = False Then Exit Function

    For K = 1 To SSL(0)
        
        If SST(K) = "Open-L" Then
            BN = BN + 1
            For j = 1 To SSL(0)
                If SBL(j) = 0 Then
                    SBL(j) = BN
                   Exit For
                End If
            Next j
            'SBL(NOB + 1) = BN
            'Body (NOB + 1)
            NOB = NOB + 1

        ElseIf SST(K) = "Open-R" Then
            If BN <= 0 Then
            Form1.Sheet.Text = Form1.Sheet.Text + vbNewLine + "Error : error in the Brackets format "
            TempB = False
            'Exit Function
            Else
            BN = BN - 1
            For j = 1 To SSL(0)
                If SBL(j) = 0 Then
                    SBL(j) = BN
                    Exit For
                End If
            Next j
            'SBL(NOB + 1) = BN
            NOB = NOB + 1
            'Exit Function
            End If
        ElseIf Not SSV(K) = "" Then
        SSB(NOB) = SSB(NOB) + 1
        End If
    Next K
End Function




' this function will take the a keyword from a postion and analyze what
' to do next , it will return the final type value and save it in Syntaxvalue varible
' as the last found value in this function , it depand on IsIt ( int , int ) function
Public Function SyntaxAna(SyntaxStart As Integer) As String
Syn = Syn + 1
    Select Case SST(SyntaxStart)
        Case "Open-L"
            BN = BN + 1
            If SyntaxAna(SyntaxStart + 1) = "error" Then MsgBox "Error : Syntax error happen", vbOKOnly + vbCritical, "error"
        
Case "F-car":
            SST(0) = isit(SyntaxStart + 1, SSB(SBL(BN + 1)) + SyntaxStart + 1)
            If SSB(SBL(BN)) >= 2 Then SSB(SBL(BN)) = SSB(SBL(BN)) - 2
            If SST(0) = "List" Then
            SyntaxValue = SSV(SyntaxStart + 3)
            SyntaxAna = "Chart"
            BuildBody SyntaxStart - 1, SyntaxStart
            BuildBody SyntaxStart, SyntaxStart
            BuildBody SyntaxStart + 1, SyntaxStart + 1 + SSB(SBL(BN)) + 2
            Else
            Form1.Sheet.Text = Form1.Sheet.Text + vbNewLine + "Error : car function should have List after it " + vbNewLine
            Exit Function
            End If
            GoTo en:
            
Case "F-cdr":
            SST(0) = isit(SyntaxStart + 1, SSB(SBL(BN + 1)) + SyntaxStart + 1)
            If SST(0) = "List" Then
            SyntaxValue = "("
                For j = SyntaxStart + 4 To SSB(SBL(BN)) + SyntaxStart + 3 - 1
                SyntaxValue = SyntaxValue & " " & SSV(j) & " "
                Next j
                SyntaxValue = SyntaxValue + ")"
                If SSB(SBL(BN)) >= 2 Then SSB(SBL(BN)) = SSB(SBL(BN)) - 1
                BuildBody SyntaxStart - 1, SyntaxStart
                BuildBody SyntaxStart + 1, SyntaxStart + 1
                If SyntaxValue = "( )" Then
                SyntaxAna = "Empty-List"
                Else
                SyntaxAna = "List"
                TempP = TempP + 1
                End If
                GoTo en:
            ElseIf SST(0) = "Empty-List" Then
            Form1.Sheet.Text = Form1.Sheet.Text + vbNewLine + "Error : the list is empty " + vbNewLine
            Exit Function
            ElseIf SST(0) = "Chart" Then
            Form1.Sheet.Text = Form1.Sheet.Text + vbNewLine + "Error : cdr function should have List after it Not a chart" + vbNewLine
            Exit Function
            Else
            Form1.Sheet.Text = Form1.Sheet.Text + vbNewLine + "Error : cdr function should have List after it " + vbNewLine
            Exit Function
            End If
            
Case "Member":
            SST(0) = isit(SyntaxStart + 1, SyntaxStart + 2)
            If SSB(SBL(BN)) >= 2 Then SSB(SBL(BN)) = SSB(SBL(BN)) - 2
                
                If SST(0) = "Number" Or SST(0) = "Chart" Or SST(0) = "List" Then
                    Dim TempS As String, TempS2 As String
                        If SST(0) = "Number" Then
                            TempS = SSV(SyntaxStart + 1)
                            i = 2
                        ElseIf SST(0) = "Chart" Then
                            TempS = SSV(SyntaxStart + 2)
                            i = 3
                        ElseIf SST(0) = "List" Then
                            For i = SyntaxStart + 4 To SyntaxStart + 4 + SSB(SBL(BN - 1))
                            TempS = TempS + SSV(i)
                            Next i
                        End If
                    
                    SST(0) = isit(SyntaxStart + i, SSB(SBL(BN)) + SyntaxStart + i)
                    If SST(0) = "List" Then
                    For j = SyntaxStart + i + 2 To SyntaxStart + SSB(SBL(BN) + 1) + i + 1
                        TempS2 = TempS2 + SSV(j)
                    Next j
                    TempP = InStr(TempS2, TempS) + 1
                    If TempP = 1 Then
                    SyntaxAna = "Empty-List"
                    GoTo en:
                    End If
                    SyntaxValue = "("
                        For j = SyntaxStart + TempP + i To SyntaxStart + i + TempP + (SSB(SBL(BN - 1)) - TempP) + 1
                            SyntaxValue = SyntaxValue & " " & SSV(j) & " "
                        Next j
                        SyntaxValue = SyntaxValue + ")"
                        SyntaxAna = "List"
                        BuildBody SyntaxStart, SyntaxStart - 2
                        BuildBody SyntaxStart + 2, SyntaxStart
                        TempP = TempP + 1
                        GoTo en:
                        
                    ElseIf SST(0) = "Empty-List" Then
                    Form1.Sheet.Text = Form1.Sheet.Text + vbNewLine + "Error : the list is empty " + vbNewLine
                    Exit Function
                    ElseIf SST(0) = "Chart" Then
                    Form1.Sheet.Text = Form1.Sheet.Text + vbNewLine + "Error : Member function should have a number or a list or a chart then another List after it Not a chart" + vbNewLine
                    Exit Function
                    Else
                    Form1.Sheet.Text = Form1.Sheet.Text + vbNewLine + "Error : Member function should have a number or a list or a chart then another List after it " + vbNewLine
                    Exit Function
                    End If
            End If
            
Case "Operation", "Relational-Operation":
            SST(0) = isit(SyntaxStart + 1, SyntaxStart + 2)
            If SST(0) = "Number" Or GetValue(SyntaxValue) = "Number" Then
            SST(0) = isit(SyntaxStart + 2, SyntaxStart + 3)
                If SST(0) = "Number" Or GetValue(SyntaxValue) = "Number" Then
                Select Case SSV(SyntaxStart)
                Case "+": SyntaxValue = CDbl(SSV(SyntaxStart + 1)) + CDbl(SSV(SyntaxStart + 2))
                Case "-": SyntaxValue = CDbl(SSV(SyntaxStart + 1)) - CDbl(SSV(SyntaxStart + 2))
                Case "*": SyntaxValue = CDbl(SSV(SyntaxStart + 1)) * CDbl(SSV(SyntaxStart + 2))
                Case "\": SyntaxValue = CDbl(SSV(SyntaxStart + 1)) \ CDbl(SSV(SyntaxStart + 2))
                Case ">": SyntaxValue = CDbl(SSV(SyntaxStart + 1)) > CDbl(SSV(SyntaxStart + 2))
                Case "<": SyntaxValue = CDbl(SSV(SyntaxStart + 1)) < CDbl(SSV(SyntaxStart + 2))
                Case ">=": SyntaxValue = CDbl(SSV(SyntaxStart + 1)) >= CDbl(SSV(SyntaxStart + 2))
                Case "<=": SyntaxValue = CDbl(SSV(SyntaxStart + 1)) <= CDbl(SSV(SyntaxStart + 2))
                Case "=": SyntaxValue = CDbl(SSV(SyntaxStart + 1)) = CDbl(SSV(SyntaxStart + 2))
                Case "<>": SyntaxValue = CDbl(SSV(SyntaxStart + 1)) <> CDbl(SSV(SyntaxStart + 2))
                End Select
                If SyntaxValue = "True" Then SyntaxValue = "#t"
                If SyntaxValue = "False" Then SyntaxValue = "#f"
                    
                    If SST(SyntaxStart) = "Operation" Then
                    SyntaxAna = "Number"
                    ElseIf SST(SyntaxStart) = "Relational-Operation" Then
                    SyntaxAna = "Boolean"
                    End If
                    
                    BuildBody SyntaxStart - 1, SyntaxStart
                    BuildBody SyntaxStart, SyntaxStart + 1
                    SSV(SyntaxStart - 1) = SyntaxValue
                    GoTo en:
                Else
                Form1.Sheet.Text = Form1.Sheet.Text + vbNewLine + "Error : operations must have two numbers" + vbNewLine
                Exit Function
                End If
            Else
            Form1.Sheet.Text = Form1.Sheet.Text + vbNewLine + "Error : operations must have two numbers" + vbNewLine
            Exit Function
            End If
        
Case "Boolean-Operation":
        SST(0) = isit(SyntaxStart + 1, SSB(SBL(BN)) + SyntaxStart + 1)
            If SST(0) = "Boolean" Then
            If SSV(SyntaxStart) = "not" Then
                If SyntaxValue = "#t" Then TempB = False
                If SyntaxValue = "#f" Then temb = True
                BuildBody SyntaxStart - 1, SyntaxStart
                BuildBody SyntaxStart, SyntaxStart + 1
            Else
                SST(0) = isit(SyntaxStart + 2, SSB(SBL(BN)) + SyntaxStart + 2)
                 If SST(0) = "Boolean" Then
                    Select Case SSV(SyntaxStart)
                        Case "and": TempB = (SSV(SyntaxStart + 1)) = (SSV(SyntaxStart + 2))
                        Case "or": TempB = ((SSV(SyntaxStart + 1) = "#t") Or (SSV(SyntaxStart + 2) = "#t"))
                    End Select
                    BuildBody SyntaxStart - 1, SyntaxStart
                    BuildBody SyntaxStart, SyntaxStart + 1
                  Else
                  Form1.Sheet.Text = Form1.Sheet.Text + vbNewLine + "Error : Boolean Operations must have two Boolean" + vbNewLine
                  Exit Function
                  End If
            End If
                If TempB = True Then SyntaxValue = "#t"
                If TempB = False Then SyntaxValue = "#f"
                SyntaxAna = "Boolean"
                SSV(SyntaxStart - 1) = SyntaxValue
                GoTo en:
            Else
            Form1.Sheet.Text = Form1.Sheet.Text + vbNewLine + "Error : Boolean Operations ( except not ) must have two Boolean" + vbNewLine
            Exit Function
            End If
                
Case "List-Function"
            SyntaxValue = "( "
            For j = SyntaxStart + 1 To SSB(SBL(BN)) + SyntaxStart - 1  ' + 1 - 2
                SST(0) = isit(j + 0, j + 1)
                Select Case SST(0)
                    Case "Number"
                    SyntaxValue = SyntaxValue & " " & SSV(j) & " "
                    Case "Chart"
                    SyntaxValue = SyntaxValue & " " & SSV(j + 1) & " "
                    j = j + 1
                    Case "List"
                    For K = j To SSB(SBL(BN - 1)) + j
                    SyntaxValue = SyntaxValue & " " & SSV(K) & " "
                    Next K
                    SyntaxValue = SyntaxValue + ")"
                    Case Else
                    Form1.Sheet.Text = Form1.Sheet.Text + vbNewLine + "Error : List function accept only numbers , charts and lists "
                    SyntaxAna = "Error"
                    Exit Function
                    Exit Function
                End Select
            Next j
            SyntaxValue = SyntaxValue + ")"
            SyntaxAna = "List"
            
            BuildBody SyntaxStart, SyntaxStart
            For j = SyntaxStart To SSB(SBL(BN - 4)) + SyntaxStart - 1
            If SST(j) = "Quate" Then
                    BuildBody j + 0, j + 0
            End If
            Next j
            GoTo en:
            
Case "Null?":
                SST(0) = isit(SyntaxStart + 1, SyntaxStart + 2)
                If SST(0) = "NULL" Then
                SyntaxValue = "#t"
                Else
                SyntaxValue = "#f"
                End If
                SyntaxAna = "Boolean"
                SSV(SyntaxStart - 1) = SyntaxValue
                BuildBody SyntaxStart - 1, SyntaxStart
                BuildBody SyntaxStart, SyntaxStart + 2
                GoTo en:

Case "Length":
            SST(0) = isit(SyntaxStart + 1, SyntaxStart + 2)
                If SST(0) = "List" Then
                    SyntaxValue = SSB(SBL(BN))
                    SyntaxAna = "Number"
                    SST(SyntaxStart - 1) = "Number"
                    BuildBody SyntaxStart, SyntaxStart + SSB(SBL(BN)) + 4
                    SSV(SyntaxStart - 1) = SyntaxValue
                    GoTo en:
                Else
                    SyntaxAna = "Error"
                    Form1.Sheet.Text = Form1.Sheet.Text + vbNewLine & "Error: Length function excpect a List after it"
                    Exit Function
                End If
                
Case "IsMember":
                SST(0) = isit(SyntaxStart + 1, SyntaxStart + 2)
                If SST(0) = "Number" Then
                    SST(0) = isit(SyntaxStart + 2, SyntaxStart + 3)
                    If SST(0) = "List" Then
                        SyntaxAna = "Boolean"
                        SyntaxValue = "#f"
                            For j = SyntaxStart + 4 To SyntaxStart + 4 + SSB(SBL(BN - 1))
                            If SSV(j) = SSV(SyntaxStart + 1) Then SyntaxValue = "#t"
                            Next j
                            BuildBody SyntaxStart - 1, SyntaxStart + SSB(SBL(BN)) + 4
                            SSV(SyntaxStart - 1) = SyntaxValue
                        GoTo en:
                    Else
                    Form1.Sheet.Text = Form1.Sheet.Text + vbNewLine & "Error: Member function excpect a number then a list after it"
                Exit Function
                End If
                Else
                Form1.Sheet.Text = Form1.Sheet.Text + vbNewLine & "Error: Member function excpect a number then a list after it"
                Exit Function
                End If
                
Case "Display":
            SST(0) = isit(SyntaxStart + 1, SyntaxStart + 2)
            If SST(0) = "String" Then
            GoTo en:
            Else
            Form1.Sheet.Text = Form1.Sheet.Text + vbNewLine + ("Error : can only display Strings ")
            Exit Function
            End If
            
Case "If-Stat":
            For i = SyntaxStart + 2 To SSL(0)
                If SST(i) = "Then-Part" Then GoTo FoundThenPart:
            Next i
             GoTo MissingThenPart:
FoundThenPart:
            SST(0) = isit(SyntaxStart + 1, i)
            If SST(0) = "Boolean" Then
                
                If SyntaxValue = "#t" Then If SyntaxAna(4) = "Error" Then Form1.Sheet.Text = Form1.Sheet.Text + vbNewLine + "Error: If statement without then part"
                For K = i To SSL(0)
                If SST(K) = "Else-Part" Then GoTo elsepartfound:
                Next K
                GoTo en:
elsepartfound:  If SyntaxValue = "#f" Then If SyntaxAna(K + 1) = "Error" Then Form1.Sheet.Text = Form1.Sheet.Text + vbNewLine + "Error: in the statement after the else part"
                If 1 = 2 Then
MissingThenPart:             Form1.Sheet.Text = Form1.Sheet.Text + vbNewLine + "Error: If statement without then part"
                             Exit Function
                End If
            Else
            Form1.Sheet.Text = Form1.Sheet.Text + vbNewLine + "Error: after If-statement expect a boolean expration"
            Exit Function
            End If
            
Case "Cons":
        SyntaxValue = ""
        SST(0) = isit(SyntaxStart + 1, SyntaxStart + 2)
        If SST(0) = "Chart" Then
            SST(0) = isit(SyntaxStart + 3, SyntaxStart + 4)
            If SST(0) = "Chart" Then
                SyntaxAna = "List"
                SyntaxValue = "( " & SSV(SyntaxStart + 2) & " . " & SSV(SyntaxStart + 4) + " )"
                GoTo en:
            ElseIf SST(0) = "List" Then
                
                SyntaxValue = "( " + SSV(SyntaxStart + 2)
                For j = SyntaxStart + 5 To SyntaxStart + 4 + SSB(SBL(BN - 1))
                SyntaxValue = SyntaxValue + " " + SSV(j)
                Next j
                SyntaxAna = "List"
                SyntaxValue = SyntaxValue + " )"
                GoTo en:
            Else
            Form1.Sheet.Text = Form1.Sheet.Text + vbNewLine + "Error: after the chart comes another chart or list"
            Exit Function
            End If
            
        ElseIf SST(0) = "List" Then
            Dim TempS3 As String
            For i = SyntaxStart + 2 To SyntaxStart + 3 + SSB(SBL(BN - 1))
                TempS3 = TempS3 + " " + SSV(i)
            Next i
            SST(0) = isit(i, i + 1)
                If SST(0) = "Chart" Then
                SyntaxValue = "( " + TempS3 + " . " + SSV(i + 1) + " )"
                ElseIf SST(0) = "List" Then
                SyntaxValue = "( " + TempS3 + " "
                For j = i + 2 To i + 3 + SSB(SBL(BN))
                SyntaxValue = SyntaxValue + " " + SSV(j)
                Next j
                SyntaxValue = SyntaxValue + " )"
                Else
                Form1.Sheet.Text = Form1.Sheet.Text + vbNewLine + "Error: after the list comes another chart or list"
                Exit Function
                End If
                
        Else
        SyntaxAna = "List"
        Form1.Sheet.Text = Form1.Sheet.Text + vbNewLine + "Error: after cons comes two charts or lists"
        Exit Function
        End If
        GoTo en:
        
Case "Append"
            SyntaxValue = ""
            SST(0) = isit(SyntaxStart + 1, SyntaxStart + 2)
            If SST(0) = "List" Then
            Dim TempS4 As String
            For i = SyntaxStart + 3 To SyntaxStart + 2 + SSB(SBL(BN - 1))
                TempS4 = TempS4 + " " + SSV(i)
            Next i
            SST(0) = isit(i + 1, i + 2)
                If SST(0) = "Chart" Then
                    SyntaxValue = "( " + TempS4 + " . " + SSV(i + 2) + " )"
                ElseIf SST(0) = "List" Then
                SyntaxValue = "(" + TempS4
                For j = i + 3 To i + 3 + SSB(SBL(BN))
                SyntaxValue = SyntaxValue + " " + SSV(j)
                Next j
                SyntaxValue = SyntaxValue + " )"
                Else
                    SyntaxAna = "Error"
                    Form1.Sheet.Text = Form1.Sheet.Text + vbNewLine + "Error: after append comes a list then a charts or a list"
                    Exit Function
                End If
            Else
            Form1.Sheet.Text = Form1.Sheet.Text + vbNewLine + "Error: after append comes a list then a charts or a list"
            Exit Function
            End If
            SyntaxAna = "List"
            GoTo en:
        
Case "Equal?":
            SST(0) = isit(SyntaxStart + 1, SyntaxStart + 2)
            If SST(0) = "List" Then
            
            ElseIf SST(0) = "Chart" Then
            If SSV(SyntaxStart + 2) = SSV(SyntaxStart + 4) Then
            SyntaxValue = "#t"
            Else
            SyntaxValue = "#f"
            End If
            
            ElseIf SST(0) = "Number" Then
            If SSV(SyntaxStart + 1) = SSV(SyntaxStart + 2) Then
            SyntaxValue = "#t"
            Else
            SyntaxValue = "#f"
            End If
            
            ElseIf SST(0) = "Varible" Then
            
            Else
            
            End If
            SyntaxAna = "Boolean"
            GoTo en:
            
Case "Let-Function"
        
        Case "":
    End Select
    
en:
Syn = Syn - 1
    SyntaxValue = Trim(SyntaxValue)
If Syn = 0 Or DebugMode Then Form1.Sheet.Text = Form1.Sheet.Text + vbNewLine & SyntaxValue
If SyntaxAna = "List" Or SyntaxAna = "Chart" Then SyntaxValue = "' " & SyntaxValue
End Function





' This function will check what is the value of the keys thats found in the array
' from the Fist to LaSt ( usally they are X to X + current body length )
' it will depand on syntaxana (int ) if it will face another body

Public Function isit(FiSt As Integer, LaSt As Integer) As String

For K = FiSt To LaSt
    Select Case SST(K)
    Case "Open-L"
    BN = BN + 1
    SST(0) = SyntaxAna(K + 1)
    isit = SST(0)
    
    If Mid(SSV(K), 1, 1) = "'" Then
    SSV(K) = "'"
    SST(0) = "Chart"
        If SSV(K + 1) = "(" Then SST(0) = "List"
    ElseIf Mid(SSV(K), 1, 1) = "#" Then
    SSV(K) = SyntaxValue
    SST(0) = "Boolean"
    Else
    SSV(K) = SyntaxValue
    SST(0) = GetValue(SyntaxValue)
    End If
    'isit = SyntaxAna(k + 1)
    'SSV(k) = SyntaxValue
    'If Mid(SSV(k), 1, 1) = "'" Then SSV(k) = "'"
    'SST(0) = GetValue(SyntaxValue)
    'BuildBody k + 1, k + SSB(SBL(BN))
    Exit Function
    Case "Open-R"
    isit = "Error: Something is missing"
    Case "Quate"
    BN = BN + 1
        If SST(K + 1) = "Open-L" And Not SST(K + 2) = "Open-R" Then
            isit = "List"
        ElseIf SST(K + 1) = "Open-L" And SST(K + 2) = "Open-R" Then
            isit = "NULL"
        Else
            isit = "Chart"
        End If
        Exit Function
        
    Case "Number"
    isit = "Number"
    Exit Function
    
    Case "Boolean"
    isit = "Boolean"
    SyntaxValue = SSV(K)
    Exit Function
    
    Case "String"
    isit = "String"
    Exit Function
    
    Case eles: isit = "Error"
    End Select
Next K

End Function



' build body is used to rearrange the sst array , when the inner body value is found
' it is replaced by its value and this function will be called to take the rest
' array index into the empty space ( it will calculate the empty space as the last
' body length - 1 ( for the replaced value ) ) ( long note for small function ... funny things can happend ! )

Function BuildBody(FiSt As Integer, LaSt As Integer)
    
    For j = FiSt To SSL(0) - 1
        SSV(j) = SSV(j + 1 + (LaSt - FiSt))
        SST(j) = SST(j + 1 + (LaSt - FiSt))
    Next j
    
    If SSL(0) > FiSt Then SSL(0) = SSL(0) + (FiSt - LaSt - 1)

End Function




Function isNumber(ByVal strString As String) As Boolean
Dim i As Integer

For i = 1 To Len(strString)
    
    If Asc(Mid(strString, i, 1)) < Asc("0") Or Asc(Mid(strString, i, 1)) > Asc("9") Or Asc(Mid(strString, i, 1)) = Asc("-") Then
        isNumber = False
        Exit Function
    End If
    
Next i

isNumber = True
End Function



as you can see if you don't have a VB6 IDE or not familiar with VB codes you might find it hard to read it in here

any way now i wrote the idea of the code and the code itself, sometimes the idea is right but the code itself is wrong, sometimes your idea is wrong but some how the code is written in a wrong way and it is working (for a god sake advice if this the case delete the code and write another one), i am thankful for your time reading my long post and i would like to hear what do you think, about the idea the principle of the code and the code itself, if you wanna blame me because the code is too long and it should be less by 5 lines because someone wrote it shorter then this by 5 lines, or if you didn't like the idea and think you can say something funny then please send it to me as a private message.

if you think the whole idea is wrong or you didn't like idea itself then feel free to tell us about that, if you have a project and wanna use this idea feel free to do so but please don't just copy the code and say it was your work.

thanks for your time and hope you will like it

This post has been edited by Cookiesliyr: 14 March 2010 - 09:14 AM


Is This A Good Question/Topic? 0
  • +

Replies To: The String Separator iinterpreter

#2 Cookiesliyr  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 136
  • Joined: 16-May 09

Re: The String Separator iinterpreter

Posted 09 May 2010 - 11:55 PM

===================================================================

Hello again ! sorry I haven't replay in here for this long, i have two reasons, I though some one would like to add something or will suggest something and the other reason actually I was busy preparing to travel to the USA to study in EWU in Washington ! I am a university student again ... and it took me some time to settle in here and solve the endless problems river.

So, I have done today 3 functions and they are more efficient then before because in before I wasn't sure what am I doing. The class will be use static methods so you won't have to make an object for the string separator, the functions and variables will be all private except one which will take the string and pass it to all the functions to interpreted but for now for testing sake they are all public so I can check if the result is the right one or not In an easy way, as i said i will make in in C# this time.

the code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace String_Separator{
    class StringSeparatorC{

        public static string [,] vvs = new string [100,2];  // this array will save the lexes

        // this guy will add spaces after and before some charcters 
        public static string spaceDealer(string S){
            string sTem = "";

            for (int i = 0; i < S.Length ; i++){
                switch (S.Substring(i, 1))
                {
                    case "+": case "-": case "*": case "\\": case "{" : case "}" : case "(" : case ")":
                        if (i != 0) { if (S.Substring(i-1,1) != " " ){sTem += " " ;}} 
                        if (S.Substring(i + 1, 1) != " ") {sTem += S.Substring(i, 1) + " ";}
                    break;
                    default :
                    sTem += S.Substring(i, 1);
                    break;
                }
            }
            return sTem;
        }

        // this guy will save the lexims in their array, the loop depends on the spaces
        public static void stringSeparator(string S){       
            int vvsp = 0;
            int j;
            for (int i =0 ; i < S.Length  ; i++){
                j = 0;
                while ((i < S.Length - j + 1) && (!S.Substring(i, j).Contains(" "))){
                    j++;
                } 
                vvs[vvsp, 0] = S.Substring(i, j - 1);
                vvs[vvsp, 1] = getType(vvsp);
                vvsp++;
                i = i + j- 1;
            }
        }

        // just to get the meaning of the lex for the interpreter
        public static string getType (int i){
            string myType ="not-found";     // if it haven't been identified then it is unknow
            double isDouble ;               // for checking if it is a real number
            int isInt;                      // for checking if it just a number

            switch (vvs[i,0]){      // key words list, it will increase in time
                case "+" : case "-": case "*": case "\\":
                return "math-operation";
                case "(": return "open-B";
                case ")": return "close-B";
                case "{": return "open-C";
                case "}": return "close-C";

            }
            
            if (vvs[i, 0].Substring(0, 1) == "\""){ // for checking if it is a Strings
                if (! vvs[i,0].Substring(1,vvs[i,0].Length-2).Contains ("\""))
                    if (vvs[i,0].Substring(vvs[i,0].Length-1,1) == "\"")
                        return "string";
            }

            if (int.TryParse(vvs[i, 0], out isInt))
                return "number";

            if (double.TryParse(vvs[i, 0], out isDouble))
                return "real-number";
                
            return myType;
        }

        public static string syntaxAna(){
            // soo ... what next ?
        }
    }
}



here i will stop today and i will think about how will i make the syntaxAna ? i might let the interpreter accept more then one line at once so i can let it save some variables and make it more complex then before or i might keep it as it is interpret only one line but it can save and destroy variables i believe if i wanna make it accept more then one line and make it more complex i better do it in a different way .. you know what do i mean.

the syntaxAna can be like this
public static string syntaxAna(){
    switch (vvs[0,1){
        case "number":
            // make more switches 
        break;
        case "if-stat": //it is not ready yet but just an example
            // check for Boolean expressions 
    }
}



i believe i will do it like this but i would like to give me your opinion first, sorry it took me that long to replay i hope i will get you the news soon
Was This Post Helpful? 0
  • +
  • -

#3 Raynes  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 611
  • View blog
  • Posts: 2,815
  • Joined: 05-January 09

Re: The String Separator iinterpreter

Posted 10 May 2010 - 10:51 AM

They tried to get you to build a Common Lisp interpreter? Holy shit. I assume they didn't expect it to meet the standards right? The CL spec is HUGE. Even I can't bring myself to read that thing.
Was This Post Helpful? 0
  • +
  • -

#4 Cookiesliyr  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 136
  • Joined: 16-May 09

Re: The String Separator iinterpreter

Posted 10 May 2010 - 12:48 PM

well not all the methods, and my old interpreter wasn't complete too i cold not do allot of functions and some functions weren't work completely like the addition in common lisp can be like this

(+ 1 2 3 4 5 6)

i just made it to add two numbers and i lost some grades on that XD but that project is over long time ago and i wanna now to make a better interpreter in C# which we can use as a script language for our bigger projects and i will use the same concepts of my old one but i wanna improve it before i start working on it, so it is not about Common lisp anymore, and by the way thanks for passing by and writing some comments Raynes :)

i have noticed that my new spaceDealer() doesn't delete the extra space i will fix that, but i came here to talk about the syntaxAna(), as you can see my old syntaxAna() function have a big nested If Statements and allot of elses and returns in the middle and some time it drive the reader mad XD so what i am thinking to do is to make ... an array of classes instead !

i will create a class which have two virtual function and i need to pick a good name for it ... lets say ... expressionsClass, then i will make for each expressions have its own class which will Inherit this class and override the method to check if the elements are valid (the lexs) and then execute and return the value to the syntaxAna ... so the methods name are checkStatement() and executeStatment()

or should i use interfaces some how to do that ? i prefer not

so the code will be simlier to this inside the syntaxAna()

foreach (expressionsClass exp in expList ){
    if (exp.checkStatement()){
        return exp.executeStatment(paremeters);
    }
}



but before that we have to defined each class separately one for each statement one for math operations for example one for If-else , one for switch and so on.

this will make the interpreter more general and we can add more statements without messing around with the nested loop but the question is ... is there a way to let me add more function to the interpreter without changing the code ? i have no idea how to make an up grade files so far and i am trying to find out how, this is my idea for the syntax analyzes but i hope if any one interested and suggest a better way to do that.

one of the expert guys told me it is better to use Hashing instead of multiple array for faster executing and i can use a structures which hold two strings and put them in one array instead of using the multiple array too, i will take a good look in them and i will change the earlier code.

This post has been edited by Cookiesliyr: 10 May 2010 - 12:49 PM

Was This Post Helpful? 0
  • +
  • -

#5 Cookiesliyr  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 136
  • Joined: 16-May 09

Re: The String Separator iinterpreter

Posted 17 May 2010 - 11:48 AM

Hi again,

Sorry for being late i was busy with some other stuff and when i post the last post i didn't test the code and it has allot of bugs, i have made allot of changes and successfully made one exp which work, i will try to add more later one and test if the the syntax analyzer will work fine or it will mess all the story, so i will repeat myself again and describe the functions again so you don't have to go back and read about them.

First of all i have a new class file called VVS and it is the one who will take control of all Lexs for the interpreter and i moved the getType() function into it and made it private so the code it :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace String_Separator
{
    public class VVS{
        // static fileds
        public static VVS[] lexAr = new VVS[100];  // this array will save the lexes

        public static int P1 = -1 ;
        public static int Length = 0 ;

        public static void setLength(int x) { Length = x; }
        public static int getLength() { return Length; }
        
        
        // non static fileds
        public string lValue;       //lexim value
        public string lType;        //lexim type

        public void setData(string lexValue){
            lValue = lexValue;
            lType = getType(lValue);
            Length++;
        }

        public static void fixArray(int first, int last, string newLex) {
            VVS.lexAr[first] = new VVS();
            VVS.lexAr[first].setData(newLex);      // this has made a logic error it added 1 to the length
            for (int i = 1; i < Length; i++) {
                lexAr[i + first] = lexAr[i + last ];
            }
            Length -= (last - first) + 1 ;  // - 1 for the new VVS created
        }
        // a helper method that get the meaning of the lex
        private static string getType(string S) {
            string myType = "not-found";     // if it haven't been identified then it is unknow
            double isDouble;               // for checking if it is a real number
            int isInt;                      // for checking if it just a number

            switch (S) {      // key words list, it will increase in time
                case "+":
                case "-":
                case "*":
                case "\\":
                    return "math-operation";
                case "(": return "open-B";
                case ")": return "close-B";
                case "{": return "open-C";
                case "}": return "close-C";

            }

            if (S.Substring(0, 1) == "\"") { // for checking if it is a Strings
                if (!S.Substring(1, S.Length - 2).Contains("\""))
                    if (S.Substring(S.Length - 1, 1) == "\"")
                        return "string";
            }

            if (int.TryParse(S, out isInt))
                return "number";

            if (double.TryParse(S, out isDouble))
                return "real-number";

            return myType;
        }
    }
}



The fixArray() will be invoke from the ExpC() functions when it get a new lex, so it will send to the fixArray() the new lex to replace the old lexs which will be decided from the first lex to the last one and it will adjust the array for this purpose ... for example if the ExpC() have made a math operation on this code

1 + 4 * 5 = multiplication => 1 + 20

Here the ExpC() have a new lex which is 20 but it need to remove the three lexs "4 * 5" so it will invoke fixArray(2,4, "20") so yes the fixArray() will make the array adjustment in the way we want it to be, this one will be so useful when i start working on bodies and make the analyzer work recursively with isit() and i might make some adjustment if needed

The other class file is the interpreter file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace String_Separator{
    class StringSeparatorC{

        // this method will initialize some varibles it should called before it is used 
        public static void initialize() {
            ExpC.expAr[0] = new MathOp();
        }

        public static string process(string S) {
            stringSeparator(spaceDealer(S));
            return syntaxAna();
        }

        // this guy will add spaces after and before some charcters 
        public static string spaceDealer(string S){
            string sTem = "";
            S = S.Trim();

            for (int i = 0; i < S.Length ; i++){
                switch (S.Substring(i, 1))
                {
                    case " ":
                        if (S.Substring(i + 1, 1) != " " ) { sTem += " "; }
                    break;
                    case "+": case "-": case "*": case "\\": case "{" : case "}" : case "(" : case ")":
                        if (i != 0) { if (S.Substring(i-1,1) != " " ){sTem += " " ;}}
                        sTem += S.Substring(i, 1);
                        if (S.Substring(i + 1, 1) != " ") {sTem +=  " ";}
                    break;               
                    default :
                    sTem += S.Substring(i, 1);
                    break;
                }
            }

            return sTem;
        }

        // this guy will save the lexims in their array, the loop depends on the spaces
        public static void stringSeparator(string S){       
            int vvsp = 0;
            int j;
            for (int i =0 ; i < S.Length  ; i++){
                j = 0;
                while ((i < S.Length - j + 1) && (!S.Substring(i, j).Contains(" "))){
                    j++;
                }
                VVS.lexAr[vvsp] = new VVS();
                VVS.lexAr[vvsp].setData(S.Substring(i, j - 1));
                vvsp++;
                i = i + j- 1;
            }
        }

        // we will pass our first lex to all the expersion and see which one is the right one
        // if we catched the kind of expersion type we will pass lex array and excute the code
        public static string syntaxAna(){
            foreach (ExpC exp in ExpC.expAr) {
                if (exp.checkV(VVS.lexAr[0])) { return exp.executeS(); }
            }
            return "Error";
        }
    }
}



The initialize() is needed to be called before the interpreter can be used

The process() function is the only one should be public and the other as public but for the testing sake i made all of them public, process() is the one which will be called from the outside world and send the string which the user want to interpreter whiten this function.

The spaceDealer() will fix and code spaces and add or remove them in a way that the interpreter can understand (or the stringSeparator() is the one who will use it)

The stringSeparator() is the one who will take each string and save is as a lex in the lexArray.

The syntaxAna() will pass the first lex to all the ExpC() and see which one can catch this lex and make this operation, so for example if we have this code

if ( x = 25 ) then y = 20 else y = 10

So what will the syntaxAna() do ? it will take the first lex (if) only and send it to an array of expressions and see which one can deal with it, if it catch the right class of expressions it will check which way of expressions is the code by using a list of isit()

So ... after killing you of boredom or lost you with confusion how about to see the expressions class file ?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace String_Separator{

    abstract class ExpC{
        public static ExpC[] expAr = new ExpC[1];

        public abstract Boolean checkV(VVS firstLex); // this will check if this is the class we need
        public abstract string executeS();   // this will applay the formulas 

        protected static string isit (VVS lex){
            if (lex == null)
                return "null";

            switch (lex.lType){
                case "string": case "math-operation":
                    return lex.lType;
                case "number": case "real-number":
                    return "number";
                //case "(": return "open-B";
                //    return StringSeparatorC.syntaxAna(VVS.body()); // to do later
                default :
                return "unkown";

            }
        }
    }

    class MathOp : ExpC{
        public override Boolean checkV(VVS firstLex) {
                return isit(firstLex) == "number";
        }

        public override string executeS() {

            for (int i = 1; (i < VVS.getLength()) && (VVS.getLength() != 1) ; i += 2) {
                if ((isit(VVS.lexAr [i]) == "math-operation") && (isit(VVS.lexAr[i +1]) == "number"))
                {
                    switch (VVS.lexAr[i].lValue){
                        case "*" : case "\\":
                            if (VVS.lexAr[i].lValue == "*")
                                VVS.fixArray(i - 1, i + 1, "" + ((Int32.Parse(VVS.lexAr[i - 1].lValue)) * Int32.Parse(VVS.lexAr[i + 1].lValue)));
                            else if (VVS.lexAr[i].lValue == "\\")
                                VVS.fixArray(i - 1, i + 1, "" + ((Int32.Parse(VVS.lexAr[i - 1].lValue)) / Int32.Parse(VVS.lexAr[i + 1].lValue)));
                               i -= 2;
                        break;
                    }
                }
                else {
                    return "Error in the first run";
                }
            }

            for (int i = 1; (i < VVS.getLength()) && (VVS.getLength() != 1); i += 2) {
                if ((isit(VVS.lexAr[i]) == "math-operation") && (isit(VVS.lexAr[i + 1]) == "number")) {
                    switch (VVS.lexAr[i].lValue) {
                        case "-":case "+":
                            if (VVS.lexAr[i].lValue == "-")
                                VVS.fixArray(i - 1, i + 1, "" + ((Int32.Parse(VVS.lexAr[i - 1].lValue)) - Int32.Parse(VVS.lexAr[i + 1].lValue)));
                            else if (VVS.lexAr[i].lValue == "+")
                                VVS.fixArray(i - 1, i + 1, "" + ((Int32.Parse(VVS.lexAr[i - 1].lValue)) + Int32.Parse(VVS.lexAr[i + 1].lValue)));
                            i -= 2;
                            break;
                    }
                }
                else {
                    return "Error in the secound run";
                }
            }    

            return VVS.lexAr[0].lValue ;
        }   
    }
}




The ExpC Class is an abstract class which we can use to implement all expressions classes, it has two Functions the

    public abstract Boolean checkV(VVS firstLex); 
    public abstract string executeS();   



The checkV() will check is this class (the children class) is the right class or not, if yes the syntax analyzer will use it to execute the function by executeS()

You can copy the code in 3 different files and make another one to use them as the GUI for the user and send the text to the process(string S) function and it will return the result as a string so you ... now if you -hopefully- understood the interpreter how does it work you can make your own expressions and we can all use these expressions by only copying the class code, but we need to add the keys to the interpreter inside the VSS class first as well

I hope you like what you have read so far and tell me if you liked it, didn't liked it or think there is better ways to do these stuff and this is the most important, what is missing so far is to let this interpreter deal with bodies "( )" and save variables in memory and let isit() recognize it and add more expressions ..... it is seems allot XS

Thanks for your time and hope it will help anyone to get an idea to be reused or if any one will like to use it in his programs (it can be used for games yes) feel free to do so but please leave a note that this code has been written by me that's all what i asks.
Was This Post Helpful? 0
  • +
  • -

#6 Cookiesliyr  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 136
  • Joined: 16-May 09

Re: The String Separator iinterpreter

Posted 13 June 2010 - 01:35 PM

hello again :)

hope everyone is fine, it is summer vacation for some and summer school for other ... for me it is summer troubles >_> i don't wanna tell you my life story this time so lets get straight to what have i done to my project so far, if you don't know what i am talking about please read the last post.

1) SpaceDealer problems have gotten take care of, i don't think i will have any problem with this guy anymore - i hope -___-" -

2) i add a new class BodyBlock this guy will save the info to take care of bodies in the code just like the VVS -brand new-

3) i add a new method in StringSeparatorC which is BodyBuilder(), it will build the bodies for us, i adjusted this code allot and it made allot of troubles for me.

4) i change all the return type that deals with the VVS into VVS objects Rather then string , i was returning a String that represent either the value or the type of the VVS just like my older design of the interpreter and it made allot of logic errors behind the scene but thanks god if found it in a short time, the modified functions are executeS(), isit(),spaceDealer(),stringSeparator() and syntaxAna()

5) i made the getType() function mange the interpreter errors, so any error happen in anywhere it will assign the error to the VVS , when the process() run it, it will check the VVS which the syntaxAna() return if it is "success" it will return the result if not it will return the error message.

6) i made the MathOp class to have one loop rather then two and changed the way which the ExpC works, instead of letting it deal with the VVS from A to Z it will work on one array from First to Last ... why ? so i can make it work in recursion way, so the executeS() now will take two int variables (first,last) which represent the scope of the execute.

7) in isit() when it find an "open-B" it will first delete the "(" and ")" by finding there location from the BodyBlock (which hold all the body info) and send them to the fixArray() function which lives in the VVS to delete them and shift the lexArray to remove the gap, after that it will increment the BN (body number) by one that indicate we have just entered a body so any thing which will work with bodies will know which body are we working on now, after that it will return a VVS which contain the result of the body ... how ? for example if we have this code

2 + 4 * 2 * ( 2 + 2 )

when the MathOp see the "+" it will ignore it in the first run because we need to deal with the higher Priority operations first, next it will see the "*" which we should execute so the result will be

2 + 8 * ( 2 + 2 )
^
here the pointer is still in it's position but because we made an operation we will decrement the for loop pointer by two -which is i in this case-

2 + 8 * ( 2 + 2 )
^
here we have found another operation which is "8 + (" but "(" is not a number -yet- what will isit() do -the method which responsible to detect the lexems types- well call another syntaxAna with the body scope to determined the resulting lex and return it to the previous syntaxAna as a new lex

2 + 2
== > 4

2 + 8 * 4
^

here the pointer haven't changed it position yet because all of that happened in the if statement

if ((isit(VVS.lexAr[i]).lType == "math-operation") && (isit(VVS.lexAr[i + 1]).lType == "number")){



when it find that second lex is an "math-operation" it will return the lex type from isit() only, but when the isit() see a new body it will call a new syntaxAna () to get a new lex and it will check it type to see if it is the right one to deal with or not

8) in the fixArray() in the VVS class has changed to shift the bodies location if they are after the the deletion

for example -our old example- when the first operation occurred

2 + 4 * 2 * ( 2 + 2 )
== > 2 + 8 * ( 2 + 2 )

the bodies still point to their old location and this gave me some headache before i could figure out how to solve

for (int i = BodyBlock.getBN(); i < BodyBlock.bodyAr.Length; i++) {
    if (last -1 < BodyBlock.bodyAr[i].getFirst())
         BodyBlock.bodyAr[i].setBodyBlock(BodyBlock.bodyAr[i].getFirst() - (last - first), BodyBlock.bodyAr[i].getLast() - (last - first));
}



9) i made some miner changes to some variables because they were making some logical errors that they work half of the time and doesn't work on the other half of time, and some of them i rearrange them as well, i can't remember all of them and i have no intention to mention them as well ...

so this time instead of posting the code let me attach it so you can test it -it is written in C#-, if you like to see the code or you don't have the C# or it didn't work and want from me to post it just replay in here and i will post it in here


=====================================

some stuff that bugs me
1) when i enter mixed bodies like this
( 2 + ( 3 +4 ) * 3 ) + 4 + ( 5 * 3 )

it gives me an error, i don't see any mistake in my paper design and the code seems clean but i have to debug it -aggaaaaaain- and see what's the problem ... i hope it is a minor problem because if it is a huge problem i can't change the design or the code in one night, it took me allot of time to adjust both the design and the code for the recursive problem already and i hope i won't need to get back to version 1.0.1 and start over all the work

2) i haven't made much of functions yet because -ExpC- because i want to make sure the the bases are working 100% correctly not even 99.9% is accepted because if there is a problem in the Spacedealer() for example it will make tons of problems while the code proceeds in, i have to make sure first that all the guys are working fine first before i improve the MathOp class to let it deals with some more complex operations like Min() Max() Avg() SQL() Con() Sin() Tan() and the power operation ^ and the mod %, not only that it will deal with Boolean expressions as well and return true or false, so its name might changed later on to more general things and when it find a math operation it will call the MathOp class, if it finds a boolean operation it will call the BoolOp class within inside this general.

3) i haven't made any try catch because i have -once again- to make sure that everything works fine before i make some exception handling, what happens if i made some try and catch that they might hide some logic errors from me and that is not what i want it to happened, so if you download the interpreter and try it out be patient with it. it will fire allot of exceptions and close the program on you.

4) for some people who doesn't know what is going on i am sure they are thinking (why is he making all this disruption just to make a calculator) .... yes i am making a complex calculator ... NOOOOOOOOOOoooooooo i am not making a calculator people, the MathOp is one of the bases of my interpreter which will deal with all the math operations in my codes for example when you write such a code

if ( M < (P * 2) 1 ) {
.... 



or

for ( int i = X + 3 * Y ; i < Z ; i++){
...



so yes these things are expected to be solve within the interpreter and i have to make sure it works 100% correct before i can publish my interpreter and make it useful

5) how can i let the programmers update the ExpC.cs file which hold all the ExpC classes without messing up with the exe file or anything else, for example if it is in java i will simply replace the class file and it will work, but in here it is a different story, after i make a setup for this interpreter and have an .exe file which hold the program how can i make an update for it without changing anything i just want be able to change the ExpC.cs file and make the .exe works find, my syntaxAna() make a check for all available ExpC classes and add them into an array and pass the first lexem to them to check which class can work with it, i made all this fuss just to make sure i can update my interpreter with new ExpC class in an easy way, but so far i haven't found the solution for that.


Thank you for your reading and hope if this project is helpful for anyone and if there is any suggesting or comment please feel free to do so

Attached File(s)


This post has been edited by Cookiesliyr: 13 June 2010 - 01:44 PM

Was This Post Helpful? 0
  • +
  • -

#7 FreezingHazard  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 148
  • Joined: 22-February 10

Re: The String Separator iinterpreter

Posted 13 June 2010 - 02:31 PM

Well long azzss post man.

But I couldn't read it all that'd be like a book to me.

I did read enough, however..

Anyhow if you want I got an interpreter in about 30 lines of code, it starts at 3k lines of code, then went to 1.5k lines of code after a revision, now another revision it's at 30 lines and has more functions than the 3k lined original.

Don't ask me how I got it down to about 30 lines, but I did and all it took was some brain work.

The interpreter is extremely, lightweight, I mean "light".

It's in C#..
Was This Post Helpful? 0
  • +
  • -

#8 Cookiesliyr  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 136
  • Joined: 16-May 09

Re: The String Separator iinterpreter

Posted 13 June 2010 - 07:10 PM

thanks for advise in advance ... but wander how will ur 3k line code will help anyone in this topic ... and what does it do ? is it more powerful than this one ? explain how does it work then , and why is this interpreter is way too long ?

if you can't read all the post and you will judge on what have your read so far then i am not sure what i can say about that ... it is your problem you don't want to see what it can do and yet your are comparing it with something that i can't see in your post ...

btw the posts contain different versions of my old interpreter and the new one and in every post it's version code, so yes the finial version is the attached file only btw ...

i am writing this hopping to see some one who can use it or give some advise how i can improve it so it can be useful so if you see my interpreter is not that useful because it is too long (i believe it didn't pass 400 lines yet with spaces and comments) i really wish to see what's wrong in it because i am just a noob and you are Mr Expert and i am seeking your knowledge so yes i want to see the 30 lines of code interpreter of yours please and please post it in here too

This post has been edited by Cookiesliyr: 13 June 2010 - 07:13 PM

Was This Post Helpful? 1
  • +
  • -

#9 Cookiesliyr  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 136
  • Joined: 16-May 09

Re: The String Separator iinterpreter

Posted 25 June 2010 - 02:35 PM

well hi again everyone, sorry for the last angry replay and i apologize to FreezingHazard because i misunderstand him i though he was making fun of me, but i didn't realize that he was trying to help with a short replay and i read his replay in a very bad time and i wrote the replay without thinking, so i am sorry again for everything i said back.

i only wish if i can see what's wrong within my code if it is really that bad how can i write it in a better way, reminding you i am still designing the program ... actually i was using two design one for my old String Interpreter which was design on VB6 and i made a new one in fast for the C# with the new ideas, and because the build was based on other design without being careful i wrote and draw it with the new features on the old design that's why it was full with logic error ... i should remove the old design from my head and draw a complete a new one

well to be honest i haven't start on writing a new one yet that's why i haven't work on this project for a while but i fixed some bugs and you can say this is version 1.0.04 i will try to write a new design for it before i start working on it again

the version history is inside the code as a comment at the start of every .cs file

thanks for your time,
best regards.

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#10 Cookiesliyr  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 136
  • Joined: 16-May 09

Re: The String Separator iinterpreter

Posted 15 August 2010 - 08:37 AM

hi everyone,

i just finally finished my summer school and i would like to submit my finial version for SSI, i have add four new functions and a help menu to see the functions syntax, still i couldn't compile the ExpC class as dll file so i can add new SSI functions without the need for new install.

the SSI itself is not useful now but if i can use it as a part of my other projects it can be useful, but maybe it is better to learn how to make a parsing tree and make the job in more professional way ... i am not sure which is better in my case any way i hope if you can give it a try and tell me about it.

thanks for all the support and the emails i got from you about the SSI
best regards

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1