X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.scl.commands%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fcommands%2Finternal%2Fchecker%2FChecker.java;fp=bundles%2Forg.simantics.scl.commands%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fcommands%2Finternal%2Fchecker%2FChecker.java;h=7003431760b40a0bbe6b074b550246c9686665a5;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.scl.commands/src/org/simantics/scl/commands/internal/checker/Checker.java b/bundles/org.simantics.scl.commands/src/org/simantics/scl/commands/internal/checker/Checker.java new file mode 100644 index 000000000..700343176 --- /dev/null +++ b/bundles/org.simantics.scl.commands/src/org/simantics/scl/commands/internal/checker/Checker.java @@ -0,0 +1,60 @@ +package org.simantics.scl.commands.internal.checker; + +import java.util.Map; + +import org.simantics.scl.compiler.elaboration.modules.SCLValue; +import org.simantics.scl.compiler.top.ValueNotFound; +import org.simantics.scl.compiler.types.Type; +import org.simantics.scl.compiler.types.Types; +import org.simantics.scl.compiler.types.exceptions.MatchException; +import org.simantics.scl.compiler.types.util.MultiFunction; +import org.simantics.scl.osgi.SCLOsgi; + +/** + * Checks given parameters + * + * @author Hannu Niemistö + */ +public class Checker { + Object check; + CheckItem item; + + private Checker(Object check, CheckItem item) { + this.check = check; + this.item = item; + } + + public boolean check(Object[] parameters) { + return item.check(check, parameters, 0); + } + + private static CheckItem checkItemForType(Type type) throws MatchException { + if(type == Types.BOOLEAN) + return BooleanCheckItem.INSTANCE; + if(Types.isFunction(type)) { + MultiFunction mfun = Types.matchFunction(type, 1); + return new FunctionCheckItem(mfun.parameterTypes[0], checkItemForType(mfun.returnType)); + } + else if(Types.isApply(Types.MAYBE, 1, type)) { + Type componentType = Types.matchApply(Types.MAYBE, type); + return new MaybeCheckItem(checkItemForType(componentType)); + } + else + throw new MatchException(); + } + + public static Checker create(String name) { + try { + SCLValue checkRef = SCLOsgi.MODULE_REPOSITORY.getValueRef(name); + Object check = SCLOsgi.MODULE_REPOSITORY.getValue(name); + return new Checker(check, checkItemForType(checkRef.getType())); + } catch(ValueNotFound e) { + // If we don't find a check, it always succeeds + return new Checker(Boolean.TRUE, BooleanCheckItem.INSTANCE); + } catch (MatchException e) { + // Should not happens + e.printStackTrace(); + return new Checker(Boolean.FALSE, BooleanCheckItem.INSTANCE); + } + } +}