Skip to content

Instantly share code, notes, and snippets.

@dkorolev
Created June 12, 2022 18:03
Show Gist options
  • Save dkorolev/03cda1b005fda259d227e1388224d4f5 to your computer and use it in GitHub Desktop.
Save dkorolev/03cda1b005fda259d227e1388224d4f5 to your computer and use it in GitHub Desktop.
Example Rego => JavaScript transpiled code for `dkorolev/jsopa` README.
let function_bodies = {};
let plans = {};
const opa_builtins = {
plus: (args) => { return { t: 'number', v: args[0].v + args[1].v }; },
minus: (args) => { return { t: 'number', v: args[0].v - args[1].v }; },
mul: (args) => { return { t: 'number', v: args[0].v * args[1].v }; },
numbers: {
range: (args) => {
let v = [];
for (let i = args[0].v; i <= args[1].v; ++i) {
v.push(i);
}
return { t: 'array', v };
}
}
};
const internal_to_external_impl = {
number: (x) => { return x; },
string: (x) => { return x; },
boolean: (x) => { return x; },
array: (x) => { return x; },
object: (x) => {
let result = {};
for (let k in x) {
result[k] = internal_to_external(x[k]);
}
return result;
},
};
const internal_to_external = (x) => {
if (Array.isArray(x)) {
if (x.length === 1) {
return internal_to_external(x[0]);
} else {
let result = [];
x.forEach(e => result.push(internal_to_external(e)));
return result;
}
} else if (x === undefined || x.t === undefined) {
return undefined;
} else {
return internal_to_external_impl[x.t](x.v);
}
};
const external_to_internal = (v) => {
const t = typeof v;
if (t === 'string' || t === 'number' || t === 'boolean') {
return { t, v };
} else if (t === 'object') {
let result = { t: 'object', v: {}};
for (let k in v) {
result.v[k] = external_to_internal(v[k]);
}
return result;
} else {
return undefined;
}
};
module.exports.main = (input, data) => {
return internal_to_external(plans.main(external_to_internal(input), external_to_internal(data)));
};
const opa_get_function_impl = (f) => {
if (typeof f === 'number') {
return function_bodies[f];
} else {
return f.builtin_func;
}
};
const wrap_for_assignment = (x) => {
if (typeof x === 'string') {
return {t: 'string', v: x};
} else if (typeof x === 'boolean') {
return {t: 'boolean', v: x};
} else {
return x;
}
};
const static_strings = [
"result",
"a",
"b",
];
const function_signatures = [
{ name: "g0.data.smoke.sum",
arg_index: [
0,
1,
],
retval_index: 2
},
];
function_bodies[0] = (args) => { let retval = null; let locals = []; function_signatures[0].arg_index.forEach((a, i) => { locals[a] = args[i]; });
(() => {
locals[3] = undefined;
if (JSON.stringify(Boolean(true)) === JSON.stringify(Boolean(false))) return;
locals[4] = locals[0].v["a"];
locals[5] = wrap_for_assignment(locals[4]);
locals[6] = locals[0].v["b"];
locals[7] = wrap_for_assignment(locals[6]);
locals[8] = (() => { let args = [];
args[0] = locals[5];
args[1] = locals[7];
return opa_get_function_impl({builtin_func: opa_builtins.plus})(args)})();
locals[9] = wrap_for_assignment(locals[8]);
if (locals[3] !== undefined) return; locals[3] = wrap_for_assignment(locals[9]);
})();
(() => {
if (locals[3] === undefined) return;
if (locals[2] !== undefined) return; locals[2] = wrap_for_assignment(locals[3]);
})();
(() => {
retval = locals[2];
})();
return retval; };
plans["main"] = (input, data) => { let locals = [input, data]; let result = [];
(() => {
locals[2] = (() => { let args = [];
args[0] = locals[0];
args[1] = locals[1];
return opa_get_function_impl(0)(args)})();
locals[3] = wrap_for_assignment(locals[2]);
locals[4] = { t: 'object', v: {} };
locals[4].v["result"] = locals[3];
result.push(locals[4]);
})();
return result; };
module.exports.plans = plans;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment