Moved matching functionality to dedicated module
This commit is contained in:
parent
c326d0b229
commit
0b85b12551
|
@ -56,7 +56,8 @@ let rec matchInput = (input: itype, r: expressionValue): option<value> =>
|
||||||
| _ => None
|
| _ => None
|
||||||
}
|
}
|
||||||
|
|
||||||
module MatchSimple = {
|
module Matcher = {
|
||||||
|
module MatchSimple = {
|
||||||
type t = DifferentName | SameNameDifferentArguments | FullMatch
|
type t = DifferentName | SameNameDifferentArguments | FullMatch
|
||||||
|
|
||||||
let isFullMatch = (match: t) =>
|
let isFullMatch = (match: t) =>
|
||||||
|
@ -70,9 +71,9 @@ module MatchSimple = {
|
||||||
| SameNameDifferentArguments => true
|
| SameNameDifferentArguments => true
|
||||||
| _ => false
|
| _ => false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module Match = {
|
module Match = {
|
||||||
type t<'a, 'b> = DifferentName | SameNameDifferentArguments('a) | FullMatch('b)
|
type t<'a, 'b> = DifferentName | SameNameDifferentArguments('a) | FullMatch('b)
|
||||||
|
|
||||||
let isFullMatch = (match: t<'a, 'b>): bool =>
|
let isFullMatch = (match: t<'a, 'b>): bool =>
|
||||||
|
@ -86,9 +87,9 @@ module Match = {
|
||||||
| SameNameDifferentArguments(_) => true
|
| SameNameDifferentArguments(_) => true
|
||||||
| _ => false
|
| _ => false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module FnDefinition = {
|
module FnDefinition = {
|
||||||
type definitionMatch = MatchSimple.t
|
type definitionMatch = MatchSimple.t
|
||||||
|
|
||||||
let getArgValues = (f: fnDefinition, args: array<expressionValue>): option<array<value>> => {
|
let getArgValues = (f: fnDefinition, args: array<expressionValue>): option<array<value>> => {
|
||||||
|
@ -116,31 +117,12 @@ module FnDefinition = {
|
||||||
matchAssumingSameName(f, args)
|
matchAssumingSameName(f, args)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let run = (f: fnDefinition, args: array<expressionValue>) => {
|
|
||||||
let argValues = getArgValues(f, args)
|
|
||||||
switch argValues {
|
|
||||||
| Some(values) => f.run(values)
|
|
||||||
| None => Error("Impossible")
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
module Function = {
|
module Function = {
|
||||||
type definitionId = int
|
type definitionId = int
|
||||||
type match = Match.t<array<definitionId>, definitionId>
|
type match = Match.t<array<definitionId>, definitionId>
|
||||||
|
|
||||||
let make = (~name, ~definitions): function => {
|
|
||||||
name: name,
|
|
||||||
definitions: definitions,
|
|
||||||
}
|
|
||||||
|
|
||||||
let makeDefinition = (~name, ~inputs, ~run): fnDefinition => {
|
|
||||||
name: name,
|
|
||||||
inputs: inputs,
|
|
||||||
run: run,
|
|
||||||
}
|
|
||||||
|
|
||||||
let match = (f: function, fnName: string, args: array<expressionValue>): match => {
|
let match = (f: function, fnName: string, args: array<expressionValue>): match => {
|
||||||
let matchedDefinition = () =>
|
let matchedDefinition = () =>
|
||||||
E.A.getIndexBy(f.definitions, r =>
|
E.A.getIndexBy(f.definitions, r =>
|
||||||
|
@ -164,18 +146,18 @@ module Function = {
|
||||||
Match.DifferentName,
|
Match.DifferentName,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module RegistryMatch = {
|
module RegistryMatch = {
|
||||||
type match = {
|
type match = {
|
||||||
fnName: string,
|
fnName: string,
|
||||||
inputIndex: int,
|
inputIndex: int,
|
||||||
}
|
}
|
||||||
type t = Match.t<array<match>, match>
|
type t = Match.t<array<match>, match>
|
||||||
let makeMatch = (fnName: string, inputIndex: int) => {fnName: fnName, inputIndex: inputIndex}
|
let makeMatch = (fnName: string, inputIndex: int) => {fnName: fnName, inputIndex: inputIndex}
|
||||||
}
|
}
|
||||||
|
|
||||||
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, Function.match(l, fnName, args)))
|
let functionMatchPairs = r->E.A2.fmap(l => (l, Function.match(l, fnName, args)))
|
||||||
let getFullMatch = E.A.getBy(functionMatchPairs, ((_, match: Function.match)) =>
|
let getFullMatch = E.A.getBy(functionMatchPairs, ((_, match: Function.match)) =>
|
||||||
|
@ -226,11 +208,48 @@ module Registry = {
|
||||||
registry
|
registry
|
||||||
->E.A.getBy(fn => fn.name === fnName)
|
->E.A.getBy(fn => fn.name === fnName)
|
||||||
->E.O.bind(fn => E.A.get(fn.definitions, inputIndex))
|
->E.O.bind(fn => E.A.get(fn.definitions, inputIndex))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module FnDefinition = {
|
||||||
|
let getArgValues = (f: fnDefinition, args: array<expressionValue>): option<array<value>> => {
|
||||||
|
let mainInputTypes = f.inputs
|
||||||
|
if E.A.length(f.inputs) !== E.A.length(args) {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
E.A.zip(mainInputTypes, args)
|
||||||
|
->E.A2.fmap(((input, arg)) => matchInput(input, arg))
|
||||||
|
->E.A.O.openIfAllSome
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let run = (f: fnDefinition, args: array<expressionValue>) => {
|
||||||
|
let argValues = getArgValues(f, args)
|
||||||
|
switch argValues {
|
||||||
|
| Some(values) => f.run(values)
|
||||||
|
| None => Error("Impossible")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module Function = {
|
||||||
|
type definitionId = int
|
||||||
|
let make = (~name, ~definitions): function => {
|
||||||
|
name: name,
|
||||||
|
definitions: definitions,
|
||||||
|
}
|
||||||
|
|
||||||
|
let makeDefinition = (~name, ~inputs, ~run): fnDefinition => {
|
||||||
|
name: name,
|
||||||
|
inputs: inputs,
|
||||||
|
run: run,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module Registry = {
|
||||||
let matchAndRun = (r: registry, fnName: string, args: array<expressionValue>) => {
|
let matchAndRun = (r: registry, fnName: string, args: array<expressionValue>) => {
|
||||||
switch findMatches(r, fnName, args) {
|
switch Matcher.Registry.findMatches(r, fnName, args) {
|
||||||
| Match.FullMatch(m) =>
|
| Matcher.Match.FullMatch(m) =>
|
||||||
fullMatchToDef(r, m)->E.O2.fmap(r => {
|
Matcher.Registry.fullMatchToDef(r, m)->E.O2.fmap(r => {
|
||||||
FnDefinition.run(r, args)
|
FnDefinition.run(r, args)
|
||||||
})
|
})
|
||||||
| _ => None
|
| _ => None
|
||||||
|
|
Loading…
Reference in New Issue
Block a user