First part of cleanup

This commit is contained in:
Ozzie Gooen 2022-05-18 16:49:22 -04:00
parent c1de95b39a
commit f7f94cbcb1

View File

@ -30,18 +30,6 @@ type function = {
type registry = array<function> type registry = array<function>
module Function = {
let make = (name, definitions): function => {
name: name,
definitions: definitions,
}
let makeDefinition = (name, inputs, run): fnDefinition => {
name: name,
inputs: inputs,
run: run,
}
}
let rec matchInput = (input: itype, r: expressionValue): option<value> => let rec matchInput = (input: itype, r: expressionValue): option<value> =>
switch (input, r) { switch (input, r) {
| (I_Number, EvNumber(f)) => Some(Number(f)) | (I_Number, EvNumber(f)) => Some(Number(f))
@ -67,21 +55,23 @@ let rec matchInput = (input: itype, r: expressionValue): option<value> =>
| _ => None | _ => None
} }
type match = DifferentName | SameNameDifferentArguments(string) | Match(string, array<value>) module FnDefinition = {
type definitionMatch =
DifferentName | SameNameDifferentArguments(string) | Match(string, array<value>)
let isFullMatch = (match: match) => let isFullMatch = (match: definitionMatch) =>
switch match { switch match {
| Match(_, _) => true | Match(_, _) => true
| _ => false | _ => false
} }
let isNameMatchOnly = (match: match) => let isNameMatchOnly = (match: definitionMatch) =>
switch match { switch match {
| SameNameDifferentArguments(_) => true | SameNameDifferentArguments(_) => true
| _ => false | _ => false
} }
let matchSingleSameName = (f: fnDefinition, args: array<expressionValue>) => { let matchSingleSameName = (f: fnDefinition, args: array<expressionValue>) => {
let inputTypes = f.inputs let inputTypes = f.inputs
if E.A.length(f.inputs) !== E.A.length(args) { if E.A.length(f.inputs) !== E.A.length(args) {
SameNameDifferentArguments(f.name) SameNameDifferentArguments(f.name)
@ -95,47 +85,50 @@ let matchSingleSameName = (f: fnDefinition, args: array<expressionValue>) => {
| None => SameNameDifferentArguments(f.name) | None => SameNameDifferentArguments(f.name)
} }
} }
} }
let matchSingle = (f: fnDefinition, fnName: string, args: array<expressionValue>) => { let match = (f: fnDefinition, fnName: string, args: array<expressionValue>) => {
if f.name !== fnName { if f.name !== fnName {
DifferentName DifferentName
} else { } else {
matchSingleSameName(f, args) matchSingleSameName(f, args)
} }
}
} }
let match = (f: function, fnName: string, args: array<expressionValue>) => { module Function = {
let matchedDefinition = () => type match = [#FullMatch(int) | #NameMatchOnly(array<int>) | #NoMatch]
E.A.getByOpen(f.definitions, r => matchSingle(r, fnName, args), isFullMatch) let isFullMatch = (t: match) =>
let getMatchedNameOnlyDefinition = () =>
E.A.getByOpen(f.definitions, r => matchSingle(r, fnName, args), isNameMatchOnly)
E.A.O.firstSomeFnWithDefault([matchedDefinition, getMatchedNameOnlyDefinition], DifferentName)
}
module IndexMatch = {
type t = [#FullMatch(int) | #NameMatchOnly(array<int>) | #NoMatch]
let isFullMatch = (t: t) =>
switch t { switch t {
| #FullMatch(_) => true | #FullMatch(_) => true
| _ => false | _ => false
} }
let isNameOnlyMatch = (t: t) => let isNameOnlyMatch = (t: match) =>
switch t { switch t {
| #NameMatchOnly(_) => true | #NameMatchOnly(_) => true
| _ => false | _ => false
} }
} let make = (name, definitions): function => {
name: name,
definitions: definitions,
}
let makeDefinition = (name, inputs, run): fnDefinition => {
name: name,
inputs: inputs,
run: run,
}
let match2 = (f: function, fnName: string, args: array<expressionValue>) => { let match = (f: function, fnName: string, args: array<expressionValue>): match => {
let matchedDefinition = () => let matchedDefinition = () =>
E.A.getIndexBy(f.definitions, r => isFullMatch(matchSingle(r, fnName, args))) |> E.O.fmap(r => E.A.getIndexBy(f.definitions, r =>
#FullMatch(r) FnDefinition.isFullMatch(FnDefinition.match(r, fnName, args))
) ) |> E.O.fmap(r => #FullMatch(r))
let getMatchedNameOnlyDefinition = () => { let getMatchedNameOnlyDefinition = () => {
let nameMatchIndexes = let nameMatchIndexes =
f.definitions f.definitions
->E.A2.fmapi((index, r) => isNameMatchOnly(matchSingle(r, fnName, args)) ? Some(index) : None) ->E.A2.fmapi((index, r) =>
FnDefinition.isNameMatchOnly(FnDefinition.match(r, fnName, args)) ? Some(index) : None
)
->E.A.O.concatSomes ->E.A.O.concatSomes
switch nameMatchIndexes { switch nameMatchIndexes {
| [] => None | [] => None
@ -144,9 +137,10 @@ let match2 = (f: function, fnName: string, args: array<expressionValue>) => {
} }
E.A.O.firstSomeFnWithDefault([matchedDefinition, getMatchedNameOnlyDefinition], #NoMatch) E.A.O.firstSomeFnWithDefault([matchedDefinition, getMatchedNameOnlyDefinition], #NoMatch)
}
} }
module IndexMatch2 = { module RegistryMatch = {
type match = { type match = {
fnName: string, fnName: string,
inputIndex: int, inputIndex: int,
@ -167,11 +161,11 @@ module IndexMatch2 = {
module Registry = { module Registry = {
let findExactMatches = (r: registry, fnName: string, args: array<expressionValue>) => { let findExactMatches = (r: registry, fnName: string, args: array<expressionValue>) => {
let functionMatchPairs = r->E.A2.fmap(l => (l, match2(l, fnName, args))) let functionMatchPairs = r->E.A2.fmap(l => (l, Function.match(l, fnName, args)))
let getFullMatch = E.A.getBy(functionMatchPairs, ((_, match)) => IndexMatch.isFullMatch(match)) let getFullMatch = E.A.getBy(functionMatchPairs, ((_, match)) => Function.isFullMatch(match))
let fullMatch: option<IndexMatch2.match> = getFullMatch->E.O.bind(((fn, match)) => let fullMatch: option<RegistryMatch.match> = getFullMatch->E.O.bind(((fn, match)) =>
switch match { switch match {
| #FullMatch(index) => Some(IndexMatch2.makeMatch(fn.name, index)) | #FullMatch(index) => Some(RegistryMatch.makeMatch(fn.name, index))
| _ => None | _ => None
} }
) )
@ -179,17 +173,17 @@ module Registry = {
} }
let findNameMatches = (r: registry, fnName: string, args: array<expressionValue>) => { let findNameMatches = (r: registry, fnName: string, args: array<expressionValue>) => {
let functionMatchPairs = r->E.A2.fmap(l => (l, match2(l, fnName, args))) let functionMatchPairs = r->E.A2.fmap(l => (l, Function.match(l, fnName, args)))
let getNameMatches = let getNameMatches =
functionMatchPairs functionMatchPairs
->E.A2.fmap(((fn, match)) => IndexMatch.isNameOnlyMatch(match) ? Some((fn, match)) : None) ->E.A2.fmap(((fn, match)) => Function.isNameOnlyMatch(match) ? Some((fn, match)) : None)
->E.A.O.concatSomes ->E.A.O.concatSomes
let matches = let matches =
getNameMatches getNameMatches
->E.A2.fmap(((fn, match)) => ->E.A2.fmap(((fn, match)) =>
switch match { switch match {
| #NameMatchOnly(indexes) => | #NameMatchOnly(indexes) =>
indexes->E.A2.fmap(index => IndexMatch2.makeMatch(fn.name, index)) indexes->E.A2.fmap(index => RegistryMatch.makeMatch(fn.name, index))
| _ => [] | _ => []
} }
) )
@ -208,7 +202,7 @@ module Registry = {
} }
} }
let fullMatchToDef = (registry: registry, {fnName, inputIndex}: IndexMatch2.match): option< let fullMatchToDef = (registry: registry, {fnName, inputIndex}: RegistryMatch.match): option<
fnDefinition, fnDefinition,
> => > =>
registry registry
@ -216,7 +210,7 @@ module Registry = {
->E.O.bind(fn => E.A.get(fn.definitions, inputIndex)) ->E.O.bind(fn => E.A.get(fn.definitions, inputIndex))
let runDef = (fnDefinition: fnDefinition, args: array<expressionValue>) => { let runDef = (fnDefinition: fnDefinition, args: array<expressionValue>) => {
switch matchSingleSameName(fnDefinition, args) { switch FnDefinition.matchSingleSameName(fnDefinition, args) {
| Match(_, values) => fnDefinition.run(values) | Match(_, values) => fnDefinition.run(values)
| _ => Error("Impossible") | _ => Error("Impossible")
} }