dope2/main.js
2025-03-23 13:25:47 +01:00

111 lines
3.2 KiB
JavaScript

import { select } from '@inquirer/prompts';
import { ModulePoint } from "./lib/point.js";
import { DefaultMap, pretty } from './util.js';
import { symbolFunction } from './structures/types.js';
import { ModuleStd } from './stdlib.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
for (const {i, t} of mod.l) {
this.types.getdefault(i, true).push(t);
this.instances.getdefault(t, true).push(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);
}
}
}
}
// 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:[
...ModuleStd.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])}`,
};
}
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;
}