progress and some refactoring

This commit is contained in:
Joeri Exelmans 2025-03-31 15:35:02 +02:00
parent d236eca5e5
commit d8ca2f3999
25 changed files with 376 additions and 163 deletions

View file

@ -2,7 +2,7 @@ import { assign, makeGeneric, unify } from "../generics/generics.js";
import { Bool, Int } from "../primitives/types.js";
import { constructorLeft, constructorRight, match } from "../structures/sum.js";
import { fnType, sumType } from "../structures/types.js";
import { pretty } from "../util.js";
import { pretty } from '../util/pretty.js';
const IntOrBool = sumType(Int)(Bool);

View file

@ -1,10 +1,11 @@
import { select } from '@inquirer/prompts';
import { select, number } from '@inquirer/prompts';
import { ModulePoint } from "../lib/point.js";
import { DefaultMap, pretty, prettyT } from '../util.js';
import { symbolFunction } from '../structures/types.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 { Type } from "../primitives/types.js";
import { assign, makeGeneric, unify } from '../generics/generics.js';
import { Double, Int, Type } from "../primitives/types.js";
import { eqType } from '../type.js';
class Context {
constructor(mod) {
@ -21,7 +22,7 @@ class Context {
this.functionsTo = new DefaultMap(() => new Set()); // type to incoming function
for (const t of this.instances.m.keys()) {
if (t.symbol === symbolFunction) {
if (isFunction(t)) {
// 't' is a function signature
for (const fn of this.instances.getdefault(t)) {
this.functionsFrom.getdefault(t.params[0], true).add(fn);
@ -67,7 +68,9 @@ let ctx = new Context({l:[
const prettyIT = ({i, t}) => ({
strI: isType(i) ? prettyT(i) : pretty(i),
strT: prettyT(t),
})
// strI: pretty(i),
// strT: pretty(t),
});
const toChoices = ([i, types]) => {
return [...types].map(t => {
@ -131,7 +134,7 @@ async function listAllFunctions() {
...[...ctx.types.m.entries()]
.filter(([i, types]) => {
for (const type of types) {
if (type.symbol === symbolFunction)
if (isFunction(type))
return true;
}
return false;
@ -192,7 +195,7 @@ async function listAllInstances() {
}
async function instanceOrTypeOrFnOptions({i, t}) {
if (t.symbol === symbolFunction) {
if (isFunction(t)) {
return functionOptions(i, t);
}
if (isType(i)) {
@ -257,13 +260,29 @@ 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 select({
message: `select parameter for function ${strI} :: ${strT}`,
choices: [
"(go back)",
... [...ctx.instances.getdefault(inType)].flatMap(i => toChoices([i, ctx.types.getdefault(i)])),
],
});
const choice = await (async () => {
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 (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};
}
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;
}
@ -296,7 +315,7 @@ async function instanceOptions(i,t) {
async function transform(i, t) {
const {strI, strT} = prettyIT({i, t});
console.log(ctx.functionsFrom.getdefault(t));
// console.log(ctx.functionsFrom.getdefault(t));
const choice = await select({
message: `choose transformation to perform on ${strI} :: ${strT}`,