dope2/main.js

173 lines
5.1 KiB
JavaScript

import {Function, ModuleMetaCircular, getIn, getOut} from "./metacircular.js";
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 { makeSquare } from "./lib/square.js";
import {makeIdFn} from "./lib/id.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) {
this.functionsFrom = new DefaultMap(() => []);
this.functionsTo = new DefaultMap(() => []);
this.types = new DefaultMap(() => []);
this.instances = new DefaultMap(() => []);
for (const {i, t} of mod.l) {
this.types.getdefault(i, true).push(t);
this.instances.getdefault(t, true).push(i);
}
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);
}
}
}
// 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 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)},
]};
import { select } from '@inquirer/prompts';
import { ModulePoint } from "./lib/point.js";
const ctx = new Context({l:[
// ...ModuleMetaCircular.l,
// ...ModuleTyped.l,
// // ...ModuleConformable,
// // ...ModuleConformanceCheckConforms,
// // ...ModuleNum,
// // ...ModuleEq,
// ...ModuleBool.l,
// ...ModuleInt.l,
// ...ModuleDouble.l,
// // ...ModuleSquare,
// // ...ModuleList,
// ...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,
...ModulePoint.l,
]});
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;
}