simplify: no distinction between generic types and 'normal' types.
This commit is contained in:
parent
b4826605af
commit
a664ddac8a
27 changed files with 535 additions and 360 deletions
39
lib/compare/dynamic.js
Normal file
39
lib/compare/dynamic.js
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
import { getInst, getType } from "../primitives/dynamic.js";
|
||||
import { SymbolBool, SymbolBottom, SymbolByte, SymbolChar, SymbolDouble, SymbolDynamic, SymbolInt, SymbolUUID, SymbolType, SymbolUnit } from "../primitives/primitive_types.js";
|
||||
import { symbolDict, symbolFunction, symbolList, symbolProduct, symbolSet, symbolSum } from "../structures/type_constructors.js";
|
||||
import { compareBools, compareNumbers, compareStrings, compareSymbols, compareUnits } from "./primitives.js";
|
||||
import { compareDicts, compareFunctions, compareLists, compareProducts, compareSets, compareSums } from "./structures.js";
|
||||
import { compareTypes } from "./type.js";
|
||||
|
||||
export const compareDynamic = x => y =>
|
||||
compareTypes(getType(x))(getType(y))
|
||||
|| makeCompareFn(getType(x))(getInst(x))(getInst(y));
|
||||
|
||||
const typeSymbolToCmp = new Map([
|
||||
[SymbolInt , compareNumbers],
|
||||
[SymbolBool , compareBools],
|
||||
[SymbolDouble , compareNumbers],
|
||||
[SymbolByte , compareNumbers],
|
||||
[SymbolChar , compareStrings],
|
||||
[SymbolUnit , compareUnits],
|
||||
[SymbolBottom , _ => _ => { throw new Error("Bottom!"); }],
|
||||
[SymbolUUID , compareSymbols],
|
||||
// [SymbolGenericType, ?] TODO
|
||||
[SymbolType , compareTypes],
|
||||
[SymbolDynamic, compareDynamic],
|
||||
|
||||
// these functions take extra comparison callbacks:
|
||||
[symbolFunction, compareFunctions],
|
||||
[symbolSum , compareSums],
|
||||
[symbolProduct , compareProducts],
|
||||
[symbolList , compareLists],
|
||||
[symbolSet , compareSets],
|
||||
[symbolDict , compareDicts],
|
||||
]);
|
||||
|
||||
export const makeCompareFn = type => {
|
||||
return type.params.reduce(
|
||||
(acc, cur) => acc(makeCompareFn(cur(type))),
|
||||
typeSymbolToCmp.get(type.symbol)
|
||||
);
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue