prompto.literal.Literal Maven / Gradle / Ivy
The newest version!
package prompto.literal;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import prompto.error.PromptoError;
import prompto.expression.IExpression;
import prompto.parser.CodeSection;
import prompto.runtime.Context;
import prompto.utils.CodeWriter;
import prompto.value.IValue;
public abstract class Literal extends CodeSection implements IExpression {
Supplier text;
protected T value;
protected Literal(String text, T value) {
this(()->text, value);
}
protected Literal(Supplier text, T value) {
this.text = text;
this.value = value;
}
@Override
public void toDialect(CodeWriter writer) {
writer.append(escapedText(writer.getEscapeMode()));
}
public String escapedText(int escapeMode) {
String value = text.get();
if(escapeMode <= 0 || !value.contains("'"))
return value;
else {
// can't get regex to work with '
return value.chars()
.mapToObj(c->(String)(c=='\'' ? "\\'" : Character.valueOf((char)c).toString()))
.collect(Collectors.joining());
}
}
@Override
public String toString() {
return text.get();
}
public T getValue() {
return value;
}
@Override
public IValue interpret(Context context) throws PromptoError {
return value;
}
@Override
public int hashCode() {
return value.hashCode();
}
@Override
public boolean equals(Object obj) {
if(obj instanceof Literal)
return value.equals(((Literal>)obj).value);
else
return value.equals(obj);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy