java-commons/muscript/src/main/java/io/gitlab/jfronny/muscript/ast/NullLiteral.java

63 lines
1.5 KiB
Java
Raw Normal View History

2023-01-20 18:52:57 +01:00
package io.gitlab.jfronny.muscript.ast;
import io.gitlab.jfronny.muscript.compiler.*;
import io.gitlab.jfronny.muscript.data.Scope;
2023-01-20 18:52:57 +01:00
import io.gitlab.jfronny.muscript.data.dynamic.DNull;
import io.gitlab.jfronny.muscript.annotations.CanThrow;
import io.gitlab.jfronny.muscript.ast.literal.DynamicLiteral;
import io.gitlab.jfronny.muscript.error.LocationalException;
import java.io.IOException;
2022-06-13 13:31:54 +02:00
@CanThrow
public final class NullLiteral extends Expr<Object> {
2023-01-20 17:47:41 +01:00
public NullLiteral(int chStart, int chEnd) {
super(Order.Primary, chStart, chEnd);
2022-06-13 13:31:54 +02:00
}
@Override
public Type getResultType() {
return Type.Dynamic;
}
@Override
public Object get(Scope dataRoot) {
return null;
}
@Override
public NullLiteral optimize() {
return this;
}
@Override
public void decompile(ExprWriter writer) throws IOException {
writer.append("null");
}
@Override
public DynamicExpr asDynamicExpr() {
2023-01-20 17:47:41 +01:00
return new DynamicLiteral<>(chStart, chEnd, new DNull());
2022-06-13 13:31:54 +02:00
}
@Override
public NumberExpr asNumberExpr() {
2023-01-20 17:47:41 +01:00
throw new LocationalException(chStart, chEnd, "Attempted to convert null to a number");
2022-06-13 13:31:54 +02:00
}
@Override
public StringExpr asStringExpr() {
2023-01-20 18:52:57 +01:00
return literal(chStart, chEnd, "null");
2022-06-13 13:31:54 +02:00
}
@Override
public BoolExpr asBoolExpr() {
2023-01-20 17:47:41 +01:00
throw new LocationalException(chStart, chEnd, "Attempted to convert null to a boolean");
}
@Override
public boolean equals(Object obj) {
return obj instanceof NullLiteral;
}
}