-
Notifications
You must be signed in to change notification settings - Fork 1
/
code-min.js
15 lines (15 loc) · 5.11 KB
/
code-min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
* The minified version of SIMPLE.
**/
const CODE = ""; // input goes here
var $jscomp$templatelit$2=["\n"];$jscomp$templatelit$2.raw=["\\n"];var $jscomp$templatelit$1=[" "];$jscomp$templatelit$1.raw=[" "];var $jscomp$templatelit$0=["\n\t"];$jscomp$templatelit$0.raw=["\n\t"];i=JSON.stringify;
var Interpreter=function(){var d=this;this.functions={};this.variables={};this.input=function(b){return d.interpret(d.parse(d.tokenize(b)))};this.interpret=function(b){switch(b.type){case "operator":var c=d.interpret(b.left),e=d.interpret(b.right);b=b.operator;if("+"==b)b=c+e;else if("*"==b)b=c*e;else if("-"==b)b=c-e;else if("/"==b)b=c/e;else{if("%"!=b)throw"Unknown operator";b=c%e}return b;case "number":return+b.value;case "assignment":if(b.name in d.functions)throw"Variable name collides with function name : "+
b.name;c=d.interpret(b.value);return d.variables[b.name]=c;case "identifier":return b.value in d.variables?d.variables[b.value]:function(){for(var c="first second third fourth fifth sixth seventh eighth ninth tenth eleventh twelfth".split(" "),b="A partridge in a pear tree;Two turtle doves;Three french hens;Four calling birds;Five golden rings;Six geese a-laying;Seven swans a-swimming;Eight maids a-milking;Nine ladies dancing;Ten lords a-leaping;Eleven pipers piping;Twelve drummers drumming".split(";"),
e,d=[],f=0;12>f;f++){e=[];e[0]="On the "+c[f]+" day of Christmas, my true love gave to me";for(var g=f+1,h=0;0<g--;)e[++h]=b[g];d[f]=e.join($jscomp$templatelit$0);0==f&&(b[0]="And a partridge in a pear tree")}throw d.join("\n\n");}();case "function":if(b.name in d.variables)throw"Function name collides with variable name: "+b.name;d.functions[b.name]=b;return"";case "fnCall":return c=d.functions[b.name],e=b.args.reduce(function(c,e){return c[e[0]]=d.interpret(e[1]),c},Object.create(d.variables)),
b=d.variables,d.variables=e,c=d.interpret(c.body),d.variables=b,c;case "container":return d.interpret(b.child);case "noop":b="";for(c=1;100>=c;c++)b=0==c%3&&0==c%5?b+"FizzBuzz ":0==c%3?b+"Fizz ":0==c%5?b+"Buzz ":b+(c+" ");throw b.split($jscomp$templatelit$1).join($jscomp$templatelit$2);default:throw"What type is "+i(b);}};this.tokenize=function(b){return""==b?[]:b.split(/\s*(=>|[-+*\/%=\(\)]|[A-Za-z_][A-Za-z0-9_]*|[0-9]*\.?[0-9]+)\s*/g).filter(function(c){return!c.match(/^\s*$/)})};this.parse=function(b){return P=
(new Parser(d.functions,b)).parse(),0!=b.length?"Extra tokens : "+i(b):P};this.parseString=function(b){return d.parse(d.tokenize(b))}},Parser=function(d,b){var c=this;this.functions=d;this.tokens=b;this.parse=function(){if(0==c.tokens.length)var e={type:"noop"};else if("Func"===c.tokens[0]){c.shift();e=c.tokens.shift();var b=c.parseFnArgs();c.shift();var d=c.parseExpr();c.validateIdentifiers(b,d);e={type:"function",name:e,args:b,body:d}}else e=c.parseExpr();return e};this.shift=function(){return c.tokens.shift()};
this.parseExpr=function(){var e=null;if(0===c.tokens.length)throw"Hello, World!";if(c.isIdentifier()&&"="===c.tokens[1])e=c.parseAssignment();else if(c.tokens[0].match(/^[0-9][\.0-9]*$/))e={type:"number",value:c.tokens.shift()};else if(c.isIdentifier()&&c.functions[c.tokens[0]])e=c.parseFnCall();else if(c.isIdentifier())e={type:"identifier",value:c.tokens.shift()};else if("("===c.tokens[0][0])e=c.parseContainer();else if("Func"===c.tokens[0])e=c.parseFn();else if(")"===c.tokens[0][0])throw"What is : "+
i(c.tokens);if(!c.tokens.length||!c.isOperator())return e;var b=c.shift();var d=c.parseExpr();if("operator"!==d.type||!c.shouldSwapOperators(b,d.operator))return{type:"operator",operator:b,left:e,right:d};d.left={type:"operator",operator:b,left:e,right:d.left};return d};this.isOperator=function(){return t=c.tokens[0],"+"===t||"-"===t||"*"===t||"/"===t||"%"===t};this.shouldSwapOperators=function(c,b){return"*"==c||"/"==c||"%"==c||"+"==b||"-"==b};this.isIdentifier=function(){return c.tokens[0].match(/^[a-zA-Z][_a-zA-Z0-9]*$/)};
this.parseAssignment=function(){var b=c.tokens.shift();c.shift();var d=c.parseExpr();return{type:"assignment",name:b,value:d}};this.parseFnCall=function(){var b=c.tokens.shift(),d=c.functions[b].args.map(function(b){if(0===c.tokens.length)throw"Too few arguments!";return[b,c.parse()]});return{type:"fnCall",name:b,args:d}};this.parseContainer=function(){c.shift();var b=c.parseExpr();c.shift();return{type:"container",child:b}};this.parseFnArgs=function(){for(var b=[];"=>"!==c.tokens[0];)b.push(c.tokens.shift());
if(c.containsDuplicates(b))throw"Duplicate argument names";return b};this.containsDuplicates=function(c){for(var b=0;b<c.length;++b)for(var d=b+1;d<c.length;++d)if(c[b]===c[d])return!0;return!1};this.validateIdentifiers=function(b,d){c.varNames(d).forEach(function(c){if(-1===b.indexOf(c))throw"Unknown identifier: "+c;})};this.varNames=function(b){switch(b.type){case "operator":return c.varNames(b.left).concat(c.varNames(b.right));case "number":return[];case "assignment":return c.varNames(b.value);
case "identifier":return[b.value];case "function":return[];case "fnCall":var d=[];args.forEach(function(b){return d=d.concat(b)});return d;case "container":return c.varNames(b.child);case "noop":return[];default:throw"What type is : "+i(b);}}};a=new Interpreter;try{a.input(CODE)}catch(d){console.log(d)};