Created
April 2, 2015 08:32
-
-
Save mickesv/229e0f918119ee39b79f to your computer and use it in GitHub Desktop.
Brainfuck implementation in Math.js
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
# Implementation of Brainfuck in Math.js | |
# Why? Because I can, and because I wanted to find out if Math.js was Turing complete | |
# | |
# Does not support nested iterations or input yet. Embryo for nested iterations is there though. | |
# 1=dp(start at 10), 2=out, 3="",4=ip, 5=lastip, 6=mute, 7=isp | |
d=zeros(100);d[1]=10;d[2]="";d[3]="";d[4]=1;d[5]=1;d[6]=0;d[7]=100; | |
mov(d,dir)=subset(d,index(1),d[1]+dir); | |
mod(d,m)=subset(d,index(d[1]),d[d[1]]+m); | |
prn(d)=subset(d,index(2),d[d[1]]); | |
prs(d)=p[d[4]]==">"?mov(d,1):p[d[4]]=="<"?mov(d,-1):p[d[4]]=="+"?mod(d,1):p[d[4]]=="-"?mod(d,-1):p[d[4]]=="."?prn(d):d; | |
puip(d)=d[d[1]]==0?subset(d,index(6),1):subset(subset(d,index(d[7]),d[4]),index(7),d[7]-1); | |
poip(d)=d[d[1]]==0?subset(d,index(7),d[7]+1):subset(subset(d,index(4),d[d[7]+1]-1),index(7),d[7]+1); | |
ipi(d)=subset(prs(subset(d,index(5),d[4])),index(4),d[4]+1); | |
itc(x)=d=d[4]<=size(p)[1]?(p[d[4]]=="["?puip(ipi(d)):p[d[4]]=="]"?poip(ipi(d)):ipi(d)):[1,"","",size(p)[1]+1,1,1,100]; | |
pprn(d)=(p[d[5]]=="." and d[6]==0)?d[2]:d[3]; | |
exe(v,i,a)=pprn(itc(v)); | |
p="++++[>+.<-]>.>++."; | |
map(1:100,exe) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment