New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@fraczak/k

Package Overview
Dependencies
Maintainers
1
Versions
35
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@fraczak/k - npm Package Compare versions

Comparing version 1.0.1 to 1.1.0

bnat.k

3

index.js

@@ -12,3 +12,4 @@ // Generated by CoffeeScript 2.7.0

compile = function(script) {
return (function(exp) {
return (function({defs, exp}) {
run.defs = defs;
return function(data) {

@@ -15,0 +16,0 @@ return run(exp, data);

// Generated by CoffeeScript 2.7.0
(function() {
//!/usr/bin/env coffee
var fs, jsonStream, k, kScript, prog,
var fs, jsonStream, k, kScript, oneJson, prog,
splice = [].splice;

@@ -13,3 +13,3 @@

({kScript, jsonStream} = (function(args) {
({kScript, jsonStream, oneJson} = (function(oneJson, args) {
var e;

@@ -29,2 +29,6 @@ try {

jsonStream = (function(arg) {
if (arg === "-1") {
oneJson = true;
arg = args.shift();
}
if (arg == null) {

@@ -35,41 +39,58 @@ return process.stdin;

})(args.shift());
return {kScript, jsonStream};
return {kScript, jsonStream, oneJson};
} catch (error) {
e = error;
console.error(e);
console.error(`Usage: ${prog} ( k-expr | -k k-file ) [ json-file ]`);
console.error(`Usage: ${prog} ( k-expr | -k k-file ) [ -1 ] [ json-file ]`);
console.error(` E.g., echo '{\"a\": 12}' | ${prog} '[(),()]'`);
return process.exit(-1);
}
})(process.argv.slice(2)));
})(false, process.argv.slice(2)));
(function(buffer, line) {
return jsonStream.on('data', function(data) {
var e, first, i, json, last, len, ref, rest, results, todo;
[first, ...rest] = data.toString("utf8").split("\n");
buffer.push(first);
if (rest.length > 0) {
todo = buffer.join("");
ref = rest, [...rest] = ref, [last] = splice.call(rest, -1);
buffer = [last];
todo = [todo, ...rest];
results = [];
for (i = 0, len = todo.length; i < len; i++) {
json = todo[i];
if (!json.match(/^[ \n\t]*(?:#.*)?$/)) {
try {
console.log(JSON.stringify(kScript(JSON.parse(json))));
} catch (error) {
e = error;
console.error(`Problem [line ${line}]: '${json}'`);
console.error(e);
if (oneJson) {
(function(buffer) {
jsonStream.on('data', function(data) {
return buffer.push(data);
});
return jsonStream.on('end', function() {
var e;
try {
return console.log(JSON.stringify(kScript(JSON.parse(buffer.join('')))));
} catch (error) {
e = error;
return console.error(e);
}
});
})([]);
} else {
(function(buffer, line) {
return jsonStream.on('data', function(data) {
var e, first, i, json, last, len, ref, rest, results, todo;
[first, ...rest] = data.toString("utf8").split("\n");
buffer.push(first);
if (rest.length > 0) {
todo = buffer.join("");
ref = rest, [...rest] = ref, [last] = splice.call(rest, -1);
buffer = [last];
todo = [todo, ...rest];
results = [];
for (i = 0, len = todo.length; i < len; i++) {
json = todo[i];
if (!json.match(/^[ \n\t]*(?:#.*)?$/)) {
try {
console.log(JSON.stringify(kScript(JSON.parse(json))));
} catch (error) {
e = error;
console.error(`Problem [line ${line}]: '${json}'`);
console.error(e);
}
}
results.push(line = line + 1);
}
results.push(line = line + 1);
return results;
}
return results;
}
});
})([], 0);
});
})([], 0);
}
}).call(this);
{
"name": "@fraczak/k",
"version": "1.0.1",
"description": "k-language for untyped JSON like data transformation",
"version": "1.1.0",
"description": "k-language for JSON-like data transformation",
"main": "index.js",

@@ -9,3 +9,3 @@ "scripts": {

"prepublishOnly": "coffee -c *.coffee; echo '#!/usr/bin/env node' | cat - k.js > k.sh",
"test": "coffee -c symbol-table.coffee; jison parser.jison; coffee test.coffee;"
"test": "coffee -c symbol-table.coffee; jison parser.jison; coffee test.coffee"
},

@@ -12,0 +12,0 @@ "author": "Wojciech Fraczak",

@@ -75,8 +75,8 @@ /* parser generated by jison 0.4.18 */

var parser = (function(){
var o=function(k,v,o,l){for(o=o||{},l=k.length;l--;o[k[l]]=v);return o},$V0=[1,12],$V1=[1,13],$V2=[1,14],$V3=[1,4],$V4=[1,5],$V5=[1,6],$V6=[1,8],$V7=[1,11],$V8=[4,6,8,11,13,16,18,19,20,21,22,23,26],$V9=[2,24],$Va=[2,10],$Vb=[2,13],$Vc=[4,6,8,11,13,15,16,18,19,20,21,22,23,26],$Vd=[15,26],$Ve=[18,20,26],$Vf=[4,6,8,13,16,19,21,23],$Vg=[2,22],$Vh=[2,23];
var o=function(k,v,o,l){for(o=o||{},l=k.length;l--;o[k[l]]=v);return o},$V0=[4,6,8,15,17,20,22,31,33],$V1=[1,7],$V2=[1,15],$V3=[1,16],$V4=[1,8],$V5=[1,9],$V6=[1,10],$V7=[1,11],$V8=[1,14],$V9=[1,20],$Va=[4,6,8,12,15,17,20,22,31,33],$Vb=[2,25],$Vc=[1,24],$Vd=[1,25],$Ve=[1,26],$Vf=[4,6,8,12,14,15,17,19,20,21,22,23,26,31,32,33],$Vg=[2,42],$Vh=[2,28],$Vi=[2,8],$Vj=[2,12],$Vk=[19,26],$Vl=[21,23,26],$Vm=[19,23,26],$Vn=[2,38],$Vo=[2,39];
var parser = {trace: function trace () { },
yy: {},
symbols_: {"error":2,"name":3,"NAME":4,"str":5,"STRING":6,"int":7,"INT":8,"input_with_eof":9,"comp":10,"EOF":11,"exp":12,"LC":13,"labelled":14,"RC":15,"LB":16,"list":17,"RB":18,"LA":19,"RA":20,"LP":21,"RP":22,"DOT":23,"non_empty_labelled":24,"comp_label":25,"COMMA":26,"non_empty_list":27,"$accept":0,"$end":1},
terminals_: {2:"error",4:"NAME",6:"STRING",8:"INT",11:"EOF",13:"LC",15:"RC",16:"LB",18:"RB",19:"LA",20:"RA",21:"LP",22:"RP",23:"DOT",26:"COMMA"},
productions_: [0,[3,1],[5,1],[7,1],[9,2],[10,1],[10,2],[12,3],[12,3],[12,3],[12,1],[12,2],[12,3],[12,1],[12,1],[12,2],[12,2],[12,2],[14,0],[14,1],[24,1],[24,3],[25,2],[25,2],[17,0],[17,1],[27,1],[27,3]],
symbols_: {"error":2,"name":3,"NAME":4,"str":5,"STRING":6,"int":7,"INT":8,"input_with_eof":9,"defs":10,"comp":11,"EOF":12,"EQ":13,"SC":14,"DOLLAR":15,"code":16,"LC":17,"labelled_codes":18,"RC":19,"LB":20,"RB":21,"LA":22,"RA":23,"non_empty_labelled_codes":24,"code_label":25,"COMMA":26,"COL":27,"exp":28,"labelled":29,"list":30,"LP":31,"RP":32,"DOT":33,"non_empty_labelled":34,"comp_label":35,"non_empty_list":36,"$accept":0,"$end":1},
terminals_: {2:"error",4:"NAME",6:"STRING",8:"INT",12:"EOF",13:"EQ",14:"SC",15:"DOLLAR",17:"LC",19:"RC",20:"LB",21:"RB",22:"LA",23:"RA",26:"COMMA",27:"COL",31:"LP",32:"RP",33:"DOT"},
productions_: [0,[3,1],[5,1],[7,1],[9,3],[10,0],[10,5],[10,6],[16,1],[16,3],[16,3],[16,3],[18,0],[18,1],[24,1],[24,3],[25,2],[25,2],[25,3],[25,3],[11,1],[11,2],[28,3],[28,3],[28,3],[28,1],[28,2],[28,3],[28,1],[28,1],[28,2],[28,2],[28,2],[28,2],[29,0],[29,1],[34,1],[34,3],[35,2],[35,2],[35,3],[35,3],[30,0],[30,1],[36,1],[36,3]],
performAction: function anonymous(yytext, yyleng, yylineno, yy, yystate /* action[1] */, $$ /* vstack */, _$ /* lstack */) {

@@ -98,64 +98,109 @@ /* this == yyval */

// console.log(JSON.stringify($$[$0-1], "", 2));
return $$[$0-1];
result = {defs: $$[$0-2], exp: $$[$0-1]};
// console.log(JSON.stringify(result, "", 2));
return result;
break;
case 5: case 19: case 25:
case 5:
this.$ = {codes: {}, rels: {}};
break;
case 6:
$$[$0-4].rels[$$[$0-3]] = $$[$0-1]; this.$ = $$[$0-4];
break;
case 7:
$$[$0-5].codes[$$[$0-3]] = $$[$0-1]; this.$ = $$[$0-5];
break;
case 8:
this.$ = {code: "ref", ref: $$[$0]};
break;
case 9:
this.$ = {code: "product",
product: $$[$0-1].reduce((r, lc) => {
r[lc.label] = lc.code;
return r }
, {})};
break;
case 10:
this.$ = {code: "vector", vector: $$[$0-1]};
break;
case 11:
this.$ = {code: "union",
union: $$[$0-1].reduce((r, lc) => {
r[lc.label] = lc.code;
return r }
, {})};
break;
case 12:
this.$ = []
break;
case 13: case 20: case 35: case 43:
this.$ = $$[$0];
break;
case 6:
case 14: case 44:
this.$ = [$$[$0]];
break;
case 15: case 45:
this.$ = [].concat($$[$0-2],$$[$0]);
break;
case 16: case 17:
this.$ = {label: $$[$0], code: $$[$0-1]};
break;
case 18: case 19:
this.$ = {label: $$[$0-2], code: $$[$0]};
break;
case 21:
this.$ = s.comp($$[$0-1], $$[$0]);
break;
case 7:
case 22:
this.$ = $$[$0-1];
break;
case 8:
case 23:
this.$ = {op: "vector", vector: $$[$0-1]};
break;
case 9:
case 24:
this.$ = {op: "union", union: $$[$0-1]};
break;
case 10:
case 25:
this.$ = {op: "ref", ref: $$[$0]};
break;
case 11:
case 26:
this.$ = s.identity;
break;
case 12:
case 27:
this.$ = $$[$0-1];
break;
case 13:
case 28:
this.$ = {op: "str", str: $$[$0]};
break;
case 14:
case 29:
this.$ = {op: "int", int: $$[$0]};
break;
case 15: case 16: case 17:
case 30: case 31: case 32:
this.$ = {op: "dot", dot: $$[$0]};
break;
case 18:
case 33:
this.$ = {op: "code", code: $$[$0]};
break;
case 34:
this.$ = {op: "product", product: []};
break;
case 20:
case 36:
this.$ = {op: "product", product: [$$[$0]]};
break;
case 21:
case 37:
$$[$0-2].product = [].concat($$[$0-2].product,$$[$0]); this.$ = $$[$0-2];
break;
case 22: case 23:
case 38: case 39:
this.$ = {label: $$[$0], exp: $$[$0-1]};
break;
case 24:
case 40: case 41:
this.$ = {label: $$[$0-2], exp: $$[$0]};
break;
case 42:
this.$ = [];
break;
case 26:
this.$ = [$$[$0]];
break;
case 27:
this.$ = [].concat($$[$0-2],$$[$0]);
break;
}
},
table: [{3:7,4:$V0,5:9,6:$V1,7:10,8:$V2,9:1,10:2,12:3,13:$V3,16:$V4,19:$V5,21:$V6,23:$V7},{1:[3]},{3:7,4:$V0,5:9,6:$V1,7:10,8:$V2,11:[1,15],12:16,13:$V3,16:$V4,19:$V5,21:$V6,23:$V7},o($V8,[2,5]),{3:7,4:$V0,5:9,6:$V1,7:10,8:$V2,10:20,12:3,13:$V3,14:17,15:[2,18],16:$V4,19:$V5,21:$V6,23:$V7,24:18,25:19},{3:7,4:$V0,5:9,6:$V1,7:10,8:$V2,10:23,12:3,13:$V3,16:$V4,17:21,18:$V9,19:$V5,21:$V6,23:$V7,27:22},{3:7,4:$V0,5:9,6:$V1,7:10,8:$V2,10:23,12:3,13:$V3,16:$V4,17:24,19:$V5,20:$V9,21:$V6,23:$V7,27:22},o($V8,$Va),{3:7,4:$V0,5:9,6:$V1,7:10,8:$V2,10:26,12:3,13:$V3,16:$V4,19:$V5,21:$V6,22:[1,25],23:$V7},o($V8,$Vb),o($V8,[2,14]),{3:29,4:$V0,5:28,6:$V1,7:27,8:$V2},o($Vc,[2,1]),o($Vc,[2,2]),o($V8,[2,3]),{1:[2,4]},o($V8,[2,6]),{15:[1,30]},{15:[2,19],26:[1,31]},o($Vd,[2,20]),{3:32,4:$V0,5:33,6:$V1,7:10,8:$V2,12:16,13:$V3,16:$V4,19:$V5,21:$V6,23:$V7},{18:[1,34]},o([18,20],[2,25],{26:[1,35]}),o($Ve,[2,26],{3:7,5:9,7:10,12:16,4:$V0,6:$V1,8:$V2,13:$V3,16:$V4,19:$V5,21:$V6,23:$V7}),{20:[1,36]},o($V8,[2,11]),{3:7,4:$V0,5:9,6:$V1,7:10,8:$V2,12:16,13:$V3,16:$V4,19:$V5,21:$V6,22:[1,37],23:$V7},o($V8,[2,15]),o($V8,[2,16]),o($V8,[2,17]),o($V8,[2,7]),{3:7,4:$V0,5:9,6:$V1,7:10,8:$V2,10:20,12:3,13:$V3,16:$V4,19:$V5,21:$V6,23:$V7,25:38},o($Vf,$Va,{15:$Vg,26:$Vg}),o($Vf,$Vb,{15:$Vh,26:$Vh}),o($V8,[2,8]),{3:7,4:$V0,5:9,6:$V1,7:10,8:$V2,10:39,12:3,13:$V3,16:$V4,19:$V5,21:$V6,23:$V7},o($V8,[2,9]),o($V8,[2,12]),o($Vd,[2,21]),o($Ve,[2,27],{3:7,5:9,7:10,12:16,4:$V0,6:$V1,8:$V2,13:$V3,16:$V4,19:$V5,21:$V6,23:$V7})],
defaultActions: {15:[2,4]},
table: [o($V0,[2,5],{9:1,10:2}),{1:[3]},{3:4,4:$V1,5:12,6:$V2,7:13,8:$V3,11:3,15:[1,5],17:$V4,20:$V5,22:$V6,28:6,31:$V7,33:$V8},{3:19,4:$V1,5:12,6:$V2,7:13,8:$V3,12:[1,17],15:$V9,17:$V4,20:$V5,22:$V6,28:18,31:$V7,33:$V8},o($Va,$Vb,{13:[1,21]}),{3:22,4:$V1,16:23,17:$Vc,20:$Vd,22:$Ve},o($Vf,[2,20]),o([4,6,8,12,13,14,15,17,19,20,21,22,23,26,27,31,32,33],[2,1]),{3:31,4:$V1,5:32,6:$V2,7:13,8:$V3,11:30,15:$V9,17:$V4,19:[2,34],20:$V5,22:$V6,28:6,29:27,31:$V7,33:$V8,34:28,35:29},{3:19,4:$V1,5:12,6:$V2,7:13,8:$V3,11:35,15:$V9,17:$V4,20:$V5,21:$Vg,22:$V6,28:6,30:33,31:$V7,33:$V8,36:34},{3:19,4:$V1,5:12,6:$V2,7:13,8:$V3,11:35,15:$V9,17:$V4,20:$V5,22:$V6,23:$Vg,28:6,30:36,31:$V7,33:$V8,36:34},{3:19,4:$V1,5:12,6:$V2,7:13,8:$V3,11:38,15:$V9,17:$V4,20:$V5,22:$V6,28:6,31:$V7,32:[1,37],33:$V8},o($Vf,$Vh),o($Vf,[2,29]),{3:41,4:$V1,5:40,6:$V2,7:39,8:$V3},o([4,6,8,12,14,15,17,19,20,21,22,23,26,27,31,32,33],[2,2]),o($Vf,[2,3]),{1:[2,4]},o($Vf,[2,21]),o($Vf,$Vb),{3:42,4:$V1,16:23,17:$Vc,20:$Vd,22:$Ve},{3:19,4:$V1,5:12,6:$V2,7:13,8:$V3,11:43,15:$V9,17:$V4,20:$V5,22:$V6,28:6,31:$V7,33:$V8},o($Va,$Vi,{13:[1,44]}),o($Vf,[2,33]),{3:49,4:$V1,5:50,6:$V2,16:48,17:$Vc,18:45,19:$Vj,20:$Vd,22:$Ve,24:46,25:47},{3:42,4:$V1,16:51,17:$Vc,20:$Vd,22:$Ve},{3:49,4:$V1,5:50,6:$V2,16:48,17:$Vc,18:52,20:$Vd,22:$Ve,23:$Vj,24:46,25:47},{19:[1,53]},{19:[2,35],26:[1,54]},o($Vk,[2,36]),{3:55,4:$V1,5:56,6:$V2,7:13,8:$V3,15:$V9,17:$V4,20:$V5,22:$V6,28:18,31:$V7,33:$V8},o($V0,$Vb,{27:[1,57]}),o($V0,$Vh,{27:[1,58]}),{21:[1,59]},o([21,23],[2,43],{26:[1,60]}),o($Vl,[2,44],{5:12,7:13,28:18,3:19,4:$V1,6:$V2,8:$V3,15:$V9,17:$V4,20:$V5,22:$V6,31:$V7,33:$V8}),{23:[1,61]},o($Vf,[2,26]),{3:19,4:$V1,5:12,6:$V2,7:13,8:$V3,15:$V9,17:$V4,20:$V5,22:$V6,28:18,31:$V7,32:[1,62],33:$V8},o($Vf,[2,30]),o($Vf,[2,31]),o($Vf,[2,32]),o($Vf,$Vi),{3:19,4:$V1,5:12,6:$V2,7:13,8:$V3,14:[1,63],15:$V9,17:$V4,20:$V5,22:$V6,28:18,31:$V7,33:$V8},{3:42,4:$V1,16:64,17:$Vc,20:$Vd,22:$Ve},{19:[1,65]},o([19,23],[2,13],{26:[1,66]}),o($Vm,[2,14]),{3:67,4:$V1,5:68,6:$V2},o([4,6],$Vi,{27:[1,69]}),{27:[1,70]},{21:[1,71]},{23:[1,72]},o($Vf,[2,22]),{3:31,4:$V1,5:32,6:$V2,7:13,8:$V3,11:30,15:$V9,17:$V4,20:$V5,22:$V6,28:6,31:$V7,33:$V8,35:73},o($V0,$Vb,{19:$Vn,26:$Vn}),o($V0,$Vh,{19:$Vo,26:$Vo}),{3:19,4:$V1,5:12,6:$V2,7:13,8:$V3,11:74,15:$V9,17:$V4,20:$V5,22:$V6,28:6,31:$V7,33:$V8},{3:19,4:$V1,5:12,6:$V2,7:13,8:$V3,11:75,15:$V9,17:$V4,20:$V5,22:$V6,28:6,31:$V7,33:$V8},o($Vf,[2,23]),{3:19,4:$V1,5:12,6:$V2,7:13,8:$V3,11:76,15:$V9,17:$V4,20:$V5,22:$V6,28:6,31:$V7,33:$V8},o($Vf,[2,24]),o($Vf,[2,27]),o($V0,[2,6]),{14:[1,77]},o($Vf,[2,9]),{3:49,4:$V1,5:50,6:$V2,16:48,17:$Vc,20:$Vd,22:$Ve,25:78},o($Vm,[2,16]),o($Vm,[2,17]),{3:42,4:$V1,16:79,17:$Vc,20:$Vd,22:$Ve},{3:42,4:$V1,16:80,17:$Vc,20:$Vd,22:$Ve},o($Vf,[2,10]),o($Vf,[2,11]),o($Vk,[2,37]),o($Vk,[2,40],{5:12,7:13,28:18,3:19,4:$V1,6:$V2,8:$V3,15:$V9,17:$V4,20:$V5,22:$V6,31:$V7,33:$V8}),o($Vk,[2,41],{5:12,7:13,28:18,3:19,4:$V1,6:$V2,8:$V3,15:$V9,17:$V4,20:$V5,22:$V6,31:$V7,33:$V8}),o($Vl,[2,45],{5:12,7:13,28:18,3:19,4:$V1,6:$V2,8:$V3,15:$V9,17:$V4,20:$V5,22:$V6,31:$V7,33:$V8}),o($V0,[2,7]),o($Vm,[2,15]),o($Vm,[2,18]),o($Vm,[2,19])],
defaultActions: {17:[2,4]},
parseError: function parseError (str, hash) {

@@ -646,34 +691,42 @@ if (hash.recoverable) {

break;
case 3:return 19;
case 3:return 22;
break;
case 4:return 13;
case 4:return 17;
break;
case 5:return 16;
case 5:return 20;
break;
case 6:return 21;
case 6:return 31;
break;
case 7:return 20;
case 7:return 23;
break;
case 8:return 15;
case 8:return 19;
break;
case 9:return 18;
case 9:return 21;
break;
case 10:return 22;
case 10:return 32;
break;
case 11:return 23;
case 11:return 13;
break;
case 12:return 26;
case 12:return 33;
break;
case 13:return 6
case 13:return 26;
break;
case 14:return 4;
case 14:return 14;
break;
case 15:return 8;
case 15:return 27;
break;
case 16:return 11;
case 16:return 15;
break;
case 17:return 6
break;
case 18:return 4;
break;
case 19:return 8;
break;
case 20:return 12;
break;
}
},
rules: [/^(?:[/][*]([*][^/]|[^*])*[*][/])/,/^(?:(\/\/|#|%|--)[^\n]*)/,/^(?:\s+)/,/^(?:<)/,/^(?:\{)/,/^(?:\[)/,/^(?:\()/,/^(?:>)/,/^(?:\})/,/^(?:\])/,/^(?:\))/,/^(?:\.)/,/^(?:,)/,/^(?:"[^\"\n]*"|'[^\'\n]*')/,/^(?:[a-zA-Z_][a-zA-Z0-9_]*)/,/^(?:0|[-]?[1-9][0-9]*)/,/^(?:$)/],
conditions: {"INITIAL":{"rules":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],"inclusive":true}}
rules: [/^(?:[/][*]([*][^/]|[^*])*[*][/])/,/^(?:(\/\/|#|%|--)[^\n]*)/,/^(?:\s+)/,/^(?:<)/,/^(?:\{)/,/^(?:\[)/,/^(?:\()/,/^(?:>)/,/^(?:\})/,/^(?:\])/,/^(?:\))/,/^(?:=)/,/^(?:\.)/,/^(?:,)/,/^(?:;)/,/^(?::)/,/^(?:\$)/,/^(?:"[^\"\n]*"|'[^\'\n]*')/,/^(?:[a-zA-Z_][a-zA-Z0-9_?!]*)/,/^(?:0|[-]?[1-9][0-9]*)/,/^(?:$)/],
conditions: {"INITIAL":{"rules":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20],"inclusive":true}}
});

@@ -680,0 +733,0 @@ return lexer;

# k-language
Another JSON-to-JSON transformation notation. A `k`-expression
Another JSON transformation notation. A `k`-expression
(script) defines a __partial function__, i.e., a function which, in

@@ -14,10 +14,11 @@ general, is not defined for every input. `k`-expressions can be

2. product, e.g., vector:
2. product, e.g., vector or structure:
[ E1, E2, E3, ... ]
or structure:
{ E1 "e1", E2 "e2", E3 "e3", ... }
Now, structure can also be written similarly to JSON, ie.,
{ e1: E1, e2: E2, e3: E3, ... }
3. union (merge), e.g.:

@@ -46,7 +47,6 @@

false
Those functions are always defined, i.e., they ignore their argument.
null
3. There is a number of built-in functions: `GT`, `EQ`, `PLUS`,
`TIMES`, `MINUS`, `DIV`, `CONCAT`, `toJSON`, `fromJSON`. For example
`TIMES`, `MINUS`, `DIV`, `CONCAT`, `toJSON`, `fromJSON`, `CONS`, `SNOC`. For example

@@ -66,4 +66,41 @@ [1, 2, 3] PLUS --> integer constant 6

## User defined functions
`k`-expression can be prefixed by function definitions. E.g.:
dec = [(),1] MINUS;
factorial = <
[1,()] GT 1,
[dec factorial, ()] TIMES
>;
{ x: (), "x!": factorial }
## Value encodings (also called _types_ or _codes_)
There are three predefined value encodings: `int`, `string`, and `bool`. The language
supports `code`-expressions:
* product, e.g., `{x: int, y: int, flag: bool}`
* disjoint union, e.g., `<x: string, y: bool>`
* vector, e.g., `[ int ]` (all elements of the vector use the same encoding)
One can define recursive codes. E.g.,
$ tree = <leaf: string, tree: {left: tree, right: tree}>;
Each _code_ definition starts with a `$`.
The above example defines new code called _tree_.
The code can be then used in a `k`-expression as a filter. A `code`-expression
within `k`-expression is again prefixed by `$`.
$tree = <leaf: string, tree: {left: tree, right: tree}>;
inc = [(),1] PLUS;
height = $ tree <
.leaf 0,
.tree [.left height, .right height] <GT .0, .1> inc
> $ int;
height
## Command line script

@@ -119,3 +156,3 @@

$ ./k.coffee -k test.k -j my-objects.json
$ ./k.coffee -k test.k my-objects.json
{"x&y":"x=432 & y=123"}

@@ -185,2 +222,1 @@ {"x&y":"onlyx=987"}

// RETURNS: {"nom":"Mark","age":"enfant"}
// Generated by CoffeeScript 2.7.0
(function() {
var builtin, run, valid,
var builtin, codes, run, valid, verify,
modulo = function(a, b) { return (+a % (b = +b) + b) % b; };

@@ -16,2 +16,6 @@

builtin = {
"_log_": function(arg) {
console.log(`_log_: ${JSON.stringify(arg)}`);
return arg;
},
"GT": function(args) {

@@ -87,5 +91,57 @@ var _, ok;

return JSON.parse(x);
},
"CONS": function([x, y]) {
return [x, ...y];
},
"SNOC": function(x) {
if (x.length > 1) {
return [x[0], x.slice(1)];
}
}
};
codes = {
"int": function(x) {
return Number.isInteger(x);
},
"string": function(x) {
return (x instanceof String) || ('string' === typeof x);
},
"bool": function(x) {
return x === true || x === false;
}
};
verify = function(code, value) {
switch (code.code) {
case "ref":
return (function(c) {
if (c != null) {
return verify(c, value);
}
return codes[code.ref](value);
})(run.defs.codes[code.ref]);
case "vector":
return value.every(function(x) {
return verify(code.vector, x);
});
case "product":
return (function(fields) {
if (fields.length !== Object.keys(code.product).length) {
return false;
}
return fields.every(function(label) {
return verify(code.product[label], value[label]);
});
})(Object.keys(value));
case "union":
return (function(fields) {
if (fields.length !== 1) {
return false;
}
return verify(code.union[fields[0]], value[fields[0]]);
})(Object.keys(value));
}
};
run = function(exp, value) {

@@ -98,2 +154,7 @@ var e, i, len, ref, result;

switch (exp.op) {
case "code":
if (verify(exp.code, value)) {
return value;
}
break;
case "identity":

@@ -105,8 +166,14 @@ return value;

case "ref":
return (function(value) {
if (value === void 0) {
throw new Error("Undefined");
return (function(defn) {
if (defn != null) {
return run(defn, value);
} else {
return (function(value) {
if (value === void 0) {
throw new Error("Undefined");
}
return value;
})(builtin[exp.ref](value));
}
return value;
})(builtin[exp.ref](value));
})(run.defs.rels[exp.ref]);
case "dot":

@@ -155,2 +222,4 @@ return value[exp.dot];

}, {});
default:
return console.log(exp.op);
}

@@ -157,0 +226,0 @@ } catch (error) {}

@@ -76,2 +76,45 @@ // Generated by CoffeeScript 2.7.0

k_expression = "$t = < i: int, t: [ t ] > ; <$t, $int>";
k_fn = k.compile(k_expression);
console.log(`var k_fn = k.compile('${k_expression}');`);
console.log("");
[
1,
{
i: 1
},
[
{
i: 2
},
{
i: 3
},
{
t: []
}
],
{
"t": [
{
"i": 2
},
{
"i": 3
},
{
"t": []
}
]
}
].map(function(data) {
console.log(`k_fn(${JSON.stringify(data)});`);
console.log(`// RETURNS: ${JSON.stringify(k_fn(data))}`);
return console.log("");
});
}).call(this);

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc