118 lines
3.6 KiB
Text
118 lines
3.6 KiB
Text
Module Checkit {
|
|
\\ Brain**** Compiler
|
|
|
|
Escape Off
|
|
\\ no Esc function so we can use Ctrl+Z when input characters to terminate BF
|
|
\\ ctrl+c open dialog for exit - by default in console mode
|
|
|
|
Const skipmonitor as boolean=true, output as boolean=True
|
|
Const ob$="{",cb$="}"
|
|
Gosub CallOne
|
|
\\ We use a group object with events.
|
|
|
|
Group WithEvents BF=BrainF()
|
|
|
|
Function BF_monitor {
|
|
\\ Event functions have same scope as the module where belong
|
|
If skipmonitor Then exit
|
|
Read New pc, mem
|
|
Print pc, mem
|
|
Print "Press space bar": While Key$<>" " {}
|
|
}
|
|
Function BF_newline {
|
|
If not skipmonitor then Print "newline" : exit
|
|
if output then Print
|
|
}
|
|
Function BF_print {
|
|
Read New c$
|
|
If not skipmonitor then Print "character:";c$ : exit
|
|
if output then Print c$;
|
|
}
|
|
|
|
Program$ = {++++++[>++++++++++++<-]>.
|
|
>++++++++++[>++++++++++<-]>+.
|
|
+++++++..+++.>++++[>+++++++++++<-]>.
|
|
<+++[>----<-]>.<<<<<+++[>+++++<-]>.
|
|
>>.+++.------.--------.>>+.
|
|
}
|
|
Report Program$
|
|
ExecBF(Program$)
|
|
End
|
|
|
|
Sub ExecBF(Code$)
|
|
ClearMem()
|
|
code$=filter$(code$, " "+chr$(10)+chr$(13))
|
|
code$<=replace$(".","@", code$)
|
|
code$<=replace$("-","-.D()", code$)
|
|
code$<=replace$("+","-.A()", code$)
|
|
code$<=replace$("<","-.L()", code$)
|
|
code$<=replace$(">","-.R()", code$)
|
|
code$<=replace$("@","-.P()", code$)
|
|
code$<=replace$("[","-.S("+ob$,code$)
|
|
code$<=replace$("]",cb$+")",code$)
|
|
code$<=replace$(",","-.K()", code$)
|
|
Rem : Print code$
|
|
BF.Eval code$
|
|
Print
|
|
End Sub
|
|
Sub ClearMem()
|
|
Dim cMem(1 to 30000)=0
|
|
For BF {
|
|
.Pc=1
|
|
.Zero=True
|
|
.Mem()=cMem()
|
|
}
|
|
End Sub
|
|
CallOne:
|
|
Class BrainF {
|
|
events "monitor", "newline", "print"
|
|
Dim Mem()
|
|
Pc=1, Zero as Boolean=True
|
|
Module UpdateZero {
|
|
.Zero<=.Mem(.Pc)=0
|
|
call event "monitor", .pc, .Mem(.pc)
|
|
}
|
|
Function A { \\ +
|
|
.Mem(.Pc)++
|
|
.UpdateZero
|
|
}
|
|
Function D { \\ -
|
|
.Mem(.Pc)--
|
|
.UpdateZero
|
|
}
|
|
Function R { \\ >
|
|
If .Pc=30000 Then Error "Upper Bound Error"
|
|
.Pc++
|
|
.UpdateZero
|
|
}
|
|
Function L { \\ <
|
|
If .Pc=1 Then Error "Lower Bound Error"
|
|
.Pc--
|
|
.UpdateZero
|
|
}
|
|
Function P { \\ .
|
|
Select Case .Mem(.Pc)
|
|
Case >31
|
|
Call Event "print", Chr$(.Mem(.Pc))
|
|
Case 10
|
|
Call Event "newline"
|
|
End Select
|
|
}
|
|
Function K { \\ ,
|
|
.Mem(.Pc)=Asc(Key$)
|
|
\\ ctrl+z for exit
|
|
If .Mem(.Pc)=26 Then Error "Finished"
|
|
.UpdateZero
|
|
}
|
|
Function S { \\ [
|
|
If .Zero then =0: exit
|
|
Read newEval$
|
|
Do {ret=Eval(newEval$)} until .Zero
|
|
}
|
|
Module Eval {
|
|
ret=eval(Letter$)
|
|
}
|
|
}
|
|
Return
|
|
}
|
|
Checkit
|