isbpl/bf.isbpl

84 lines
2 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
2022-06-30 02:20:21 +02:00
"---\n" puts
2022-05-23 10:30:53 +02:00
"Java" try {
s Context new eval
} {
pop pop printStackTrace0
}
0
}
func read {
with s ;
2022-06-30 02:20:21 +02:00
[ s STREAM.read stream dup -1 eq if { pop pop "" 2 stop } _char ] _string
2022-05-23 10:30:53 +02:00
}
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
JIO context debug if {
stdin itos puts
}
2022-05-23 10:30:53 +02:00
} =code
def c
while { s read dup =c "" eq not } {
2022-06-30 02:20:21 +02:00
c puts
2022-05-23 10:30:53 +02:00
code " " strconcat ( c parse ) strconcat =code
}
2022-06-30 02:20:21 +02:00
"\n" puts
2022-05-23 10:30:53 +02:00
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 ;
2022-05-23 10:35:26 +02:00
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 }
2022-05-23 10:30:53 +02:00
""
}