Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ' Gambas class file
- Private parseTree As New Expresion[]
- Private errores As String = ""
- Public Sub _new(s As String)
- Dim a As Integer
- Dim ope As Boolean
- Dim token As String[]
- Dim valor As Float
- Dim ExpresionMenos As TerminalExpresion_Minus
- Dim ExpresionMas As TerminalExpresion_Plus
- Dim ExpresionNumero As TerminalExpresion_Number
- Dim ExpresionDivide As TerminalExpresion_Div
- Dim ExpresionMulti As TerminalExpresion_Multi
- 'elimino espacios iniciales y finales
- s = RTrim$(s)
- s = LTrim$(s)
- token = Split(s, " ")
- For a = 0 To token.count - 1
- 'detectar operadores
- ope = False
- Select Case token[a]
- Case "+"
- ExpresionMas = New TerminalExpresion_Plus
- parseTree.Add(ExpresionMas)
- ope = True
- Case "-"
- ExpresionMenos = New TerminalExpresion_Minus
- parseTree.Add(ExpresionMenos)
- ope = True
- Case "*"
- ExpresionMulti = New TerminalExpresion_Multi
- parseTree.Add(ExpresionMulti)
- ope = True
- Case "/"
- ExpresionDivide = New TerminalExpresion_Div
- parseTree.Add(ExpresionDivide)
- ope = True
- End Select
- 'detectar nuemros
- If ope = False Then
- 'puede que sea un numero
- Try valor = Val(Replace$((token[a]), ".", ","))
- If Error Then
- If token[a] = " " Then
- 'espacio en blanco en la expresion
- Continue
- Else
- errores = "Error al intentar traducir la expresion (" & s & "), fallo en: [" & token[a] & "]"
- Return
- Endif
- Else
- 'no ha habido errores...
- ExpresionNumero = New TerminalExpresion_Number(valor)
- parseTree.Add(ExpresionNumero)
- Endif
- Endif
- Next
- End
- Public Function evaluate() As Variant[]
- Dim contexto As New Float[]
- Dim e As Expresion
- Dim estado As Variant[]
- If errores <> "" Then
- 'al parsear la expresion no se ha detectado un error.
- Return [False, 0, errores]
- Endif
- For Each e In parseTree
- estado = e.interpreter(contexto)
- If estado[0] = False Then
- Return [False, 0, "Error: " & estado[1]]
- Endif
- Next
- Return [True, contexto.Pop(), ""]
- End
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement