Skip to content

Instantly share code, notes, and snippets.

@fjnl
Created Dec 12, 2011
Embed
What would you like to do?
function ariel_eval(src_ari, reader, writer) {
var src =
'(function(reader, writer) {' +
' var stack = [];' +
' var sp = 0;';
var INSNS = [
{ re: /^\s*あり\s*あり\s*あり/,
js: '++sp;' },
{ re: /^\s*あり\s*あり\s*える/,
js: '--sp;' },
{ re: /^\s*あり\s*える\s*あり/,
js: 'stack[sp] = (stack[sp] === undefined ? 0 : stack[sp]) + 1;' },
{ re: /^\s*あり\s*える\s*える/,
js: 'stack[sp] = (stack[sp] === undefined ? 0 : stack[sp]) - 1;' },
{ re: /^\s*える\s*あり\s*あり/,
js: 'writer(String.fromCharCode(stack[sp]));' },
{ re: /^\s*える\s*あり\s*える/,
js: 'stack[sp] = reader().charCodeAt(0);' },
{ re: /^\s*える\s*える\s*あり/,
js: 'while(stack[sp] !== 0) {' },
{ re: /^\s*える\s*える\s*える/,
js: '}' }
];
for (var matched = true; matched;) {
matched = false;
for (var i = 0; INSNS[i]; ++i) {
var insn = INSNS[i];
var m = insn.re.exec(src_ari);
if (m) {
src += insn.js;
src_ari = src_ari.substr(m[0].length);
matched = true;
continue;
}
}
}
src += '})';
console.log(src);
eval(src)(reader, writer);
}
function ariel_console(el) {
var console = document.createElement('div');
var input = document.createElement('textarea');
input.rows = 8;
input.cols = 40;
var src = document.createElement('textarea');
src.rows = 24;
src.cols = 80;
var eval_btn = document.createElement('input');
eval_btn.type = 'button';
eval_btn.value = 'EVAL';
el.appendChild(console);
el.appendChild(input);
el.appendChild(src);
el.appendChild(eval_btn);
eval_btn.addEventListener('click', function() {
var ip = 0;
ariel_eval(
src.value,
function() { return input.value[ip++] },
function(c) { console.innerHTML += c }
);
}, false);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment