Parser example and question

Started by Frank Brübach, June 25, 2024, 11:47:09 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Frank Brübach

Hello Charles..

Perhaps you can Help to Fix my freebasic Translation to oxygen. My freebasic example is running fine.

I am Not quite Sure how to handle String Tokens() and my Error or Problem zones I have marked in my o2 example. Thanks in advance, frank


' test of my parser example of freebasic (its running fine there)
' to oxygen by frank bruebach, 24-06 + 25-06-2024
'
uses console

' test -------------------------
dim tokens() as string
Dim token As String = ""
Dim tokenCount As Integer = 0 '
ReDim string tokens(tokenCount) '
' test -------------------------

printl "ok"   


' Split() function ' may be an error inside cause of tokens() ?
Function Split(string inputs, delimiter, tokens() ) As Integer
    Dim token As String = ""
    Dim tokenCount As Integer = 0
    int i
    For i = 1 To Len(inputs)
        If Mid(inputs, i, 1) = delimiter Then
            If token <> "" Then
                'ReDim Preserve tokens(0 To tokenCount)
                ReDim string tokens(tokenCount) 'redim string s(n)
                tokens(tokenCount) = token
                tokenCount += 1
                token = ""
            End If
        Else
            token += Mid(inputs, i, 1)
        End If
    Next
    If token <> "" Then
        ReDim string tokens(tokenCount)
        tokens(tokenCount) = token
    End If

    Return tokenCount
End Function

printl "ok2"

' IsNumeric() function ok
Function IsNumeric(string value ) As Integer
    Static string numericChars
    numericChars = "0123456789.-"
    If Len(value) = 0 Then Return False
    int i
    int dotCount = 0
    For i = 1 To Len(value)
        string c = Mid(value, i, 1)
        If InStr(numericChars, c) = 0 Then
            Return False
        ElseIf c = "." Then
            dotCount += 1
            If dotCount > 1 Then
                Return False
            End If
        End If
    Next

    Return True
End Function

printl "ok3"

' new build in: EvaluateExpression function
' error inside split() functions
'
Function EvaluateExpression(string inputs) As Double
    Dim tokens() As String

'------------------ problem zone 1 ------------ //
    Split(inputs," ", tokens())
'------------------ problem zone 1 ------------ //

    Dim total As Double = 0
    Dim currentNumber As Double = 0
    Dim operators As String = "+"
    int i
    For i = 0 To UBound(tokens)
        Dim token As String = tokens(i)

        If IsNumeric(token) Then
            currentNumber = Val(token)
            If operators = "+" Then
                total += currentNumber
            ElseIf operators = "-" Then
                total -= currentNumber
            ElseIf operators = "*" Then
                total *= currentNumber
            ElseIf operators = "/" Then
                If currentNumber <> 0 Then
                    total /= currentNumber
                Else
                    Print "Error: Division by zero"
                    Exit For
                End If
            End If
            operators = "+"
        Else
            operators = token
        End If
    Next

    Return total
End Function

printl "ok4"


' Test my code with different inputs
Dim inputs As String = "2 + 3 * 10" ' 50 result ok
Dim result As Double

'------------------------ problem zone 2 ------------- //
' result = EvaluateExpression(inputs) ' error
'------------------------ problem zone 2 ------------- //

' all tests are running fine here for calculations
' + - * /
' Test my code with different inputs
' Dim inputs As String = "2 + 3 * 10" ' 50 ok
' Dim inputs As String = "5 + 6 + 10" ' 21 ok
' Dim inputs As String = "8 * 5" ' 40 ok
' Dim inputs As String = "20 / 4 " ' 5 ok
' Dim inputs As String = "-20 10" ' -10 ok
' Dim inputs As String = "-10  20" ' ok
 Dim inputs As String = "30 -20" ' 10 ok 


If result <> 0 Then
    Printl "The total is " + result
Else
    Printl "Error: Invalid input"
End If

printl result 'test was 0

' Wait for a key press before closing the console
Printl "Press any key to continue..."
'Sleep
wait
'While Inkey <> "" : Wend

print "ok5"

Charles Pegge

Hi Frank,
I have marked my changes '***'

' test of my parser example of freebasic (its running fine there)
' to oxygen by frank bruebach, 24-06 + 25-06-2024
'
uses console
indexbase 0 '*** same as freebasic

' test -------------------------
'dim tokens() as string '*** comment out
Dim token As String = ""
Dim tokenCount As Integer = 0 '
ReDim string tokens(tokenCount) '
' test -------------------------

printl "ok"   


' Split() function ' may be an error inside cause of tokens() ?
Function Split(string inputs, delimiter, *tokens(redim) ) As Integer '***  *tokens(redim)
    Dim token As String = ""
    Dim tokenCount As Integer = 0
    redim string tokens(100) '*** temp amount
    int i
    For i = 1 To Len(inputs)
        If Mid(inputs, i, 1) = delimiter Then
            If token <> "" Then
                'ReDim Preserve tokens(0 To tokenCount)
                'ReDim string tokens(tokenCount) 'redim string s(n) '*** take out
                tokens(tokenCount) = token
                tokenCount += 1
                token = ""
            End If
        Else
            token += Mid(inputs, i, 1)
        End If
    Next
    If token <> "" Then
        ReDim string tokens(tokenCount) 'trims excess elements
        tokens(tokenCount) = token
    End If

    Return tokenCount
End Function

printl "ok2"

' IsNumeric() function ok
Function IsNumeric(string value ) As Integer
    Static string numericChars
    numericChars = "0123456789.-"
    If Len(value) = 0 Then Return False
    int i
    int dotCount = 0
    For i = 1 To Len(value)
        string c = Mid(value, i, 1)
        If InStr(numericChars, c) = 0 Then
            Return False
        ElseIf c = "." Then
            dotCount += 1
            If dotCount > 1 Then
                Return False
            End If
        End If
    Next
    Return True
End Function

printl "ok3"

' new build in: EvaluateExpression function
' error inside split() functions
'
Function EvaluateExpression(string inputs) As Double
    'Dim tokens() As String '*** comment out

'------------------ problem zone 1 ------------ //
    Split(inputs," ", tokens()) '*** ok now
'------------------ problem zone 1 ------------ //
    Dim total As Double = 0
    Dim currentNumber As Double = 0
    Dim operators As String = "+"
    int i
    For i = 0 To UBound(tokens)
        Dim token As String = tokens(i)

        If IsNumeric(token) Then
            currentNumber = Val(token)
            If operators = "+" Then
                total += currentNumber
            ElseIf operators = "-" Then
                total -= currentNumber
            ElseIf operators = "*" Then
                total *= currentNumber
            ElseIf operators = "/" Then
                If currentNumber <> 0 Then
                    total /= currentNumber
                Else
                    Print "Error: Division by zero"
                    Exit For
                End If
            End If
            operators = "+"
        Else
            operators = token
        End If
    Next

    Return total
End Function

printl "ok4"


' Test my code with different inputs
Dim inputs As String = "2 + 3 * 10" ' 50 result ok
Dim result As Double

'------------------------ problem zone 2 ------------- //
result = EvaluateExpression(inputs) ' error '*** uncommented, ok now
'------------------------ problem zone 2 ------------- //

' all tests are running fine here for calculations
' + - * /
' Test my code with different inputs
' Dim inputs As String = "2 + 3 * 10" ' 50 ok
' Dim inputs As String = "5 + 6 + 10" ' 21 ok
' Dim inputs As String = "8 * 5" ' 40 ok
' Dim inputs As String = "20 / 4 " ' 5 ok
' Dim inputs As String = "-20 10" ' -10 ok
' Dim inputs As String = "-10  20" ' ok
 Dim inputs As String = "30 -20" ' 10 ok


If result <> 0 Then
    Printl "The total is " + result
Else
    Printl "Error: Invalid input"
End If

printl result 'test was 0

' Wait for a key press before closing the console
Printl "Press any key to continue..."
'Sleep
wait
'While Inkey <> "" : Wend

print "ok5" '*** comment out

Frank Brübach

#2
Hello Charles thanks for fixing my example.

But *tokens(redim)  seems to be new command or function for me. I am using oxygen issue of Begin march and this example does Run only in latest build so I suppose you included this new construct in one of a newer issue. Nevertheless its a good Idea to simplify These Things  thx

 

Charles Pegge

#3
This is fairly new syntax to support the passing of dynamic array attributes locally.

If the tokens(redim) param is removed from the split() prototype, it will be handled directly as the global dynamic array defined at the start of the program.

Function Split(string inputs, delimiter, *tokens(redim) ) As Integer