Moved matching functionality to dedicated module

This commit is contained in:
Ozzie Gooen 2022-05-19 18:17:31 -04:00
parent c326d0b229
commit 0b85b12551

View File

@ -56,6 +56,7 @@ let rec matchInput = (input: itype, r: expressionValue): option<value> =>
| _ => None
}
module Matcher = {
module MatchSimple = {
type t = DifferentName | SameNameDifferentArguments | FullMatch
@ -116,31 +117,12 @@ module FnDefinition = {
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 = {
type definitionId = int
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 matchedDefinition = () =>
E.A.getIndexBy(f.definitions, r =>
@ -226,11 +208,48 @@ module Registry = {
registry
->E.A.getBy(fn => fn.name === fnName)
->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>) => {
switch findMatches(r, fnName, args) {
| Match.FullMatch(m) =>
fullMatchToDef(r, m)->E.O2.fmap(r => {
switch Matcher.Registry.findMatches(r, fnName, args) {
| Matcher.Match.FullMatch(m) =>
Matcher.Registry.fullMatchToDef(r, m)->E.O2.fmap(r => {
FnDefinition.run(r, args)
})
| _ => None