87 lines
3.0 KiB
Plaintext
87 lines
3.0 KiB
Plaintext
' Intérprete de brainfuck
|
|
' FB 1.05.0 Win64
|
|
'
|
|
|
|
Const BF_error_memoria_saturada As Integer = 2
|
|
Const BF_error_memoria_insuficiente As Integer = 4
|
|
Const BF_error_codigo_saturado As Integer = 8
|
|
Const BF_error_desbordamiento_codigo As Integer = 16
|
|
|
|
Dim BFcodigo As String = ">++++++++++[>+++>+++++++>++++++++++>+++++++++++>++++++++++++>++++++++++++++++[<]>-]>>>>>>+.<<<<++.>>+.---.<---.<<++.>>>+.>---.<+.<+++.>+.<<<+."
|
|
Dim codigo_error As Integer
|
|
|
|
Function EjecutarBF (BFcodigo As String, tammem As Uinteger) As Integer
|
|
Dim As String memoria = String(tammem, 0)
|
|
Dim As Uinteger puntero_instrucciones, puntero_datos
|
|
Dim As Integer nivel_de_alcance
|
|
|
|
For puntero_instrucciones = 0 To Len(BFcodigo)
|
|
Select Case Chr(BFcodigo[puntero_instrucciones])
|
|
Case ">"
|
|
puntero_datos += 1
|
|
If (puntero_datos > tammem - 1) Then Return BF_error_memoria_saturada
|
|
Case "<"
|
|
puntero_datos -= 1
|
|
If (puntero_datos > tammem - 1) Then Return BF_error_memoria_insuficiente
|
|
Case "+"
|
|
memoria[puntero_datos] += 1
|
|
Case "-"
|
|
memoria[puntero_datos] -= 1
|
|
Case "."
|
|
Print Chr(memoria[puntero_datos]);
|
|
Case ","
|
|
memoria[puntero_datos] = Asc(Input(1))
|
|
Case "["
|
|
If (memoria[puntero_datos] = 0) Then
|
|
Dim nivel_antiguo As Uinteger = nivel_de_alcance
|
|
nivel_de_alcance += 1
|
|
Do Until (nivel_de_alcance = nivel_antiguo)
|
|
puntero_instrucciones += 1
|
|
If (puntero_instrucciones > Len(BFcodigo) - 1) Then Return BF_error_codigo_saturado
|
|
Select Case Chr(BFcodigo[puntero_instrucciones])
|
|
Case "["
|
|
nivel_de_alcance += 1
|
|
Case "]"
|
|
nivel_de_alcance -= 1
|
|
End Select
|
|
Loop
|
|
Else
|
|
nivel_de_alcance += 1
|
|
End If
|
|
Continue For
|
|
Case "]"
|
|
If (memoria[puntero_datos] = 0) Then
|
|
nivel_de_alcance -= 1
|
|
Continue For
|
|
Else
|
|
Dim nivel_antiguo As Integer = nivel_de_alcance
|
|
nivel_de_alcance -= 1
|
|
Do Until (nivel_de_alcance = nivel_antiguo)
|
|
puntero_instrucciones -= 1
|
|
If (puntero_instrucciones > Len(BFcodigo) - 1) Then Return BF_error_desbordamiento_codigo
|
|
Select Case Chr(BFcodigo[puntero_instrucciones])
|
|
Case "["
|
|
nivel_de_alcance += 1
|
|
Case "]"
|
|
nivel_de_alcance -= 1
|
|
End Select
|
|
Loop
|
|
End If
|
|
Continue For
|
|
Case Else
|
|
Continue For
|
|
End Select
|
|
Next puntero_instrucciones
|
|
Return -1
|
|
End Function
|
|
|
|
|
|
Cls
|
|
codigo_error = EjecutarBF(BFcodigo, 1024)
|
|
If codigo_error Then
|
|
Sleep
|
|
Else
|
|
Print "codigo de error: " & codigo_error
|
|
End If
|
|
End
|