This commit is contained in:
Joeri Exelmans 2025-03-24 17:28:07 +01:00
parent 6af72e525c
commit 145835ad5d
22 changed files with 153 additions and 90 deletions

85
main.js
View file

@ -3,7 +3,8 @@ import { ModulePoint } from "./lib/point.js";
import { DefaultMap, pretty, prettyT } from './util.js';
import { symbolFunction } from './structures/types.js';
import { ModuleStd } from './stdlib.js';
import { Type } from './type.js';
import { Type } from "./primitives/types.js";
import { assign, makeGeneric, unify } from './generics/generics.js';
class Context {
constructor(mod) {
@ -11,14 +12,14 @@ class Context {
this.types = new DefaultMap(() => new Set()); // instance to type
this.instances = new DefaultMap(() => new Set()); // type to instance
this.functionsFrom = new DefaultMap(() => new Set()); // type to outgoing function
this.functionsTo = new DefaultMap(() => new Set()); // type to incoming function
for (const {i, t} of mod.l) {
this.types.getdefault(i, true).add(t);
this.instances.getdefault(t, true).add(i);
}
this.functionsFrom = new DefaultMap(() => new Set()); // type to outgoing function
this.functionsTo = new DefaultMap(() => new Set()); // type to incoming function
for (const t of this.instances.m.keys()) {
if (t.symbol === symbolFunction) {
// 't' is a function signature
@ -28,6 +29,24 @@ class Context {
}
}
}
// this.typeVarAssigns = new Map();
// for (const t of this.instances.m.keys()) {
// if (t.typeVars) {
// for (const t2 of this.instances.m.keys()) {
// const genericT2 = (t2.typeVars === undefined)
// ? makeGeneric(() => t2)
// : t2;
// try {
// const unification = unify(t, t2);
// console.log(unification);
// } catch (e) {
// // skip
// }
// }
// }
// }
}
addToCtx({i, t}) {
@ -38,13 +57,13 @@ class Context {
}
}
let ctx = new Context({l:[
...ModuleStd.l,
...ModulePoint.l,
]});
const prettyIT = ({i, t}) => ({
strI: isType(i) ? prettyT(i) : pretty(i),
strT: prettyT(t),
@ -74,7 +93,11 @@ async function topPrompt() {
],
});
if (action === "list all types") {
await listAllTypes();
await listInstances(Type);
// await listAllTypes();
}
if (action === "list all functions") {
await listAllFunctions();
}
if (action === "list all") {
await listAllInstances();
@ -82,22 +105,46 @@ async function topPrompt() {
return topPrompt();
}
async function listAllTypes() {
// async function listAllTypes() {
// const choice = await select({
// message: "select type:",
// choices: [
// "(go back)",
// ...[...ctx.instances.m.keys()].map(t => ({
// value: t,
// name: prettyT(t),
// })),
// ]
// });
// if (choice === "(go back)") {
// return;
// }
// await typeOptions(choice);
// return listAllTypes();
// }
async function listAllFunctions() {
const choice = await select({
message: "select type:",
message: "select function:",
choices: [
"(go back)",
...[...ctx.instances.m.keys()].map(t => ({
value: t,
name: prettyT(t),
})),
]
...[...ctx.types.m.entries()]
.filter(([i, types]) => {
for (const type of types) {
if (type.symbol === symbolFunction)
return true;
}
return false;
})
.flatMap(toChoices),
],
});
if (choice === "(go back)") {
return;
}
await typeOptions(choice);
return listAllTypes();
const {i, t} = choice;
await functionOptions(i, t);
return listAllFunctions();
}
async function typeOptions(t) {
@ -106,8 +153,9 @@ async function typeOptions(t) {
choices: [
"(go back)",
"list instances",
"list outgoing functions",
"list incoming functions",
// "list outgoing functions",
// "list incoming functions",
"print raw",
"treat as instance",
],
});
@ -117,6 +165,9 @@ async function typeOptions(t) {
else if (choice === "list instances") {
await listInstances(t);
}
else if (choice === "print raw") {
console.log(pretty(t));
}
else if (choice === "treat as instance") {
await instanceOptions(t, Type)
}