Skip to content

Instantly share code, notes, and snippets.

@haileys
Created October 23, 2011 00:56
Show Gist options
  • Save haileys/1306711 to your computer and use it in GitHub Desktop.
Save haileys/1306711 to your computer and use it in GitHub Desktop.
var program = "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.";
var memory = [];
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;
} else if(program[ip] === '<') {
--ptr;
} else if(program[ip] === '+') {
memory[ptr] = (memory[ptr] || 0) + 1;
} else if(program[ip] === '-') {
memory[ptr] = (memory[ptr] || 0) - 1;
} else if(program[ip] === '.') {
console.log(String.fromCharCode(memory[ptr]));
}
++ip;
}
main:
.local program
.local memory
.local ptr
.local stack
.local ip
.local nesting
push "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>."
set program
array 0
set memory
push 0
set ptr
array 0
set stack
push 0
set ip
push ip
push program
member length
seq
not
jif 163
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 159
push program
push ip
index
push "]"
seq
jif 84
push stack
dup
member pop
thiscall 0
set ip
push continue
jmp 159
push program
push ip
index
push ">"
seq
jif 94
push ptr
inc
set ptr
jmp 159
push program
push ip
index
push "<"
seq
jif 104
push ptr
dec
set ptr
jmp 159
push program
push ip
index
push "+"
seq
jif 123
push memory
push ptr
index
dup
jit 119
pop
push 0
push 1
add
push memory
push ptr
setindex
jmp 159
push program
push ip
index
push "-"
seq
jif 142
push memory
push ptr
index
dup
jit 138
pop
push 0
push 1
sub
push memory
push ptr
setindex
jmp 159
push program
push ip
index
push "."
seq
jif 159
push console
dup
member log
push String
dup
member fromCharCode
push memory
push ptr
index
thiscall 1
thiscall 1
push ip
inc
set ip
jmp 16
undefined
ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment