turn the function for creating new types (or type constructors) into a DOPE function

This commit is contained in:
Joeri Exelmans 2025-03-31 17:35:30 +02:00
parent d8ca2f3999
commit a0e3aa0cb3
12 changed files with 112 additions and 58 deletions

View file

@ -1,10 +1,10 @@
import { select, number } from '@inquirer/prompts';
import { select, number, input } from '@inquirer/prompts';
import { ModulePoint } from "../lib/point.js";
import { DefaultMap } from "../util/defaultmap.js";
import { pretty } from '../util/pretty.js';
import { isFunction, prettyT } from '../structures/types.js';
import { ModuleStd } from '../stdlib.js';
import { Double, Int, Type } from "../primitives/types.js";
import { Double, Int, SymbolT, Type } from "../primitives/types.js";
import { eqType } from '../type.js';
class Context {
@ -260,33 +260,49 @@ async function functionOptions(fn, fnT) {
async function callFunction(fn, fnT) {
const {strI, strT} = prettyIT({i: fn, t: fnT});
const inType = fnT.params[0];
const choice = await (async () => {
const choice = await select({
message: `select parameter for function ${strI} :: ${strT}`,
choices: [
"(go back)",
"(new)",
... [...ctx.instances.getdefault(inType)].flatMap(i => toChoices([i, ctx.types.getdefault(i)])),
],
});
let i;
if (choice === "(go back)") {
return;
}
if (choice === "(new)") {
if (eqType(inType)(Int)) {
const n = await number({
message: `enter an integer (leave empty to go back):`,
step: 1, // only integers
});
return (n === undefined) ? "(go back)" : {i: BigInt(n), t: Int};
if (n === undefined) {
return;
}
i = BigInt(n);
}
if (eqType(inType)(Double)) {
else if (eqType(inType)(Double)) {
const n = await number({
message: `enter a number (leave empty to go back):`,
step: 'any',
});
return (n === undefined) ? "(go back)" : {i: n, t: Int};
if (n === undefined) {
return;
}
i = n;
}
else if (eqType(inType)(SymbolT)) {
console.log("Note: you are creating a new Symbol. Even if the description matches that of another symbol (e.g., \"Int\"), a new Symbol will be created that is unique and only equal to itself.");
const symbolDescr = await input({message: "enter symbol description:"});
i = Symbol(symbolDescr);
}
else {
console.log("no prompt handler for creating new", prettyT(inType));
return callFunction(fn, fnT);
}
return 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);
}
@ -327,6 +343,7 @@ async function transform(i, t) {
if (choice === "(go back)") {
return;
}
const {i:fn,t:fnT} = choice;
await apply(i, fn, fnT);
return transform(i, t);
@ -334,6 +351,7 @@ async function transform(i, t) {
async function apply(i, fn, fnT) {
const result = fn(i);
// console.log(fn, '(', i, ')', '=', result);
const resultType = fnT.params[1];
// update context with newly produced value
ctx = ctx.addToCtx({i: result, t: resultType});