java-commons/muscript/src/main/java/io/gitlab/jfronny/muscript/data/dynamic/additional/DCallableObject.java

53 lines
1.6 KiB
Java

package io.gitlab.jfronny.muscript.data.dynamic.additional;
import io.gitlab.jfronny.muscript.ast.DynamicExpr;
import io.gitlab.jfronny.muscript.ast.dynamic.*;
import io.gitlab.jfronny.muscript.compiler.CodeLocation;
import io.gitlab.jfronny.muscript.data.dynamic.*;
import io.gitlab.jfronny.muscript.data.dynamic.lens.DCallableLens;
import io.gitlab.jfronny.muscript.data.dynamic.type.*;
import java.util.*;
public record DCallableObject(Map<String, ? extends Dynamic> value, DTypeObject objectSignature, DCallable callable) implements DObject {
public DCallableObject(Map<String, ? extends Dynamic> value, DCallable callable) {
this(value, new DTypeObject(null), callable);
}
public DCallableObject {
Objects.requireNonNull(value);
Objects.requireNonNull(objectSignature);
Objects.requireNonNull(callable);
}
@Override
public Map<String, ? extends Dynamic> getValue() {
return value;
}
@Override
public boolean isCallable() {
return true;
}
@Override
public DCallable asCallable() {
return new DCallableLens(this, callable::getValue);
}
@Override
public DynamicExpr toExpr() {
return new Call(CodeLocation.NONE, new Bind(CodeLocation.NONE, new Variable(CodeLocation.NONE, "callableObject"), DObject.super.toExpr()), List.of(new Call.Arg(callable.toExpr().asDynamicExpr(), false)));
}
@Override
public DType getSignature() {
return new DTypeAnd(Set.of(objectSignature, callable.getSignature()));
}
@Override
public String toString() {
return Dynamic.serialize(this);
}
}