add interactive prompt
This commit is contained in:
parent
ce192b49f2
commit
94efde3e65
22 changed files with 599 additions and 138 deletions
199
main.js
199
main.js
|
|
@ -3,92 +3,169 @@ import {ModuleTyped} from "./typed.js";
|
|||
import {ModuleBool} from "./primitives/bool.js";
|
||||
import { Double_to_Double_to_Double, ModuleDouble, mulDouble } from "./primitives/double.js";
|
||||
import {Int_to_Int_to_Int, ModuleInt, mulInt} from "./primitives/int.js";
|
||||
import {Int} from "./primitives/symbols.js";
|
||||
import { makeSquare } from "./lib/square.js";
|
||||
import {makeIdFn} from "./lib/id.js";
|
||||
import {ModuleValues} from "./lib/values.js";
|
||||
|
||||
import {DefaultMap} from "./util.js";
|
||||
import {DefaultMap, pretty} from "./util.js";
|
||||
import { ModuleModule } from "./structures/module.js";
|
||||
import { ModuleList } from "./structures/list.js";
|
||||
import {Int, Bool, Double, Byte} from "./primitives/symbols.js";
|
||||
import { makeListModule } from "./structures/list_common.js";
|
||||
import { makeProductType } from "./structures/product.js";
|
||||
import { makeSumType } from "./structures/sum.js";
|
||||
import { lsType, prodType, sumType } from "./type_registry.js";
|
||||
|
||||
class Context {
|
||||
constructor(mod) {
|
||||
// input type -> Function-signature
|
||||
this.functionsFrom = new DefaultMap(() => []);
|
||||
this.functionsTo = new DefaultMap(() => []);
|
||||
this.types = new DefaultMap(() => []);
|
||||
this.instances = new DefaultMap(() => []);
|
||||
|
||||
|
||||
for (const {i, t} of mod) {
|
||||
this.addInstance({i, t});
|
||||
for (const {i, t} of mod.l) {
|
||||
this.types.getdefault(i, true).push(t);
|
||||
this.instances.getdefault(t, true).push(i);
|
||||
}
|
||||
|
||||
const callFunctionOnEveryValue = (fn, fnType, indent=1) => {
|
||||
const inType = getIn(fnType);
|
||||
const outType = getOut(fnType);
|
||||
console.log();
|
||||
if (fn.name !== "") {
|
||||
console.log("--".repeat(indent), fn, ':', fnType);
|
||||
}
|
||||
for (const i of this.instances.getdefault(inType)) {
|
||||
try {
|
||||
const result = fn(i);
|
||||
console.log("--".repeat(indent+1), i, '->', result);
|
||||
if (this.types.getdefault(outType).includes(Function)) {
|
||||
if (indent < 5) {
|
||||
callFunctionOnEveryValue(result, outType, indent+2);
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.log("--".repeat(indent+1), i, `-> (exception: ${e})`);
|
||||
}
|
||||
for (const fnType of this.instances.getdefault(Function)) {
|
||||
for (const fn of this.instances.getdefault(fnType)) {
|
||||
this.functionsFrom.getdefault(getIn(fnType), true).push(fn);
|
||||
this.functionsTo.getdefault(getOut(fnType), true).push(fn);
|
||||
}
|
||||
}
|
||||
for (const fntypes of this.functionsFrom.m.values()) {
|
||||
for (const fntype of fntypes) {
|
||||
for (const fn of this.instances.getdefault(fntype)) {
|
||||
callFunctionOnEveryValue(fn, fntype);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
addInstance({i, t}) {
|
||||
const arr = this.types.getdefault(i, true);
|
||||
arr.push(t);
|
||||
const arrI = this.instances.getdefault(t, true);
|
||||
arrI.push(i);
|
||||
if (t === Function) {
|
||||
const arr = this.functionsFrom.getdefault(getIn(i), true);
|
||||
arr.push(i);
|
||||
const arrTo = this.functionsTo.getdefault(getOut(i), true);
|
||||
arrTo.push(i);
|
||||
}
|
||||
else {
|
||||
}
|
||||
}
|
||||
// const callFunctionOnEveryValue = (fn, fnType, indent=1) => {
|
||||
// const inType = getIn(fnType);
|
||||
// const outType = getOut(fnType);
|
||||
// console.log();
|
||||
// if (fn.name !== "") {
|
||||
// console.log("--".repeat(indent), fn, ':', fnType);
|
||||
// }
|
||||
// for (const i of this.instances.getdefault(inType)) {
|
||||
// try {
|
||||
// const result = fn(i);
|
||||
// console.log("--".repeat(indent+1), i, '->', result);
|
||||
// if (this.types.getdefault(outType).includes(Function)) {
|
||||
// if (indent < 5) {
|
||||
// callFunctionOnEveryValue(result, outType, indent+2);
|
||||
// }
|
||||
// }
|
||||
// } catch (e) {
|
||||
// console.log("--".repeat(indent+1), i, `-> (exception: ${e})`);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// for (const fntypes of this.functionsFrom.m.values()) {
|
||||
// for (const fntype of fntypes) {
|
||||
// for (const fn of this.instances.getdefault(fntype)) {
|
||||
// callFunctionOnEveryValue(fn, fntype);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
const ListOfDouble = lsType(Double);
|
||||
const ListOfDoubleModule = makeListModule(Double);
|
||||
|
||||
const ctx = new Context([
|
||||
...ModuleMetaCircular,
|
||||
...ModuleTyped,
|
||||
const ListOfListOfDouble = lsType(ListOfDouble);
|
||||
const ListOfListOfDoubleModule = makeListModule(ListOfDouble);
|
||||
|
||||
const ListOfByte = lsType(Byte);
|
||||
const ListOfByteModule = makeListModule(Byte);
|
||||
|
||||
const ModuleValues = {l:[
|
||||
{i: 0n, t: Int},
|
||||
{i: 42n, t: Int},
|
||||
{i: false, t: Bool},
|
||||
{i: 3.14159265359, t: Double},
|
||||
|
||||
{i: {l:[4.2, 7.6]} , t: ListOfDouble},
|
||||
{i: {l:[{l:[4.2, 7.6]}, {l:[4.3]}]}, t: ListOfListOfDouble},
|
||||
|
||||
{i: new Uint8Array([1,2,3]), t: ListOfByte},
|
||||
|
||||
// i'm lazy
|
||||
...ListOfDoubleModule.l,
|
||||
...ListOfListOfDoubleModule.l,
|
||||
...ListOfByteModule.l,
|
||||
|
||||
|
||||
...makeProductType(Int, Bool).l,
|
||||
|
||||
...makeSumType(Int, Bool).l,
|
||||
|
||||
{i: {left: 42n, right: true}, t: prodType(Int, Bool)},
|
||||
{i: {variant: "L", value: 100n}, t: sumType(Int, Bool)},
|
||||
]};
|
||||
|
||||
|
||||
const ctx = new Context({l:[
|
||||
...ModuleMetaCircular.l,
|
||||
...ModuleTyped.l,
|
||||
// ...ModuleConformable,
|
||||
// ...ModuleConformanceCheckConforms,
|
||||
// ...ModuleNum,
|
||||
// ...ModuleEq,
|
||||
...ModuleBool,
|
||||
...ModuleInt,
|
||||
...ModuleDouble,
|
||||
...ModuleBool.l,
|
||||
...ModuleInt.l,
|
||||
...ModuleDouble.l,
|
||||
// ...ModuleSquare,
|
||||
// ...ModuleList,
|
||||
...makeIdFn(Int),
|
||||
...makeSquare({i: mulInt, t: Int_to_Int_to_Int}),
|
||||
...makeSquare({i: mulDouble, t: Double_to_Double_to_Double}),
|
||||
...ModuleList,
|
||||
...ModuleValues,
|
||||
...ModuleModule,
|
||||
]);
|
||||
...makeIdFn(Int).l,
|
||||
...makeSquare({i: mulInt, t: Int_to_Int_to_Int}).l,
|
||||
...makeSquare({i: mulDouble, t: Double_to_Double_to_Double}).l,
|
||||
...ModuleList.l,
|
||||
...ModuleValues.l,
|
||||
...ModuleModule.l,
|
||||
]});
|
||||
|
||||
import { input, select } from '@inquirer/prompts';
|
||||
|
||||
const makeChoice = ([i, t]) => {
|
||||
return {
|
||||
value: {i, t: t[0]},
|
||||
name: pretty(i),
|
||||
description: `${pretty(i)} :: ${pretty(t[0])}`,
|
||||
short: `${pretty(i)} :: ${pretty(t[0])}`,
|
||||
};
|
||||
}
|
||||
|
||||
let {i,t} = await select({
|
||||
message: "Choose value:",
|
||||
choices: [...ctx.types.entries()].map(makeChoice),
|
||||
});
|
||||
|
||||
while (true) {
|
||||
let fn, fnType;
|
||||
if (ctx.types.getdefault(t).includes(Function)) {
|
||||
fn = i;
|
||||
fnType = t;
|
||||
const s = await select({
|
||||
message: "Select input for function:",
|
||||
choices: ctx.instances.getdefault(getIn(t))
|
||||
.map(i => [i, ctx.types.getdefault(i)])
|
||||
.map(makeChoice),
|
||||
});
|
||||
i = s.i;
|
||||
t = s.t;
|
||||
}
|
||||
else{
|
||||
fn = await select({
|
||||
message: "Choose function:",
|
||||
choices: [...ctx.functionsFrom.getdefault(t).map(fn => ({
|
||||
value: fn,
|
||||
name: pretty(fn),
|
||||
description: `${pretty(fn)} :: ${pretty(ctx.types.getdefault(fn)[0])}`,
|
||||
short: `${pretty(fn)} :: ${pretty(ctx.types.getdefault(fn)[0])}`,
|
||||
})
|
||||
)],
|
||||
});
|
||||
fnType = ctx.types.getdefault(fn)[0];
|
||||
}
|
||||
const result = fn(i);
|
||||
t = getOut(fnType);
|
||||
console.log("result =", result, "::", t);
|
||||
i = result;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue