Modify all the things!

This commit is contained in:
zgrep 2018-08-14 19:24:26 -04:00
parent 541abe1440
commit 1d7dbc4dbe
3 changed files with 104 additions and 13 deletions

74
ok/ircconvert.js Normal file
View File

@ -0,0 +1,74 @@
////////////////////////////////////
//
// A companion to oK which bridges
// the gap between k-values and
// native JavaScript types.
//
// John Earnest
//
////////////////////////////////////
//
// See ok-license.txt for the license.
// Modified by zgrep because zgrep is just that annoying.
"use strict";
function tok(v) {
if (typeof v == 'number') {
return { t:0, v:v };
}
if (v === false) { return { t:0, v:0 }; }
if (v === true) { return { t:0, v:1 }; }
if (typeof v == 'string') {
var r = [];
for(var z=0;z<v.length;z++) { r[z] = { t:1, v:v.charCodeAt(z) }; }
return { t:3, v:r };
}
if (v instanceof Array) {
var r = [];
for(var z=0;z<v.length;z++) { r[z] = tok(v[z]); }
return { t:3, v:r };
}
if (typeof v == 'object') {
var r = { t:4, k:{ t:3, v:[] }, v:{ t:3, v:[] }};
var k = Object.keys(v);
for(var z=0;z<k.length;z++) {
r.k.v.push( { t:2, v:k[z] } );
r.v.v.push( tok(v[k[z]]) );
}
return r;
}
throw new Error("cannot convert '"+v+"' to a K datatype.");
}
function tojs(v) {
if (v.t == 0 || v.t == 2) {
return v.v;
}
if (v.t == 1) {
return String.fromCharCode(v.v);
}
if (v.t == 3) {
var r = [];
var same = true;
for(var z=0;z<v.v.length;z++) { r[z] = tojs(v.v[z]); same &= v.v[z].t == v.v[0].t; }
if (same && v.v.length != 0 && v.v[0].t == 1) { return r.join(""); }
return r;
}
if (v.t == 4) {
var r = {};
for(var z=0;z<v.k.v.length;z++) { r[v.k.v[z]] = tojs(v.v.v[z]); }
return r;
}
throw new Error("cannot convert '"+JSON.stringify(v)+"' to a JavaScript datatype.");
}
function trampoline(env, name, args, body) {
// construct a function-wrapper trampoline for a pseudonative.
// this permits dyadic/triadic extension functions to be properly curryable.
var arguse = []; for(var z=0; z<args.length; z++) { arguse.push({"t":7, "v":args[z]}); }
env.put(ks(name), true, {t: 5, args:args, v: [ {"t":8,"v":name,"curry":arguse} ]});
}
this.tok = tok;
this.tojs = tojs;

View File

@ -4,25 +4,25 @@ var ok = require('./ircoK');
var fs = require('fs');
var os = require('os');
var readline = require('readline');
var conv = require('./convert');
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var conv = require('./ircconvert');
const spawnSync = require('child_process').spawnSync;
const crypto = require('crypto');
const happydir = '/tmp/happyok';
// web stuff
function readAjax(x) {
var url = conv.tojs(x);
if (typeof url != 'string') { throw new Error("string expected."); }
var request = new XMLHttpRequest();
request.open('GET', url, false);
request.send(null);
return [request.status, request.responseText];
function curl(x) {
let url = conv.tojs(x);
let res = spawnSync('curl', ['-s', '-L', '--url', url], {'timeout': 5000});
if (res.stderr != "") {
throw new Error(res.stderr);
}
return res.stdout.toString('utf-8');
}
function readText(x) {
return conv.tok(curl(x));
}
function readText(x) { return conv.tok(readAjax(x)); }
function readJSON(x) {
var t = readAjax(x);
t[1] = JSON.parse(t[1]);
return conv.tok(t);
return conv.tok(JSON.parse(curl(x)));
}
ok.setIO('0:', 1, readText);

View File

@ -6,3 +6,20 @@ $ cat oK.js appendoK.js > ircoK.js
```
Now use `ircrepl.js` for a repl.
---
Differences and similarities with ok:
- `0:` reads from a URL.
- `1:` does the same, but decodes json.
- Has trig functions! `sin, cos, tan, asin, acos, atan, sinh, cosh, tanh, tanh2`
- Has other functions! `abs, pow`
- Supports `n\n`.
- Has whatever's in the [okrc](https://zgrep.org/taxreturns/okrc.txt) as a thing by default too.
- `2+2` is 5 (unless it's within some other computation, I'm not that mean).
- Some useful "commands":
- `reset` resets variables and stuff.
- `manual` links to [this manual](https://github.com/JohnEarnest/ok/blob/gh-pages/docs/Manual.md).
- `reference` or `ref` links to http://kparc.com/k.txt
- `okrc` or `rc` links to the okrc file.