environment includes mapping -> instance

This commit is contained in:
Joeri Exelmans 2025-05-10 10:00:31 +02:00
parent 1780920438
commit 7ffe5c4ed4
2 changed files with 75 additions and 36 deletions

View file

@ -1,12 +1,12 @@
// The functions in this module prompt the user for actions to perform on types, instances, etc.
import { number, select } from "@inquirer/prompts";
import { input, number, select } from "@inquirer/prompts";
import { getCompatibleInputTypes, getEnabledFunctions, getInstances, growEnv } from "../../lib/environment/env.js";
import { contains, getCompatibleInputTypes, getEnabledFunctions, getInstances, growEnv } from "../../lib/environment/env.js";
import { getInst, getType, newDynamic } from "../../lib/primitives/dynamic.js";
import { Bool, Double, Int, SymbolDynamic, Type, UUID } from "../../lib/primitives/primitive_types.js";
import { Bool, Double, Int, symbolDynamic, Type, UUID } from "../../lib/primitives/primitive_types.js";
import { eqType, getSymbol } from "../../lib/primitives/type.js";
import { fold as foldSet } from "../../lib/structures/set.js";
import { add, fold as foldSet } from "../../lib/structures/set.js";
import { symbolFunction } from "../../lib/structures/type_constructors.js";
import { pretty, prettyT } from "../../lib/util/pretty.js";
import { genUUID } from "../../lib/util/random.js";
@ -15,19 +15,32 @@ const defaultSelectOptions = {
pageSize: 13,
};
export const addToEnv = async (env, dynamic) => {
if (contains(env)(dynamic)) {
return env;
}
const name = await input({
message: `enter name for ${pretty(dynamic)} (or leave empty to skip this):`,
});
if (name === '') {
return env;
}
const newEnv = growEnv(env)(name)(dynamic);
return newEnv;
}
export const proxyDynamic = async (env, dynamic) => {
const newEnv = growEnv(env)(dynamic);
const type = getType(dynamic)
if (eqType(type)(Type)) {
return listTypeOptions(newEnv, getInst(dynamic));
return listTypeOptions(env, getInst(dynamic));
}
if (getSymbol(type) === symbolFunction) {
return listFunctionOptions(newEnv, dynamic);
return listFunctionOptions(env, dynamic);
}
if (getSymbol(type) === SymbolDynamic) {
return proxyDynamic(newEnv, getInst(dynamic));
if (getSymbol(type) === symbolDynamic) {
return proxyDynamic(env, getInst(dynamic));
}
return listInstanceOptions(newEnv, dynamic);
return listInstanceOptions(env, dynamic);
};
export const selectInstance = async (env, type, msg) => {
@ -37,7 +50,10 @@ export const selectInstance = async (env, type, msg) => {
value: instance,
name: pretty(instance),
}];
})(["(go back)", "(new)"])(instances);
})([
"(go back)",
"(new)",
])(instances);
const choice = await select({
message: `${msg} ${pretty(type)}:`,
choices,
@ -53,11 +69,13 @@ export const selectInstance = async (env, type, msg) => {
};
export const listInstances = async (env, type) => {
const choice = await selectInstance(env, type, "instances of");
if (choice === undefined) {
const instance = await selectInstance(env, type, "instances of");
if (instance === undefined) {
return;
}
await proxyDynamic(env, newDynamic(choice)(type));
const dynamic = newDynamic(instance)(type);
const newEnv = await addToEnv(env, dynamic);
await proxyDynamic(newEnv, dynamic);
return await listInstances(env, type);
};
@ -79,14 +97,15 @@ export const listTypeOptions = async (env, type) => {
const i = await createInstance(type);
if (i !== undefined) {
const dynamic = newDynamic(i)(type);
await proxyDynamic(env, dynamic);
const newEnv = await addToEnv(env, dynamic);
await proxyDynamic(newEnv, dynamic);
}
}
if (choice === "list instances") {
await listInstances(env, type);
}
if (choice === "treat as instance") {
await listInstanceOptions(newDynamic(type)(Type));
await listInstanceOptions(env, newDynamic(type)(Type));
}
return await listTypeOptions(env, type);
};
@ -213,8 +232,11 @@ export const call = async (env, funDynamic) => {
if (inValue === undefined) {
return;
}
const envWithInput = growEnv(env)(newDynamic(inValue)(choice.inType));
const inDynamic = newDynamic(inValue)(choice.inType);
const envWithInput = await addToEnv(env, inDynamic);
const outValue = getInst(funDynamic)(inValue);
await proxyDynamic(envWithInput, newDynamic(outValue)(choice.outType));
return call(env, funDynamic);
const outDynamic = newDynamic(outValue)(choice.outType)
const envWithOutput = await addToEnv(envWithInput, outDynamic);
await proxyDynamic(envWithOutput, outDynamic);
return call(envWithOutput, funDynamic);
};