diff --git a/examples/prompt.js b/examples/prompt.js index 740fd44..c17e27d 100644 --- a/examples/prompt.js +++ b/examples/prompt.js @@ -2,12 +2,13 @@ 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 { isFunction } from '../structures/types.js'; import { ModuleStd } from '../stdlib.js'; import { Double, GenericType, Int, SymbolT, Type } from "../primitives/types.js"; import { eqType } from '../primitives/type.js'; import { Any } from "../primitives/types.js"; import { assignFn, makeGeneric, onlyOccurring } from '../generics/generics.js'; +import { prettyT } from '../util/pretty.js'; // import {emitKeypressEvents} from 'node:readline'; diff --git a/generics/generics.js b/generics/generics.js index 93c0371..5a987dc 100644 --- a/generics/generics.js +++ b/generics/generics.js @@ -1,7 +1,7 @@ import { eqType } from "../primitives/type.js"; import { zip } from "../util/util.js"; import { pretty } from '../util/pretty.js'; -import { prettyT } from "../structures/types.js"; +import { prettyT } from "../util/pretty.js"; // constructor for generic types // for instance, the type: diff --git a/structures/types.js b/structures/types.js index 12bc350..7526a33 100644 --- a/structures/types.js +++ b/structures/types.js @@ -52,36 +52,3 @@ export const lsType = makeTypeConstructor(symbolList)(1); export const symbolSet = Symbol('Set'); export const setType = makeTypeConstructor(symbolSet)(1); - - -// 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(", ")})`; -} diff --git a/util/pretty.js b/util/pretty.js index 9b0681b..fbf0c93 100644 --- a/util/pretty.js +++ b/util/pretty.js @@ -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(", ")})`; +} diff --git a/versioning/types.js b/versioning/types.js index 8068653..ac55fb0 100644 --- a/versioning/types.js +++ b/versioning/types.js @@ -3,7 +3,8 @@ import { Int } from "../primitives/types.js"; import { enumType } from "../structures/enum.js"; import { newProduct } from "../structures/product.js"; import { structType } from "../structures/struct.js"; -import { prettyT, prodType } from "../structures/types.js"; +import { prodType } from "../structures/types.js"; +import { prettyT } from "../util/pretty.js"; const Slot = structType([ newProduct("value")(() => Value),