running fine.
now I wanted to include some enums with TokenTypes... but my example doesn't work anymore
perhaps somebody can help.
thanks, lionheart
Code: Select all
' 1) FreeBASIC parser example by lionheart 25-06-2024 go
' 2) add enums... 24-01-2025, lionheart
' example doesn't work anymore, need little help
'
' Define the Enum and Type
Enum TokenType
TK_INTEGER
TK_PLUS
TK_MINUS
TK_MULTIPLY
TK_DIVIDE
TK_LPAREN
TK_RPAREN
TK_ASSIGN
TK_IDENTIFIER
TK_PRINT
TK_EOF
TK_UNKNOWN
End Enum
Type Token
Type1 As TokenType
Value As String
End Type
' Split() function
Function Split(ByVal inputs As String, ByRef tokens2() As token) As Integer '' error
Dim token1 As String = ""
Dim tokenCount As Integer = 0
Dim i As Integer
For i = 1 To Len(inputs)
Dim c As String = Mid(inputs, i, 1)
If c = " " Then
If token1 <> "" Then
ReDim Preserve tokens2(0 To tokenCount)
tokens2(tokenCount).Value = token1
tokens2(tokenCount).Type1 = GetTokenType(token1) '' error
tokenCount += 1
token1 = ""
End If
Else
token1 += c
End If
Next
If token1 <> "" Then
ReDim Preserve tokens2(0 To tokenCount)
tokens2(tokenCount).Value = token1
tokens2(tokenCount).Type1 = GetTokenType(token1) '' error
End If
Return tokenCount
End Function
'
Function GetTokenType(ByVal value As String) As TokenType
If IsNumeric(value) Then '' error
Return TK_INTEGER
ElseIf value = "+" Then '' error
Return TK_PLUS
ElseIf value = "-" Then '' error
Return TK_MINUS
ElseIf value = "*" Then '' error
Return TK_MULTIPLY
ElseIf value = "/" Then '' error
Return TK_DIVIDE
ElseIf value = "(" Then '' error
Return TK_LPAREN
ElseIf value = ")" Then '' error
Return TK_RPAREN
Else
Return TK_UNKNOWN
End If
End Function
' IsNumeric() function
Function IsNumeric(ByVal value As String) As Integer
Static numericChars As String
numericChars = "0123456789.-"
If Len(value) = 0 Then Return False
Dim dotCount As Integer = 0
For i As Integer = 1 To Len(value)
Dim c As String = 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
' EvaluateExpression function
Function EvaluateExpression(ByVal inputs As String) As Double
Dim tokens() As Token
Dim tokenCount As Integer = Split(inputs, tokens())
Dim total As Double = 0
Dim currentNumber As Double = 0
Dim operators As String = "+"
For i As Integer = 0 To UBound(tokens)
Dim token As Token = tokens(i)
If token.Type1 = TK_INTEGER Then
currentNumber = Val(token.Value)
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 = "+"
ElseIf token.Type1 = TK_PLUS Then
operators = "+"
ElseIf token.Type1 = TK_MINUS Then
operators = "-"
ElseIf token.Type1 = TK_MULTIPLY Then
operators = "*"
ElseIf token.Type1 = TK_DIVIDE Then
operators = "/"
End If
Next
Return total
End Function
' Test the code with different inputs
Dim inputs As String = "2 + 3 * 10" ' 50 result ok
Dim result As Double = EvaluateExpression(inputs)
If result <> 0 Then
Print "The total is "; result
Else
Print "Error: Invalid input"
End If
' Wait for a key press before closing the console
Print "Press any key to continue..."
Sleep
While Inkey <> "" : Wend
' ends