Created
October 6, 2016 00:05
-
-
Save io4/c82798e6dfd918e37fe11f67283184a7 to your computer and use it in GitHub Desktop.
VoidLang implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var run = require("./parser"); | |
run(",&[!#]|^&",false,function(o){ | |
console.log("> "+o); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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