Last active
July 9, 2024 03:33
-
-
Save miura1729/a61184f5c9827c852bd70b4f78689599 to your computer and use it in GitHub Desktop.
bf in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PROG = " A mandelbrot set fractal viewer in brainf*** written by Erik Bosman | |
+++++++++++++[->++>>>+++++>++>+<<<<<<]>>>>>++++++>--->>>>>>>>>>+++++++++++++++[[ | |
>>>>>>>>>]+[<<<<<<<<<]>>>>>>>>>-]+[>>>>>>>>[-]>]<<<<<<<<<[<<<<<<<<<]>>>>>>>>[-]+ | |
<<<<<<<+++++[-[->>>>>>>>>+<<<<<<<<<]>>>>>>>>>]>>>>>>>+>>>>>>>>>>>>>>>>>>>>>>>>>> | |
>+<<<<<<<<<<<<<<<<<[<<<<<<<<<]>>>[-]+[>>>>>>[>>>>>>>[-]>>]<<<<<<<<<[<<<<<<<<<]>> | |
>>>>>[-]+<<<<<<++++[-[->>>>>>>>>+<<<<<<<<<]>>>>>>>>>]>>>>>>+<<<<<<+++++++[-[->>> | |
>>>>>>+<<<<<<<<<]>>>>>>>>>]>>>>>>+<<<<<<<<<<<<<<<<[<<<<<<<<<]>>>[[-]>>>>>>[>>>>> | |
>>[-<<<<<<+>>>>>>]<<<<<<[->>>>>>+<<+<<<+<]>>>>>>>>]<<<<<<<<<[<<<<<<<<<]>>>>>>>>> | |
[>>>>>>>>[-<<<<<<<+>>>>>>>]<<<<<<<[->>>>>>>+<<+<<<+<<]>>>>>>>>]<<<<<<<<<[<<<<<<< | |
<<]>>>>>>>[-<<<<<<<+>>>>>>>]<<<<<<<[->>>>>>>+<<+<<<<<]>>>>>>>>>+++++++++++++++[[ | |
>>>>>>>>>]+>[-]>[-]>[-]>[-]>[-]>[-]>[-]>[-]>[-]<<<<<<<<<[<<<<<<<<<]>>>>>>>>>-]+[ | |
>+>>>>>>>>]<<<<<<<<<[<<<<<<<<<]>>>>>>>>>[>->>>>[-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>[>>>>>>[-<<<<<<->>>>>>]+< | |
<<<<<[->>>>>>->>[-<<<<<<<<+>>>>>>>>]<<<<<<<<[->>>>>>>>+<<<<<<<<<<<<<<<<<[<<<<<<< | |
<<]>>>>[-]+>>>>>[>>>>>>>>>]>+<]]+>>>>>>>>[-<<<<<<<<->>>>>>>>]+<<<<<<<<[->>>>>>>> | |
-<<[-<<<<<<+>>>>>>]<<<<<<[->>>>>>+<<<<<<<<<<<<<<<[<<<<<<<<<]>>>[-]+>>>>>>[>>>>>> | |
>>>]>[-]+<]]+>[-<[>>>>>>>>>]<<<<<<<<]>>>>>>>>]<<<<<<<<<[<<<<<<<<<]>>>>[-]<<<++++ | |
+[-[->>>>>>>>>+<<<<<<<<<]>>>>>>>>>]>>>>>->>>>>>>>>>>>>>>>>>>>>>>>>>>-<<<<<<[<<<< | |
<<<<<]]>>>]" | |
def compile(prog0) | |
prog = "" | |
prog0.each_char do |ch| | |
if "<>+-[].,".include?(ch) then | |
prog << ch | |
end | |
end | |
bytes = [] | |
n = 0 | |
i = 0 | |
proglen = prog.size | |
proglen.times do |cnt| | |
ch = prog[cnt] | |
sign = 2 | |
case ch | |
when '>', '<' | |
if ch == '<' then | |
sign = 0 | |
end | |
n += 1 | |
if ch != prog[i + 1] then | |
bytes << ((n << 4) | 0b000 | sign) | |
n = 0 | |
end | |
when '+', '-' | |
if ch == '-' then | |
sign = 0 | |
end | |
n += 1 | |
if ch != prog[i + 1] then | |
bytes << ((n << 4) | 0b001 | sign) | |
n = 0 | |
end | |
when '.' | |
bytes << 0b100 | |
when ',' | |
bytes << 0b110 | |
when '[' | |
nest = 0 | |
j = i + 1 | |
off = 1 | |
while prog[j] != ']' or nest != 0 do | |
case prog[j] | |
when '[' | |
nest += 1 | |
off += 1 | |
when ']' | |
nest -= 1 | |
off += 1 | |
when '<', '>', '+', '-' | |
prevch = prog[j] | |
while prevch == prog[j + 1] do | |
j += 1 | |
end | |
off += 1 | |
when '.', ',' | |
off += 1 | |
end | |
j += 1 | |
end | |
bytes << ((off << 4) | 0b111) | |
when ']' | |
nest = 0 | |
j = i - 1 | |
off = 1 | |
while prog[j] != '[' or nest != 0 do | |
case prog[j] | |
when ']' | |
nest += 1 | |
off += 1 | |
when '[' | |
nest -= 1 | |
off += 1 | |
when '<', '>', '+', '-' | |
prevch = prog[j] | |
while prevch == prog[j - 1] do | |
j -= 1 | |
end | |
off += 1 | |
when '.', ',' | |
off += 1 | |
end | |
j -= 1 | |
end | |
bytes << ((off << 4) | 0b101) | |
end | |
i += 1 | |
end | |
optimize(bytes) | |
end | |
def optimize(bytes) | |
bytes.each_with_index do |n, i| | |
if n & 7 == 7 then | |
# [ instruction | |
nextpos = n >> 4 | |
if nextpos == 2 and bytes[i + 1] - (1 << 4) == 0b001 then | |
# [ - ] Zero clear | |
bytes[i] = (bytes[i] & (~15)) | 0b1000 | |
elsif nextpos == 5 and bytes[i + 1] - (1 << 4) == 0b001 and | |
bytes[i + 2] & 0b101 == 0b000 and bytes[i + 3] & 0b101 == 0b001 and | |
bytes[i + 4] & 0b101 == 0b000 then | |
# [->>+<<] or [->>-<<] Add or Sub multiple | |
bytes[i] = (bytes[i] & ~((1 << 4) - 1)) | 0b1001 | (bytes[i + 3] & 0b010) | |
elsif nextpos == 2 and bytes[i + 1] & 0b101 == 0b000 then | |
# [>>>...] or [<<<...] search 0 memptr | |
bytes[i] = (bytes[i] & ~((1 << 4) - 1)) | 0b1100 | (bytes[i + 1] & 0b010) | |
end | |
end | |
end | |
bytes | |
end | |
def disasm_one(byte) | |
res = "" | |
case byte & 15 | |
when 0 | |
res << "> " + (byte >> 4).to_s | |
when 1 | |
res << "- " + (byte >> 4).to_s | |
when 2 | |
res << "< " + (byte >> 4).to_s | |
when 3 | |
res << "+ " + (byte >> 4).to_s | |
when 4 | |
res << ". " + (byte >> 4).to_s | |
when 5 | |
res << "[ " + (byte >> 4).to_s | |
when 6 | |
res << ", " + (byte >> 4).to_s | |
when 7 | |
res << ", " + (byte >> 4).to_s | |
when 8 | |
res << "CLR " + (byte >> 4).to_s | |
when 9 | |
res << "-m " + (byte >> 4).to_s | |
when 11 | |
res << "+m " + (byte >> 4).to_s | |
when 12 | |
res << ">s " + (byte >> 4).to_s | |
when 13 | |
res << ">s " + (byte >> 4).to_s | |
end | |
end | |
def disasm(bytes) | |
bytes.each_with_index do |byte, i| | |
print "#{i} #{disasm_one(byte)}\n" | |
end | |
end | |
def eval(bytes) | |
mem = [] | |
1024.times do |i| | |
mem[i] = 0 | |
end | |
pc = 0 | |
ptr = 0 | |
while inst = bytes[pc] do | |
pc += 1 | |
if (inst & 0b1101) == 0 then | |
ptr += (inst >> 4) * ((inst & 2) - 1) | |
inst = bytes[pc] | |
pc += 1 | |
end | |
if (inst & 8) == 0 then | |
# Basic instruction | |
if (inst & 4) == 0 then | |
# + - | |
mem[ptr] += (inst >> 4) * ((inst & 2) - 1) | |
else | |
# . , [ ] | |
if (inst & 1) == 0 then | |
# . , | |
if inst & 2 == 0 then | |
print(mem[inst].chr) | |
else | |
#mem[inst] = getc | |
end | |
else | |
# [ ] | |
if (inst & 2) == 2 then | |
if mem[ptr] == 0 then | |
pc += (inst >> 4) | |
end | |
else | |
if mem[ptr] != 0 then | |
pc -= (inst >> 4) | |
end | |
end | |
end | |
end | |
else | |
# Extend instruction | |
if (inst & 4) == 0 then | |
if (inst & 1) == 0 then | |
# memory clear | |
mem[ptr] = 0 | |
else | |
# add/sub memory | |
offset = bytes[pc + 1] >> 4 | |
sign = (bytes[pc + 1] & 2) - 1 | |
mem[ptr + offset * sign] += mem[ptr] * ((inst & 2) - 1) | |
mem[ptr] = 0 | |
end | |
else | |
if (inst & 1) == 0 then | |
# search 0 cell | |
step = bytes[pc] >> 4 | |
sign = (bytes[pc] & 2) - 1 | |
while mem[ptr] != 0 do | |
ptr += step * sign | |
end | |
end | |
end | |
pc += (inst >> 4) | |
end | |
end | |
end | |
#MTypeInf::inference_main { | |
bytes = compile(PROG) | |
#disasm(bytes) | |
eval(bytes) | |
#} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment