interactive prompt

This commit is contained in:
Joeri Exelmans 2025-03-23 17:46:39 +01:00
parent bc91d9bf39
commit 3596e01c28
9 changed files with 298 additions and 105 deletions

320
main.js
View file

@ -1,61 +1,33 @@
import { select } from '@inquirer/prompts';
import { ModulePoint } from "./lib/point.js";
import { DefaultMap, pretty } from './util.js';
import { DefaultMap, pretty, prettyT } from './util.js';
import { symbolFunction } from './structures/types.js';
import { ModuleStd } from './stdlib.js';
import { Type } from './type.js';
class Context {
constructor(mod) {
this.functionsFrom = new DefaultMap(() => []); // type to outgoing function
this.functionsTo = new DefaultMap(() => []); // type to incoming function
this.types = new DefaultMap(() => []); // instance to type
this.instances = new DefaultMap(() => []); // type to instance
this.types = new DefaultMap(() => new Set()); // instance to type
this.instances = new DefaultMap(() => new Set()); // type to instance
this.functionsFrom = new DefaultMap(() => new Set()); // type to outgoing function
this.functionsTo = new DefaultMap(() => new Set()); // type to incoming function
for (const {i, t} of mod.l) {
this.types.getdefault(i, true).push(t);
this.instances.getdefault(t, true).push(i);
this.types.getdefault(i, true).add(t);
this.instances.getdefault(t, true).add(i);
}
for (const [i, types] of this.instances.m.entries()) {
for (const t of types) {
if (t.symbol === symbolFunction) {
// 'i' is a function
this.functionsFrom.getdefault(t.params[0], true).push(i);
this.functionsFrom.getdefault(t.params[1], true).push(i);
for (const t of this.instances.m.keys()) {
if (t.symbol === symbolFunction) {
// 't' is a function signature
for (const fn of this.instances.getdefault(t)) {
this.functionsFrom.getdefault(t.params[0], true).add(fn);
this.functionsTo .getdefault(t.params[1], true).add(fn);
}
}
}
}
}
// 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 ctx = new Context({l:[
@ -63,49 +35,231 @@ const ctx = new Context({l:[
...ModulePoint.l,
]});
const makeChoice = ([i, t]) => {
return {
value: {i, t: t[0]},
name: pretty(i),
description: ` :: ${pretty(t[0])}`,
short: `${pretty(i)} :: ${pretty(t[0])}`,
};
const prettyIT = ({i, t}) => ({
strI: isType(i) ? prettyT(i) : pretty(i),
strT: prettyT(t),
})
const toChoices = ([i, types]) => {
return [...types].map(t => {
const {strI, strT} = prettyIT({i, t});
return {
value: {i, t},
name: strI,
description: ` :: ${strT}`,
short: `${strI} :: ${strT}`,
};
});
}
let {i,t} = await select({
message: "Choose value:",
choices: [...ctx.types.entries()].map(makeChoice),
});
const isType = i => ctx.types.getdefault(i).has(Type);
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;
async function topPrompt() {
const action = await select({
message: "What do you want to do?",
choices: [
"list all types",
"list all functions",
"list all",
],
});
if (action === "list all types") {
await listAllTypes();
}
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];
if (action === "list all") {
await listAllInstances();
}
return topPrompt();
}
async function listAllTypes() {
const choice = await select({
message: "select type:",
choices: [
"(go back)",
...[...ctx.instances.m.keys()].map(t => ({
value: t,
name: prettyT(t),
})),
]
});
if (choice === "(go back)") {
return;
}
await typeOptions(choice);
return listAllTypes();
}
async function typeOptions(t) {
const choice = await select({
message: `actions for type ${prettyT(t)} :: Type`,
choices: [
"(go back)",
"list instances",
"list outgoing functions",
"list incoming functions",
"treat as instance",
],
});
if (choice === "(go back)") {
return;
}
else if (choice === "list instances") {
await listInstances(t);
}
else if (choice === "treat as instance") {
await instanceOptions(t, Type)
}
else {
console.log("unimplemented:", choice);
}
return typeOptions(t);
}
async function listAllInstances() {
const choice = await select({
message: `all instances:`,
choices: [
"(go back)",
... [...ctx.types.m.keys()].flatMap(i => toChoices([i, ctx.types.getdefault(i)])),
],
})
if (choice === "(go back)") {
return;
}
return instanceOrTypeOrFnOptions(choice);
}
async function instanceOrTypeOrFnOptions({i, t}) {
if (t.symbol === symbolFunction) {
return functionOptions(i, t);
}
if (isType(i)) {
return typeOptions(i);
}
return instanceOptions(i,t);
}
async function listInstances(t) {
const choice = await select({
message: `instances of ${prettyT(t)}:`,
choices: [
"(go back)",
... [...ctx.instances.getdefault(t)].flatMap(i => toChoices([i, [t]])),
],
});
if (choice === "(go back)") {
return;
}
return instanceOrTypeOrFnOptions(choice);
}
async function listTypes(i) {
const {strI} = prettyIT({i,t:Type});
const choice = await select({
message: `type(s) of ${strI}:`,
choices: [
"(go back)",
... [...ctx.types.getdefault(i)].flatMap(t => toChoices([t, ctx.types.getdefault(t)])),
],
});
if (choice === "(go back)") {
return;
}
const {i: chosenType} = choice;
await typeOptions(chosenType);
return listTypes(i);
}
async function functionOptions(fn, fnT) {
const {strI, strT} = prettyIT({i: fn, t: fnT});
const choice = await select({
message: `actions for function ${strI} :: ${strT}`,
choices: [
"(go back)",
"call",
"treat as instance",
],
});
if (choice === "(go back)") {
return;
}
if (choice === "call") {
await callFunction(fn, fnT);
}
if (choice === "treat as instance") {
await instanceOptions(fn, fnT);
}
return functionOptions(fn, fnT);
}
async function callFunction(fn, fnT) {
const {strI, strT} = prettyIT({i: fn, t: fnT});
const inType = fnT.params[0];
const choice = await select({
message: `select parameter for function ${strI} :: ${strT}`,
choices: [
"(go back)",
... [...ctx.instances.getdefault(inType)].flatMap(i => toChoices([i, ctx.types.getdefault(i)])),
],
});
if (choice === "(go back)") {
return;
}
const {i, t} = choice;
await apply(i, fn, fnT);
return callFunction(fn, fnT);
}
async function instanceOptions(i,t) {
const {strI, strT} = prettyIT({i,t});
const choice = await select({
message: `actions for instance ${strI} :: ${strT}`,
choices: [
"(go back)",
"transform",
"list type(s)",
]
})
if (choice === "(go back)") {
return;
}
if (choice === "transform") {
await transform(i, t)
}
if (choice === "list type(s)") {
await listTypes(i);
}
return await instanceOptions(i,t);
}
async function transform(i, t) {
const {strI, strT} = prettyIT({i, t});
console.log(ctx.functionsFrom.getdefault(t));
const choice = await select({
message: `choose transformation to perform on ${strI} :: ${strT}`,
choices: [
"(go back)",
... [...ctx.functionsFrom.getdefault(t)].flatMap(fn => toChoices([fn, ctx.types.getdefault(fn)])),
],
});
if (choice === "(go back)") {
return;
}
const {i:fn,t:fnT} = choice;
await apply(i, fn, fnT);
return transform(i, t);
}
async function apply(i, fn, fnT) {
const result = fn(i);
t = getOut(fnType);
console.log("result =", result, "::", t);
i = result;
const resultType = fnT.params[1];
const {strI: strResult, strT: strResultType} = prettyIT({i: result, t: resultType});
console.log(`result = ${strResult} :: ${strResultType}`);
return instanceOrTypeOrFnOptions({i: result, t: resultType});
}
topPrompt();