User:BoundedBeans/Interpreters

From Esolang
Jump to navigation Jump to search

This is a page devoted to short interpreters/compilers I've written for esolangs that already have an implementation, just for science I guess.

Brainfuck in dzaima/APL

53-bit signed cells (64-bit floating point), infinite tape both left and right. Each character of input must be on its own line (, will take a whole line but ignore any characters after the first). Does not support outputting ASCII control characters, 8-bit ASCII characters, or Unicode characters, and will error out if the program tries to output a cell greater than 255 (0-32 and 127-255 will just print a space instead of their respective byte).

Please note I am not good at APL so this code is trash (the conditionals use the eval of a string indexed in a nested array. Every time.)

ascii←'!"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~',⍨32⍴' '
ascii[11]←"\n"[1]
ascii←({⍵,' '}⍣129)ascii
set←{arr←⊃⍵[1] ⋄ ind←⊃⍵[2] ⋄ val←⊃⍵[3] ⋄ arr[ind]←val ⋄ arr}
tape←1⍴0 ⋄ ip←dp←1 ⋄ code←⍞ ⋄ data←(⊂tape),(⊂dp),(⊂code),(⊂ip)
left←{dp←1-⍨⊃⍵[2] ⋄ tape←⍎⊃((⊂'⊃⍵[1]'),⊂'(0,⊃⍵[1])⊣dp←1')[1+dp<1] ⋄ (⊂tape),(⊂dp),⍵[3],⍵[4]}
right←{dp←1+⊃⍵[2] ⋄ tape←⍎⊃((⊂'⊃⍵[1]'),⊂'((⊃⍵[1]),0)')[1+dp>⍴⊃⍵[1]] ⋄ (⊂tape),(⊂dp),⍵[3],⍵[4]}
change←{dp←⊃⍵[2] ⋄ tape←⊃⍵[1] ⋄ ind←dp ⋄ val←⍶ ind⌷tape ⋄ tape←set (⊂tape),(⊂ind),(⊂val) ⋄ (⊂tape),(⊂dp),⍵[3],⍵[4]}
plus←{⍵+1} change ⋄ minus←{⍵-1} change 
out←{⍞←⍎⊃((⊂'ascii[(⊃⍵[1])[⊃⍵[2]]]'),⊂'''''')[1+0=(⊃⍵[2])⌷⊃⍵[1]] ⋄ ⍵}
in←{inpline←⍞ ⋄ inp←⍎⊃((⊂'0'),⊂'⍸ascii=1⌷inpline')[1+0<⍴inpline] ⋄ dp←⊃⍵[2] ⋄ tape←⊃⍵[1] ⋄ tape←set (⊂tape),(⊂dp),⊂1⌷inp ⋄ (⊂tape),(⊂dp),⍵[3],⍵[4]}
open_←'0⊣open←{ip←⊃⍵[4] ⋄ code←⊃⍵[3] ⋄ ip←(({code←⊃⍵[1] ⋄ ip←1+⊃⍵[2] ⋄ d←⊃⍵[3] ⋄ d←⍎⊃('
open_←open_,'(⊂''d''),⊂''d+1'')[1+''[''=ip⌷code] ⋄ d←⍎⊃((⊂''d''),⊂''d-1'')[1+'']''=ip⌷code] ⋄ (⊂'
open_←open_,'code),(⊂ip),(⊂d)}⍣{0=⊃⍵[3]})(⊂code),(⊂ip),(⊂1))[2] ⋄ (⍵[1]),(⍵[2]),(⊂code),⊂ip}'
⍎open_ ⋄ ⎕ERASE 'open_'
close_←'0⊣close←{ip←⊃⍵[4] ⋄ code←⊃⍵[3] ⋄ ip←(({code←⊃⍵[1] ⋄ ip←1-⍨⊃⍵[2] ⋄ d←⊃⍵[3] ⋄ d←⍎⊃('
close_←close_,'(⊂''d''),⊂''d+1'')[1+'']''=ip⌷code] ⋄ d←⍎⊃((⊂''d''),⊂''d-1'')[1+''[''=ip⌷code] ⋄ (⊂'
close_←close_,'code),(⊂ip),(⊂d)}⍣{0=⊃⍵[3]})(⊂code),(⊂ip),(⊂1))[2] ⋄ (⍵[1]),(⍵[2]),(⊂code),⊂ip}'
⍎close_ ⋄ ⎕ERASE 'close_'
opencond←{dp←⊃⍵[2] ⋄ tape←⊃⍵[1] ⋄ ⍎⊃((⊂'⍵'),⊂'open ⍵')[1+0=dp⌷tape]}
closecond←{dp←⊃⍵[2] ⋄ tape←⊃⍵[1] ⋄ ⍎⊃((⊂'⍵'),⊂'close ⍵')[1+1-0=dp⌷tape]}
exec←{⍎⊃({⍵,' ⍵'}¨('left' 'right' 'plus' 'minus' 'out' 'in' 'opencond' 'closecond'))[⍸'<>+-.,[]'=(⊃⍵[3])[⊃⍵[4]]]}
advance←{⍎⊃(((⊂'0'),⊂'{(⍵[1]),(⍵[2]),(⍵[3]),(⊂1+⊃⍵[4])}⍵')[1+(⊃⍵[4])<⍴⊃⍵[3]])}
({advance exec ⍵}⍣{⍵≡0})data