添加 IN 、NOTIN 运算符
如 5 IN [4,5,6] 结果 是 true
添加运算符 或 重写原运算符
// 扩展的
AviatorEvaluator.addOpFunction(OperatorType.IN,new AviatorInFunction());
AviatorEvaluator.addOpFunction(OperatorType.NOTIN,new AviatorNotInFunction());
// 重写的
AviatorEvaluator.addOpFunction(OperatorType.ADD,new AviatorAddFunction());
AviatorEvaluator.addOpFunction(OperatorType.EQ,new AviatorEqFunction());
实现运算符的运算过程
public class AviatorInFunction extends AbstractFunction {
@Override
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1,
final AviatorObject arg2) {
return AviatorBoolean.valueOf(ExtendFunctionUtil.inFunction(env, arg1, arg2));
}
@Override
public String getName() {
return "IN";
}
}
重写 原先的类 加进去运算符
public enum OperatorType {
IN("IN", 2),
NOTIN("NOTIN", 2),
BIT_OR("|", 2),
BIT_AND("&", 2),
......
}
public interface CodeGenerator {
.....
public void onAdd(Token<?> lookhead);
public void onIn(Token<?> lookhead);
public void onNotIn(Token<?> lookhead);
.....
}
根据需要 调整优先级:
public class ExpressionParser implements Parser {
...
public void expr() {
inOrNotin();
while (true) {
Token<?> opToken = this.lookhead;
if (expectChar('+')) {
move(true);
inOrNotin();
getCodeGeneratorWithTimes().onAdd(opToken);
} else if (expectChar('-')) {
move(true);
inOrNotin();
getCodeGeneratorWithTimes().onSub(opToken);
} else {
break;
}
}
}
public void inOrNotin() {
term();
while (true) {
Token<?> opToken = this.lookhead;
if (opToken!=null && OperatorType.IN.getToken().equals(opToken.getLexeme())) {
move(true);
term();
getCodeGeneratorWithTimes().onIn(opToken);
}else if (opToken!=null && OperatorType.NOTIN.getToken().equals(opToken.getLexeme())) {
move(true);
term();
getCodeGeneratorWithTimes().onNotIn(opToken);
}else{
break;
}
}
}
...
}
CodeGenerator 的 子类 好像也得重写