Skip to content

Instantly share code, notes, and snippets.

@haileys
Created October 23, 2011 05:03
Show Gist options
  • Save haileys/1306897 to your computer and use it in GitHub Desktop.
Save haileys/1306897 to your computer and use it in GitHub Desktop.
var program = "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.";
var memory = [0];
var ptr = 0;
var stack = [];
var ip = 0;
while(ip < program.length) {
if(program[ip] === '[') {
if(!memory[ptr]) {
var nesting = 0;
while(true) {
if(program[ip] === '[') {
++nesting;
} else if(program[ip] === ']') {
if(--nesting === 0) {
break;
}
}
++ip;
}
} else {
stack.push(ip);
}
} else if(program[ip] === ']') {
ip = stack.pop();
continue;
} else if(program[ip] === '>') {
++ptr;
memory[ptr] = memory[ptr] || 0;
} else if(program[ip] === '<') {
--ptr;
memory[ptr] = memory[ptr] || 0;
} else if(program[ip] === '+') {
memory[ptr] += 1;
} else if(program[ip] === '-') {
memory[ptr] -= 1;
} else if(program[ip] === '.') {
console._print(String.fromCharCode(memory[ptr]));
}
++ip;
}
main:
.local program
.local memory
.local ptr
.local stack
.local ip
.local nesting
push "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>."
set program
push 0
array 1
set memory
push 0
set ptr
array 0
set stack
push 0
set ip
push ip
push program
member length
lt
jif 173
push program
push ip
index
push "["
seq
jif 71
push memory
push ptr
index
not
jif 65
push 0
set nesting
true
jif 64
push program
push ip
index
push "["
seq
jif 47
push nesting
inc
set nesting
jmp 60
push program
push ip
index
push "]"
seq
jif 60
push nesting
dec
set nesting
push 0
seq
jif 60
jmp 64
push ip
inc
set ip
jmp 35
jmp 70
push stack
dup
member push
push ip
thiscall 1
jmp 169
push program
push ip
index
push "]"
seq
jif 84
push stack
dup
member pop
thiscall 0
set ip
jmp 17
jmp 169
push program
push ip
index
push ">"
seq
jif 104
push ptr
inc
set ptr
push memory
push ptr
push memory
push ptr
index
dup
jit 102
pop
push 0
setindex
jmp 169
push program
push ip
index
push "<"
seq
jif 124
push ptr
dec
set ptr
push memory
push ptr
push memory
push ptr
index
dup
jit 122
pop
push 0
setindex
jmp 169
push program
push ip
index
push "+"
seq
jif 138
push memory
push ptr
dup 2
index
push 1
add
setindex
jmp 169
push program
push ip
index
push "-"
seq
jif 152
push memory
push ptr
dup 2
index
push 1
sub
setindex
jmp 169
push program
push ip
index
push "."
seq
jif 169
push console
dup
member _print
push String
dup
member fromCharCode
push memory
push ptr
index
thiscall 1
thiscall 1
push ip
inc
set ip
jmp 17
undefined
ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment