63 lines
2.0 KiB
Java
63 lines
2.0 KiB
Java
package io.gitlab.jfronny.muscript.data.dynamic.additional;
|
|
|
|
import io.gitlab.jfronny.muscript.ast.DynamicExpr;
|
|
import io.gitlab.jfronny.muscript.ast.dynamic.Call;
|
|
import io.gitlab.jfronny.muscript.ast.dynamic.Variable;
|
|
import io.gitlab.jfronny.muscript.ast.literal.NumberLiteral;
|
|
import io.gitlab.jfronny.muscript.compiler.CodeLocation;
|
|
import io.gitlab.jfronny.muscript.compiler.ExprWriter;
|
|
import io.gitlab.jfronny.muscript.data.dynamic.*;
|
|
import io.gitlab.jfronny.muscript.data.dynamic.lens.DNumberLens;
|
|
import io.gitlab.jfronny.muscript.data.dynamic.lens.DStringLens;
|
|
|
|
import java.io.IOException;
|
|
import java.time.LocalDate;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.function.Supplier;
|
|
|
|
public record DDate(Supplier<LocalDate> date) implements DObject {
|
|
@Override
|
|
public Map<String, Dynamic> getValue() {
|
|
return Map.of(
|
|
"year", DFinal.of(date.get().getYear()),
|
|
"month", DFinal.of(date.get().getMonthValue()),
|
|
"day", DFinal.of(date.get().getDayOfMonth())
|
|
);
|
|
}
|
|
|
|
@Override
|
|
public boolean isString() {
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public DString asString() {
|
|
return new DStringLens(this, this::toString);
|
|
}
|
|
|
|
@Override
|
|
public boolean isNumber() {
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public DNumber asNumber() {
|
|
return new DNumberLens(this, () -> date.get().toEpochDay());
|
|
}
|
|
|
|
@Override
|
|
public DynamicExpr toExpr() {
|
|
return new Call(CodeLocation.NONE, new Variable(CodeLocation.NONE, "date"), List.of(
|
|
new Call.Arg(new NumberLiteral(CodeLocation.NONE, date.get().getYear()).asDynamicExpr(), false),
|
|
new Call.Arg(new NumberLiteral(CodeLocation.NONE, date.get().getMonthValue()).asDynamicExpr(), false),
|
|
new Call.Arg(new NumberLiteral(CodeLocation.NONE, date.get().getDayOfMonth()).asDynamicExpr(), false)
|
|
));
|
|
}
|
|
|
|
@Override
|
|
public String toString() {
|
|
return date.get().toString();
|
|
}
|
|
}
|