isbpl/bf.isbpl

78 lines
1.7 KiB
Text
Raw Normal View History

2022-05-23 10:30:53 +02:00
2022-05-23 10:33:48 +02:00
"Brainfuck->ISBPL transpiler" #
2022-05-23 10:30:53 +02:00
native acopy
"#stream.isbpl" include
"#multi.isbpl" include
func main {
with args ;
args alen 0 eq if {
"Error: Couldn't find file [no]\n" puts
1
2 stop
}
def filename args 0 aget =filename
def s args 0 aget STREAM.create.file.in stream =s
s makeCode =s
2022-05-23 10:33:48 +02:00
"Write code to ISBPL file" #
2022-05-23 10:30:53 +02:00
filename ".isbpl" strconcat STREAM.create.file.out stream
! s swap stream.write
STREAM.close stream
"Java" try {
s Context new eval
} {
pop pop printStackTrace0
}
0
}
func read {
with s ;
[ s STREAM.read stream dup -1 eq if { ] pop "" 2 stop } _char ] _string
}
2022-05-23 10:33:48 +02:00
"Read BF and convert to ISBPL" #
2022-05-23 10:30:53 +02:00
func makeCode {
with s ;
2022-05-23 10:33:48 +02:00
"Initialization code as stringblock" #
2022-05-23 10:30:53 +02:00
def code string! {
"#stream.isbpl" include
def stdin "/dev/stdin" STREAM.create.file.in stream =stdin
def idx 0 =idx
def arr 10000 anew =arr
while { idx 10000 lt } {
arr idx 0 aput
idx ++ =idx
}
1000 =idx
} =code
def c
while { s read dup =c "" eq not } {
code " " strconcat ( c parse ) strconcat =code
}
code
}
2022-05-23 10:33:48 +02:00
"Convert a BF char to ISBPL" #
2022-05-23 10:30:53 +02:00
func parse {
with c ;
c "+" eq if { "arr idx arr idx aget ++ 256 % aput" 2 stop }
c "-" eq if { "arr idx arr idx aget -- 256 % aput" 2 stop }
c ">" eq if { "idx ++ =idx" 2 stop }
c "<" eq if { "idx -- =idx" 2 stop }
c "[" eq if { "while { arr idx aget } {" 2 stop }
c "]" eq if { "}" 2 stop }
c "." eq if { "arr idx aget _char putchar" 2 stop }
c "," eq if { "arr idx stdin STREAM.read stream _int 256 % aput" 2 stop }
""
}