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

@ -2,12 +2,13 @@ import { select, number, input } from '@inquirer/prompts';
import { ModulePoint } from "../lib/point.js"; import { ModulePoint } from "../lib/point.js";
import { DefaultMap } from "../util/defaultmap.js"; import { DefaultMap } from "../util/defaultmap.js";
import { pretty } from '../util/pretty.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 { ModuleStd } from '../stdlib.js';
import { Double, GenericType, Int, SymbolT, Type } from "../primitives/types.js"; import { Double, GenericType, Int, SymbolT, Type } from "../primitives/types.js";
import { eqType } from '../primitives/type.js'; import { eqType } from '../primitives/type.js';
import { Any } from "../primitives/types.js"; import { Any } from "../primitives/types.js";
import { assignFn, makeGeneric, onlyOccurring } from '../generics/generics.js'; import { assignFn, makeGeneric, onlyOccurring } from '../generics/generics.js';
import { prettyT } from '../util/pretty.js';
// import {emitKeypressEvents} from 'node:readline'; // import {emitKeypressEvents} from 'node:readline';

View file

@ -1,7 +1,7 @@
import { eqType } from "../primitives/type.js"; import { eqType } from "../primitives/type.js";
import { zip } from "../util/util.js"; import { zip } from "../util/util.js";
import { pretty } from '../util/pretty.js'; import { pretty } from '../util/pretty.js';
import { prettyT } from "../structures/types.js"; import { prettyT } from "../util/pretty.js";
// constructor for generic types // constructor for generic types
// for instance, the type: // for instance, the type:

View file

@ -52,36 +52,3 @@ export const lsType = makeTypeConstructor(symbolList)(1);
export const symbolSet = Symbol('Set'); export const symbolSet = Symbol('Set');
export const setType = makeTypeConstructor(symbolSet)(1); 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(", ")})`;
}

View file

@ -1,5 +1,38 @@
import { inspect } from 'node:util'; import { inspect } from 'node:util';
import { symbolFunction, symbolList, symbolProduct, symbolSum } from '../structures/types.js';
export function pretty(obj) { export function pretty(obj) {
return inspect(obj, { colors: true, depth: null, breakLength: 120 }); 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(", ")})`;
}

View file

@ -3,7 +3,8 @@ import { Int } from "../primitives/types.js";
import { enumType } from "../structures/enum.js"; import { enumType } from "../structures/enum.js";
import { newProduct } from "../structures/product.js"; import { newProduct } from "../structures/product.js";
import { structType } from "../structures/struct.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([ const Slot = structType([
newProduct("value")(() => Value), newProduct("value")(() => Value),