move function

This commit is contained in:
Joeri Exelmans 2025-04-20 21:14:40 +02:00
parent 8a4bd44f04
commit 55c5d7cffa
5 changed files with 38 additions and 36 deletions

View file

@ -1,5 +1,38 @@
import { inspect } from 'node:util';
import { symbolFunction, symbolList, symbolProduct, symbolSum } from '../structures/types.js';
export function pretty(obj) {
return inspect(obj, { colors: true, depth: null, breakLength: 120 });
}
// Pretty print type
export function prettyT(type) {
// console.log("pretty:", type);
if (typeof type === "symbol") {
return type.description;
}
if (type.typeVars) {
if (type.typeVars.size > 0) {
return `${[...type.typeVars].map(prettyT).sort((a, b) => a.localeCompare(b)).join(",")}: ${prettyT(type.type)}`;
}
else {
return prettyT(type.type);
}
}
if (type.symbol === symbolFunction) {
return `(${prettyT(type.params[0])} -> ${prettyT(type.params[1])})`;
}
if (type.symbol === symbolList) {
return `[${prettyT(type.params[0])}]`;
}
if (type.symbol === symbolProduct) {
return `(${prettyT(type.params[0])} × ${prettyT(type.params[1])})`;
}
if (type.symbol === symbolSum) {
return `(${prettyT(type.params[0])} | ${prettyT(type.params[1])})`;
}
if (type.params.length === 0) {
return type.symbol.description;
}
return `${type.symbol.description}(${type.params.map(prettyT).join(", ")})`;
}