Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String.fromCodePoint() and A.e(a) #42

Merged
merged 3 commits into from
Jan 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/master.html
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,8 @@ <h3>`.d(f)`</h3>
<p>Maps each item in `this` through `f`, then returns `true` if any are truthy, and `false` otherwise.</p>
<h3>`.e(f)`</h3>
<p>Maps each item in `this` through `f`, then returns `true` if all are truthy, and `false` otherwise.</p>
<h3>`.e(a)`</h3>
<p>Returns `true` if the length of `this``is equal to the length of `a` and each element in `this` is equal to the corresponing element in `a`, and `false` otherwise.</p>
<h3>`.f(f)`</h3>
<p>Selects the items in `this` that return truthily to `f`.</p>
<h3>`.f(a)`</h3>
Expand Down
6 changes: 4 additions & 2 deletions docs/v2.html
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,8 @@ <h3>`.d(f)`</h3>
<p>Maps each item in `this` through `f`, then returns `true` if any are truthy, and `false` otherwise.</p>
<h3>`.e(f)`</h3>
<p>Maps each item in `this` through `f`, then returns `true` if all are truthy, and `false` otherwise.</p>
<h3>`.e(a)`</h3>
<p>Returns `true` if the length of `this``is equal to the length of `a` and every element in `this` is equal to the corresponing element in `a`, and `false` otherwise.</p>
<h3>`.f(f)`</h3>
<p>Selects the items in `this` that return truthily to `f`.</p>
<h3>`.f(a)`</h3>
Expand Down Expand Up @@ -466,8 +468,8 @@ <h3>`.b(n,n)`</h3>
<p>Returns `this` bounded with min `n₁` and max `n₂`.</p>
<h3>`.c(n=1)`</h3>
<p>Returns `this` rounded up to the nearest multiple of `n`.</p>
<h3>`.d()`</h3>
<p>Returns the char at code-point `this`.</p>
<h3>`.d(n=0)`</h3>
<p>Returns the char at code-point `this+n`.</p>
<h3>`.e(n)`</h3>
<p>Returns `this*Math.pow(10,n)`.</p>
<h3>`.f(n=1)`</h3>
Expand Down
28 changes: 14 additions & 14 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Japt interpreter</title>
<link rel="stylesheet" type="text/css" href="../style/style.css">
<link href='https://fonts.googleapis.com/css?family=Open+Sans:400,700' rel='stylesheet' type='text/css'>
Expand Down Expand Up @@ -64,7 +64,7 @@ <h1>&nbsp;&nbsp;Japt Interpreter</h1>
var defv = "1.4.5";
var v = defv;
var code, input;

function escapedBtoA(text) {
return btoa(
text.replace(/./g,function(x){
Expand All @@ -74,11 +74,11 @@ <h1>&nbsp;&nbsp;Japt Interpreter</h1>
})
);
}

function escapedAtoB(text) {
return atob(text).replace(/\\u[0-9A-Fa-f]{4}/g,function(x){return String.fromCharCode(parseInt(x.slice(2),16))});
}

function splitAddress() {
var args=location.search.match(/[&?](code|input|v)=([^&]|&(?!(code|input|v)=))*/g);
if (args) for (var q in args) {
Expand Down Expand Up @@ -125,7 +125,7 @@ <h1>&nbsp;&nbsp;Japt Interpreter</h1>
var script = document.createElement('script');
script.setAttribute("src", "https://rawgit.com/ETHproductions/japt/" + (realv === 'v2' ? 'master' : realv) + file);
document.getElementsByTagName('head')[0].appendChild(script);

var docs = document.getElementById('docs');
function addDocs(header, text) {
docs.innerHTML = header;
Expand All @@ -138,7 +138,7 @@ <h1>&nbsp;&nbsp;Japt Interpreter</h1>
})
.replace(/arrow:([a-z-]+)/gi, '<a class="arrow"; id="toggle-$1"; onclick="toggleTab(\'$1\')" style="background-color:#3E2">▼</a>');
}

var versionText;
function addMaster (x) {
return function () {
Expand All @@ -149,7 +149,7 @@ <h1>&nbsp;&nbsp;Japt Interpreter</h1>
});
}
}

if (iscommit) {
versionText = "<p>You're using Japt " + (isv2 ? "v2 " : "") + "<a target='_blank' href='https://github.com/ETHproductions/japt/tree/" + realv + "'>commit " + realv.slice(0, 7) + "</a>.</p>";
getWebpage('https://rawgit.com/ETHproductions/Japt/' + realv + '/docs/' + (isv2 ? 'v2.html' : 'master.html'), function (response) {
Expand Down Expand Up @@ -185,7 +185,7 @@ <h1>&nbsp;&nbsp;Japt Interpreter</h1>
request.open("GET", url);
request.send();
}

function setup() {
if(typeof code !== "undefined" && !document.getElementById("code").value) document.getElementById("code").value = code;
if(typeof input !== "undefined" && !document.getElementById("input").value) document.getElementById("input").value = input;
Expand All @@ -195,27 +195,27 @@ <h1>&nbsp;&nbsp;Japt Interpreter</h1>
}
update();
}

function update() {
var code = document.getElementById("code").value, encoding = "ISO-8859-1";
if (/[^\x00-\xff]/.test(code)) code = unescape(encodeURI(code)), encoding = "UTF-8";
document.getElementById('bytes').innerHTML = code.length + " byte" + (code.length === 1 ? "" : "s") + " in " + encoding;
document.getElementById('js-code').value = (window.Japt || window).transpile(document.getElementById('code').value);
}

function getURL() {
var code = escapedBtoA(document.getElementById("code").value);
var input = escapedBtoA(document.getElementById("input").value);
document.getElementById("output").value = "https://ethproductions.github.io/japt/?v="+v+"&code="+code+"&input="+input;
}

function toggleTab(tab) {
var style = document.getElementById(tab).style;
if (style.display == "none") style.display = "block"; else style.display = "none";
var text = document.getElementById("toggle-" + tab);
if (text.innerHTML == "▼") text.innerHTML="▲"; else text.innerHTML = "▼";
}

function toggleAll(src) {
var arrows = document.getElementsByClassName("arrow");
for (var i in arrows) {
Expand All @@ -225,7 +225,7 @@ <h1>&nbsp;&nbsp;Japt Interpreter</h1>
}
src.innerHTML = (src.innerHTML[0] === "▼" ? "▲ Collapse all" : "▼ Expand all");
}

function onrun() {
var start;
if (window.Japt) {
Expand All @@ -250,7 +250,7 @@ <h1>&nbsp;&nbsp;Japt Interpreter</h1>
document.getElementById('time').innerHTML = '(took ' + ((new Date - start) / 1e3).toFixed(3) + ' seconds)';
}
}

splitAddress();
var int = setInterval(function(){if(window.Japt || window.run) setup(), clearInterval(int)}, 100);
</script>
Expand Down
68 changes: 34 additions & 34 deletions src/japt-interpreter.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var isnode = typeof window === "undefined";
if (isnode) var shoco = require("../dependencies/shoco");
Object.defineProperty(isnode ? global : window, "K", { enumerable: false, configurable: true, get: function() { return fb(_K, new Date()); }, set: function(x) { return _K=x; }});

var pairs_1_3 = {
var pairs_1_3 = {
// Unicode shortcuts
// Using \u<hex> to avoid encoding incompatibilities
"@": "XYZ{",
Expand Down Expand Up @@ -69,7 +69,7 @@ var pairs_1_3 = {
"\xDF": "$rp$(" // ß - 223
};

var pairs_2_0 = {
var pairs_2_0 = {
// A new list of Unicode shortcuts
// Using \u<hex> to avoid encoding incompatibilities
"@": "XYZ{",
Expand Down Expand Up @@ -188,7 +188,7 @@ df(Array,'a',function(x,y){if(id(y))x=functify(x,y);return typeof x=="function"?
df(Array,'b',function(x,y){if(id(y))x=functify(x,y);return typeof x=="function"?this.map(function(a,b,c){return!!x(a,fb(y,b),c)}).indexOf(true):this.indexOf(x)});
df(Array,'c',function(x){if(id(x))return this.concat(x);var f=[];for(var i of this){if(i instanceof Array)for(var j of i.c())f.push(j);else f.push(i);}return f});
df(Array,'d',function(x,y){x=fb(x,function(y){return!!y});x=functify(x,y);return this.some(function(a,b,c){return x(a,fb(y,b),c)})});
df(Array,'e',function(x,y){x=fb(x,function(y){return!!y});x=functify(x,y);return this.every(function(a,b,c){return x(a,fb(y,b),c)})});
df(Array,'e',function(x,y){if(x instanceof Array){return this.length===x.length&&this.every(function(a,b){return a==x[b]})}else{x=fb(x,function(y){return!!y});x=functify(x,y);return this.every(function(a,b,c){return x(a,fb(y,b),c)})}});
df(Array,'f',function(x,y){if(x instanceof Array){y=fb(y,0)%3;if(y===2)return this.filter(function(q){var a=x.indexOf(q);if(~a)x.splice(a,1);return~a});else if(y===1)return this.filter(function(q,i,a){return~x.indexOf(q)&&a.indexOf(q)===i});else return this.filter(function(q){return~x.indexOf(q)})}x=fb(x,function(y){return!!y});x=functify(x,y);return this.filter(function(a,b,c){return x(a,fb(y,b),c)})});
df(Array,'g',function(x){var l=this.length;x=pm(fb(x,0),l);return this[x]});
df(Array,'h',function(x,y){var l=this.length,z;if(!id(y))y=x,x=0;if(typeof x!=="number"&&typeof y==="number")z=x,x=y,y=z;x=pm(x,l);this[x]=y;return this});
Expand Down Expand Up @@ -242,7 +242,7 @@ df(Array,'\xFB',function(x,y){return this.pad(x,y,0)});
df(Number,'a',function(x){x=fb(x,0);return Math.abs(this-x)});
df(Number,'b',function(x,y){return this<x?x:this>y?y:this});
df(Number,'c',function(x){x=fb(x,1);return Math.ceil(this/x)*x});
df(Number,'d',function(x){x=fb(x,0);return String.fromCharCode(this+x)});
df(Number,'d',function(x){x=fb(x,0);return String.fromCodePoint(this+x)});
df(Number,'e',function(x){return this*Math.pow(10,x)});
df(Number,'f',function(x){x=fb(x,1);return Math.floor(this/x)*x});
df(Number,'g',function(){return this.toString()=="NaN"?"NaN":this<0?-1:this>0?1:0});
Expand Down Expand Up @@ -342,7 +342,7 @@ Math.S = Math.SQRT_2;
Math.T = Math.PI * 2;

// String compression
shoco.c = function (str) { return Array.prototype.map.call(shoco.compress(str), function (char) { return String.fromCharCode(char) }).join('') };
shoco.c = function (str) { return Array.prototype.map.call(shoco.compress(str), function (char) { return String.fromCodePoint(char) }).join('') };
shoco.d = function (str) { return shoco.decompress(new Uint8Array( ( str.constructor == Array ? str[0] : str ).split('').map(function (char) {return char.charCodeAt(0)})))};

void(0);
Expand Down Expand Up @@ -471,10 +471,10 @@ function deparen(snippet) {
var rp, program;

var Japt = {

stdout: null,
stderr: null,

clear_output: function() {
if (isnode) {
// Not sure how to do this... Would console.log("\033c") work?
Expand All @@ -491,7 +491,7 @@ var Japt = {
}
}
},

output: function(x) {
Japt.implicit_output = false;
if (isnode) {
Expand All @@ -502,13 +502,13 @@ var Japt = {
alert ("Error: Japt.stdout must be sent to an HTMLElement");
}
},

stop: function() {
for (var i = 0; i < Japt.intervals.length; i++)
clearInterval(Japt.intervals[i]);
Japt.intervals = [];
},

error: function(msg) {
if (isnode) {
process.stderr.write(msg);
Expand All @@ -519,7 +519,7 @@ var Japt = {
alert ("Error: Japt.stderr must be sent to an HTMLElement");
}
},

evalInput: function(input) {
if (input.constructor === Array) return input;
var input_mode = "next", current, processed = [], level = 0;
Expand Down Expand Up @@ -610,15 +610,15 @@ var Japt = {
}
return processed;
},

strings: [],
use_safe: false,
is_safe: false,
implicit_output: true,

run: function(code, input, safe, before, onsuccess, onerror) {
Japt.clear_output();

input = Japt.evalInput(input);
A = 10,
B = 11,
Expand Down Expand Up @@ -657,9 +657,9 @@ var Japt = {
X = 3 in N ? N[3] : 0,
Y = 4 in N ? N[4] : 0,
Z = 5 in N ? N[5] : 0;

Japt.use_safe = fb(safe, false), Japt.is_safe = true, Japt.implicit_output = true, Japt.flags = input.flags || {};

code = Japt.transpile(code);
if (!Japt.is_safe) {
if (onerror) onerror(new Error("Raw JS cannot be used in safe mode"));
Expand Down Expand Up @@ -687,7 +687,7 @@ var Japt = {
return program.cache[id] = eval(code);
};
var result;

if (Japt.flags.m || Japt.flags.d || Japt.flags.e || Japt.flags.f || Japt.flags.æ) {
if (Japt.flags.d) result = false;
else if (Japt.flags.e) result = true;
Expand Down Expand Up @@ -728,32 +728,32 @@ var Japt = {
} else {
result = program(U,V,W,X,Y,Z);
}

if (Japt.flags.hasOwnProperty('h')) result = result.g(-1);
else if (Japt.flags.hasOwnProperty('g')) result = result.g(Japt.flags.g === true ? 0 : Japt.flags.g);

if (Japt.flags.hasOwnProperty('!')) result = !result;
else if (Japt.flags.hasOwnProperty('¡')) result = !!result;

if (Japt.flags.N) result = +result;

if (Japt.flags.P && result instanceof Array) result = result.join("");
else if (Japt.flags.Q) result = JSON.stringify(result);
else if (Japt.flags.R && result instanceof Array) result = result.join("\n");
else if (Japt.flags.S && result instanceof Array) result = result.join(" ");

if (Japt.flags.x) result = result.x();

if (onsuccess) onsuccess(result);
} catch (e) {
if (onerror) onerror(e);
}
},

transpile: function(code) {
Japt.strings = [];
Japt.intervals = [];

function pretranspile(code) {
var level = 0, // Current number of parentheses or curly braces that we're inside
extrabraces = Array(20).fill(0),
Expand All @@ -766,7 +766,7 @@ var Japt = {
lines = [],
strchars = Array(20).fill(""),
internary = false;

for (; i < code.length; ++i) {
var char = code[i];
if (level === 0) {
Expand Down Expand Up @@ -915,13 +915,13 @@ var Japt = {
lines.push(subtranspile(newcode));
return lines.join("");
}

function subtranspile(code) {
var level = 0, // Current number of parentheses or curly braces that we're inside
i = 0,
outp = "", // Temporary output
extraparen = false;

for (i = 0; i < code.length; ++i) {
var char = code[i],
opMatch = code.slice(i).match(/^(===|!==|==|!=|>>>|>>|<<|&&|\|\||>=|<=|\+(?!\+)|-(?!-)|\.(?!\d)|[*%^&|<>,])(?!=)/),
Expand All @@ -941,7 +941,7 @@ var Japt = {
code = code.slice(0,i)+'2'+code.slice(i);
else if ((outp === "" || outp.slice(-1) === ";") && /[a-zà-ÿ*/%^|&<=>?]/.test(char))
outp += "U";

if (char === "\"") {
var tms = code.slice(i).match(/"(\d+)"/)[0];
outp += tms;
Expand Down Expand Up @@ -1034,14 +1034,14 @@ var Japt = {
outp += char;
}
}

outp = fixParens(outp);

return outp;
}

var outp = pretranspile(code);

outp = outp
.replace(/(\+\+|--)[A-Z]|[A-Z](\+\+|--)/g, function(s) { Japt.strings.push("(" + s + ")"); return "\"" + (Japt.strings.length - 1) + "\""; })
.replace(/[,;]/g, "$& ")
Expand All @@ -1052,7 +1052,7 @@ var Japt = {
outp = outp.replace(/"(\d+)"/g,function(_,a){return Japt.strings[+a]});
return outp;
},

eval: function(code) {
return eval(Japt.transpile(code));
}
Expand Down
Loading