Map 定义初始化表达式,添加支持 key 可以为 int、long、float、double、true、false、null 常量

This commit is contained in:
James
2017-12-10 19:40:40 +08:00
parent bd1949fc56
commit e0abc218b1
6 changed files with 117 additions and 68 deletions

View File

@@ -20,54 +20,25 @@ import com.jfinal.template.expr.Sym;
import com.jfinal.template.stat.Scope;
/**
* STR INT LONG FLOAT DOUBLE true false null
* STR INT LONG FLOAT DOUBLE TRUE FALSE NULL
*/
public class Const extends Expr {
public static final Const TRUE = new Const(Boolean.TRUE, Sym.TRUE);
public static final Const FALSE = new Const(Boolean.FALSE, Sym.FALSE);
public static final Const NULL = new Const(null, Sym.NULL);
public static final Const TRUE = new Const(Sym.TRUE, Boolean.TRUE);
public static final Const FALSE = new Const(Sym.FALSE, Boolean.FALSE);
public static final Const NULL = new Const(Sym.NULL, null);
private Sym type;
private Object value;
private final Sym type;
private final Object value;
/**
* 构造 TRUE FALSE NULL 常量,无需对 value 进行转换
* INT LONG FLOAT DOUBLE 常量已在 NumTok 中转换成了确切的类型,无需再次转换
*/
private Const(Object value, Sym type) {
public Const(Sym type, Object value) {
this.type = type;
this.value = value;
}
public Const(Sym type, String value) {
this.type = type;
this.value = typeConvert(type, value);
}
private Object typeConvert(Sym type, String value) {
switch (type) {
case STR:
return value;
case INT:
return Integer.parseInt(value);
case LONG:
return Long.parseLong(value);
case FLOAT:
return Float.parseFloat(value);
case DOUBLE:
return Double.parseDouble(value);
/*
case TRUE:
case FALSE:
return Boolean.parseBoolean(value);
case NULL:
return null;
*/
default:
throw new RuntimeException("never happend");
}
}
public Object eval(Scope scope) {
return value;
}
@@ -108,6 +79,10 @@ public class Const extends Expr {
return type == Sym.DOUBLE;
}
public boolean isNumber() {
return value instanceof Number;
}
public Object getValue() {
return value;
}
@@ -136,6 +111,10 @@ public class Const extends Expr {
return (Double)value;
}
public Number getNumber() {
return (Number)value;
}
public String toString() {
return value != null ? value.toString() : "null";
}