-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitman-es6.js
77 lines (71 loc) · 3.11 KB
/
bitman-es6.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//requires javascript-bignum library
//initial vars+functions
ahead=[]
ip=0
stack=[]
ret=[]
c=''
bits=32
Array.prototype.pick=function(n){return this[this.length-n-1]}
Array.prototype.chunk=function(r){var t,n=[],e=0;for(t=this.length/r;t>e;)n[e]=this.splice(0,r),e++;return n}
//lookahead stuff
seek=c=>(!ahead[ip]&&(ahead[ip]=ip+code.slice(ip).indexOf(c)),ahead[ip])
matching_brace=_=>{
var start=ip
if(!ahead[start]){
while(ip<code.length){
c=code[++ip]
if(c==']')break;
if(braces[c])ip=braces[c]();
}
ahead[start]=ip
}
return ahead[start]
}
braces={
'"':x=>seek('"',++ip),
"'":x=>ip+1,
"[":matching_brace
}
put=s=>out.textContent+=s
//functions
commands={
'1':x=>{stack[stack.length-1].push(1);stack[stack.length-1][0]?bits++:stack[stack.length-1].shift()},
'+':x=>{stack.push(Array(bits).fill(0))},
'$':x=>{stack.push(stack.pick(+BigInteger('0b'+stack.pop().join``)))},
'%':x=>{stack.pop()},
'@':x=>{stack.push(stack.pick(x=+BigInteger('0b'+stack.pop().join``))),stack.splice(stack.length-x-2,1)},
'&':x=>{x=stack.pop();stack.push(stack.pop().map((a,y)=>x[y]&a))},
'|':x=>{x=stack.pop();stack.push(stack.pop().map((a,y)=>x[y]|a))},
'^':x=>{x=stack.pop();stack.push(stack.pop().map((a,y)=>x[y]^a))},
'~':x=>{stack.push(stack.pop().map(x=>+!x))},
'<':x=>{stack[stack.length-1].push(0);stack[stack.length-1][0]?bits++:stack[stack.length-1].shift()},
'>':x=>{stack[stack.length-1].pop(),stack[stack.length-1].unshift(0)},
'[':x=>{stack.push(a=[0,...BigInteger(ip).toString(2)].map(x=>+x));a.length>bits&&(bits=a.length);ip=matching_brace()},
']':x=>{ip=ret.pop()},
'!':x=>{ret.push(ip);ip=BigInteger('0b'+stack.pop().join``)},
'?':x=>{x=+BigInteger('0b'+stack.pop().join``);if(+BigInteger('0b'+stack.pop().join``))ret.push(ip),ip=x},
'=':x=>{op=+BigInteger('0b'+stack.pop().join``);commands[code[++ip]]=x=>(ret.push(ip),ip=op)},
';':x=>{put(stack.pop().join``.replace(/0*/g,''))},
':':x=>{put(BigInteger('0b'+stack.pop().join``)<<0)},
'.':x=>{put(BigInteger('0b'+stack.pop().join``))},
',':x=>{put(String.fromCharCode(BigInteger('0b'+stack.pop().join``)))}
}
//good-to-know data for runtime
log=_=>stats.innerHTML=`Code │ ${format=code.replace(/[\x00-\x1f]/g,x=>String.fromCharCode(x.charCodeAt()+9216)).replace(/</g,'<').replace(/>/g,'>').split(/(&[gl]t;)|/).filter(x=>x),format[ip]=`<span style=background-color:#60cc76>${format[ip]||""}</span>`,format.join``}
IP │ ${ip}
Bits │ ${bits}
Return Stack │ ${JSON.stringify(ret)}
Stack │ ${stack.length?stack.slice(0).reverse().map(x=>x.join``).join`
│ `:''}`
nsc.oninput=onload=_=>(code=nsc.value,log())
//actual parsing
parse=_=>{
c=code[ip]
log()
if(commands[c])commands[c]();stack=stack.map(x=>Array(bits).fill(x[0]).concat(x).slice(-bits));ip++
}
//clearing everything before starting prog
init=_=>(code=nsc.value,ahead=[],ip=0,bits=32,stack=[],ret=[],out.innerHTML="",console.clear())
//determines either full or timed run
run=_=>{init();if(time.checked)interval=setInterval('ip<code.length?parse():(clearInterval(interval),log())',ms.value||1);else for(;ip<code.length;)parse();log()}