Redivider/Brainfuck Interpreter

From Esolang
Jump to navigation Jump to search

Brainfuck interpreter written in Redivider by User:MizardX.

Code

# Brainfuck interpreter, written in Redivider, by MizardX
# Version: 1.0
#
# The BF program and the input to the BF program is expected
# as input to this interpeter. They should be separated by
# a exclamation point ("!").
#
# More info about Redivider and Brainfuck:
# http://esoteric.voxelperfect.net/wiki/Redivider
# http://esoteric.voxelperfect.net/wiki/Brainfuck

main:
  { program: /[^!]*/;
    /!/|"";
    input: /[\S\s]*/;
    run("","",input)[program] }
| "Invalid input".

run(memleft,memright,input):
  { /[^[\]<>.,+-]*\+/; run(memleft,(realinc+/.*/)[memright],input) }
| { /[^[\]<>.,+-]*-/;  run(memleft,(realdec+/.*/)[memright],input) }
| { /[^[\]<>.,+-]*</;  
    h: (/\d+,/|"")[memleft];
    t: ({/\d+,/;/.*/}|"")[memleft];
    run(t,h+memright,input) }
| { /[^[\]<>.,+-]*>/;
    h: (/\d+,/|"0,")[memright];
    t: ({/\d+,/;/.*/}|"")[memright];
    run(h+memleft,t,input) }
| { /[^[\]<>.,+-]*\./;
    itoa[(/\d+,/|"0,")[memright]] + run(memleft,memright,input) }
| { /[^[\]<>.,+-]*,/;
    t: ({/\d+,/;/.*/}|"")[memright];
    read: atoi[(/./|"\0000")[input]];
    rest: ({/./;/.*/}|"")[input];
    run(memleft,read+","+t,rest) }
| { /[^[\]<>.,+-]*\[/;
    program: /.*/;
    loop: grabloop[program];
    tail: skiploop[program];
    (
      { /0,/;
        run(memleft,memright,input)[tail]
      }
    | run(memleft,memright,input)[loop + "[" + loop + "]" + tail]
    )[memright] }
| { /[^[\]<>.,+-]*\]/;
    "## Error: unexpected ']' ##" }
| { /[^[\]<>.,+-]*/; "" }.

realinc: {/255\b/;"0,"}|inc+(/,/|",").
inc:
  { prefix: /\d*(?=\d)/;
    { /9/;
      inc[prefix] + "0" }
  | { digit: /\d/;
      prefix + inc1[digit] } }
| "1".
realdec: {/0\b|,|$/;"255"}|{/0*(?=\d)/;/.*/}[dec].
dec:
  {
    prefix: /\d*(?=\d)/;
    {
      digit: /0/;
      dec[prefix] + "9"
    }|{
      digit: /\d/;
      prefix + dec1[digit]
    }
  }|"".

inc1: {/0/;"1"}|{/1/;"2"}|{/2/;"3"}|{/3/;"4"}|{/4/;"5"}|{/5/;"6"}|{/6/;"7"}|{/7/;"8"}|{/8/;"9"}|{/9/;"0"}|"".
dec1: {/9/;"8"}|{/8/;"7"}|{/7/;"6"}|{/6/;"5"}|{/5/;"4"}|{/4/;"3"}|{/3/;"2"}|{/2/;"1"}|{/1/;"0"}|{/0/;"9"}|"".

grabloop:
  /\[/ + grabloop + /\]/ + grabloop
| /[^[\]]+/ + grabloop
| "".

skiploop:
  { grabloop; /\]/; /.*/ }
| "".

atoi:
  {/\000/;  "0"} | {/\001/;  "1"} | {/\002/;  "2"} | {/\003/;  "3"} | {/\004/;  "4"} | {/\005/;  "5"}
| {/\006/;  "6"} | {/\007/;  "7"} | {/\010/;  "8"} | {/\011/;  "9"} | {/\012/; "10"} | {/\013/; "11"}
| {/\014/; "12"} | {/\015/; "13"} | {/\016/; "14"} | {/\017/; "15"} | {/\020/; "16"} | {/\021/; "17"}
| {/\022/; "18"} | {/\023/; "19"} | {/\024/; "20"} | {/\025/; "21"} | {/\026/; "22"} | {/\027/; "23"}
| {/\030/; "24"} | {/\031/; "25"} | {/\032/; "26"} | {/\033/; "27"} | {/\034/; "28"} | {/\035/; "29"}
| {/\036/; "30"} | {/\037/; "31"} | {/\040/; "32"} | {/\041/; "33"} | {/\042/; "34"} | {/\043/; "35"}
| {/\044/; "36"} | {/\045/; "37"} | {/\046/; "38"} | {/\047/; "39"} | {/\050/; "40"} | {/\051/; "41"}
| {/\052/; "42"} | {/\053/; "43"} | {/\054/; "44"} | {/\055/; "45"} | {/\056/; "46"} | {/\057/; "47"}
| {/\060/; "48"} | {/\061/; "49"} | {/\062/; "50"} | {/\063/; "51"} | {/\064/; "52"} | {/\065/; "53"}
| {/\066/; "54"} | {/\067/; "55"} | {/\070/; "56"} | {/\071/; "57"} | {/\072/; "58"} | {/\073/; "59"}
| {/\074/; "60"} | {/\075/; "61"} | {/\076/; "62"} | {/\077/; "63"} | {/\100/; "64"} | {/\101/; "65"}
| {/\102/; "66"} | {/\103/; "67"} | {/\104/; "68"} | {/\105/; "69"} | {/\106/; "70"} | {/\107/; "71"}
| {/\110/; "72"} | {/\111/; "73"} | {/\112/; "74"} | {/\113/; "75"} | {/\114/; "76"} | {/\115/; "77"}
| {/\116/; "78"} | {/\117/; "79"} | {/\120/; "80"} | {/\121/; "81"} | {/\122/; "82"} | {/\123/; "83"}
| {/\124/; "84"} | {/\125/; "85"} | {/\126/; "86"} | {/\127/; "87"} | {/\130/; "88"} | {/\131/; "89"}
| {/\132/; "90"} | {/\133/; "91"} | {/\134/; "92"} | {/\135/; "93"} | {/\136/; "94"} | {/\137/; "95"}
| {/\140/; "96"} | {/\141/; "97"} | {/\142/; "98"} | {/\143/; "99"} | {/\144/;"100"} | {/\145/;"101"}
| {/\146/;"102"} | {/\147/;"103"} | {/\150/;"104"} | {/\151/;"105"} | {/\152/;"106"} | {/\153/;"107"}
| {/\154/;"108"} | {/\155/;"109"} | {/\156/;"110"} | {/\157/;"111"} | {/\160/;"112"} | {/\161/;"113"}
| {/\162/;"114"} | {/\163/;"115"} | {/\164/;"116"} | {/\165/;"117"} | {/\166/;"118"} | {/\167/;"119"}
| {/\170/;"120"} | {/\171/;"121"} | {/\172/;"122"} | {/\173/;"123"} | {/\174/;"124"} | {/\175/;"125"}
| {/\176/;"126"} | {/\177/;"127"} | {/\200/;"128"} | {/\201/;"129"} | {/\202/;"130"} | {/\203/;"131"}
| {/\204/;"132"} | {/\205/;"133"} | {/\206/;"134"} | {/\207/;"135"} | {/\210/;"136"} | {/\211/;"137"}
| {/\212/;"138"} | {/\213/;"139"} | {/\214/;"140"} | {/\215/;"141"} | {/\216/;"142"} | {/\217/;"143"}
| {/\220/;"144"} | {/\221/;"145"} | {/\222/;"146"} | {/\223/;"147"} | {/\224/;"148"} | {/\225/;"149"}
| {/\226/;"150"} | {/\227/;"151"} | {/\230/;"152"} | {/\231/;"153"} | {/\232/;"154"} | {/\233/;"155"}
| {/\234/;"156"} | {/\235/;"157"} | {/\236/;"158"} | {/\237/;"159"} | {/\240/;"160"} | {/\241/;"161"}
| {/\242/;"162"} | {/\243/;"163"} | {/\244/;"164"} | {/\245/;"165"} | {/\246/;"166"} | {/\247/;"167"}
| {/\250/;"168"} | {/\251/;"169"} | {/\252/;"170"} | {/\253/;"171"} | {/\254/;"172"} | {/\255/;"173"}
| {/\256/;"174"} | {/\257/;"175"} | {/\260/;"176"} | {/\261/;"177"} | {/\262/;"178"} | {/\263/;"179"}
| {/\264/;"180"} | {/\265/;"181"} | {/\266/;"182"} | {/\267/;"183"} | {/\270/;"184"} | {/\271/;"185"}
| {/\272/;"186"} | {/\273/;"187"} | {/\274/;"188"} | {/\275/;"189"} | {/\276/;"190"} | {/\277/;"191"}
| {/\300/;"192"} | {/\301/;"193"} | {/\302/;"194"} | {/\303/;"195"} | {/\304/;"196"} | {/\305/;"197"}
| {/\306/;"198"} | {/\307/;"199"} | {/\310/;"200"} | {/\311/;"201"} | {/\312/;"202"} | {/\313/;"203"}
| {/\314/;"204"} | {/\315/;"205"} | {/\316/;"206"} | {/\317/;"207"} | {/\320/;"208"} | {/\321/;"209"}
| {/\322/;"210"} | {/\323/;"211"} | {/\324/;"212"} | {/\325/;"213"} | {/\326/;"214"} | {/\327/;"215"}
| {/\330/;"216"} | {/\331/;"217"} | {/\332/;"218"} | {/\333/;"219"} | {/\334/;"220"} | {/\335/;"221"}
| {/\336/;"222"} | {/\337/;"223"} | {/\340/;"224"} | {/\341/;"225"} | {/\342/;"226"} | {/\343/;"227"}
| {/\344/;"228"} | {/\345/;"229"} | {/\346/;"230"} | {/\347/;"231"} | {/\350/;"232"} | {/\351/;"233"}
| {/\352/;"234"} | {/\353/;"235"} | {/\354/;"236"} | {/\355/;"237"} | {/\356/;"238"} | {/\357/;"239"}
| {/\360/;"240"} | {/\361/;"241"} | {/\362/;"242"} | {/\363/;"243"} | {/\364/;"244"} | {/\365/;"245"}
| {/\366/;"246"} | {/\367/;"247"} | {/\370/;"248"} | {/\371/;"249"} | {/\372/;"250"} | {/\373/;"251"}
| {/\374/;"252"} | {/\375/;"253"} | {/\376/;"254"} | {/\377/;"255"}.

itoa:
  {  /0\b/;"\0000"} | {  /1\b/;"\0001"} | {  /2\b/;"\0002"} | {  /3\b/;"\0003"} | {  /4\b/;"\0004"}
| {  /5\b/;"\0005"} | {  /6\b/;"\0006"} | {  /7\b/;"\0007"} | {  /8\b/;"\0010"} | {  /9\b/;"\0011"}
| { /10\b/;"\0012"} | { /11\b/;"\0013"} | { /12\b/;"\0014"} | { /13\b/;"\0015"} | { /14\b/;"\0016"}
| { /15\b/;"\0017"} | { /16\b/;"\0020"} | { /17\b/;"\0021"} | { /18\b/;"\0022"} | { /19\b/;"\0023"}
| { /20\b/;"\0024"} | { /21\b/;"\0025"} | { /22\b/;"\0026"} | { /23\b/;"\0027"} | { /24\b/;"\0030"}
| { /25\b/;"\0031"} | { /26\b/;"\0032"} | { /27\b/;"\0033"} | { /28\b/;"\0034"} | { /29\b/;"\0035"}
| { /30\b/;"\0036"} | { /31\b/;"\0037"} | { /32\b/;"\0040"} | { /33\b/;"\0041"} | { /34\b/;"\0042"}
| { /35\b/;"\0043"} | { /36\b/;"\0044"} | { /37\b/;"\0045"} | { /38\b/;"\0046"} | { /39\b/;"\0047"}
| { /40\b/;"\0050"} | { /41\b/;"\0051"} | { /42\b/;"\0052"} | { /43\b/;"\0053"} | { /44\b/;"\0054"}
| { /45\b/;"\0055"} | { /46\b/;"\0056"} | { /47\b/;"\0057"} | { /48\b/;"\0060"} | { /49\b/;"\0061"}
| { /50\b/;"\0062"} | { /51\b/;"\0063"} | { /52\b/;"\0064"} | { /53\b/;"\0065"} | { /54\b/;"\0066"}
| { /55\b/;"\0067"} | { /56\b/;"\0070"} | { /57\b/;"\0071"} | { /58\b/;"\0072"} | { /59\b/;"\0073"}
| { /60\b/;"\0074"} | { /61\b/;"\0075"} | { /62\b/;"\0076"} | { /63\b/;"\0077"} | { /64\b/;"\0100"}
| { /65\b/;"\0101"} | { /66\b/;"\0102"} | { /67\b/;"\0103"} | { /68\b/;"\0104"} | { /69\b/;"\0105"}
| { /70\b/;"\0106"} | { /71\b/;"\0107"} | { /72\b/;"\0110"} | { /73\b/;"\0111"} | { /74\b/;"\0112"}
| { /75\b/;"\0113"} | { /76\b/;"\0114"} | { /77\b/;"\0115"} | { /78\b/;"\0116"} | { /79\b/;"\0117"}
| { /80\b/;"\0120"} | { /81\b/;"\0121"} | { /82\b/;"\0122"} | { /83\b/;"\0123"} | { /84\b/;"\0124"}
| { /85\b/;"\0125"} | { /86\b/;"\0126"} | { /87\b/;"\0127"} | { /88\b/;"\0130"} | { /89\b/;"\0131"}
| { /90\b/;"\0132"} | { /91\b/;"\0133"} | { /92\b/;"\0134"} | { /93\b/;"\0135"} | { /94\b/;"\0136"}
| { /95\b/;"\0137"} | { /96\b/;"\0140"} | { /97\b/;"\0141"} | { /98\b/;"\0142"} | { /99\b/;"\0143"}
| {/100\b/;"\0144"} | {/101\b/;"\0145"} | {/102\b/;"\0146"} | {/103\b/;"\0147"} | {/104\b/;"\0150"}
| {/105\b/;"\0151"} | {/106\b/;"\0152"} | {/107\b/;"\0153"} | {/108\b/;"\0154"} | {/109\b/;"\0155"}
| {/110\b/;"\0156"} | {/111\b/;"\0157"} | {/112\b/;"\0160"} | {/113\b/;"\0161"} | {/114\b/;"\0162"}
| {/115\b/;"\0163"} | {/116\b/;"\0164"} | {/117\b/;"\0165"} | {/118\b/;"\0166"} | {/119\b/;"\0167"}
| {/120\b/;"\0170"} | {/121\b/;"\0171"} | {/122\b/;"\0172"} | {/123\b/;"\0173"} | {/124\b/;"\0174"}
| {/125\b/;"\0175"} | {/126\b/;"\0176"} | {/127\b/;"\0177"} | {/128\b/;"\0200"} | {/129\b/;"\0201"}
| {/130\b/;"\0202"} | {/131\b/;"\0203"} | {/132\b/;"\0204"} | {/133\b/;"\0205"} | {/134\b/;"\0206"}
| {/135\b/;"\0207"} | {/136\b/;"\0210"} | {/137\b/;"\0211"} | {/138\b/;"\0212"} | {/139\b/;"\0213"}
| {/140\b/;"\0214"} | {/141\b/;"\0215"} | {/142\b/;"\0216"} | {/143\b/;"\0217"} | {/144\b/;"\0220"}
| {/145\b/;"\0221"} | {/146\b/;"\0222"} | {/147\b/;"\0223"} | {/148\b/;"\0224"} | {/149\b/;"\0225"}
| {/150\b/;"\0226"} | {/151\b/;"\0227"} | {/152\b/;"\0230"} | {/153\b/;"\0231"} | {/154\b/;"\0232"}
| {/155\b/;"\0233"} | {/156\b/;"\0234"} | {/157\b/;"\0235"} | {/158\b/;"\0236"} | {/159\b/;"\0237"}
| {/160\b/;"\0240"} | {/161\b/;"\0241"} | {/162\b/;"\0242"} | {/163\b/;"\0243"} | {/164\b/;"\0244"}
| {/165\b/;"\0245"} | {/166\b/;"\0246"} | {/167\b/;"\0247"} | {/168\b/;"\0250"} | {/169\b/;"\0251"}
| {/170\b/;"\0252"} | {/171\b/;"\0253"} | {/172\b/;"\0254"} | {/173\b/;"\0255"} | {/174\b/;"\0256"}
| {/175\b/;"\0257"} | {/176\b/;"\0260"} | {/177\b/;"\0261"} | {/178\b/;"\0262"} | {/179\b/;"\0263"}
| {/180\b/;"\0264"} | {/181\b/;"\0265"} | {/182\b/;"\0266"} | {/183\b/;"\0267"} | {/184\b/;"\0270"}
| {/185\b/;"\0271"} | {/186\b/;"\0272"} | {/187\b/;"\0273"} | {/188\b/;"\0274"} | {/189\b/;"\0275"}
| {/190\b/;"\0276"} | {/191\b/;"\0277"} | {/192\b/;"\0300"} | {/193\b/;"\0301"} | {/194\b/;"\0302"}
| {/195\b/;"\0303"} | {/196\b/;"\0304"} | {/197\b/;"\0305"} | {/198\b/;"\0306"} | {/199\b/;"\0307"}
| {/200\b/;"\0310"} | {/201\b/;"\0311"} | {/202\b/;"\0312"} | {/203\b/;"\0313"} | {/204\b/;"\0314"}
| {/205\b/;"\0315"} | {/206\b/;"\0316"} | {/207\b/;"\0317"} | {/208\b/;"\0320"} | {/209\b/;"\0321"}
| {/210\b/;"\0322"} | {/211\b/;"\0323"} | {/212\b/;"\0324"} | {/213\b/;"\0325"} | {/214\b/;"\0326"}
| {/215\b/;"\0327"} | {/216\b/;"\0330"} | {/217\b/;"\0331"} | {/218\b/;"\0332"} | {/219\b/;"\0333"}
| {/220\b/;"\0334"} | {/221\b/;"\0335"} | {/222\b/;"\0336"} | {/223\b/;"\0337"} | {/224\b/;"\0340"}
| {/225\b/;"\0341"} | {/226\b/;"\0342"} | {/227\b/;"\0343"} | {/228\b/;"\0344"} | {/229\b/;"\0345"}
| {/230\b/;"\0346"} | {/231\b/;"\0347"} | {/232\b/;"\0350"} | {/233\b/;"\0351"} | {/234\b/;"\0352"}
| {/235\b/;"\0353"} | {/236\b/;"\0354"} | {/237\b/;"\0355"} | {/238\b/;"\0356"} | {/239\b/;"\0357"}
| {/240\b/;"\0360"} | {/241\b/;"\0361"} | {/242\b/;"\0362"} | {/243\b/;"\0363"} | {/244\b/;"\0364"}
| {/245\b/;"\0365"} | {/246\b/;"\0366"} | {/247\b/;"\0367"} | {/248\b/;"\0370"} | {/249\b/;"\0371"}
| {/250\b/;"\0372"} | {/251\b/;"\0373"} | {/252\b/;"\0374"} | {/253\b/;"\0375"} | {/254\b/;"\0376"}
| {/255\b/;"\0377"}.


This looks cool! Good work. --Keymaker 08:29, 16 November 2008 (UTC)