[muscript] Rename optic to dynamic to better represent its purpose in the context of muscript
This commit is contained in:
parent
25aa22b560
commit
4f3fdbb9fd
@ -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<String, OAny<?>> value) implements OObject {
|
||||
public record ExpressionParameter(Map<String, Dynamic<?>> value) implements DObject {
|
||||
public ExpressionParameter() {
|
||||
this(new HashMap<>());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, OAny<?>> getValue() {
|
||||
public Map<String, Dynamic<?>> getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public ExpressionParameter set(String key, OAny<?> value) {
|
||||
public ExpressionParameter set(String key, Dynamic<?> value) {
|
||||
this.value.put(key, value);
|
||||
return this;
|
||||
}
|
||||
|
@ -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()));
|
||||
}
|
||||
|
@ -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<T> {
|
||||
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<T> {
|
||||
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<T> {
|
||||
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<T> {
|
||||
}
|
||||
|
||||
@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<T> {
|
||||
}
|
||||
|
||||
@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<T> {
|
||||
}
|
||||
|
||||
@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<OAny<?>> {
|
||||
public static abstract class DynamicExpr extends Expr<Dynamic<?>> {
|
||||
@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<T> {
|
||||
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<T> {
|
||||
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<T> {
|
||||
public static final class NullExpr extends Expr<Object> {
|
||||
@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<T> {
|
||||
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<T> {
|
||||
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<T> {
|
||||
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;
|
||||
}
|
||||
};
|
||||
|
@ -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<Expr.ObjectExpr> args = new ArrayList<>(2);
|
||||
List<Expr.DynamicExpr> 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() {
|
||||
|
@ -1,5 +1,5 @@
|
||||
package io.gitlab.jfronny.muscript.compiler;
|
||||
|
||||
public enum Type {
|
||||
String, Number, Boolean, Object
|
||||
String, Number, Boolean, Dynamic
|
||||
}
|
||||
|
@ -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<ObjectExpr> args;
|
||||
public class Call extends Expr.DynamicExpr {
|
||||
private final DynamicExpr left;
|
||||
private final List<DynamicExpr> args;
|
||||
|
||||
public Call(ObjectExpr left, List<ObjectExpr> args) {
|
||||
public Call(DynamicExpr left, List<DynamicExpr> 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)));
|
||||
}
|
||||
}
|
||||
|
@ -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) {
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
};
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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");
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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) {
|
||||
|
@ -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) {
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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");
|
||||
|
@ -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 + ":");
|
||||
|
@ -0,0 +1,4 @@
|
||||
package io.gitlab.jfronny.muscript.dynamic;
|
||||
|
||||
public interface DBool extends Dynamic<Boolean> {
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package io.gitlab.jfronny.muscript.dynamic;
|
||||
|
||||
import java.util.function.*;
|
||||
|
||||
public interface DCallable extends Dynamic<Function<DList, Dynamic<?>>> {
|
||||
default Dynamic<?> call(DList args) {
|
||||
return getValue().apply(args);
|
||||
}
|
||||
|
||||
default Dynamic<?> call(Dynamic<?>... args) {
|
||||
return call(DFinal.of(args));
|
||||
}
|
||||
}
|
@ -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<T> implements OAny<T> {
|
||||
public abstract class DContainer<T> implements Dynamic<T> {
|
||||
private T value;
|
||||
|
||||
@Override
|
@ -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<String, Dynamic<?>> values, DEnumEntry value) implements DObject {
|
||||
public DEnum(List<String> values, String value) {
|
||||
this(createMap(values, value), new DEnumEntry(value, values.indexOf(value), true));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Dynamic<?>> getValue() {
|
||||
return values;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DString asString() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DNumber asNumber() {
|
||||
return value.asNumber();
|
||||
}
|
||||
|
||||
private static Map<String, Dynamic<?>> createMap(List<String> values, String value) {
|
||||
Map<String, Dynamic<?>> 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);
|
||||
}
|
||||
}
|
||||
}
|
@ -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>(T value) implements OAny<T> {
|
||||
public static OBool of(boolean b) {
|
||||
public record DFinal<T>(T value) implements Dynamic<T> {
|
||||
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<String, OAny<?>> b) {
|
||||
public static DObject of(Map<String, Dynamic<?>> 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<OList, OAny<?>> b) {
|
||||
public static DCallable of(Function<DList, Dynamic<?>> b) {
|
||||
return new FCallable(b);
|
||||
}
|
||||
|
||||
@ -40,7 +40,7 @@ public record OFinal<T>(T value) implements OAny<T> {
|
||||
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>(T value) implements OAny<T> {
|
||||
}
|
||||
}
|
||||
|
||||
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>(T value) implements OAny<T> {
|
||||
}
|
||||
}
|
||||
|
||||
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>(T value) implements OAny<T> {
|
||||
}
|
||||
}
|
||||
|
||||
private record FObject(Map<String, OAny<?>> value) implements OObject {
|
||||
private record FObject(Map<String, Dynamic<?>> value) implements DObject {
|
||||
@Override
|
||||
public Map<String, OAny<?>> getValue() {
|
||||
public Map<String, Dynamic<?>> getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@ -88,9 +88,9 @@ public record OFinal<T>(T value) implements OAny<T> {
|
||||
}
|
||||
}
|
||||
|
||||
private record FList(List<OAny<?>> value) implements OList {
|
||||
private record FList(List<Dynamic<?>> value) implements DList {
|
||||
@Override
|
||||
public List<OAny<?>> getValue() {
|
||||
public List<Dynamic<?>> getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@ -100,9 +100,9 @@ public record OFinal<T>(T value) implements OAny<T> {
|
||||
}
|
||||
}
|
||||
|
||||
private record FCallable(Function<OList, OAny<?>> value) implements OCallable {
|
||||
private record FCallable(Function<DList, Dynamic<?>> value) implements DCallable {
|
||||
@Override
|
||||
public Function<OList, OAny<?>> getValue() {
|
||||
public Function<DList, Dynamic<?>> getValue() {
|
||||
return value;
|
||||
}
|
||||
|
@ -0,0 +1,12 @@
|
||||
package io.gitlab.jfronny.muscript.dynamic;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public interface DList extends Dynamic<List<Dynamic<?>>> {
|
||||
default Dynamic<?> get(int i) {
|
||||
return getValue().get(i);
|
||||
}
|
||||
default int size() {
|
||||
return getValue().size();
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package io.gitlab.jfronny.muscript.dynamic;
|
||||
|
||||
public final class DNull implements Dynamic<Object> {
|
||||
@Override
|
||||
public Object getValue() {
|
||||
return null;
|
||||
}
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
package io.gitlab.jfronny.muscript.dynamic;
|
||||
|
||||
public interface DNumber extends Dynamic<Double> {
|
||||
}
|
@ -1,9 +1,9 @@
|
||||
package io.gitlab.jfronny.muscript.optic;
|
||||
package io.gitlab.jfronny.muscript.dynamic;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public interface OObject extends OAny<Map<String, OAny<?>>> {
|
||||
default OAny<?> get(String key) {
|
||||
public interface DObject extends Dynamic<Map<String, Dynamic<?>>> {
|
||||
default Dynamic<?> get(String key) {
|
||||
return getValue().get(key);
|
||||
}
|
||||
|
@ -0,0 +1,4 @@
|
||||
package io.gitlab.jfronny.muscript.dynamic;
|
||||
|
||||
public interface DString extends Dynamic<String> {
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
package io.gitlab.jfronny.muscript.dynamic;
|
||||
|
||||
import io.gitlab.jfronny.commons.*;
|
||||
|
||||
public interface Dynamic<T> {
|
||||
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");
|
||||
}
|
||||
}
|
@ -1,37 +0,0 @@
|
||||
package io.gitlab.jfronny.muscript.optic;
|
||||
|
||||
import io.gitlab.jfronny.commons.*;
|
||||
|
||||
public interface OAny<T> {
|
||||
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");
|
||||
}
|
||||
}
|
@ -1,4 +0,0 @@
|
||||
package io.gitlab.jfronny.muscript.optic;
|
||||
|
||||
public interface OBool extends OAny<Boolean> {
|
||||
}
|
@ -1,13 +0,0 @@
|
||||
package io.gitlab.jfronny.muscript.optic;
|
||||
|
||||
import java.util.function.*;
|
||||
|
||||
public interface OCallable extends OAny<Function<OList, OAny<?>>> {
|
||||
default OAny<?> call(OList args) {
|
||||
return getValue().apply(args);
|
||||
}
|
||||
|
||||
default OAny<?> call(OAny<?>... args) {
|
||||
return call(OFinal.of(args));
|
||||
}
|
||||
}
|
@ -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<String, OAny<?>> values, OEnumEntry value) implements OObject {
|
||||
public OEnum(List<String> values, String value) {
|
||||
this(createMap(values, value), new OEnumEntry(value, values.indexOf(value), true));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, OAny<?>> getValue() {
|
||||
return values;
|
||||
}
|
||||
|
||||
@Override
|
||||
public OString asString() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ONumber asNumber() {
|
||||
return value.asNumber();
|
||||
}
|
||||
|
||||
private static Map<String, OAny<?>> createMap(List<String> values, String value) {
|
||||
Map<String, OAny<?>> 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);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,12 +0,0 @@
|
||||
package io.gitlab.jfronny.muscript.optic;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public interface OList extends OAny<List<OAny<?>>> {
|
||||
default OAny<?> get(int i) {
|
||||
return getValue().get(i);
|
||||
}
|
||||
default int size() {
|
||||
return getValue().size();
|
||||
}
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
package io.gitlab.jfronny.muscript.optic;
|
||||
|
||||
public final class ONull implements OAny<Object> {
|
||||
@Override
|
||||
public Object getValue() {
|
||||
return null;
|
||||
}
|
||||
}
|
@ -1,4 +0,0 @@
|
||||
package io.gitlab.jfronny.muscript.optic;
|
||||
|
||||
public interface ONumber extends OAny<Double> {
|
||||
}
|
@ -1,4 +0,0 @@
|
||||
package io.gitlab.jfronny.muscript.optic;
|
||||
|
||||
public interface OString extends OAny<String> {
|
||||
}
|
@ -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),
|
||||
|
Loading…
Reference in New Issue
Block a user