partway through getting a bunch of list stuff converted to Operators.

This commit is contained in:
Talia-12 2023-05-31 20:17:04 +10:00
parent e67c61a23e
commit 07cc518719
21 changed files with 255 additions and 64 deletions

View file

@ -26,8 +26,12 @@ public interface Arithmetic {
// Lists
HexPattern INDEX = HexPattern.fromAngles("deeed", HexDir.NORTH_WEST);
HexPattern SLICE = HexPattern.fromAngles("qaeaqwded", HexDir.NORTH_WEST);
HexPattern APPEND = HexPattern.fromAngles("edqde", HexDir.SOUTH_WEST);
HexPattern UNAPPEND = HexPattern.fromAngles("qaeaq", HexDir.NORTH_WEST);
HexPattern REV = HexPattern.fromAngles("qqqaede", HexDir.EAST);
HexPattern INDEX_OF = HexPattern.fromAngles("dedqde", HexDir.EAST);
HexPattern REMOVE = HexPattern.fromAngles("edqdewaqa", HexDir.SOUTH_WEST);
HexPattern REPLACE = HexPattern.fromAngles("wqaeaqw", HexDir.NORTH_WEST);
HexPattern CONS = HexPattern.fromAngles("ddewedd", HexDir.SOUTH_EAST);
HexPattern UNCONS = HexPattern.fromAngles("aaqwqaa", HexDir.SOUTH_WEST);
}

View file

@ -1,37 +0,0 @@
package at.petrak.hexcasting.api.casting.arithmetic.impls;
import at.petrak.hexcasting.api.casting.arithmetic.Arithmetic;
import at.petrak.hexcasting.api.casting.arithmetic.operator.Operator;
import at.petrak.hexcasting.api.casting.math.HexPattern;
import java.util.List;
public enum ListArithmetic implements Arithmetic {
INSTANCE;
public static final List<HexPattern> OPS = List.of(
INDEX,
SLICE,
ADD,
ABS,
REV,
INDEX_OF,
REMOVE,
REPLACE
);
@Override
public String arithName() {
return null;
}
@Override
public Iterable<HexPattern> opTypes() {
return null;
}
@Override
public Operator getOperator(HexPattern pattern) {
return null;
}
}

View file

@ -1,6 +1,6 @@
package at.petrak.hexcasting.api.casting.arithmetic.operator;
import at.petrak.hexcasting.api.casting.arithmetic.IotaMultiPredicate;
import at.petrak.hexcasting.api.casting.arithmetic.predicates.IotaMultiPredicate;
import at.petrak.hexcasting.api.casting.iota.Iota;
import at.petrak.hexcasting.api.casting.iota.IotaType;

View file

@ -1,7 +1,7 @@
package at.petrak.hexcasting.api.casting.arithmetic.operator;
import at.petrak.hexcasting.api.casting.arithmetic.IotaMultiPredicate;
import at.petrak.hexcasting.api.casting.arithmetic.predicates.IotaMultiPredicate;
import at.petrak.hexcasting.api.casting.iota.Iota;
import java.util.List;

View file

@ -1,7 +1,7 @@
package at.petrak.hexcasting.api.casting.arithmetic.operator;
import at.petrak.hexcasting.api.casting.arithmetic.IotaMultiPredicate;
import at.petrak.hexcasting.api.casting.arithmetic.predicates.IotaMultiPredicate;
import at.petrak.hexcasting.api.casting.iota.Iota;
import java.util.List;

View file

@ -1,4 +1,4 @@
package at.petrak.hexcasting.api.casting.arithmetic;
package at.petrak.hexcasting.api.casting.arithmetic.predicates;
import at.petrak.hexcasting.api.casting.iota.Iota;
@ -9,19 +9,29 @@ public interface IotaMultiPredicate {
static IotaMultiPredicate all(IotaPredicate child) {
return new All(child);
}
static IotaMultiPredicate bofa(IotaPredicate first, IotaPredicate second) {
return new Any(first, second);
static IotaMultiPredicate pair(IotaPredicate first, IotaPredicate second) {
return new Pair(first, second);
}
static IotaMultiPredicate triple(IotaPredicate first, IotaPredicate second, IotaPredicate third) {
return new Triple(first, second, third);
}
static IotaMultiPredicate any(IotaPredicate needs, IotaPredicate fallback) {
return new Any(needs, fallback);
}
record Bofa(IotaPredicate first, IotaPredicate second) implements IotaMultiPredicate {
record Pair(IotaPredicate first, IotaPredicate second) implements IotaMultiPredicate {
@Override
public boolean test(Iterable<Iota> iotas) {
var it = iotas.iterator();
return it.hasNext() && first.test(it.next()) && it.hasNext() && second.test(it.next()) && !it.hasNext();
}
}
record Triple(IotaPredicate first, IotaPredicate second, IotaPredicate third) implements IotaMultiPredicate {
@Override
public boolean test(Iterable<Iota> iotas) {
var it = iotas.iterator();
return it.hasNext() && first.test(it.next()) && it.hasNext() && second.test(it.next()) && it.hasNext() && third.test(it.next()) && !it.hasNext();
}
}
record Any(IotaPredicate needs, IotaPredicate fallback) implements IotaMultiPredicate {
@Override
public boolean test(Iterable<Iota> iotas) {

View file

@ -1,4 +1,4 @@
package at.petrak.hexcasting.api.casting.arithmetic;
package at.petrak.hexcasting.api.casting.arithmetic.predicates;
import at.petrak.hexcasting.api.casting.iota.Iota;
import at.petrak.hexcasting.api.casting.iota.IotaType;
@ -28,4 +28,6 @@ public interface IotaPredicate {
return iota.getType().equals(this.type);
}
}
IotaPredicate TRUE = iota -> true;
}

View file

@ -1,8 +1,8 @@
package at.petrak.hexcasting.api.casting.arithmetic.impls;
package at.petrak.hexcasting.common.casting.arithmetic;
import at.petrak.hexcasting.api.casting.arithmetic.Arithmetic;
import at.petrak.hexcasting.api.casting.arithmetic.IotaMultiPredicate;
import at.petrak.hexcasting.api.casting.arithmetic.IotaPredicate;
import at.petrak.hexcasting.api.casting.arithmetic.predicates.IotaMultiPredicate;
import at.petrak.hexcasting.api.casting.arithmetic.predicates.IotaPredicate;
import at.petrak.hexcasting.api.casting.arithmetic.operator.Operator;
import at.petrak.hexcasting.api.casting.arithmetic.operator.OperatorBinary;
import at.petrak.hexcasting.api.casting.arithmetic.operator.OperatorUnary;

View file

@ -0,0 +1,75 @@
package at.petrak.hexcasting.common.casting.arithmetic;
import at.petrak.hexcasting.api.casting.arithmetic.Arithmetic;
import at.petrak.hexcasting.api.casting.arithmetic.predicates.IotaMultiPredicate;
import at.petrak.hexcasting.api.casting.arithmetic.predicates.IotaPredicate;
import at.petrak.hexcasting.api.casting.arithmetic.operator.Operator;
import at.petrak.hexcasting.api.casting.iota.DoubleIota;
import at.petrak.hexcasting.api.casting.iota.ListIota;
import at.petrak.hexcasting.common.casting.arithmetic.operator.list.*;
import at.petrak.hexcasting.api.casting.arithmetic.operator.OperatorUnary;
import at.petrak.hexcasting.api.casting.math.HexPattern;
import java.util.List;
import static at.petrak.hexcasting.api.casting.arithmetic.operator.Operator.downcast;
import static at.petrak.hexcasting.common.lib.hex.HexIotaTypes.*;
public enum ListArithmetic implements Arithmetic {
INSTANCE;
public static final List<HexPattern> OPS = List.of(
INDEX,
SLICE,
APPEND,
UNAPPEND,
ADD,
ABS,
REV,
INDEX_OF,
REMOVE,
REPLACE,
CONS,
UNCONS
);
@Override
public String arithName() {
return "list_ops";
}
@Override
public Iterable<HexPattern> opTypes() {
return OPS;
}
@Override
public Operator getOperator(HexPattern pattern) {
if (pattern.equals(INDEX)) {
return OperatorIndex.INSTANCE;
} else if (pattern.equals(SLICE)) {
return OperatorSlice.INSTANCE;
} else if (pattern.equals(APPEND)) {
return OperatorAppend.INSTANCE;
} else if (pattern.equals(UNAPPEND)) {
return OperatorUnappend.INSTANCE;
} else if (pattern.equals(ADD)) {
return OperatorConcat.INSTANCE;
} else if (pattern.equals(ABS)) {
return new OperatorUnary(IotaMultiPredicate.any(IotaPredicate.ofType(LIST), IotaPredicate.ofType(LIST)), iota -> new DoubleIota(downcast(iota, LIST).getList().size()));
} else if (pattern.equals(REV)) {
return new OperatorUnary(IotaMultiPredicate.any(IotaPredicate.ofType(LIST), IotaPredicate.ofType(LIST)), iota -> new ListIota(downcast(iota, LIST).getList()));
} else if (pattern.equals(INDEX_OF)) {
return new OperatorUnary(IotaMultiPredicate.any(IotaPredicate.ofType(LIST), IotaPredicate.ofType(LIST)), iota -> iota);
} else if (pattern.equals(REMOVE)) {
return new OperatorUnary(IotaMultiPredicate.any(IotaPredicate.ofType(LIST), IotaPredicate.ofType(LIST)), iota -> iota);
} else if (pattern.equals(REPLACE)) {
return new OperatorUnary(IotaMultiPredicate.any(IotaPredicate.ofType(LIST), IotaPredicate.ofType(LIST)), iota -> iota);
} else if (pattern.equals(CONS)) {
return new OperatorUnary(IotaMultiPredicate.any(IotaPredicate.ofType(LIST), IotaPredicate.ofType(LIST)), iota -> iota);
} else if (pattern.equals(UNCONS)) {
return new OperatorUnary(IotaMultiPredicate.any(IotaPredicate.ofType(LIST), IotaPredicate.ofType(LIST)), iota -> iota);
}
return null;
}
}

View file

@ -1,12 +1,15 @@
package at.petrak.hexcasting.api.casting.arithmetic.impls;
package at.petrak.hexcasting.common.casting.arithmetic;
import at.petrak.hexcasting.api.casting.arithmetic.Arithmetic;
import at.petrak.hexcasting.api.casting.arithmetic.IotaMultiPredicate;
import at.petrak.hexcasting.api.casting.arithmetic.IotaPredicate;
import at.petrak.hexcasting.api.casting.arithmetic.predicates.IotaMultiPredicate;
import at.petrak.hexcasting.api.casting.arithmetic.predicates.IotaPredicate;
import at.petrak.hexcasting.api.casting.arithmetic.operator.*;
import at.petrak.hexcasting.api.casting.iota.DoubleIota;
import at.petrak.hexcasting.api.casting.iota.Vec3Iota;
import at.petrak.hexcasting.api.casting.math.HexPattern;
import at.petrak.hexcasting.common.casting.arithmetic.operator.vec.OperatorPack;
import at.petrak.hexcasting.common.casting.arithmetic.operator.vec.OperatorUnpack;
import at.petrak.hexcasting.common.casting.arithmetic.operator.vec.OperatorVec3Delegating;
import net.minecraft.world.phys.Vec3;
import java.util.ArrayList;

View file

@ -0,0 +1,30 @@
package at.petrak.hexcasting.common.casting.arithmetic.operator
import at.petrak.hexcasting.api.casting.SpellList
import at.petrak.hexcasting.api.casting.iota.DoubleIota
import at.petrak.hexcasting.api.casting.iota.Iota
import at.petrak.hexcasting.api.casting.iota.ListIota
import at.petrak.hexcasting.api.casting.mishaps.MishapInvalidIota
import kotlin.math.abs
import kotlin.math.roundToInt
fun Iterator<IndexedValue<Iota>>.nextList(argc: Int = 0): SpellList {
val (idx, x) = this.next()
if (x is ListIota) {
return x.list
} else {
throw MishapInvalidIota.ofType(x, if (argc == 0) idx else argc - (idx + 1), "list")
}
}
fun Iterator<IndexedValue<Iota>>.nextPositiveIntUnderInclusive(max: Int, argc: Int = 0): Int {
val (idx, x) = this.next()
if (x is DoubleIota) {
val double = x.double
val rounded = double.roundToInt()
if (abs(double - rounded) <= DoubleIota.TOLERANCE && rounded in 0..max) {
return rounded
}
}
throw MishapInvalidIota.of(x, if (argc == 0) idx else argc - (idx + 1), "int.positive.less.equal", max)
}

View file

@ -0,0 +1,18 @@
package at.petrak.hexcasting.common.casting.arithmetic.operator.list
import at.petrak.hexcasting.api.casting.arithmetic.operator.Operator
import at.petrak.hexcasting.api.casting.arithmetic.predicates.IotaMultiPredicate
import at.petrak.hexcasting.api.casting.arithmetic.predicates.IotaPredicate
import at.petrak.hexcasting.api.casting.asActionResult
import at.petrak.hexcasting.api.casting.iota.Iota
import at.petrak.hexcasting.common.casting.arithmetic.operator.nextList
import at.petrak.hexcasting.common.lib.hex.HexIotaTypes.*
object OperatorAppend : Operator(2, IotaMultiPredicate.pair(IotaPredicate.ofType(LIST), IotaPredicate.TRUE)) {
override fun apply(iotas: Iterable<Iota>): Iterable<Iota> {
val it = iotas.iterator().withIndex()
val list = it.nextList(arity).toMutableList()
list.add(it.next().value)
return list.asActionResult
}
}

View file

@ -0,0 +1,19 @@
package at.petrak.hexcasting.common.casting.arithmetic.operator.list
import at.petrak.hexcasting.api.casting.arithmetic.operator.Operator
import at.petrak.hexcasting.api.casting.arithmetic.predicates.IotaMultiPredicate
import at.petrak.hexcasting.api.casting.arithmetic.predicates.IotaPredicate
import at.petrak.hexcasting.api.casting.asActionResult
import at.petrak.hexcasting.api.casting.iota.Iota
import at.petrak.hexcasting.common.casting.arithmetic.operator.nextList
import at.petrak.hexcasting.common.lib.hex.HexIotaTypes.*
object OperatorConcat : Operator(2, IotaMultiPredicate.all(IotaPredicate.ofType(LIST))) {
override fun apply(iotas: Iterable<Iota>): Iterable<Iota> {
val it = iotas.iterator().withIndex()
val lhs = it.nextList(arity).toMutableList()
val rhs = it.nextList(arity)
lhs.addAll(rhs)
return lhs.asActionResult
}
}

View file

@ -0,0 +1,19 @@
package at.petrak.hexcasting.common.casting.arithmetic.operator.list
import at.petrak.hexcasting.api.casting.arithmetic.predicates.IotaMultiPredicate
import at.petrak.hexcasting.api.casting.arithmetic.predicates.IotaPredicate
import at.petrak.hexcasting.api.casting.arithmetic.operator.Operator
import at.petrak.hexcasting.api.casting.iota.Iota
import at.petrak.hexcasting.api.casting.iota.NullIota
import at.petrak.hexcasting.common.lib.hex.HexIotaTypes.*
import kotlin.math.roundToInt
object OperatorIndex : Operator(2, IotaMultiPredicate.pair(IotaPredicate.ofType(LIST), IotaPredicate.ofType(DOUBLE))) {
override fun apply(iotas: Iterable<Iota>): Iterable<Iota> {
val it = iotas.iterator()
val list = downcast(it.next(), LIST).list.toMutableList()
val index = downcast(it.next(), DOUBLE).double
val x = list.getOrElse(index.roundToInt()) { NullIota() }
return listOf(x)
}
}

View file

@ -0,0 +1,25 @@
package at.petrak.hexcasting.common.casting.arithmetic.operator.list
import at.petrak.hexcasting.api.casting.arithmetic.predicates.IotaMultiPredicate
import at.petrak.hexcasting.api.casting.arithmetic.predicates.IotaPredicate
import at.petrak.hexcasting.api.casting.arithmetic.operator.Operator
import at.petrak.hexcasting.api.casting.asActionResult
import at.petrak.hexcasting.api.casting.iota.Iota
import at.petrak.hexcasting.common.casting.arithmetic.operator.nextList
import at.petrak.hexcasting.common.casting.arithmetic.operator.nextPositiveIntUnderInclusive
import at.petrak.hexcasting.common.lib.hex.HexIotaTypes.*
import kotlin.math.max
import kotlin.math.min
object OperatorSlice : Operator(3, IotaMultiPredicate.triple(IotaPredicate.ofType(LIST), IotaPredicate.ofType(DOUBLE), IotaPredicate.ofType(DOUBLE))) {
override fun apply(iotas: Iterable<Iota>): Iterable<Iota> {
val it = iotas.iterator().withIndex()
val list = it.nextList(arity).toList()
val index0 = it.nextPositiveIntUnderInclusive(list.size, arity)
val index1 = it.nextPositiveIntUnderInclusive(list.size, arity)
if (index0 == index1)
return emptyList<Iota>().asActionResult
return list.subList(min(index0, index1), max(index0, index1)).asActionResult
}
}

View file

@ -0,0 +1,19 @@
package at.petrak.hexcasting.common.casting.arithmetic.operator.list
import at.petrak.hexcasting.api.casting.arithmetic.operator.Operator
import at.petrak.hexcasting.api.casting.arithmetic.predicates.IotaMultiPredicate
import at.petrak.hexcasting.api.casting.arithmetic.predicates.IotaPredicate
import at.petrak.hexcasting.api.casting.iota.Iota
import at.petrak.hexcasting.api.casting.iota.ListIota
import at.petrak.hexcasting.api.casting.iota.NullIota
import at.petrak.hexcasting.common.casting.arithmetic.operator.nextList
import at.petrak.hexcasting.common.lib.hex.HexIotaTypes.*
object OperatorUnappend : Operator(1, IotaMultiPredicate.all(IotaPredicate.ofType(LIST))) {
override fun apply(iotas: Iterable<Iota>): Iterable<Iota> {
val it = iotas.iterator().withIndex()
val list = it.nextList(arity).toMutableList()
val last = list.removeLastOrNull() ?: NullIota()
return listOf(ListIota(list), last)
}
}

View file

@ -1,8 +1,9 @@
package at.petrak.hexcasting.api.casting.arithmetic.operator;
package at.petrak.hexcasting.common.casting.arithmetic.operator.vec;
import at.petrak.hexcasting.api.casting.arithmetic.IotaMultiPredicate;
import at.petrak.hexcasting.api.casting.arithmetic.IotaPredicate;
import at.petrak.hexcasting.api.casting.arithmetic.predicates.IotaMultiPredicate;
import at.petrak.hexcasting.api.casting.arithmetic.predicates.IotaPredicate;
import at.petrak.hexcasting.api.casting.arithmetic.operator.Operator;
import at.petrak.hexcasting.api.casting.iota.Iota;
import at.petrak.hexcasting.api.casting.iota.Vec3Iota;
import at.petrak.hexcasting.common.lib.hex.HexIotaTypes;

View file

@ -1,8 +1,9 @@
package at.petrak.hexcasting.api.casting.arithmetic.operator;
package at.petrak.hexcasting.common.casting.arithmetic.operator.vec;
import at.petrak.hexcasting.api.casting.arithmetic.IotaMultiPredicate;
import at.petrak.hexcasting.api.casting.arithmetic.IotaPredicate;
import at.petrak.hexcasting.api.casting.arithmetic.predicates.IotaMultiPredicate;
import at.petrak.hexcasting.api.casting.arithmetic.predicates.IotaPredicate;
import at.petrak.hexcasting.api.casting.arithmetic.operator.Operator;
import at.petrak.hexcasting.api.casting.iota.DoubleIota;
import at.petrak.hexcasting.api.casting.iota.Iota;
import at.petrak.hexcasting.common.lib.hex.HexIotaTypes;

View file

@ -1,10 +1,11 @@
package at.petrak.hexcasting.api.casting.arithmetic.operator;
package at.petrak.hexcasting.common.casting.arithmetic.operator.vec;
import at.petrak.hexcasting.api.casting.arithmetic.IotaMultiPredicate;
import at.petrak.hexcasting.api.casting.arithmetic.IotaPredicate;
import at.petrak.hexcasting.api.casting.arithmetic.predicates.IotaMultiPredicate;
import at.petrak.hexcasting.api.casting.arithmetic.predicates.IotaPredicate;
import at.petrak.hexcasting.api.casting.arithmetic.IterPair;
import at.petrak.hexcasting.api.casting.arithmetic.TripleIterable;
import at.petrak.hexcasting.api.casting.arithmetic.impls.DoubleArithmetic;
import at.petrak.hexcasting.api.casting.arithmetic.operator.Operator;
import at.petrak.hexcasting.common.casting.arithmetic.DoubleArithmetic;
import at.petrak.hexcasting.api.casting.iota.DoubleIota;
import at.petrak.hexcasting.api.casting.iota.Iota;
import at.petrak.hexcasting.api.casting.iota.Vec3Iota;

View file

@ -1,11 +1,12 @@
package at.petrak.hexcasting.common.lib.hex;
import at.petrak.hexcasting.api.casting.arithmetic.engine.ArithmeticEngine;
import at.petrak.hexcasting.api.casting.arithmetic.impls.DoubleArithmetic;
import at.petrak.hexcasting.api.casting.arithmetic.impls.Vec3Arithmetic;
import at.petrak.hexcasting.common.casting.arithmetic.DoubleArithmetic;
import at.petrak.hexcasting.common.casting.arithmetic.ListArithmetic;
import at.petrak.hexcasting.common.casting.arithmetic.Vec3Arithmetic;
import java.util.List;
public class HexArithmetics {
public static ArithmeticEngine ENGINE = new ArithmeticEngine(List.of(DoubleArithmetic.INSTANCE, Vec3Arithmetic.INSTANCE));
public static ArithmeticEngine ENGINE = new ArithmeticEngine(List.of(DoubleArithmetic.INSTANCE, Vec3Arithmetic.INSTANCE, ListArithmetic.INSTANCE));
}