package org.shedlang.compiler.shid.instructions; import org.shedlang.compiler.types.NamespaceName; import org.shedlang.compiler.types.SimpleConcreteType; import org.shedlang.compiler.types.SimpleStructType; import org.shedlang.compiler.types.UserDefinedEffect; import org.zwobble.precisely.Matcher; import java.math.BigInteger; import java.util.List; import static org.zwobble.precisely.Matchers.*; public class ShidMatchers { private ShidMatchers() { } public static Matcher isShidBlock( Matcher> instructions ) { return instanceOf( ShidBlock.class, has("instructions", x -> x.instructions(), instructions) ); } public static Matcher isShidCall( int positionalArgCount, List namedArgNames ) { return instanceOf( ShidCall.class, has("positionalArgCount", x -> x.positionalArgCount(), equalTo(positionalArgCount)), has("namedArgNames", x -> x.namedArgNames(), equalTo(namedArgNames)) ); } public static Matcher isShidDefineFunctionParam(String name) { return has("name", x -> x.name(), equalTo(name)); } public static Matcher isShidDiscard() { return instanceOf( ShidDiscard.class ); } public static Matcher isShidDuplicate() { return instanceOf(ShidDuplicate.class); } public static Matcher isShidEffectHandlerBreak(UserDefinedEffect effect) { return instanceOf( ShidEffectHandlerBreak.class, has("effect", x -> x.effect(), equalTo(effect)) ); } public static Matcher isShidFieldLoad( ShidFieldLoad.Receiver receiver, String fieldName ) { return instanceOf( ShidFieldLoad.class, has("receiver", x -> x.receiver(), equalTo(receiver)), has("fieldName", x -> x.fieldName(), equalTo(fieldName)) ); } public static Matcher isShidFieldLoad( SimpleStructType receiverType, String fieldName ) { return isShidFieldLoad( new ShidFieldLoad.Receiver.Struct(receiverType), fieldName ); } public static Matcher isShidIf( Matcher trueBody, Matcher falseBody ) { return instanceOf( ShidIf.class, has("trueBody", x -> x.trueBody(), trueBody), has("falseBody", x -> x.falseBody(), falseBody) ); } public static Matcher isShidIntAdd() { return instanceOf(ShidIntAdd.class); } public static Matcher isShidIntEquals() { return instanceOf(ShidIntEquals.class); } public static Matcher isShidIntGreaterThan() { return instanceOf(ShidIntGreaterThan.class); } public static Matcher isShidIntGreaterThanOrEqual() { return instanceOf(ShidIntGreaterThanOrEqual.class); } public static Matcher isShidIntLessThan() { return instanceOf(ShidIntLessThan.class); } public static Matcher isShidIntLessThanOrEqual() { return instanceOf(ShidIntLessThanOrEqual.class); } public static Matcher isShidIntMultiply() { return instanceOf(ShidIntMultiply.class); } public static Matcher isShidIntNotEqual() { return instanceOf(ShidIntNotEqual.class); } public static Matcher isShidIntSub() { return instanceOf(ShidIntSub.class); } public static Matcher isShidLocalLoad(String name) { return instanceOf( ShidLocalLoad.class, has("name", x -> x.name(), equalTo(name)) ); } public static Matcher isShidLocalStore(String name) { return instanceOf( ShidLocalStore.class, has("name", x -> x.name(), equalTo(name)) ); } public static Matcher isShidLoop( Matcher body ) { return instanceOf( ShidLoop.class, has("body", x -> x.body(), body) ); } public static Matcher isShidLoopBreak() { return instanceOf(ShidLoopBreak.class); } public static Matcher isShidLoopContinue() { return instanceOf(ShidLoopContinue.class); } public static Matcher isShidMethodLoad( SimpleConcreteType type, String methodName ) { return instanceOf( ShidMethodLoad.class, has("type", x -> x.type(), equalTo(type)), has("methodName", x -> x.methodName(), equalTo(methodName)) ); } public static Matcher isShidMethodStore( SimpleConcreteType type, String methodName ) { return instanceOf( ShidMethodStore.class, has("type", x -> x.type(), equalTo(type)), has("methodName", x -> x.methodName(), equalTo(methodName)) ); } public static Matcher isShidNamespaceFieldStore( NamespaceName namespaceName, String fieldName ) { return instanceOf( ShidNamespaceFieldStore.class, has("namespaceName", x -> x.namespaceName(), equalTo(namespaceName)), has("fieldName", x -> x.fieldName(), equalTo(fieldName)) ); } public static Matcher isShidNamespaceImport( NamespaceName namespaceName ) { return instanceOf( ShidNamespaceImport.class, has("namespaceName", x -> x.namespaceName(), equalTo(namespaceName)) ); } public static Matcher isShidPushBool(boolean value) { return instanceOf( ShidPushBool.class, has("value", x -> x.value(), equalTo(value)) ); } public static Matcher isShidPushInt(int value) { return instanceOf( ShidPushInt.class, has("value", x -> x.value(), equalTo(BigInteger.valueOf(value))) ); } public static Matcher isShidPushString(String value) { return instanceOf( ShidPushString.class, has("value", x -> x.value(), equalTo(value)) ); } public static Matcher isShidPushUnit() { return instanceOf( ShidPushUnit.class ); } public static Matcher isShidResume() { return instanceOf( ShidResume.class ); } public static Matcher isShidReturn() { return instanceOf( ShidReturn.class ); } public static Matcher isShidStringAdd() { return instanceOf(ShidStringAdd.class); } public static Matcher isShidSwitch( Matcher> cases ) { return instanceOf( ShidSwitch.class, has("cases", x -> x.cases(), cases) ); } public static Matcher isShidSwitchCase( SimpleStructType caseType, Matcher body ) { return allOf( has("caseType", x -> x.caseType(), equalTo(caseType)), has("body", x -> x.body(), body) ); } public static Matcher isShidTupleCreate(int elementCount) { return instanceOf( ShidTupleCreate.class, has("elementCount", x -> x.elementCount(), equalTo(elementCount)) ); } public static Matcher isShidTupleGet(int elementIndex) { return instanceOf( ShidTupleGet.class, has("elementIndex", x -> x.elementIndex(), equalTo(elementIndex)) ); } public static Matcher isShidVarLoad(String name) { return instanceOf( ShidVarLoad.class, has("name", x -> x.name(), equalTo(name)) ); } public static Matcher isShidVarStore(String name) { return instanceOf( ShidVarStore.class, has("name", x -> x.name(), equalTo(name)) ); } }