-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
38 lines (32 loc) · 875 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
Chuck Norris Interpreter
by @angrykoala <angrykoala@outlook.es>
Allows you to execute CNPL code
*/
var bigInt = require('big-integer');
var ABC = require('./lib/abc.js');
// cnpl regex
var regex = /\[0\]{\d+}/;
module.exports = {
// Executes JavaScript cnpl code
execute: function(code) {
var txt = this.cnpl2js(code);
eval(txt);
},
// Translates from cnpl to js
cnpl2js: function(code) {
var cnpl = code.match(regex)[0];
var value = cnpl.slice(4, -1);
var num = bigInt(value);
var b1 = num.toString(2);
var b2 = b1.substring(1);
var txt = ABC.toAscii(b2);
return txt;
},
// Compiles to cnpl code
compile: function(jsCode) {
var cnc = 1 + ABC.toBinary(jsCode, "");
var num = bigInt(cnc, 2);
return "[0]{" + num.toString() + "}";
}
};