Skip to content

Instantly share code, notes, and snippets.

@io4
Created Oct 6, 2016
Embed
What would you like to do?
VoidLang implementation
var run = require("./parser");
run(",&[!#]|^&",false,function(o){
console.log("> "+o);
});
var readlineSync = require('readline-sync');
var _EXIT = "__EXIT__";
function run(code, debug, printf){
var printFunc = printf || console.log;
var stack = [];
var output = "";
var is_push = false;
var is_loop = true;
var loop_addr = 0;
for(var i = 0;i<=code.length;i++){
if (debug && code[i]!=";") {
console.log(code[i],stack,output);
}
if(is_push&&code[i]!="\""){
stack.push(code[i]);
} else {
switch(code[i]){
case "\"":
is_push = !is_push;
break;
case "!":
output+=stack.shift();
break;
case "*":
stack.unshift(stack.shift()*stack.shift());
break;
case "/":
stack.unshift(stack.shift()/stack.shift());
break;
case "+":
stack.unshift(Number(stack.shift())+Number(stack.shift()));
break;
case "-":
stack.unshift(stack.shift()-stack.shift());
break;
case "[":
is_loop = true;
loop_addr = i;
break;
case "]":
if(is_loop) i = loop_addr;
break;
case "~":
is_loop = false;
loop_addr = 0;
break;
case "?":
if(stack[0]==0) {
is_loop = false;
loop_addr = 0;
}
break;
case "#":
if(stack.length==0||stack.length==1) {
is_loop = false;
loop_addr = 0;
}
break;
case ".":
stack.shift();
break;
case "=":
if (arguments.callee == run) return _EXIT;
return output;
break;
case "%":
stack.unshift(Math.ceil(Math.random()*255));
break;
case "$":
stack.unshift(stack.length);
break;
case "_":
if(!isNaN(parseInt(stack[0]))) {
stack.unshift(String.fromCharCode(parseInt(stack.shift())))
}
break;
case "&":
var o = run(stack.join(""),debug);
if (o == _EXIT) return output;
stack=[o];
break;
case ",":
stack.unshift(readlineSync.question());
break;
case "^":
stack.unshift(code);
break;
case "|":
printFunc(output);
output = "";
break;
case ";":
break;
}
}
}
return output;
}
module.exports = run;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment