[muscript] Rename optic to dynamic to better represent its purpose in the context of muscript

This commit is contained in:
Johannes Frohnmeyer 2022-06-07 12:19:58 +02:00
parent 25aa22b560
commit 4f3fdbb9fd
Signed by: Johannes
GPG Key ID: E76429612C2929F4
38 changed files with 266 additions and 267 deletions

View File

@ -1,20 +1,20 @@
package io.gitlab.jfronny.muscript; package io.gitlab.jfronny.muscript;
import io.gitlab.jfronny.muscript.optic.*; import io.gitlab.jfronny.muscript.dynamic.*;
import java.util.*; import java.util.*;
public record ExpressionParameter(Map<String, OAny<?>> value) implements OObject { public record ExpressionParameter(Map<String, Dynamic<?>> value) implements DObject {
public ExpressionParameter() { public ExpressionParameter() {
this(new HashMap<>()); this(new HashMap<>());
} }
@Override @Override
public Map<String, OAny<?>> getValue() { public Map<String, Dynamic<?>> getValue() {
return value; return value;
} }
public ExpressionParameter set(String key, OAny<?> value) { public ExpressionParameter set(String key, Dynamic<?> value) {
this.value.put(key, value); this.value.put(key, value);
return this; return this;
} }

View File

@ -1,11 +1,11 @@
package io.gitlab.jfronny.muscript; package io.gitlab.jfronny.muscript;
import io.gitlab.jfronny.muscript.optic.*; import io.gitlab.jfronny.muscript.dynamic.*;
import java.text.*; import java.text.*;
import java.util.*; import java.util.*;
import static io.gitlab.jfronny.muscript.optic.OFinal.*; import static io.gitlab.jfronny.muscript.dynamic.DFinal.*;
public class StandardLib { public class StandardLib {
private static final Random rnd = new Random(); private static final Random rnd = new Random();
@ -32,7 +32,7 @@ public class StandardLib {
} }
// Numbers // Numbers
public static OAny<?> round(OList args) { public static Dynamic<?> round(DList args) {
if (args.size() == 1) { if (args.size() == 1) {
return of(Math.round(args.get(0).asNumber().getValue())); 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()); 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())); 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()); 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())); 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()); 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())); 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()); if (args.size() == 0) return of(rnd.nextDouble());
else if (args.size() == 2) { else if (args.size() == 2) {
double min = args.get(0).asNumber().getValue(); double min = args.get(0).asNumber().getValue();
@ -72,22 +72,22 @@ public class StandardLib {
} }
// Strings // 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()); 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()); 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()); 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()); 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()); 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())); 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()); 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())); return of(args.get(0).asString().getValue().replace(args.get(1).asString().getValue(), args.get(2).asString().getValue()));
} }

View File

