Move matchers to type package
This commit is contained in:
@@ -1,39 +1,5 @@
|
|||||||
# from smnp.type.model import Type
|
# from smnp.type.model import Type
|
||||||
#
|
#
|
||||||
from smnp.type.model import Type
|
|
||||||
|
|
||||||
|
|
||||||
class Matcher:
|
|
||||||
def __init__(self, objectType, matcher, string):
|
|
||||||
self.type = objectType
|
|
||||||
self.matcher = matcher
|
|
||||||
self.string = string
|
|
||||||
|
|
||||||
def match(self, value):
|
|
||||||
if self.type is not None and self.type != value.type:
|
|
||||||
return False
|
|
||||||
return self.matcher(value)
|
|
||||||
|
|
||||||
def andWith(self, matcher):
|
|
||||||
if self.type != matcher.type:
|
|
||||||
raise RuntimeError("Support types of matches are not the same")
|
|
||||||
string = f"[{self.string} and {matcher.string}]"
|
|
||||||
return Matcher(self.type, lambda x: self.match(x) and matcher.match(x), string)
|
|
||||||
|
|
||||||
def orWith(self, matcher):
|
|
||||||
string = f"[{self.string} or {matcher.string}]"
|
|
||||||
return Matcher(None, lambda x: self.match(x) or matcher.match(x), string)
|
|
||||||
|
|
||||||
def __eq__(self, other):
|
|
||||||
return self.type == other.type and self.string == other.string
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def __str__(self):
|
|
||||||
return self.string
|
|
||||||
|
|
||||||
def __repr__(self):
|
|
||||||
return self.__str__()
|
|
||||||
|
|
||||||
|
|
||||||
class Signature:
|
class Signature:
|
||||||
@@ -65,7 +31,7 @@ def varargSignature(varargMatcher, *basicSignature):
|
|||||||
|
|
||||||
def doesNotMatchVararg(basicSignature):
|
def doesNotMatchVararg(basicSignature):
|
||||||
return (False, *[None for n in basicSignature], None)
|
return (False, *[None for n in basicSignature], None)
|
||||||
#
|
|
||||||
|
|
||||||
|
|
||||||
def signature(*signature):
|
def signature(*signature):
|
||||||
@@ -88,65 +54,3 @@ def doesNotMatch(sign):
|
|||||||
return (False, *[None for n in sign])
|
return (False, *[None for n in sign])
|
||||||
|
|
||||||
|
|
||||||
def allTypes():
|
|
||||||
allowedTypes = [t for t in Type if t != Type.VOID]
|
|
||||||
return ofTypes(*allowedTypes)
|
|
||||||
|
|
||||||
|
|
||||||
def ofTypes(*types):
|
|
||||||
def check(value):
|
|
||||||
return value.type in types
|
|
||||||
return Matcher(None, check, f"<{', '.join([t.name.lower() for t in types])}>")
|
|
||||||
|
|
||||||
def ofType(type):
|
|
||||||
def check(value):
|
|
||||||
return value.type == type
|
|
||||||
|
|
||||||
return Matcher(None, check, type.name.lower())
|
|
||||||
|
|
||||||
def listOf(*types):
|
|
||||||
def check(value):
|
|
||||||
return len([item for item in value.value if not item.type in types]) == 0
|
|
||||||
|
|
||||||
return Matcher(Type.LIST, check, f"{Type.LIST.name.lower()}<{', '.join([t.name.lower() for t in types])}>")
|
|
||||||
|
|
||||||
def listOfMatchers(*matchers):
|
|
||||||
def check(value):
|
|
||||||
matched = 0
|
|
||||||
for item in value.value:
|
|
||||||
matched += 1 if any(matcher.match(item) for matcher in matchers) else 0
|
|
||||||
|
|
||||||
return matched == len(value.value)
|
|
||||||
|
|
||||||
return Matcher(Type.LIST, check, f"{Type.LIST.name.lower()}<{', '.join([m.string for m in matchers])}>")
|
|
||||||
|
|
||||||
def mapOfMatchers(keyMatchers, valueMatchers):
|
|
||||||
def check(map):
|
|
||||||
matched = 0
|
|
||||||
for key, value in map.value.items():
|
|
||||||
matched += 1 if any(matcher.match(key) for matcher in keyMatchers) \
|
|
||||||
and any(matcher.match(value) for matcher in valueMatchers) else 0
|
|
||||||
|
|
||||||
return matched == len(map.value)
|
|
||||||
|
|
||||||
return Matcher(Type.MAP, check, f"{Type.MAP.name.lower()}<{', '.join([m.string for m in keyMatchers])}><{', '.join([m.string for m in valueMatchers])}>")
|
|
||||||
|
|
||||||
|
|
||||||
def listMatches(*pattern):
|
|
||||||
def check(value):
|
|
||||||
return signature(*pattern).check(value.value)[0]
|
|
||||||
|
|
||||||
return Matcher(Type.LIST, check, f"({', '.join([str(m) for m in pattern])})")
|
|
||||||
|
|
||||||
def recursiveListMatcher(matcher):
|
|
||||||
if matcher.type == Type.LIST:
|
|
||||||
raise RuntimeError(f"Passed matcher will be handling non-list types, so it cannot have type set to {Type.LIST}")
|
|
||||||
|
|
||||||
def check(value):
|
|
||||||
if value.type != Type.LIST:
|
|
||||||
return matcher.match(value)
|
|
||||||
for item in value.value:
|
|
||||||
return check(item)
|
|
||||||
|
|
||||||
return Matcher(Type.LIST, check, f"[LISTS OF {str(matcher)}]")
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
from functools import reduce
|
from functools import reduce
|
||||||
|
|
||||||
from smnp.library.model import Function
|
from smnp.library.model import Function
|
||||||
from smnp.library.signature import varargSignature, ofTypes
|
from smnp.library.signature import varargSignature
|
||||||
from smnp.type.model import Type
|
from smnp.type.model import Type
|
||||||
|
from smnp.type.signature.matcher.type import ofTypes
|
||||||
|
|
||||||
_signature = varargSignature(ofTypes(Type.LIST))
|
_signature = varargSignature(ofTypes(Type.LIST))
|
||||||
def _function(env, vararg):
|
def _function(env, vararg):
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
from smnp.library.model import Function
|
from smnp.library.model import Function
|
||||||
from smnp.library.signature import varargSignature, allTypes
|
from smnp.library.signature import varargSignature
|
||||||
from smnp.type.model import Type
|
from smnp.type.model import Type
|
||||||
|
from smnp.type.signature.matcher.type import allTypes
|
||||||
|
|
||||||
_signature = varargSignature(allTypes())
|
_signature = varargSignature(allTypes())
|
||||||
def _function(env, vararg):
|
def _function(env, vararg):
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
from smnp.error.runtime import RuntimeException
|
from smnp.error.runtime import RuntimeException
|
||||||
from smnp.library.model import CombinedFunction, Function
|
from smnp.library.model import CombinedFunction, Function
|
||||||
from smnp.library.signature import signature, ofType, ofTypes
|
from smnp.library.signature import signature
|
||||||
from smnp.type.model import Type
|
from smnp.type.model import Type
|
||||||
|
from smnp.type.signature.matcher.type import ofTypes, ofType
|
||||||
|
|
||||||
_signature1 = signature(ofType(Type.LIST), ofType(Type.INTEGER))
|
_signature1 = signature(ofType(Type.LIST), ofType(Type.INTEGER))
|
||||||
def _function1(env, list, index):
|
def _function1(env, list, index):
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
from smnp.library.model import Function, CombinedFunction
|
from smnp.library.model import Function, CombinedFunction
|
||||||
from smnp.library.signature import varargSignature, listMatches, ofTypes, allTypes, signature, listOfMatchers
|
from smnp.library.signature import varargSignature, signature
|
||||||
from smnp.type.model import Type
|
from smnp.type.model import Type
|
||||||
|
from smnp.type.signature.matcher.list import listOfMatchers, listMatches
|
||||||
|
from smnp.type.signature.matcher.type import allTypes, ofTypes
|
||||||
|
|
||||||
_signature1 = varargSignature(listMatches(ofTypes(Type.INTEGER, Type.STRING, Type.NOTE), allTypes()))
|
_signature1 = varargSignature(listMatches(ofTypes(Type.INTEGER, Type.STRING, Type.NOTE), allTypes()))
|
||||||
def _function1(env, vararg):
|
def _function1(env, vararg):
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
from smnp.library.model import CombinedFunction, Function
|
from smnp.library.model import CombinedFunction, Function
|
||||||
from smnp.library.signature import signature, ofType
|
from smnp.library.signature import signature
|
||||||
from smnp.note.model import Note
|
from smnp.note.model import Note
|
||||||
from smnp.type.model import Type
|
from smnp.type.model import Type
|
||||||
|
from smnp.type.signature.matcher.type import ofType
|
||||||
|
|
||||||
_signature1 = signature(ofType(Type.INTEGER))
|
_signature1 = signature(ofType(Type.INTEGER))
|
||||||
def _function1(env, upper):
|
def _function1(env, upper):
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
from smnp.library.model import CombinedFunction, Function
|
from smnp.library.model import CombinedFunction, Function
|
||||||
from smnp.library.signature import signature, ofTypes
|
from smnp.library.signature import signature
|
||||||
from smnp.module.mic.lib.detector.noise import NoiseDetector
|
from smnp.module.mic.lib.detector.noise import NoiseDetector
|
||||||
from smnp.type.model import Type
|
from smnp.type.model import Type
|
||||||
|
from smnp.type.signature.matcher.type import ofTypes
|
||||||
|
|
||||||
_signature1 = signature()
|
_signature1 = signature()
|
||||||
def _function1(env):
|
def _function1(env):
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
from smnp.library.model import Function
|
from smnp.library.model import Function
|
||||||
from smnp.library.signature import signature, ofType
|
from smnp.library.signature import signature
|
||||||
from smnp.type.model import Type
|
from smnp.type.model import Type
|
||||||
|
from smnp.type.signature.matcher.type import ofType
|
||||||
|
|
||||||
_signature = signature(ofType(Type.NOTE), ofType(Type.INTEGER))
|
_signature = signature(ofType(Type.NOTE), ofType(Type.INTEGER))
|
||||||
def _function(env, note, duration):
|
def _function(env, note, duration):
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
from smnp.library.model import Function, CombinedFunction
|
from smnp.library.model import Function, CombinedFunction
|
||||||
from smnp.library.signature import varargSignature, ofTypes, listOf
|
from smnp.library.signature import varargSignature
|
||||||
from smnp.note.interval import intervalToString
|
from smnp.note.interval import intervalToString
|
||||||
from smnp.note.model import Note
|
from smnp.note.model import Note
|
||||||
from smnp.type.model import Type
|
from smnp.type.model import Type
|
||||||
|
from smnp.type.signature.matcher.list import listOf
|
||||||
|
from smnp.type.signature.matcher.type import ofTypes
|
||||||
|
|
||||||
_signature1 = varargSignature(ofTypes(Type.NOTE, Type.INTEGER))
|
_signature1 = varargSignature(ofTypes(Type.NOTE, Type.INTEGER))
|
||||||
def _function1(env, vararg):
|
def _function1(env, vararg):
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
from smnp.library.model import Function
|
from smnp.library.model import Function
|
||||||
from smnp.library.signature import signature, ofType
|
from smnp.library.signature import signature
|
||||||
from smnp.type.model import Type
|
from smnp.type.model import Type
|
||||||
|
from smnp.type.signature.matcher.type import ofType
|
||||||
|
|
||||||
_signature = signature(ofType(Type.NOTE), ofType(Type.INTEGER))
|
_signature = signature(ofType(Type.NOTE), ofType(Type.INTEGER))
|
||||||
def _function(env, note, octave):
|
def _function(env, note, octave):
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
from smnp.library.model import Function, CombinedFunction
|
from smnp.library.model import Function, CombinedFunction
|
||||||
from smnp.library.signature import varargSignature, ofTypes, listOf
|
from smnp.library.signature import varargSignature
|
||||||
from smnp.note.model import Note
|
from smnp.note.model import Note
|
||||||
from smnp.type.model import Type
|
from smnp.type.model import Type
|
||||||
|
from smnp.type.signature.matcher.list import listOf
|
||||||
|
from smnp.type.signature.matcher.type import ofTypes
|
||||||
|
|
||||||
_signature1 = varargSignature(ofTypes(Type.NOTE, Type.INTEGER))
|
_signature1 = varargSignature(ofTypes(Type.NOTE, Type.INTEGER))
|
||||||
def _function1(env, vararg):
|
def _function1(env, vararg):
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
from smnp.library.model import CombinedFunction, Function
|
from smnp.library.model import CombinedFunction, Function
|
||||||
from smnp.library.signature import varargSignature, ofTypes, listOf
|
from smnp.library.signature import varargSignature
|
||||||
from smnp.type.model import Type
|
from smnp.type.model import Type
|
||||||
|
from smnp.type.signature.matcher.list import listOf
|
||||||
|
from smnp.type.signature.matcher.type import ofTypes
|
||||||
|
|
||||||
_signature1 = varargSignature(ofTypes(Type.INTEGER, Type.NOTE), ofTypes(Type.INTEGER))
|
_signature1 = varargSignature(ofTypes(Type.INTEGER, Type.NOTE), ofTypes(Type.INTEGER))
|
||||||
def _function1(env, value, vararg):
|
def _function1(env, value, vararg):
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
from smnp.library.model import CombinedFunction, Function
|
from smnp.library.model import CombinedFunction, Function
|
||||||
from smnp.library.signature import signature, listOf, ofTypes, varargSignature
|
from smnp.library.signature import signature, varargSignature
|
||||||
from smnp.type.model import Type
|
from smnp.type.model import Type
|
||||||
|
from smnp.type.signature.matcher.list import listOf
|
||||||
|
from smnp.type.signature.matcher.type import ofTypes
|
||||||
|
|
||||||
_signature1 = varargSignature(ofTypes(Type.NOTE), ofTypes(Type.INTEGER), ofTypes(Type.INTEGER))
|
_signature1 = varargSignature(ofTypes(Type.NOTE), ofTypes(Type.INTEGER), ofTypes(Type.INTEGER))
|
||||||
def _function1(env, n, m, vararg):
|
def _function1(env, n, m, vararg):
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
from smnp.library.model import Function
|
from smnp.library.model import Function
|
||||||
from smnp.library.signature import signature, ofType
|
from smnp.library.signature import signature
|
||||||
from smnp.type.model import Type
|
from smnp.type.model import Type
|
||||||
|
from smnp.type.signature.matcher.type import ofType
|
||||||
|
|
||||||
_signature = signature(ofType(Type.SOUND))
|
_signature = signature(ofType(Type.SOUND))
|
||||||
def _function(env, sound):
|
def _function(env, sound):
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
from smnp.audio.sound import Sound
|
from smnp.audio.sound import Sound
|
||||||
from smnp.library.model import Function
|
from smnp.library.model import Function
|
||||||
from smnp.library.signature import signature, ofType
|
from smnp.library.signature import signature
|
||||||
from smnp.type.model import Type
|
from smnp.type.model import Type
|
||||||
|
from smnp.type.signature.matcher.type import ofType
|
||||||
|
|
||||||
_signature = signature(ofType(Type.STRING))
|
_signature = signature(ofType(Type.STRING))
|
||||||
def _function(env, file):
|
def _function(env, file):
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
from smnp.library.model import Function
|
from smnp.library.model import Function
|
||||||
from smnp.library.signature import varargSignature, ofType
|
from smnp.library.signature import varargSignature
|
||||||
from smnp.type.model import Type
|
from smnp.type.model import Type
|
||||||
|
from smnp.type.signature.matcher.type import ofType
|
||||||
|
|
||||||
_signature = varargSignature(ofType(Type.STRING))
|
_signature = varargSignature(ofType(Type.STRING))
|
||||||
def _function(env, vararg):
|
def _function(env, vararg):
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
from smnp.library.model import Function
|
from smnp.library.model import Function
|
||||||
from smnp.library.signature import signature, ofTypes
|
from smnp.library.signature import signature
|
||||||
from smnp.module.synth.lib import player
|
from smnp.module.synth.lib import player
|
||||||
from smnp.type.model import Type
|
from smnp.type.model import Type
|
||||||
|
from smnp.type.signature.matcher.type import ofTypes
|
||||||
|
|
||||||
_signature = signature(ofTypes(Type.INTEGER))
|
_signature = signature(ofTypes(Type.INTEGER))
|
||||||
def _function(env, value):
|
def _function(env, value):
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
from smnp.library.model import CombinedFunction, Function
|
from smnp.library.model import CombinedFunction, Function
|
||||||
from smnp.library.signature import varargSignature, ofTypes, listOf
|
from smnp.library.signature import varargSignature
|
||||||
from smnp.module.synth.lib.player import playNotes
|
from smnp.module.synth.lib.player import playNotes
|
||||||
from smnp.type.model import Type
|
from smnp.type.model import Type
|
||||||
|
from smnp.type.signature.matcher.list import listOf
|
||||||
|
from smnp.type.signature.matcher.type import ofTypes
|
||||||
|
|
||||||
_signature1 = varargSignature(ofTypes(Type.NOTE, Type.INTEGER))
|
_signature1 = varargSignature(ofTypes(Type.NOTE, Type.INTEGER))
|
||||||
def _function1(env, vararg):
|
def _function1(env, vararg):
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
from smnp.error.function import IllegalArgumentException
|
from smnp.error.function import IllegalArgumentException
|
||||||
from smnp.library.model import Function
|
from smnp.library.model import Function
|
||||||
from smnp.library.signature import signature, ofTypes
|
from smnp.library.signature import signature
|
||||||
from smnp.type.model import Type
|
from smnp.type.model import Type
|
||||||
|
from smnp.type.signature.matcher.type import ofTypes
|
||||||
|
|
||||||
_signature = signature(ofTypes(Type.STRING))
|
_signature = signature(ofTypes(Type.STRING))
|
||||||
def _function(env, parameter):
|
def _function(env, parameter):
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
from smnp.library.model import Function
|
from smnp.library.model import Function
|
||||||
from smnp.library.signature import varargSignature, allTypes
|
from smnp.library.signature import varargSignature
|
||||||
|
from smnp.type.signature.matcher.type import allTypes
|
||||||
|
|
||||||
_signature = varargSignature(allTypes())
|
_signature = varargSignature(allTypes())
|
||||||
def _function(env, vararg):
|
def _function(env, vararg):
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
import sys
|
import sys
|
||||||
|
|
||||||
from smnp.library.model import Function
|
from smnp.library.model import Function
|
||||||
from smnp.library.signature import signature, ofTypes
|
from smnp.library.signature import signature
|
||||||
from smnp.type.model import Type
|
from smnp.type.model import Type
|
||||||
|
from smnp.type.signature.matcher.type import ofTypes
|
||||||
|
|
||||||
_signature = signature(ofTypes(Type.INTEGER))
|
_signature = signature(ofTypes(Type.INTEGER))
|
||||||
def _function(env, code):
|
def _function(env, code):
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
import time
|
import time
|
||||||
|
|
||||||
from smnp.library.model import Function
|
from smnp.library.model import Function
|
||||||
from smnp.library.signature import ofTypes, signature
|
from smnp.library.signature import signature
|
||||||
from smnp.type.model import Type
|
from smnp.type.model import Type
|
||||||
|
from smnp.type.signature.matcher.type import ofTypes
|
||||||
|
|
||||||
_signature = signature(ofTypes(Type.INTEGER))
|
_signature = signature(ofTypes(Type.INTEGER))
|
||||||
def _function(env, value):
|
def _function(env, value):
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
from smnp.library.model import Function
|
from smnp.library.model import Function
|
||||||
from smnp.library.signature import signature, allTypes
|
from smnp.library.signature import signature
|
||||||
from smnp.type.model import Type
|
from smnp.type.model import Type
|
||||||
|
from smnp.type.signature.matcher.type import allTypes
|
||||||
|
|
||||||
_signature = signature(allTypes())
|
_signature = signature(allTypes())
|
||||||
def _function(env, obj):
|
def _function(env, obj):
|
||||||
|
|||||||
@@ -2,8 +2,10 @@ import random as r
|
|||||||
|
|
||||||
from smnp.error.function import IllegalArgumentException
|
from smnp.error.function import IllegalArgumentException
|
||||||
from smnp.library.model import Function, CombinedFunction
|
from smnp.library.model import Function, CombinedFunction
|
||||||
from smnp.library.signature import varargSignature, listMatches, ofTypes
|
from smnp.library.signature import varargSignature
|
||||||
from smnp.type.model import Type
|
from smnp.type.model import Type
|
||||||
|
from smnp.type.signature.matcher.list import listMatches
|
||||||
|
from smnp.type.signature.matcher.type import ofTypes
|
||||||
|
|
||||||
|
|
||||||
def forType(t):
|
def forType(t):
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
from smnp.ast.node.none import NoneNode
|
from smnp.ast.node.none import NoneNode
|
||||||
from smnp.library.signature import ofType, signature
|
from smnp.library.signature import signature
|
||||||
from smnp.runtime.evaluator import Evaluator
|
from smnp.runtime.evaluator import Evaluator
|
||||||
from smnp.runtime.evaluators.function import argumentsNodeToMethodSignature, listSpecifier
|
from smnp.runtime.evaluators.function import argumentsNodeToMethodSignature, listSpecifier
|
||||||
from smnp.type.model import Type
|
from smnp.type.model import Type
|
||||||
|
from smnp.type.signature.matcher.type import ofType
|
||||||
|
|
||||||
|
|
||||||
class ExtendEvaluator(Evaluator):
|
class ExtendEvaluator(Evaluator):
|
||||||
|
|||||||
@@ -2,12 +2,15 @@ from smnp.ast.node.none import NoneNode
|
|||||||
from smnp.ast.node.ret import ReturnNode
|
from smnp.ast.node.ret import ReturnNode
|
||||||
from smnp.ast.node.variable import TypedVariableNode
|
from smnp.ast.node.variable import TypedVariableNode
|
||||||
from smnp.error.runtime import RuntimeException
|
from smnp.error.runtime import RuntimeException
|
||||||
from smnp.library.signature import signature, listOfMatchers, ofType, mapOfMatchers
|
from smnp.library.signature import signature
|
||||||
from smnp.runtime.evaluator import Evaluator, evaluate
|
from smnp.runtime.evaluator import Evaluator, evaluate
|
||||||
from smnp.runtime.evaluators.expression import expressionEvaluator
|
from smnp.runtime.evaluators.expression import expressionEvaluator
|
||||||
from smnp.runtime.evaluators.iterable import abstractIterableEvaluator
|
from smnp.runtime.evaluators.iterable import abstractIterableEvaluator
|
||||||
from smnp.runtime.tools import updatePos
|
from smnp.runtime.tools import updatePos
|
||||||
from smnp.type.model import Type
|
from smnp.type.model import Type
|
||||||
|
from smnp.type.signature.matcher.list import listOfMatchers
|
||||||
|
from smnp.type.signature.matcher.map import mapOfMatchers
|
||||||
|
from smnp.type.signature.matcher.type import ofType
|
||||||
|
|
||||||
|
|
||||||
class FunctionCallEvaluator(Evaluator):
|
class FunctionCallEvaluator(Evaluator):
|
||||||
|
|||||||
0
smnp/type/signature/__init__.py
Normal file
0
smnp/type/signature/__init__.py
Normal file
0
smnp/type/signature/matcher/__init__.py
Normal file
0
smnp/type/signature/matcher/__init__.py
Normal file
41
smnp/type/signature/matcher/list.py
Normal file
41
smnp/type/signature/matcher/list.py
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
from smnp.library.signature import signature
|
||||||
|
from smnp.type.model import Type
|
||||||
|
from smnp.type.signature.matcher.model import Matcher
|
||||||
|
|
||||||
|
|
||||||
|
def listOf(*types):
|
||||||
|
def check(value):
|
||||||
|
return len([item for item in value.value if not item.type in types]) == 0
|
||||||
|
|
||||||
|
return Matcher(Type.LIST, check, f"{Type.LIST.name.lower()}<{', '.join([t.name.lower() for t in types])}>")
|
||||||
|
|
||||||
|
|
||||||
|
def listOfMatchers(*matchers):
|
||||||
|
def check(value):
|
||||||
|
matched = 0
|
||||||
|
for item in value.value:
|
||||||
|
matched += 1 if any(matcher.match(item) for matcher in matchers) else 0
|
||||||
|
|
||||||
|
return matched == len(value.value)
|
||||||
|
|
||||||
|
return Matcher(Type.LIST, check, f"{Type.LIST.name.lower()}<{', '.join([m.string for m in matchers])}>")
|
||||||
|
|
||||||
|
|
||||||
|
def listMatches(*pattern):
|
||||||
|
def check(value):
|
||||||
|
return signature(*pattern).check(value.value)[0]
|
||||||
|
|
||||||
|
return Matcher(Type.LIST, check, f"({', '.join([str(m) for m in pattern])})")
|
||||||
|
|
||||||
|
|
||||||
|
def recursiveListMatcher(matcher):
|
||||||
|
if matcher.type == Type.LIST:
|
||||||
|
raise RuntimeError(f"Passed matcher will be handling non-list types, so it cannot have type set to {Type.LIST}")
|
||||||
|
|
||||||
|
def check(value):
|
||||||
|
if value.type != Type.LIST:
|
||||||
|
return matcher.match(value)
|
||||||
|
for item in value.value:
|
||||||
|
return check(item)
|
||||||
|
|
||||||
|
return Matcher(Type.LIST, check, f"[LISTS OF {str(matcher)}]")
|
||||||
14
smnp/type/signature/matcher/map.py
Normal file
14
smnp/type/signature/matcher/map.py
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
from smnp.type.model import Type
|
||||||
|
from smnp.type.signature.matcher.model import Matcher
|
||||||
|
|
||||||
|
|
||||||
|
def mapOfMatchers(keyMatchers, valueMatchers):
|
||||||
|
def check(map):
|
||||||
|
matched = 0
|
||||||
|
for key, value in map.value.items():
|
||||||
|
matched += 1 if any(matcher.match(key) for matcher in keyMatchers) \
|
||||||
|
and any(matcher.match(value) for matcher in valueMatchers) else 0
|
||||||
|
|
||||||
|
return matched == len(map.value)
|
||||||
|
|
||||||
|
return Matcher(Type.MAP, check, f"{Type.MAP.name.lower()}<{', '.join([m.string for m in keyMatchers])}><{', '.join([m.string for m in valueMatchers])}>")
|
||||||
29
smnp/type/signature/matcher/model.py
Normal file
29
smnp/type/signature/matcher/model.py
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
class Matcher:
|
||||||
|
def __init__(self, objectType, matcher, string):
|
||||||
|
self.type = objectType
|
||||||
|
self.matcher = matcher
|
||||||
|
self.string = string
|
||||||
|
|
||||||
|
def match(self, value):
|
||||||
|
if self.type is not None and self.type != value.type:
|
||||||
|
return False
|
||||||
|
return self.matcher(value)
|
||||||
|
|
||||||
|
def andWith(self, matcher):
|
||||||
|
if self.type != matcher.type:
|
||||||
|
raise RuntimeError("Support types of matches are not the same")
|
||||||
|
string = f"[{self.string} and {matcher.string}]"
|
||||||
|
return Matcher(self.type, lambda x: self.match(x) and matcher.match(x), string)
|
||||||
|
|
||||||
|
def orWith(self, matcher):
|
||||||
|
string = f"[{self.string} or {matcher.string}]"
|
||||||
|
return Matcher(None, lambda x: self.match(x) or matcher.match(x), string)
|
||||||
|
|
||||||
|
def __eq__(self, other):
|
||||||
|
return self.type == other.type and self.string == other.string
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return self.string
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return self.__str__()
|
||||||
20
smnp/type/signature/matcher/type.py
Normal file
20
smnp/type/signature/matcher/type.py
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
from smnp.type.model import Type
|
||||||
|
from smnp.type.signature.matcher.model import Matcher
|
||||||
|
|
||||||
|
|
||||||
|
def allTypes():
|
||||||
|
allowedTypes = [t for t in Type if t != Type.VOID]
|
||||||
|
return ofTypes(*allowedTypes)
|
||||||
|
|
||||||
|
|
||||||
|
def ofTypes(*types):
|
||||||
|
def check(value):
|
||||||
|
return value.type in types
|
||||||
|
return Matcher(None, check, f"<{', '.join([t.name.lower() for t in types])}>")
|
||||||
|
|
||||||
|
|
||||||
|
def ofType(type):
|
||||||
|
def check(value):
|
||||||
|
return value.type == type
|
||||||
|
|
||||||
|
return Matcher(None, check, type.name.lower())
|
||||||
Reference in New Issue
Block a user