38 lines
1.2 KiB
JavaScript
38 lines
1.2 KiB
JavaScript
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(", ")})`;
|
||
}
|