@ -1,13 +1,13 @@
package io.gitlab.jfronny.muscript.compiler; package io.gitlab.jfronny.muscript.compiler;
import io.gitlab.jfronny.muscript.optic.*; import io.gitlab.jfronny.muscript.dynamic.*;
import io.gitlab.jfronny.commons.StringFormatter; import io.gitlab.jfronny.commons.StringFormatter;
public abstract class Expr<T> { public abstract class Expr<T> {
public abstract Type getResultType(); 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); return get(branch, branch);
} }
@ -20,7 +20,7 @@ public abstract class Expr<T> {
if (this instanceof StringExpr e) return e; if (this instanceof StringExpr e) return e;
return new StringExpr() { return new StringExpr() {
@Override @Override
public String get(OAny<?> branch, OAny<?> dataRoot) { public String get(Dynamic<?> branch, Dynamic<?> dataRoot) {
return StringFormatter.toString(Expr.this.get(branch, 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()); throw new IllegalArgumentException("Expected number but is " + getResultType());
} }
public abstract ObjectExpr asObjectExpr(); public abstract DynamicExpr asDynamicExpr();
public boolean isNull() { public boolean isNull() {
return this instanceof NullExpr; return this instanceof NullExpr;
@ -44,11 +44,11 @@ public abstract class Expr<T> {
} }
@Override @Override
public ObjectExpr asObjectExpr() { public DynamicExpr asDynamicExpr() {
return new ObjectExpr() { return new DynamicExpr() {
@Override @Override
public OAny<?> get(OAny<?> branch, OAny<?> dataRoot) { public Dynamic<?> get(Dynamic<?> branch, Dynamic<?> dataRoot) {
return OFinal.of(BoolExpr.this.get(branch, dataRoot)); return DFinal.of(BoolExpr.this.get(branch, dataRoot));
} }
}; };
} }
@ -61,11 +61,11 @@ public abstract class Expr<T> {
} }
@Override @Override
public ObjectExpr asObjectExpr() { public DynamicExpr asDynamicExpr() {
return new ObjectExpr() { return new DynamicExpr() {
@Override @Override
public OAny<?> get(OAny<?> branch, OAny<?> dataRoot) { public Dynamic<?> get(Dynamic<?> branch, Dynamic<?> dataRoot) {
return OFinal.of(StringExpr.this.get(branch, dataRoot)); return DFinal.of(StringExpr.this.get(branch, dataRoot));
} }
}; };
} }
@ -78,28 +78,28 @@ public abstract class Expr<T> {
} }
@Override @Override
public ObjectExpr asObjectExpr() { public DynamicExpr asDynamicExpr() {
return new ObjectExpr() { return new DynamicExpr() {
@Override @Override
public OAny<?> get(OAny<?> branch, OAny<?> dataRoot) { public Dynamic<?> get(Dynamic<?> branch, Dynamic<?> dataRoot) {
return OFinal.of(NumberExpr.this.get(branch, 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 @Override
public Type getResultType() { public Type getResultType() {
return Type.Object; return Type.Dynamic;
} }
@Override @Override
public BoolExpr asBoolExpr() { public BoolExpr asBoolExpr() {
return new BoolExpr() { return new BoolExpr() {
@Override @Override
public Boolean get(OAny<?> branch, OAny<?> dataRoot) { public Boolean get(Dynamic<?> branch, Dynamic<?> dataRoot) {
return ObjectExpr.this.get(branch, dataRoot).asBool().getValue(); return DynamicExpr.this.get(branch, dataRoot).asBool().getValue();
} }
}; };
} }
@ -108,8 +108,8 @@ public abstract class Expr<T> {
public StringExpr asStringExpr() { public StringExpr asStringExpr() {
return new StringExpr() { return new StringExpr() {
@Override @Override
public String get(OAny<?> branch, OAny<?> dataRoot) { public String get(Dynamic<?> branch, Dynamic<?> dataRoot) {
return ObjectExpr.this.get(branch, dataRoot).asString().getValue(); return DynamicExpr.this.get(branch, dataRoot).asString().getValue();
} }
}; };
} }
@ -118,14 +118,14 @@ public abstract class Expr<T> {
public NumberExpr asNumberExpr() { public NumberExpr asNumberExpr() {
return new NumberExpr() { return new NumberExpr() {
@Override @Override
public Double get(OAny<?> branch, OAny<?> dataRoot) { public Double get(Dynamic<?> branch, Dynamic<?> dataRoot) {
return ObjectExpr.this.get(branch, dataRoot).asNumber().getValue(); return DynamicExpr.this.get(branch, dataRoot).asNumber().getValue();
} }
}; };
} }
@Override @Override
public ObjectExpr asObjectExpr() { public DynamicExpr asDynamicExpr() {
return this; return this;
} }
} }
@ -133,20 +133,20 @@ public abstract class Expr<T> {
public static final class NullExpr extends Expr<Object> { public static final class NullExpr extends Expr<Object> {
@Override @Override
public Type getResultType() { public Type getResultType() {
return Type.Object; return Type.Dynamic;
} }
@Override @Override
public Object get(OAny<?> branch, OAny<?> dataRoot) { public Object get(Dynamic<?> branch, Dynamic<?> dataRoot) {
return null; return null;
} }
@Override @Override
public ObjectExpr asObjectExpr() { public DynamicExpr asDynamicExpr() {
return new ObjectExpr() { return new DynamicExpr() {
@Override @Override
public OAny<?> get(OAny<?> branch, OAny<?> dataRoot) { public Dynamic<?> get(Dynamic<?> branch, Dynamic<?> dataRoot) {
return new ONull(); return new DNull();
} }
}; };
} }
@ -155,7 +155,7 @@ public abstract class Expr<T> {
public static BoolExpr literal(boolean bool) { public static BoolExpr literal(boolean bool) {
return new BoolExpr() { return new BoolExpr() {
@Override @Override
public Boolean get(OAny<?> branch, OAny<?> dataRoot) { public Boolean get(Dynamic<?> branch, Dynamic<?> dataRoot) {
return bool; return bool;
} }
}; };
@ -164,7 +164,7 @@ public abstract class Expr<T> {
public static StringExpr literal(String string) { public static StringExpr literal(String string) {
return new StringExpr() { return new StringExpr() {
@Override @Override
public String get(OAny<?> branch, OAny<?> dataRoot) { public String get(Dynamic<?> branch, Dynamic<?> dataRoot) {
return string; return string;
} }
}; };
@ -173,7 +173,7 @@ public abstract class Expr<T> {
public static NumberExpr literal(double number) { public static NumberExpr literal(double number) {
return new NumberExpr() { return new NumberExpr() {
@Override @Override
public Double get(OAny<?> branch, OAny<?> dataRoot) { public Double get(Dynamic<?> branch, Dynamic<?> dataRoot) {
return number; return number;
} }
}; };

View File

@ -162,10 +162,10 @@ public class Parser {
} }
else if (match(Token.Dot)) { else if (match(Token.Dot)) {
TokenData name = consume(Token.Identifier, "Expected field name after '.'."); 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)) { else if (match(Token.LeftBracket)) {
expr = new Get(expr.asObjectExpr(), expression()); expr = new Get(expr.asDynamicExpr(), expression());
consume(Token.RightBracket, "Expected closing bracket"); consume(Token.RightBracket, "Expected closing bracket");
} }
else { else {
@ -177,16 +177,16 @@ public class Parser {
} }
private Expr<?> finishCall(Expr<?> callee) { private Expr<?> finishCall(Expr<?> callee) {
List<Expr.ObjectExpr> args = new ArrayList<>(2); List<Expr.DynamicExpr> args = new ArrayList<>(2);
if (!check(Token.RightParen)) { if (!check(Token.RightParen)) {
do { do {
args.add(expression().asObjectExpr()); args.add(expression().asDynamicExpr());
} while (match(Token.Comma)); } while (match(Token.Comma));
} }
consume(Token.RightParen, "Expected ')' after function arguments."); consume(Token.RightParen, "Expected ')' after function arguments.");
return new Call(callee.asObjectExpr(), args); return new Call(callee.asDynamicExpr(), args);
} }
private Expr<?> primary() { private Expr<?> primary() {

View File

@ -1,5 +1,5 @@
package io.gitlab.jfronny.muscript.compiler; package io.gitlab.jfronny.muscript.compiler;
public enum Type { public enum Type {
String, Number, Boolean, Object String, Number, Boolean, Dynamic
} }

View File

@ -1,22 +1,21 @@
package io.gitlab.jfronny.muscript.compiler.expr; package io.gitlab.jfronny.muscript.compiler.expr;
import io.gitlab.jfronny.muscript.compiler.*; import io.gitlab.jfronny.muscript.compiler.*;
import io.gitlab.jfronny.muscript.optic.*; import io.gitlab.jfronny.muscript.dynamic.*;
import java.util.*; import java.util.*;
import java.util.stream.*;
public class Call extends Expr.ObjectExpr { public class Call extends Expr.DynamicExpr {
private final ObjectExpr left; private final DynamicExpr left;
private final List<ObjectExpr> args; private final List<DynamicExpr> args;
public Call(ObjectExpr left, List<ObjectExpr> args) { public Call(DynamicExpr left, List<DynamicExpr> args) {
this.left = left; this.left = left;
this.args = args; this.args = args;
} }
@Override @Override
public OAny<?> get(OAny<?> branch, OAny<?> dataRoot) { public Dynamic<?> get(Dynamic<?> branch, Dynamic<?> dataRoot) {
return left.get(branch, dataRoot).asCallable().getValue().apply(OFinal.of(args.stream().map(e -> e.get(dataRoot, dataRoot)).toArray(OAny[]::new))); return left.get(branch, dataRoot).asCallable().getValue().apply(DFinal.of(args.stream().map(e -> e.get(dataRoot, dataRoot)).toArray(Dynamic[]::new)));
} }
} }

View File

@ -1,7 +1,7 @@
package io.gitlab.jfronny.muscript.compiler.expr; package io.gitlab.jfronny.muscript.compiler.expr;
import io.gitlab.jfronny.muscript.compiler.*; import io.gitlab.jfronny.muscript.compiler.*;
import io.gitlab.jfronny.muscript.optic.*; import io.gitlab.jfronny.muscript.dynamic.*;
public class Compare extends Expr.BoolExpr { public class Compare extends Expr.BoolExpr {
private final NumberExpr left; private final NumberExpr left;
@ -17,7 +17,7 @@ public class Compare extends Expr.BoolExpr {
} }
@Override @Override
public Boolean get(OAny<?> branch, OAny<?> dataRoot) { public Boolean get(Dynamic<?> branch, Dynamic<?> dataRoot) {
double left = this.left.get(branch, dataRoot); double left = this.left.get(branch, dataRoot);
double right = this.right.get(branch, dataRoot); double right = this.right.get(branch, dataRoot);
return switch (comparator) { return switch (comparator) {

View File

@ -1,7 +1,7 @@
package io.gitlab.jfronny.muscript.compiler.expr; package io.gitlab.jfronny.muscript.compiler.expr;
import io.gitlab.jfronny.muscript.compiler.*; import io.gitlab.jfronny.muscript.compiler.*;
import io.gitlab.jfronny.muscript.optic.*; import io.gitlab.jfronny.muscript.dynamic.*;
public class Concatenate extends Expr.StringExpr { public class Concatenate extends Expr.StringExpr {
private final StringExpr left; private final StringExpr left;
@ -13,7 +13,7 @@ public class Concatenate extends Expr.StringExpr {
} }
@Override @Override
public String get(OAny<?> branch, OAny<?> dataRoot) { public String get(Dynamic<?> branch, Dynamic<?> dataRoot) {
return left.get(branch, dataRoot) + right.get(branch, dataRoot); return left.get(branch, dataRoot) + right.get(branch, dataRoot);
} }
} }

View File

@ -1,7 +1,7 @@
package io.gitlab.jfronny.muscript.compiler.expr; package io.gitlab.jfronny.muscript.compiler.expr;
import io.gitlab.jfronny.muscript.compiler.*; import io.gitlab.jfronny.muscript.compiler.*;
import io.gitlab.jfronny.muscript.optic.*; import io.gitlab.jfronny.muscript.dynamic.*;
public class Conditional extends Expr { public class Conditional extends Expr {
public final BoolExpr condition; public final BoolExpr condition;
@ -22,7 +22,7 @@ public class Conditional extends Expr {
} }
@Override @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); 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(); BoolExpr falseExpr = this.falseExpr.asBoolExpr();
return new BoolExpr() { return new BoolExpr() {
@Override @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); 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(); StringExpr falseExpr = this.falseExpr.asStringExpr();
return new StringExpr() { return new StringExpr() {
@Override @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); 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(); NumberExpr falseExpr = this.falseExpr.asNumberExpr();
return new NumberExpr() { return new NumberExpr() {
@Override @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); return condition.get(branch, dataRoot) ? trueExpr.get(branch, dataRoot) : falseExpr.get(branch, dataRoot);
} }
}; };
} }
@Override @Override
public ObjectExpr asObjectExpr() { public DynamicExpr asDynamicExpr() {
ObjectExpr trueExpr = this.trueExpr.asObjectExpr(); DynamicExpr trueExpr = this.trueExpr.asDynamicExpr();
ObjectExpr falseExpr = this.falseExpr.asObjectExpr(); DynamicExpr falseExpr = this.falseExpr.asDynamicExpr();
return new ObjectExpr() { return new DynamicExpr() {
@Override @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); return condition.get(branch, dataRoot) ? trueExpr.get(branch, dataRoot) : falseExpr.get(branch, dataRoot);
} }
}; };

View File

@ -1,7 +1,7 @@
package io.gitlab.jfronny.muscript.compiler.expr; package io.gitlab.jfronny.muscript.compiler.expr;
import io.gitlab.jfronny.muscript.compiler.*; import io.gitlab.jfronny.muscript.compiler.*;
import io.gitlab.jfronny.muscript.optic.*; import io.gitlab.jfronny.muscript.dynamic.*;
import java.util.*; import java.util.*;
@ -15,12 +15,12 @@ public class Equal extends Expr.BoolExpr {
} }
@Override @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))); return Objects.equals(unwrap(left.get(branch, dataRoot)), unwrap(right.get(branch, dataRoot)));
} }
private Object unwrap(Object o) { 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; return o;
} }
} }

View File

@ -1,26 +1,26 @@
package io.gitlab.jfronny.muscript.compiler.expr; package io.gitlab.jfronny.muscript.compiler.expr;
import io.gitlab.jfronny.muscript.compiler.*; import io.gitlab.jfronny.muscript.compiler.*;
import io.gitlab.jfronny.muscript.optic.*; import io.gitlab.jfronny.muscript.dynamic.*;
public class Get extends Expr.ObjectExpr { public class Get extends Expr.DynamicExpr {
private final ObjectExpr left; private final DynamicExpr left;
private final Expr<?> name; private final Expr<?> name;
public Get(ObjectExpr left, Expr<?> name) { public Get(DynamicExpr left, Expr<?> name) {
this.left = left; this.left = left;
this.name = name; 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"); throw new IllegalArgumentException("Name must be either a string or a number");
} }
} }
@Override @Override
public OAny<?> get(OAny<?> branch, OAny<?> dataRoot) { public Dynamic<?> get(Dynamic<?> branch, Dynamic<?> dataRoot) {
OAny<?> left = this.left.get(branch, dataRoot); Dynamic<?> left = this.left.get(branch, dataRoot);
if (left instanceof OObject o) { if (left instanceof DObject o) {
return o.get(name.asStringExpr().get(dataRoot, dataRoot)); 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()); 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"); throw new IllegalArgumentException("The element to get value \"" + name.asStringExpr().get(dataRoot, dataRoot) + "\" from is not of a valid type");

View File

@ -1,7 +1,7 @@
package io.gitlab.jfronny.muscript.compiler.expr; package io.gitlab.jfronny.muscript.compiler.expr;
import io.gitlab.jfronny.muscript.compiler.*; import io.gitlab.jfronny.muscript.compiler.*;
import io.gitlab.jfronny.muscript.optic.*; import io.gitlab.jfronny.muscript.dynamic.*;
public class Group extends Expr { public class Group extends Expr {
public final Expr expr; public final Expr expr;
@ -16,7 +16,7 @@ public class Group extends Expr {
} }
@Override @Override
public Object get(OAny branch, OAny dataRoot) { public Object get(Dynamic branch, Dynamic dataRoot) {
return expr.get(branch, dataRoot); return expr.get(branch, dataRoot);
} }
@ -36,7 +36,7 @@ public class Group extends Expr {
} }
@Override @Override
public ObjectExpr asObjectExpr() { public DynamicExpr asDynamicExpr() {
return expr.asObjectExpr(); return expr.asDynamicExpr();
} }
} }

View File

@ -1,7 +1,7 @@
package io.gitlab.jfronny.muscript.compiler.expr; package io.gitlab.jfronny.muscript.compiler.expr;
import io.gitlab.jfronny.muscript.compiler.*; import io.gitlab.jfronny.muscript.compiler.*;
import io.gitlab.jfronny.muscript.optic.*; import io.gitlab.jfronny.muscript.dynamic.*;
public class Invert extends Expr.NumberExpr { public class Invert extends Expr.NumberExpr {
private final Expr.NumberExpr inner; private final Expr.NumberExpr inner;
@ -11,7 +11,7 @@ public class Invert extends Expr.NumberExpr {
} }
@Override @Override
public Double get(OAny<?> branch, OAny<?> dataRoot) { public Double get(Dynamic<?> branch, Dynamic<?> dataRoot) {
return -inner.get(branch, dataRoot); return -inner.get(branch, dataRoot);
} }
} }

View File

@ -1,7 +1,7 @@
package io.gitlab.jfronny.muscript.compiler.expr; package io.gitlab.jfronny.muscript.compiler.expr;
import io.gitlab.jfronny.muscript.compiler.*; import io.gitlab.jfronny.muscript.compiler.*;
import io.gitlab.jfronny.muscript.optic.*; import io.gitlab.jfronny.muscript.dynamic.*;
public class LogicBiExpr extends Expr.BoolExpr { public class LogicBiExpr extends Expr.BoolExpr {
private final Expr.BoolExpr left; private final Expr.BoolExpr left;
@ -17,7 +17,7 @@ public class LogicBiExpr extends Expr.BoolExpr {
} }
@Override @Override
public Boolean get(OAny<?> branch, OAny<?> dataRoot) { public Boolean get(Dynamic<?> branch, Dynamic<?> dataRoot) {
boolean left = this.left.get(branch, dataRoot); boolean left = this.left.get(branch, dataRoot);
boolean right = this.right.get(branch, dataRoot); boolean right = this.right.get(branch, dataRoot);
return switch (comparator) { return switch (comparator) {

View File

@ -1,7 +1,7 @@
package io.gitlab.jfronny.muscript.compiler.expr; package io.gitlab.jfronny.muscript.compiler.expr;
import io.gitlab.jfronny.muscript.compiler.*; import io.gitlab.jfronny.muscript.compiler.*;
import io.gitlab.jfronny.muscript.optic.*; import io.gitlab.jfronny.muscript.dynamic.*;
public class MathBiExpr extends Expr.NumberExpr { public class MathBiExpr extends Expr.NumberExpr {
private final NumberExpr left; private final NumberExpr left;
@ -17,7 +17,7 @@ public class MathBiExpr extends Expr.NumberExpr {
} }
@Override @Override
public Double get(OAny<?> branch, OAny<?> dataRoot) { public Double get(Dynamic<?> branch, Dynamic<?> dataRoot) {
double left = this.left.get(branch, dataRoot); double left = this.left.get(branch, dataRoot);
double right = this.right.get(branch, dataRoot); double right = this.right.get(branch, dataRoot);
return switch (comparator) { return switch (comparator) {

View File

@ -1,7 +1,7 @@
package io.gitlab.jfronny.muscript.compiler.expr; package io.gitlab.jfronny.muscript.compiler.expr;
import io.gitlab.jfronny.muscript.compiler.*; import io.gitlab.jfronny.muscript.compiler.*;
import io.gitlab.jfronny.muscript.optic.*; import io.gitlab.jfronny.muscript.dynamic.*;
public class Not extends Expr.BoolExpr { public class Not extends Expr.BoolExpr {
private final BoolExpr inner; private final BoolExpr inner;
@ -11,7 +11,7 @@ public class Not extends Expr.BoolExpr {
} }
@Override @Override
public Boolean get(OAny<?> branch, OAny<?> dataRoot) { public Boolean get(Dynamic<?> branch, Dynamic<?> dataRoot) {
return !inner.get(branch, dataRoot); return !inner.get(branch, dataRoot);
} }
} }

View File

@ -1,9 +1,9 @@
package io.gitlab.jfronny.muscript.compiler.expr; package io.gitlab.jfronny.muscript.compiler.expr;
import io.gitlab.jfronny.muscript.compiler.*; 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; private final String name;
public Variable(String name) { public Variable(String name) {
@ -11,10 +11,10 @@ public class Variable extends Expr.ObjectExpr {
} }
@Override @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); if (branch.asObject().has(name)) return branch.asObject().get(name);
else if (name.contains("::")) { else if (name.contains("::")) {
OAny<?> res = branch; Dynamic<?> res = branch;
for (String s : name.split("::")) { for (String s : name.split("::")) {
if (!res.asObject().has(s)) if (!res.asObject().has(s))
throw new IllegalArgumentException("This object doesn't contain that name"); throw new IllegalArgumentException("This object doesn't contain that name");

View File

@ -1,6 +1,6 @@
package io.gitlab.jfronny.muscript.debug; package io.gitlab.jfronny.muscript.debug;
import io.gitlab.jfronny.muscript.optic.*; import io.gitlab.jfronny.muscript.dynamic.*;
import java.lang.reflect.*; import java.lang.reflect.*;
import java.util.*; import java.util.*;
@ -32,7 +32,7 @@ public class ObjectGraphPrinter {
writer.writeLine(name + " = \"" + fo + "\""); writer.writeLine(name + " = \"" + fo + "\"");
} else if (kz.isAssignableFrom(Double.class)) { } else if (kz.isAssignableFrom(Double.class)) {
writer.writeLine(name + " = " + fo); writer.writeLine(name + " = " + fo);
} else if (kz.isAssignableFrom(OAny.class)) { } else if (kz.isAssignableFrom(Dynamic.class)) {
writer.writeLine(name + " = " + fo); writer.writeLine(name + " = " + fo);
} else if (kz.isAssignableFrom(Collection.class)) { } else if (kz.isAssignableFrom(Collection.class)) {
writer.writeLine(name + ":"); writer.writeLine(name + ":");

View File

@ -0,0 +1,4 @@
package io.gitlab.jfronny.muscript.dynamic;
public interface DBool extends Dynamic<Boolean> {
}

View File

@ -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));
}
}

View File

@ -1,8 +1,8 @@
package io.gitlab.jfronny.muscript.optic; package io.gitlab.jfronny.muscript.dynamic;
import io.gitlab.jfronny.commons.StringFormatter; 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; private T value;
@Override @Override

View File

@ -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);
}
}
}

View File

@ -1,32 +1,32 @@
package io.gitlab.jfronny.muscript.optic; package io.gitlab.jfronny.muscript.dynamic;
import io.gitlab.jfronny.commons.StringFormatter; import io.gitlab.jfronny.commons.StringFormatter;
import java.util.*; import java.util.*;
import java.util.function.*; import java.util.function.*;
public record OFinal<T>(T value) implements OAny<T> { public record DFinal<T>(T value) implements Dynamic<T> {
public static OBool of(boolean b) { public static DBool of(boolean b) {
return new FBool(b); return new FBool(b);
} }
public static ONumber of(double b) { public static DNumber of(double b) {
return new FNumber(b); return new FNumber(b);
} }
public static OString of(String b) { public static DString of(String b) {
return new FString(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)); return new FObject(Map.copyOf(b));
} }
public static OList of(OAny<?>... b) { public static DList of(Dynamic<?>... b) {
return new FList(List.of(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); return new FCallable(b);
} }
@ -40,7 +40,7 @@ public record OFinal<T>(T value) implements OAny<T> {
return StringFormatter.toString(value); return StringFormatter.toString(value);
} }
private record FBool(boolean value) implements OBool { private record FBool(boolean value) implements DBool {
@Override @Override
public Boolean getValue() { public Boolean getValue() {
return value; 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 @Override
public Double getValue() { public Double getValue() {
return value; 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 @Override
public String getValue() { public String getValue() {
return value; 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 @Override
public Map<String, OAny<?>> getValue() { public Map<String, Dynamic<?>> getValue() {
return value; 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 @Override
public List<OAny<?>> getValue() { public List<Dynamic<?>> getValue() {
return value; 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 @Override
public Function<OList, OAny<?>> getValue() { public Function<DList, Dynamic<?>> getValue() {
return value; return value;
} }

View File

@ -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();
}
}

View File

@ -0,0 +1,8 @@
package io.gitlab.jfronny.muscript.dynamic;
public final class DNull implements Dynamic<Object> {
@Override
public Object getValue() {
return null;
}
}

View File

@ -0,0 +1,4 @@
package io.gitlab.jfronny.muscript.dynamic;
public interface DNumber extends Dynamic<Double> {
}

View File

@ -1,9 +1,9 @@
package io.gitlab.jfronny.muscript.optic; package io.gitlab.jfronny.muscript.dynamic;
import java.util.*; import java.util.*;
public interface OObject extends OAny<Map<String, OAny<?>>> { public interface DObject extends Dynamic<Map<String, Dynamic<?>>> {
default OAny<?> get(String key) { default Dynamic<?> get(String key) {
return getValue().get(key); return getValue().get(key);
} }

View File

@ -0,0 +1,4 @@
package io.gitlab.jfronny.muscript.dynamic;
public interface DString extends Dynamic<String> {
}

View File

@ -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");
}
}

View File

@ -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");
}
}

View File

@ -1,4 +0,0 @@
package io.gitlab.jfronny.muscript.optic;
public interface OBool extends OAny<Boolean> {
}

View File

@ -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));
}
}

View File

@ -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);
}
}
}

View File

@ -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();
}
}

View File

@ -1,8 +0,0 @@
package io.gitlab.jfronny.muscript.optic;
public final class ONull implements OAny<Object> {
@Override
public Object getValue() {
return null;
}
}

View File

@ -1,4 +0,0 @@
package io.gitlab.jfronny.muscript.optic;
public interface ONumber extends OAny<Double> {
}

View File

@ -1,4 +0,0 @@
package io.gitlab.jfronny.muscript.optic;
public interface OString extends OAny<String> {
}

View File

@ -2,11 +2,11 @@ package io.gitlab.jfronny.muscript.test;
import io.gitlab.jfronny.muscript.compiler.*; import io.gitlab.jfronny.muscript.compiler.*;
import io.gitlab.jfronny.muscript.debug.*; import io.gitlab.jfronny.muscript.debug.*;
import io.gitlab.jfronny.muscript.optic.*; import io.gitlab.jfronny.muscript.dynamic.*;
import java.util.*; import java.util.*;
import static io.gitlab.jfronny.muscript.optic.OFinal.*; import static io.gitlab.jfronny.muscript.dynamic.DFinal.*;
public class MuTestUtil { public class MuTestUtil {
public static double number(String source) { public static double number(String source) {
@ -31,7 +31,7 @@ public class MuTestUtil {
return Parser.parse(source).asStringExpr().get(makeArgs()); return Parser.parse(source).asStringExpr().get(makeArgs());
} }
public static OAny<?> makeArgs() { public static Dynamic<?> makeArgs() {
return of(Map.of( return of(Map.of(
"boolean", of(true), "boolean", of(true),
"number", of(15), "number", of(15),