From 4f3fdbb9fd5a984e246d5398ce10ef4958727a10 Mon Sep 17 00:00:00 2001 From: JFronny Date: Tue, 7 Jun 2022 12:19:58 +0200 Subject: [PATCH] [muscript] Rename optic to dynamic to better represent its purpose in the context of muscript --- .../jfronny/muscript/ExpressionParameter.java | 8 +-- .../gitlab/jfronny/muscript/StandardLib.java | 22 +++--- .../jfronny/muscript/compiler/Expr.java | 70 +++++++++---------- .../jfronny/muscript/compiler/Parser.java | 10 +-- .../jfronny/muscript/compiler/Type.java | 2 +- .../jfronny/muscript/compiler/expr/Call.java | 15 ++-- .../muscript/compiler/expr/Compare.java | 4 +- .../muscript/compiler/expr/Concatenate.java | 4 +- .../muscript/compiler/expr/Conditional.java | 20 +++--- .../jfronny/muscript/compiler/expr/Equal.java | 6 +- .../jfronny/muscript/compiler/expr/Get.java | 18 ++--- .../jfronny/muscript/compiler/expr/Group.java | 8 +-- .../muscript/compiler/expr/Invert.java | 4 +- .../muscript/compiler/expr/LogicBiExpr.java | 4 +- .../muscript/compiler/expr/MathBiExpr.java | 4 +- .../jfronny/muscript/compiler/expr/Not.java | 4 +- .../muscript/compiler/expr/Variable.java | 8 +-- .../muscript/debug/ObjectGraphPrinter.java | 4 +- .../jfronny/muscript/dynamic/DBool.java | 4 ++ .../jfronny/muscript/dynamic/DCallable.java | 13 ++++ .../DContainer.java} | 4 +- .../jfronny/muscript/dynamic/DEnum.java | 52 ++++++++++++++ .../OFinal.java => dynamic/DFinal.java} | 34 ++++----- .../jfronny/muscript/dynamic/DList.java | 12 ++++ .../jfronny/muscript/dynamic/DNull.java | 8 +++ .../jfronny/muscript/dynamic/DNumber.java | 4 ++ .../OObject.java => dynamic/DObject.java} | 6 +- .../jfronny/muscript/dynamic/DString.java | 4 ++ .../jfronny/muscript/dynamic/Dynamic.java | 37 ++++++++++ .../gitlab/jfronny/muscript/optic/OAny.java | 37 ---------- .../gitlab/jfronny/muscript/optic/OBool.java | 4 -- .../jfronny/muscript/optic/OCallable.java | 13 ---- .../gitlab/jfronny/muscript/optic/OEnum.java | 52 -------------- .../gitlab/jfronny/muscript/optic/OList.java | 12 ---- .../gitlab/jfronny/muscript/optic/ONull.java | 8 --- .../jfronny/muscript/optic/ONumber.java | 4 -- .../jfronny/muscript/optic/OString.java | 4 -- .../jfronny/muscript/test/MuTestUtil.java | 6 +- 38 files changed, 266 insertions(+), 267 deletions(-) create mode 100644 muscript/src/main/java/io/gitlab/jfronny/muscript/dynamic/DBool.java create mode 100644 muscript/src/main/java/io/gitlab/jfronny/muscript/dynamic/DCallable.java rename muscript/src/main/java/io/gitlab/jfronny/muscript/{optic/OContainer.java => dynamic/DContainer.java} (78%) create mode 100644 muscript/src/main/java/io/gitlab/jfronny/muscript/dynamic/DEnum.java rename muscript/src/main/java/io/gitlab/jfronny/muscript/{optic/OFinal.java => dynamic/DFinal.java} (63%) create mode 100644 muscript/src/main/java/io/gitlab/jfronny/muscript/dynamic/DList.java create mode 100644 muscript/src/main/java/io/gitlab/jfronny/muscript/dynamic/DNull.java create mode 100644 muscript/src/main/java/io/gitlab/jfronny/muscript/dynamic/DNumber.java rename muscript/src/main/java/io/gitlab/jfronny/muscript/{optic/OObject.java => dynamic/DObject.java} (50%) create mode 100644 muscript/src/main/java/io/gitlab/jfronny/muscript/dynamic/DString.java create mode 100644 muscript/src/main/java/io/gitlab/jfronny/muscript/dynamic/Dynamic.java delete mode 100644 muscript/src/main/java/io/gitlab/jfronny/muscript/optic/OAny.java delete mode 100644 muscript/src/main/java/io/gitlab/jfronny/muscript/optic/OBool.java delete mode 100644 muscript/src/main/java/io/gitlab/jfronny/muscript/optic/OCallable.java delete mode 100644 muscript/src/main/java/io/gitlab/jfronny/muscript/optic/OEnum.java delete mode 100644 muscript/src/main/java/io/gitlab/jfronny/muscript/optic/OList.java delete mode 100644 muscript/src/main/java/io/gitlab/jfronny/muscript/optic/ONull.java delete mode 100644 muscript/src/main/java/io/gitlab/jfronny/muscript/optic/ONumber.java delete mode 100644 muscript/src/main/java/io/gitlab/jfronny/muscript/optic/OString.java diff --git a/muscript/src/main/java/io/gitlab/jfronny/muscript/ExpressionParameter.java b/muscript/src/main/java/io/gitlab/jfronny/muscript/ExpressionParameter.java index 305fc58..67e5853 100644 --- a/muscript/src/main/java/io/gitlab/jfronny/muscript/ExpressionParameter.java +++ b/muscript/src/main/java/io/gitlab/jfronny/muscript/ExpressionParameter.java @@ -1,20 +1,20 @@ package io.gitlab.jfronny.muscript; -import io.gitlab.jfronny.muscript.optic.*; +import io.gitlab.jfronny.muscript.dynamic.*; import java.util.*; -public record ExpressionParameter(Map> value) implements OObject { +public record ExpressionParameter(Map> value) implements DObject { public ExpressionParameter() { this(new HashMap<>()); } @Override - public Map> getValue() { + public Map> getValue() { return value; } - public ExpressionParameter set(String key, OAny value) { + public ExpressionParameter set(String key, Dynamic value) { this.value.put(key, value); return this; } diff --git a/muscript/src/main/java/io/gitlab/jfronny/muscript/StandardLib.java b/muscript/src/main/java/io/gitlab/jfronny/muscript/StandardLib.java index c048cda..aa6db11 100644 --- a/muscript/src/main/java/io/gitlab/jfronny/muscript/StandardLib.java +++ b/muscript/src/main/java/io/gitlab/jfronny/muscript/StandardLib.java @@ -1,11 +1,11 @@ package io.gitlab.jfronny.muscript; -import io.gitlab.jfronny.muscript.optic.*; +import io.gitlab.jfronny.muscript.dynamic.*; import java.text.*; import java.util.*; -import static io.gitlab.jfronny.muscript.optic.OFinal.*; +import static io.gitlab.jfronny.muscript.dynamic.DFinal.*; public class StandardLib { private static final Random rnd = new Random(); @@ -32,7 +32,7 @@ public class StandardLib { } // Numbers - public static OAny round(OList args) { + public static Dynamic round(DList args) { if (args.size() == 1) { return of(Math.round(args.get(0).asNumber().getValue())); } @@ -45,22 +45,22 @@ public class StandardLib { } } - public static OAny floor(OList args) { + public static Dynamic floor(DList args) { if (args.size() != 1) throw new IllegalArgumentException("Invalid number of arguments for floor: expected 1 but got " + args.size()); return of(Math.floor(args.get(0).asNumber().getValue())); } - public static OAny ceil(OList args) { + public static Dynamic ceil(DList args) { if (args.size() != 1) throw new IllegalArgumentException("Invalid number of arguments for ceil: expected 1 but got " + args.size()); return of(Math.ceil(args.get(0).asNumber().getValue())); } - public static OAny abs(OList args) { + public static Dynamic abs(DList args) { if (args.size() != 1) throw new IllegalArgumentException("Invalid number of arguments for abs: expected 1 but got " + args.size()); return of(Math.abs(args.get(0).asNumber().getValue())); } - public static OAny random(OList args) { + public static Dynamic random(DList args) { if (args.size() == 0) return of(rnd.nextDouble()); else if (args.size() == 2) { double min = args.get(0).asNumber().getValue(); @@ -72,22 +72,22 @@ public class StandardLib { } // Strings - public static OAny toUpper(OList args) { + public static Dynamic toUpper(DList args) { if (args.size() != 1) throw new IllegalArgumentException("Invalid number of arguments for toUpper: expected 1 but got " + args.size()); return of(args.get(0).asString().getValue().toUpperCase()); } - public static OAny toLower(OList args) { + public static Dynamic toLower(DList args) { if (args.size() != 1) throw new IllegalArgumentException("Invalid number of arguments for toLower: expected 1 but got " + args.size()); return of(args.get(0).asString().getValue().toLowerCase()); } - public static OAny contains(OList args) { + public static Dynamic contains(DList args) { if (args.size() != 2) throw new IllegalArgumentException("Invalid number of arguments for contains: expected 2 but got " + args.size()); return of(args.get(0).asString().getValue().contains(args.get(1).asString().getValue())); } - public static OAny replace(OList args) { + public static Dynamic replace(DList args) { if (args.size() != 3) throw new IllegalArgumentException("Invalid number of arguments for replace: expected 3 but got " + args.size()); return of(args.get(0).asString().getValue().replace(args.get(1).asString().getValue(), args.get(2).asString().getValue())); } diff --git a/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/Expr.java b/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/Expr.java index 8faf6e4..5bf5ed8 100644 --- a/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/Expr.java +++ b/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/Expr.java @@ -1,13 +1,13 @@ package io.gitlab.jfronny.muscript.compiler; -import io.gitlab.jfronny.muscript.optic.*; +import io.gitlab.jfronny.muscript.dynamic.*; import io.gitlab.jfronny.commons.StringFormatter; public abstract class Expr { public abstract Type getResultType(); - public abstract T get(OAny branch, OAny dataRoot); + public abstract T get(Dynamic branch, Dynamic dataRoot); - public T get(OAny branch) { + public T get(Dynamic branch) { return get(branch, branch); } @@ -20,7 +20,7 @@ public abstract class Expr { if (this instanceof StringExpr e) return e; return new StringExpr() { @Override - public String get(OAny branch, OAny dataRoot) { + public String get(Dynamic branch, Dynamic dataRoot) { return StringFormatter.toString(Expr.this.get(branch, dataRoot)); } }; @@ -31,7 +31,7 @@ public abstract class Expr { throw new IllegalArgumentException("Expected number but is " + getResultType()); } - public abstract ObjectExpr asObjectExpr(); + public abstract DynamicExpr asDynamicExpr(); public boolean isNull() { return this instanceof NullExpr; @@ -44,11 +44,11 @@ public abstract class Expr { } @Override - public ObjectExpr asObjectExpr() { - return new ObjectExpr() { + public DynamicExpr asDynamicExpr() { + return new DynamicExpr() { @Override - public OAny get(OAny branch, OAny dataRoot) { - return OFinal.of(BoolExpr.this.get(branch, dataRoot)); + public Dynamic get(Dynamic branch, Dynamic dataRoot) { + return DFinal.of(BoolExpr.this.get(branch, dataRoot)); } }; } @@ -61,11 +61,11 @@ public abstract class Expr { } @Override - public ObjectExpr asObjectExpr() { - return new ObjectExpr() { + public DynamicExpr asDynamicExpr() { + return new DynamicExpr() { @Override - public OAny get(OAny branch, OAny dataRoot) { - return OFinal.of(StringExpr.this.get(branch, dataRoot)); + public Dynamic get(Dynamic branch, Dynamic dataRoot) { + return DFinal.of(StringExpr.this.get(branch, dataRoot)); } }; } @@ -78,28 +78,28 @@ public abstract class Expr { } @Override - public ObjectExpr asObjectExpr() { - return new ObjectExpr() { + public DynamicExpr asDynamicExpr() { + return new DynamicExpr() { @Override - public OAny get(OAny branch, OAny dataRoot) { - return OFinal.of(NumberExpr.this.get(branch, dataRoot)); + public Dynamic get(Dynamic branch, Dynamic dataRoot) { + return DFinal.of(NumberExpr.this.get(branch, dataRoot)); } }; } } - public static abstract class ObjectExpr extends Expr> { + public static abstract class DynamicExpr extends Expr> { @Override public Type getResultType() { - return Type.Object; + return Type.Dynamic; } @Override public BoolExpr asBoolExpr() { return new BoolExpr() { @Override - public Boolean get(OAny branch, OAny dataRoot) { - return ObjectExpr.this.get(branch, dataRoot).asBool().getValue(); + public Boolean get(Dynamic branch, Dynamic dataRoot) { + return DynamicExpr.this.get(branch, dataRoot).asBool().getValue(); } }; } @@ -108,8 +108,8 @@ public abstract class Expr { public StringExpr asStringExpr() { return new StringExpr() { @Override - public String get(OAny branch, OAny dataRoot) { - return ObjectExpr.this.get(branch, dataRoot).asString().getValue(); + public String get(Dynamic branch, Dynamic dataRoot) { + return DynamicExpr.this.get(branch, dataRoot).asString().getValue(); } }; } @@ -118,14 +118,14 @@ public abstract class Expr { public NumberExpr asNumberExpr() { return new NumberExpr() { @Override - public Double get(OAny branch, OAny dataRoot) { - return ObjectExpr.this.get(branch, dataRoot).asNumber().getValue(); + public Double get(Dynamic branch, Dynamic dataRoot) { + return DynamicExpr.this.get(branch, dataRoot).asNumber().getValue(); } }; } @Override - public ObjectExpr asObjectExpr() { + public DynamicExpr asDynamicExpr() { return this; } } @@ -133,20 +133,20 @@ public abstract class Expr { public static final class NullExpr extends Expr { @Override public Type getResultType() { - return Type.Object; + return Type.Dynamic; } @Override - public Object get(OAny branch, OAny dataRoot) { + public Object get(Dynamic branch, Dynamic dataRoot) { return null; } @Override - public ObjectExpr asObjectExpr() { - return new ObjectExpr() { + public DynamicExpr asDynamicExpr() { + return new DynamicExpr() { @Override - public OAny get(OAny branch, OAny dataRoot) { - return new ONull(); + public Dynamic get(Dynamic branch, Dynamic dataRoot) { + return new DNull(); } }; } @@ -155,7 +155,7 @@ public abstract class Expr { public static BoolExpr literal(boolean bool) { return new BoolExpr() { @Override - public Boolean get(OAny branch, OAny dataRoot) { + public Boolean get(Dynamic branch, Dynamic dataRoot) { return bool; } }; @@ -164,7 +164,7 @@ public abstract class Expr { public static StringExpr literal(String string) { return new StringExpr() { @Override - public String get(OAny branch, OAny dataRoot) { + public String get(Dynamic branch, Dynamic dataRoot) { return string; } }; @@ -173,7 +173,7 @@ public abstract class Expr { public static NumberExpr literal(double number) { return new NumberExpr() { @Override - public Double get(OAny branch, OAny dataRoot) { + public Double get(Dynamic branch, Dynamic dataRoot) { return number; } }; diff --git a/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/Parser.java b/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/Parser.java index c5e549c..6cc46b6 100644 --- a/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/Parser.java +++ b/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/Parser.java @@ -162,10 +162,10 @@ public class Parser { } else if (match(Token.Dot)) { TokenData name = consume(Token.Identifier, "Expected field name after '.'."); - expr = new Get(expr.asObjectExpr(), Expr.literal(name.lexeme)); + expr = new Get(expr.asDynamicExpr(), Expr.literal(name.lexeme)); } else if (match(Token.LeftBracket)) { - expr = new Get(expr.asObjectExpr(), expression()); + expr = new Get(expr.asDynamicExpr(), expression()); consume(Token.RightBracket, "Expected closing bracket"); } else { @@ -177,16 +177,16 @@ public class Parser { } private Expr finishCall(Expr callee) { - List args = new ArrayList<>(2); + List args = new ArrayList<>(2); if (!check(Token.RightParen)) { do { - args.add(expression().asObjectExpr()); + args.add(expression().asDynamicExpr()); } while (match(Token.Comma)); } consume(Token.RightParen, "Expected ')' after function arguments."); - return new Call(callee.asObjectExpr(), args); + return new Call(callee.asDynamicExpr(), args); } private Expr primary() { diff --git a/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/Type.java b/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/Type.java index 3a8b268..636746c 100644 --- a/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/Type.java +++ b/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/Type.java @@ -1,5 +1,5 @@ package io.gitlab.jfronny.muscript.compiler; public enum Type { - String, Number, Boolean, Object + String, Number, Boolean, Dynamic } diff --git a/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/expr/Call.java b/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/expr/Call.java index d4aa03a..296afc1 100644 --- a/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/expr/Call.java +++ b/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/expr/Call.java @@ -1,22 +1,21 @@ package io.gitlab.jfronny.muscript.compiler.expr; import io.gitlab.jfronny.muscript.compiler.*; -import io.gitlab.jfronny.muscript.optic.*; +import io.gitlab.jfronny.muscript.dynamic.*; import java.util.*; -import java.util.stream.*; -public class Call extends Expr.ObjectExpr { - private final ObjectExpr left; - private final List args; +public class Call extends Expr.DynamicExpr { + private final DynamicExpr left; + private final List args; - public Call(ObjectExpr left, List args) { + public Call(DynamicExpr left, List args) { this.left = left; this.args = args; } @Override - public OAny get(OAny branch, OAny dataRoot) { - return left.get(branch, dataRoot).asCallable().getValue().apply(OFinal.of(args.stream().map(e -> e.get(dataRoot, dataRoot)).toArray(OAny[]::new))); + public Dynamic get(Dynamic branch, Dynamic dataRoot) { + return left.get(branch, dataRoot).asCallable().getValue().apply(DFinal.of(args.stream().map(e -> e.get(dataRoot, dataRoot)).toArray(Dynamic[]::new))); } } diff --git a/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/expr/Compare.java b/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/expr/Compare.java index b813f6d..90c4fc6 100644 --- a/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/expr/Compare.java +++ b/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/expr/Compare.java @@ -1,7 +1,7 @@ package io.gitlab.jfronny.muscript.compiler.expr; import io.gitlab.jfronny.muscript.compiler.*; -import io.gitlab.jfronny.muscript.optic.*; +import io.gitlab.jfronny.muscript.dynamic.*; public class Compare extends Expr.BoolExpr { private final NumberExpr left; @@ -17,7 +17,7 @@ public class Compare extends Expr.BoolExpr { } @Override - public Boolean get(OAny branch, OAny dataRoot) { + public Boolean get(Dynamic branch, Dynamic dataRoot) { double left = this.left.get(branch, dataRoot); double right = this.right.get(branch, dataRoot); return switch (comparator) { diff --git a/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/expr/Concatenate.java b/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/expr/Concatenate.java index 015bf76..56001df 100644 --- a/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/expr/Concatenate.java +++ b/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/expr/Concatenate.java @@ -1,7 +1,7 @@ package io.gitlab.jfronny.muscript.compiler.expr; import io.gitlab.jfronny.muscript.compiler.*; -import io.gitlab.jfronny.muscript.optic.*; +import io.gitlab.jfronny.muscript.dynamic.*; public class Concatenate extends Expr.StringExpr { private final StringExpr left; @@ -13,7 +13,7 @@ public class Concatenate extends Expr.StringExpr { } @Override - public String get(OAny branch, OAny dataRoot) { + public String get(Dynamic branch, Dynamic dataRoot) { return left.get(branch, dataRoot) + right.get(branch, dataRoot); } } diff --git a/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/expr/Conditional.java b/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/expr/Conditional.java index 0319386..3847f9e 100644 --- a/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/expr/Conditional.java +++ b/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/expr/Conditional.java @@ -1,7 +1,7 @@ package io.gitlab.jfronny.muscript.compiler.expr; import io.gitlab.jfronny.muscript.compiler.*; -import io.gitlab.jfronny.muscript.optic.*; +import io.gitlab.jfronny.muscript.dynamic.*; public class Conditional extends Expr { public final BoolExpr condition; @@ -22,7 +22,7 @@ public class Conditional extends Expr { } @Override - public Object get(OAny branch, OAny dataRoot) { + public Object get(Dynamic branch, Dynamic dataRoot) { return condition.get(branch, dataRoot) ? trueExpr.get(branch, dataRoot) : falseExpr.get(branch, dataRoot); } @@ -32,7 +32,7 @@ public class Conditional extends Expr { BoolExpr falseExpr = this.falseExpr.asBoolExpr(); return new BoolExpr() { @Override - public Boolean get(OAny branch, OAny dataRoot) { + public Boolean get(Dynamic branch, Dynamic dataRoot) { return condition.get(branch, dataRoot) ? trueExpr.get(branch, dataRoot) : falseExpr.get(branch, dataRoot); } }; @@ -44,7 +44,7 @@ public class Conditional extends Expr { StringExpr falseExpr = this.falseExpr.asStringExpr(); return new StringExpr() { @Override - public String get(OAny branch, OAny dataRoot) { + public String get(Dynamic branch, Dynamic dataRoot) { return condition.get(branch, dataRoot) ? trueExpr.get(branch, dataRoot) : falseExpr.get(branch, dataRoot); } }; @@ -56,19 +56,19 @@ public class Conditional extends Expr { NumberExpr falseExpr = this.falseExpr.asNumberExpr(); return new NumberExpr() { @Override - public Double get(OAny branch, OAny dataRoot) { + public Double get(Dynamic branch, Dynamic dataRoot) { return condition.get(branch, dataRoot) ? trueExpr.get(branch, dataRoot) : falseExpr.get(branch, dataRoot); } }; } @Override - public ObjectExpr asObjectExpr() { - ObjectExpr trueExpr = this.trueExpr.asObjectExpr(); - ObjectExpr falseExpr = this.falseExpr.asObjectExpr(); - return new ObjectExpr() { + public DynamicExpr asDynamicExpr() { + DynamicExpr trueExpr = this.trueExpr.asDynamicExpr(); + DynamicExpr falseExpr = this.falseExpr.asDynamicExpr(); + return new DynamicExpr() { @Override - public OAny get(OAny branch, OAny dataRoot) { + public Dynamic get(Dynamic branch, Dynamic dataRoot) { return condition.get(branch, dataRoot) ? trueExpr.get(branch, dataRoot) : falseExpr.get(branch, dataRoot); } }; diff --git a/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/expr/Equal.java b/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/expr/Equal.java index 43dffff..84fadec 100644 --- a/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/expr/Equal.java +++ b/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/expr/Equal.java @@ -1,7 +1,7 @@ package io.gitlab.jfronny.muscript.compiler.expr; import io.gitlab.jfronny.muscript.compiler.*; -import io.gitlab.jfronny.muscript.optic.*; +import io.gitlab.jfronny.muscript.dynamic.*; import java.util.*; @@ -15,12 +15,12 @@ public class Equal extends Expr.BoolExpr { } @Override - public Boolean get(OAny branch, OAny dataRoot) { + public Boolean get(Dynamic branch, Dynamic dataRoot) { return Objects.equals(unwrap(left.get(branch, dataRoot)), unwrap(right.get(branch, dataRoot))); } private Object unwrap(Object o) { - if (o instanceof OAny a) return unwrap(a.getValue()); + if (o instanceof Dynamic a) return unwrap(a.getValue()); return o; } } diff --git a/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/expr/Get.java b/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/expr/Get.java index 909ab93..154255e 100644 --- a/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/expr/Get.java +++ b/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/expr/Get.java @@ -1,26 +1,26 @@ package io.gitlab.jfronny.muscript.compiler.expr; import io.gitlab.jfronny.muscript.compiler.*; -import io.gitlab.jfronny.muscript.optic.*; +import io.gitlab.jfronny.muscript.dynamic.*; -public class Get extends Expr.ObjectExpr { - private final ObjectExpr left; +public class Get extends Expr.DynamicExpr { + private final DynamicExpr left; private final Expr name; - public Get(ObjectExpr left, Expr name) { + public Get(DynamicExpr left, Expr name) { this.left = left; this.name = name; - if (name.getResultType() != Type.String && name.getResultType() != Type.Number && name.getResultType() != Type.Object) { + if (name.getResultType() != Type.String && name.getResultType() != Type.Number && name.getResultType() != Type.Dynamic) { throw new IllegalArgumentException("Name must be either a string or a number"); } } @Override - public OAny get(OAny branch, OAny dataRoot) { - OAny left = this.left.get(branch, dataRoot); - if (left instanceof OObject o) { + public Dynamic get(Dynamic branch, Dynamic dataRoot) { + Dynamic left = this.left.get(branch, dataRoot); + if (left instanceof DObject o) { return o.get(name.asStringExpr().get(dataRoot, dataRoot)); - } else if (left instanceof OList l) { + } else if (left instanceof DList l) { return l.get(name.asNumberExpr().get(dataRoot, dataRoot).intValue()); } throw new IllegalArgumentException("The element to get value \"" + name.asStringExpr().get(dataRoot, dataRoot) + "\" from is not of a valid type"); diff --git a/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/expr/Group.java b/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/expr/Group.java index 3cf1a85..f869981 100644 --- a/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/expr/Group.java +++ b/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/expr/Group.java @@ -1,7 +1,7 @@ package io.gitlab.jfronny.muscript.compiler.expr; import io.gitlab.jfronny.muscript.compiler.*; -import io.gitlab.jfronny.muscript.optic.*; +import io.gitlab.jfronny.muscript.dynamic.*; public class Group extends Expr { public final Expr expr; @@ -16,7 +16,7 @@ public class Group extends Expr { } @Override - public Object get(OAny branch, OAny dataRoot) { + public Object get(Dynamic branch, Dynamic dataRoot) { return expr.get(branch, dataRoot); } @@ -36,7 +36,7 @@ public class Group extends Expr { } @Override - public ObjectExpr asObjectExpr() { - return expr.asObjectExpr(); + public DynamicExpr asDynamicExpr() { + return expr.asDynamicExpr(); } } diff --git a/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/expr/Invert.java b/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/expr/Invert.java index 361c5b7..505d1ed 100644 --- a/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/expr/Invert.java +++ b/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/expr/Invert.java @@ -1,7 +1,7 @@ package io.gitlab.jfronny.muscript.compiler.expr; import io.gitlab.jfronny.muscript.compiler.*; -import io.gitlab.jfronny.muscript.optic.*; +import io.gitlab.jfronny.muscript.dynamic.*; public class Invert extends Expr.NumberExpr { private final Expr.NumberExpr inner; @@ -11,7 +11,7 @@ public class Invert extends Expr.NumberExpr { } @Override - public Double get(OAny branch, OAny dataRoot) { + public Double get(Dynamic branch, Dynamic dataRoot) { return -inner.get(branch, dataRoot); } } diff --git a/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/expr/LogicBiExpr.java b/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/expr/LogicBiExpr.java index 8b637be..db6107d 100644 --- a/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/expr/LogicBiExpr.java +++ b/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/expr/LogicBiExpr.java @@ -1,7 +1,7 @@ package io.gitlab.jfronny.muscript.compiler.expr; import io.gitlab.jfronny.muscript.compiler.*; -import io.gitlab.jfronny.muscript.optic.*; +import io.gitlab.jfronny.muscript.dynamic.*; public class LogicBiExpr extends Expr.BoolExpr { private final Expr.BoolExpr left; @@ -17,7 +17,7 @@ public class LogicBiExpr extends Expr.BoolExpr { } @Override - public Boolean get(OAny branch, OAny dataRoot) { + public Boolean get(Dynamic branch, Dynamic dataRoot) { boolean left = this.left.get(branch, dataRoot); boolean right = this.right.get(branch, dataRoot); return switch (comparator) { diff --git a/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/expr/MathBiExpr.java b/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/expr/MathBiExpr.java index 63f80a1..3542cc9 100644 --- a/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/expr/MathBiExpr.java +++ b/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/expr/MathBiExpr.java @@ -1,7 +1,7 @@ package io.gitlab.jfronny.muscript.compiler.expr; import io.gitlab.jfronny.muscript.compiler.*; -import io.gitlab.jfronny.muscript.optic.*; +import io.gitlab.jfronny.muscript.dynamic.*; public class MathBiExpr extends Expr.NumberExpr { private final NumberExpr left; @@ -17,7 +17,7 @@ public class MathBiExpr extends Expr.NumberExpr { } @Override - public Double get(OAny branch, OAny dataRoot) { + public Double get(Dynamic branch, Dynamic dataRoot) { double left = this.left.get(branch, dataRoot); double right = this.right.get(branch, dataRoot); return switch (comparator) { diff --git a/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/expr/Not.java b/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/expr/Not.java index 5408cc4..7baaaa7 100644 --- a/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/expr/Not.java +++ b/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/expr/Not.java @@ -1,7 +1,7 @@ package io.gitlab.jfronny.muscript.compiler.expr; import io.gitlab.jfronny.muscript.compiler.*; -import io.gitlab.jfronny.muscript.optic.*; +import io.gitlab.jfronny.muscript.dynamic.*; public class Not extends Expr.BoolExpr { private final BoolExpr inner; @@ -11,7 +11,7 @@ public class Not extends Expr.BoolExpr { } @Override - public Boolean get(OAny branch, OAny dataRoot) { + public Boolean get(Dynamic branch, Dynamic dataRoot) { return !inner.get(branch, dataRoot); } } diff --git a/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/expr/Variable.java b/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/expr/Variable.java index b9413cd..66dda9f 100644 --- a/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/expr/Variable.java +++ b/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/expr/Variable.java @@ -1,9 +1,9 @@ package io.gitlab.jfronny.muscript.compiler.expr; import io.gitlab.jfronny.muscript.compiler.*; -import io.gitlab.jfronny.muscript.optic.*; +import io.gitlab.jfronny.muscript.dynamic.*; -public class Variable extends Expr.ObjectExpr { +public class Variable extends Expr.DynamicExpr { private final String name; public Variable(String name) { @@ -11,10 +11,10 @@ public class Variable extends Expr.ObjectExpr { } @Override - public OAny get(OAny branch, OAny dataRoot) { + public Dynamic get(Dynamic branch, Dynamic dataRoot) { if (branch.asObject().has(name)) return branch.asObject().get(name); else if (name.contains("::")) { - OAny res = branch; + Dynamic res = branch; for (String s : name.split("::")) { if (!res.asObject().has(s)) throw new IllegalArgumentException("This object doesn't contain that name"); diff --git a/muscript/src/main/java/io/gitlab/jfronny/muscript/debug/ObjectGraphPrinter.java b/muscript/src/main/java/io/gitlab/jfronny/muscript/debug/ObjectGraphPrinter.java index ccb7528..d78d663 100644 --- a/muscript/src/main/java/io/gitlab/jfronny/muscript/debug/ObjectGraphPrinter.java +++ b/muscript/src/main/java/io/gitlab/jfronny/muscript/debug/ObjectGraphPrinter.java @@ -1,6 +1,6 @@ package io.gitlab.jfronny.muscript.debug; -import io.gitlab.jfronny.muscript.optic.*; +import io.gitlab.jfronny.muscript.dynamic.*; import java.lang.reflect.*; import java.util.*; @@ -32,7 +32,7 @@ public class ObjectGraphPrinter { writer.writeLine(name + " = \"" + fo + "\""); } else if (kz.isAssignableFrom(Double.class)) { writer.writeLine(name + " = " + fo); - } else if (kz.isAssignableFrom(OAny.class)) { + } else if (kz.isAssignableFrom(Dynamic.class)) { writer.writeLine(name + " = " + fo); } else if (kz.isAssignableFrom(Collection.class)) { writer.writeLine(name + ":"); diff --git a/muscript/src/main/java/io/gitlab/jfronny/muscript/dynamic/DBool.java b/muscript/src/main/java/io/gitlab/jfronny/muscript/dynamic/DBool.java new file mode 100644 index 0000000..a968685 --- /dev/null +++ b/muscript/src/main/java/io/gitlab/jfronny/muscript/dynamic/DBool.java @@ -0,0 +1,4 @@ +package io.gitlab.jfronny.muscript.dynamic; + +public interface DBool extends Dynamic { +} diff --git a/muscript/src/main/java/io/gitlab/jfronny/muscript/dynamic/DCallable.java b/muscript/src/main/java/io/gitlab/jfronny/muscript/dynamic/DCallable.java new file mode 100644 index 0000000..f1ee056 --- /dev/null +++ b/muscript/src/main/java/io/gitlab/jfronny/muscript/dynamic/DCallable.java @@ -0,0 +1,13 @@ +package io.gitlab.jfronny.muscript.dynamic; + +import java.util.function.*; + +public interface DCallable extends Dynamic>> { + default Dynamic call(DList args) { + return getValue().apply(args); + } + + default Dynamic call(Dynamic... args) { + return call(DFinal.of(args)); + } +} diff --git a/muscript/src/main/java/io/gitlab/jfronny/muscript/optic/OContainer.java b/muscript/src/main/java/io/gitlab/jfronny/muscript/dynamic/DContainer.java similarity index 78% rename from muscript/src/main/java/io/gitlab/jfronny/muscript/optic/OContainer.java rename to muscript/src/main/java/io/gitlab/jfronny/muscript/dynamic/DContainer.java index aff2d22..d0e763c 100644 --- a/muscript/src/main/java/io/gitlab/jfronny/muscript/optic/OContainer.java +++ b/muscript/src/main/java/io/gitlab/jfronny/muscript/dynamic/DContainer.java @@ -1,8 +1,8 @@ -package io.gitlab.jfronny.muscript.optic; +package io.gitlab.jfronny.muscript.dynamic; import io.gitlab.jfronny.commons.StringFormatter; -public abstract class OContainer implements OAny { +public abstract class DContainer implements Dynamic { private T value; @Override diff --git a/muscript/src/main/java/io/gitlab/jfronny/muscript/dynamic/DEnum.java b/muscript/src/main/java/io/gitlab/jfronny/muscript/dynamic/DEnum.java new file mode 100644 index 0000000..08d6a27 --- /dev/null +++ b/muscript/src/main/java/io/gitlab/jfronny/muscript/dynamic/DEnum.java @@ -0,0 +1,52 @@ +package io.gitlab.jfronny.muscript.dynamic; + +import java.util.*; + +/** An enum represented as an OObject with automatic conversions to a number (index) and string (selected value) */ +public record DEnum(Map> values, DEnumEntry value) implements DObject { + public DEnum(List values, String value) { + this(createMap(values, value), new DEnumEntry(value, values.indexOf(value), true)); + } + + @Override + public Map> getValue() { + return values; + } + + @Override + public DString asString() { + return value; + } + + @Override + public DNumber asNumber() { + return value.asNumber(); + } + + private static Map> createMap(List values, String value) { + Map> result = new LinkedHashMap<>(); + DEnumEntry v = new DEnumEntry(value, values.indexOf(value), true); + result.put("value", v); + for (int i = 0; i < values.size(); i++) { + result.put(values.get(i), new DEnumEntry(values.get(i), i, values.get(i).equals(value))); + } + return Map.copyOf(result); + } + + public record DEnumEntry(String value, int index, boolean selected) implements DString { + @Override + public String getValue() { + return value; + } + + @Override + public DNumber asNumber() { + return DFinal.of(index); + } + + @Override + public DBool asBool() { + return DFinal.of(selected); + } + } +} diff --git a/muscript/src/main/java/io/gitlab/jfronny/muscript/optic/OFinal.java b/muscript/src/main/java/io/gitlab/jfronny/muscript/dynamic/DFinal.java similarity index 63% rename from muscript/src/main/java/io/gitlab/jfronny/muscript/optic/OFinal.java rename to muscript/src/main/java/io/gitlab/jfronny/muscript/dynamic/DFinal.java index b77774b..1dcd0d0 100644 --- a/muscript/src/main/java/io/gitlab/jfronny/muscript/optic/OFinal.java +++ b/muscript/src/main/java/io/gitlab/jfronny/muscript/dynamic/DFinal.java @@ -1,32 +1,32 @@ -package io.gitlab.jfronny.muscript.optic; +package io.gitlab.jfronny.muscript.dynamic; import io.gitlab.jfronny.commons.StringFormatter; import java.util.*; import java.util.function.*; -public record OFinal(T value) implements OAny { - public static OBool of(boolean b) { +public record DFinal(T value) implements Dynamic { + public static DBool of(boolean b) { return new FBool(b); } - public static ONumber of(double b) { + public static DNumber of(double b) { return new FNumber(b); } - public static OString of(String b) { + public static DString of(String b) { return new FString(b); } - public static OObject of(Map> b) { + public static DObject of(Map> b) { return new FObject(Map.copyOf(b)); } - public static OList of(OAny... b) { + public static DList of(Dynamic... b) { return new FList(List.of(b)); } - public static OCallable of(Function> b) { + public static DCallable of(Function> b) { return new FCallable(b); } @@ -40,7 +40,7 @@ public record OFinal(T value) implements OAny { return StringFormatter.toString(value); } - private record FBool(boolean value) implements OBool { + private record FBool(boolean value) implements DBool { @Override public Boolean getValue() { return value; @@ -52,7 +52,7 @@ public record OFinal(T value) implements OAny { } } - private record FNumber(double value) implements ONumber { + private record FNumber(double value) implements DNumber { @Override public Double getValue() { return value; @@ -64,7 +64,7 @@ public record OFinal(T value) implements OAny { } } - private record FString(String value) implements OString { + private record FString(String value) implements DString { @Override public String getValue() { return value; @@ -76,9 +76,9 @@ public record OFinal(T value) implements OAny { } } - private record FObject(Map> value) implements OObject { + private record FObject(Map> value) implements DObject { @Override - public Map> getValue() { + public Map> getValue() { return value; } @@ -88,9 +88,9 @@ public record OFinal(T value) implements OAny { } } - private record FList(List> value) implements OList { + private record FList(List> value) implements DList { @Override - public List> getValue() { + public List> getValue() { return value; } @@ -100,9 +100,9 @@ public record OFinal(T value) implements OAny { } } - private record FCallable(Function> value) implements OCallable { + private record FCallable(Function> value) implements DCallable { @Override - public Function> getValue() { + public Function> getValue() { return value; } diff --git a/muscript/src/main/java/io/gitlab/jfronny/muscript/dynamic/DList.java b/muscript/src/main/java/io/gitlab/jfronny/muscript/dynamic/DList.java new file mode 100644 index 0000000..6ea525f --- /dev/null +++ b/muscript/src/main/java/io/gitlab/jfronny/muscript/dynamic/DList.java @@ -0,0 +1,12 @@ +package io.gitlab.jfronny.muscript.dynamic; + +import java.util.*; + +public interface DList extends Dynamic>> { + default Dynamic get(int i) { + return getValue().get(i); + } + default int size() { + return getValue().size(); + } +} diff --git a/muscript/src/main/java/io/gitlab/jfronny/muscript/dynamic/DNull.java b/muscript/src/main/java/io/gitlab/jfronny/muscript/dynamic/DNull.java new file mode 100644 index 0000000..431a72c --- /dev/null +++ b/muscript/src/main/java/io/gitlab/jfronny/muscript/dynamic/DNull.java @@ -0,0 +1,8 @@ +package io.gitlab.jfronny.muscript.dynamic; + +public final class DNull implements Dynamic { + @Override + public Object getValue() { + return null; + } +} diff --git a/muscript/src/main/java/io/gitlab/jfronny/muscript/dynamic/DNumber.java b/muscript/src/main/java/io/gitlab/jfronny/muscript/dynamic/DNumber.java new file mode 100644 index 0000000..1b1c9ac --- /dev/null +++ b/muscript/src/main/java/io/gitlab/jfronny/muscript/dynamic/DNumber.java @@ -0,0 +1,4 @@ +package io.gitlab.jfronny.muscript.dynamic; + +public interface DNumber extends Dynamic { +} diff --git a/muscript/src/main/java/io/gitlab/jfronny/muscript/optic/OObject.java b/muscript/src/main/java/io/gitlab/jfronny/muscript/dynamic/DObject.java similarity index 50% rename from muscript/src/main/java/io/gitlab/jfronny/muscript/optic/OObject.java rename to muscript/src/main/java/io/gitlab/jfronny/muscript/dynamic/DObject.java index d3a5f55..ac5eb71 100644 --- a/muscript/src/main/java/io/gitlab/jfronny/muscript/optic/OObject.java +++ b/muscript/src/main/java/io/gitlab/jfronny/muscript/dynamic/DObject.java @@ -1,9 +1,9 @@ -package io.gitlab.jfronny.muscript.optic; +package io.gitlab.jfronny.muscript.dynamic; import java.util.*; -public interface OObject extends OAny>> { - default OAny get(String key) { +public interface DObject extends Dynamic>> { + default Dynamic get(String key) { return getValue().get(key); } diff --git a/muscript/src/main/java/io/gitlab/jfronny/muscript/dynamic/DString.java b/muscript/src/main/java/io/gitlab/jfronny/muscript/dynamic/DString.java new file mode 100644 index 0000000..b87bb71 --- /dev/null +++ b/muscript/src/main/java/io/gitlab/jfronny/muscript/dynamic/DString.java @@ -0,0 +1,4 @@ +package io.gitlab.jfronny.muscript.dynamic; + +public interface DString extends Dynamic { +} diff --git a/muscript/src/main/java/io/gitlab/jfronny/muscript/dynamic/Dynamic.java b/muscript/src/main/java/io/gitlab/jfronny/muscript/dynamic/Dynamic.java new file mode 100644 index 0000000..eaef1ec --- /dev/null +++ b/muscript/src/main/java/io/gitlab/jfronny/muscript/dynamic/Dynamic.java @@ -0,0 +1,37 @@ +package io.gitlab.jfronny.muscript.dynamic; + +import io.gitlab.jfronny.commons.*; + +public interface Dynamic { + T getValue(); + + default DBool asBool() { + if (this instanceof DBool bool) return bool; + else throw new IllegalArgumentException("This value is not a bool"); + } + + default DNumber asNumber() { + if (this instanceof DNumber number) return number; + else throw new IllegalArgumentException("This value is not a number"); + } + + default DString asString() { + if (this instanceof DString string) return string; + else return DFinal.of(StringFormatter.toString(getValue())); + } + + default DObject asObject() { + if (this instanceof DObject object) return object; + else throw new IllegalArgumentException("This value is not an object"); + } + + default DList asList() { + if (this instanceof DList list) return list; + else throw new IllegalArgumentException("This value is not a list"); + } + + default DCallable asCallable() { + if (this instanceof DCallable callable) return callable; + else throw new IllegalArgumentException("This value is not a callable"); + } +} diff --git a/muscript/src/main/java/io/gitlab/jfronny/muscript/optic/OAny.java b/muscript/src/main/java/io/gitlab/jfronny/muscript/optic/OAny.java deleted file mode 100644 index df3897e..0000000 --- a/muscript/src/main/java/io/gitlab/jfronny/muscript/optic/OAny.java +++ /dev/null @@ -1,37 +0,0 @@ -package io.gitlab.jfronny.muscript.optic; - -import io.gitlab.jfronny.commons.*; - -public interface OAny { - T getValue(); - - default OBool asBool() { - if (this instanceof OBool bool) return bool; - else throw new IllegalArgumentException("This value is not a bool"); - } - - default ONumber asNumber() { - if (this instanceof ONumber number) return number; - else throw new IllegalArgumentException("This value is not a number"); - } - - default OString asString() { - if (this instanceof OString string) return string; - else return OFinal.of(StringFormatter.toString(getValue())); - } - - default OObject asObject() { - if (this instanceof OObject object) return object; - else throw new IllegalArgumentException("This value is not an object"); - } - - default OList asList() { - if (this instanceof OList list) return list; - else throw new IllegalArgumentException("This value is not a list"); - } - - default OCallable asCallable() { - if (this instanceof OCallable callable) return callable; - else throw new IllegalArgumentException("This value is not a callable"); - } -} diff --git a/muscript/src/main/java/io/gitlab/jfronny/muscript/optic/OBool.java b/muscript/src/main/java/io/gitlab/jfronny/muscript/optic/OBool.java deleted file mode 100644 index 072e50f..0000000 --- a/muscript/src/main/java/io/gitlab/jfronny/muscript/optic/OBool.java +++ /dev/null @@ -1,4 +0,0 @@ -package io.gitlab.jfronny.muscript.optic; - -public interface OBool extends OAny { -} diff --git a/muscript/src/main/java/io/gitlab/jfronny/muscript/optic/OCallable.java b/muscript/src/main/java/io/gitlab/jfronny/muscript/optic/OCallable.java deleted file mode 100644 index 01a5f89..0000000 --- a/muscript/src/main/java/io/gitlab/jfronny/muscript/optic/OCallable.java +++ /dev/null @@ -1,13 +0,0 @@ -package io.gitlab.jfronny.muscript.optic; - -import java.util.function.*; - -public interface OCallable extends OAny>> { - default OAny call(OList args) { - return getValue().apply(args); - } - - default OAny call(OAny... args) { - return call(OFinal.of(args)); - } -} diff --git a/muscript/src/main/java/io/gitlab/jfronny/muscript/optic/OEnum.java b/muscript/src/main/java/io/gitlab/jfronny/muscript/optic/OEnum.java deleted file mode 100644 index b4698c3..0000000 --- a/muscript/src/main/java/io/gitlab/jfronny/muscript/optic/OEnum.java +++ /dev/null @@ -1,52 +0,0 @@ -package io.gitlab.jfronny.muscript.optic; - -import java.util.*; - -/** An enum represented as an OObject with automatic conversions to a number (index) and string (selected value) */ -public record OEnum(Map> values, OEnumEntry value) implements OObject { - public OEnum(List values, String value) { - this(createMap(values, value), new OEnumEntry(value, values.indexOf(value), true)); - } - - @Override - public Map> getValue() { - return values; - } - - @Override - public OString asString() { - return value; - } - - @Override - public ONumber asNumber() { - return value.asNumber(); - } - - private static Map> createMap(List values, String value) { - Map> result = new LinkedHashMap<>(); - OEnumEntry v = new OEnumEntry(value, values.indexOf(value), true); - result.put("value", v); - for (int i = 0; i < values.size(); i++) { - result.put(values.get(i), new OEnumEntry(values.get(i), i, values.get(i).equals(value))); - } - return Map.copyOf(result); - } - - public record OEnumEntry(String value, int index, boolean selected) implements OString { - @Override - public String getValue() { - return value; - } - - @Override - public ONumber asNumber() { - return OFinal.of(index); - } - - @Override - public OBool asBool() { - return OFinal.of(selected); - } - } -} diff --git a/muscript/src/main/java/io/gitlab/jfronny/muscript/optic/OList.java b/muscript/src/main/java/io/gitlab/jfronny/muscript/optic/OList.java deleted file mode 100644 index 35cb612..0000000 --- a/muscript/src/main/java/io/gitlab/jfronny/muscript/optic/OList.java +++ /dev/null @@ -1,12 +0,0 @@ -package io.gitlab.jfronny.muscript.optic; - -import java.util.*; - -public interface OList extends OAny>> { - default OAny get(int i) { - return getValue().get(i); - } - default int size() { - return getValue().size(); - } -} diff --git a/muscript/src/main/java/io/gitlab/jfronny/muscript/optic/ONull.java b/muscript/src/main/java/io/gitlab/jfronny/muscript/optic/ONull.java deleted file mode 100644 index 0cc6190..0000000 --- a/muscript/src/main/java/io/gitlab/jfronny/muscript/optic/ONull.java +++ /dev/null @@ -1,8 +0,0 @@ -package io.gitlab.jfronny.muscript.optic; - -public final class ONull implements OAny { - @Override - public Object getValue() { - return null; - } -} diff --git a/muscript/src/main/java/io/gitlab/jfronny/muscript/optic/ONumber.java b/muscript/src/main/java/io/gitlab/jfronny/muscript/optic/ONumber.java deleted file mode 100644 index 072c4f2..0000000 --- a/muscript/src/main/java/io/gitlab/jfronny/muscript/optic/ONumber.java +++ /dev/null @@ -1,4 +0,0 @@ -package io.gitlab.jfronny.muscript.optic; - -public interface ONumber extends OAny { -} diff --git a/muscript/src/main/java/io/gitlab/jfronny/muscript/optic/OString.java b/muscript/src/main/java/io/gitlab/jfronny/muscript/optic/OString.java deleted file mode 100644 index 7dc2bba..0000000 --- a/muscript/src/main/java/io/gitlab/jfronny/muscript/optic/OString.java +++ /dev/null @@ -1,4 +0,0 @@ -package io.gitlab.jfronny.muscript.optic; - -public interface OString extends OAny { -} diff --git a/muscript/src/test/java/io/gitlab/jfronny/muscript/test/MuTestUtil.java b/muscript/src/test/java/io/gitlab/jfronny/muscript/test/MuTestUtil.java index 5d2bee0..b52ef66 100644 --- a/muscript/src/test/java/io/gitlab/jfronny/muscript/test/MuTestUtil.java +++ b/muscript/src/test/java/io/gitlab/jfronny/muscript/test/MuTestUtil.java @@ -2,11 +2,11 @@ package io.gitlab.jfronny.muscript.test; import io.gitlab.jfronny.muscript.compiler.*; import io.gitlab.jfronny.muscript.debug.*; -import io.gitlab.jfronny.muscript.optic.*; +import io.gitlab.jfronny.muscript.dynamic.*; import java.util.*; -import static io.gitlab.jfronny.muscript.optic.OFinal.*; +import static io.gitlab.jfronny.muscript.dynamic.DFinal.*; public class MuTestUtil { public static double number(String source) { @@ -31,7 +31,7 @@ public class MuTestUtil { return Parser.parse(source).asStringExpr().get(makeArgs()); } - public static OAny makeArgs() { + public static Dynamic makeArgs() { return of(Map.of( "boolean", of(true), "number", of(15),