use of com.googlecode.aviator.runtime.type.AviatorObject in project aviatorscript by killme2008.
the class FunctionTest method testOverloadLogicOperator.
@Test
public void testOverloadLogicOperator() {
// instance.setOption(Options.TRACE_EVAL, true);
this.instance.addOpFunction(OperatorType.AND, new AbstractFunction() {
@Override
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1, final AviatorObject arg2) {
return arg1.add(arg2, env);
}
@Override
public String getName() {
return "&&";
}
});
this.instance.addOpFunction(OperatorType.OR, new AbstractFunction() {
@Override
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1, final AviatorObject arg2) {
return arg1.sub(arg2, env);
}
@Override
public String getName() {
return "||";
}
});
assertEquals(3, this.instance.execute("1 && 2"));
assertEquals(6, this.instance.execute("1 && 2 && 3"));
assertEquals(0, this.instance.execute("1 && 2 || 3"));
assertEquals(-4, this.instance.execute("1 || 2 || 3"));
this.instance.removeOpFunction(OperatorType.AND);
this.instance.removeOpFunction(OperatorType.OR);
}
use of com.googlecode.aviator.runtime.type.AviatorObject in project aviatorscript by killme2008.
the class GrammarUnitTest method testReturnNullCustomFunction.
@Test
public void testReturnNullCustomFunction() {
this.instance.addFunction(new AbstractFunction() {
@Override
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1) {
try {
return AviatorLong.valueOf(FunctionUtils.getJavaObject(arg1, env));
} catch (Exception e) {
return null;
}
}
@Override
public String getName() {
return "SafeCastLong";
}
});
assertNull(this.instance.execute("SafeCastLong('a')"));
}
use of com.googlecode.aviator.runtime.type.AviatorObject in project aviatorscript by killme2008.
the class OperatorFunctionTest method testCustomUnaryOperatorFunction.
@Test
public void testCustomUnaryOperatorFunction() {
try {
assertEquals("3", this.instance.exec("!3"));
fail();
} catch (Exception e) {
}
this.instance.addOpFunction(OperatorType.NOT, new AbstractFunction() {
@Override
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1) {
return new AviatorString(arg1.getValue(env).toString());
}
@Override
public String getName() {
return "!";
}
});
assertEquals("3", this.instance.exec("!3"));
assertEquals("4", this.instance.exec("!a", 4));
assertEquals("3.2", this.instance.exec("!3.2"));
}
use of com.googlecode.aviator.runtime.type.AviatorObject in project aviatorscript by killme2008.
the class InterpretContext method descOperandsStack.
public String descOperandsStack() {
StringBuilder sb = new StringBuilder("<Stack, [");
int i = this.operands.size();
for (AviatorObject obj : this.operands) {
sb.append(obj.desc(this.env));
if (--i > 0) {
sb.append(", ");
}
}
sb.append("]>");
return sb.toString();
}
use of com.googlecode.aviator.runtime.type.AviatorObject in project aviatorscript by killme2008.
the class OptimizeCodeGenerator method getTokenFromOperand.
/**
* Get token from executing result
*
* @param operand
* @return
*/
private Token<?> getTokenFromOperand(final Token<?> operatorToken, final AviatorObject operand) {
Token<?> token = null;
switch(operand.getAviatorType()) {
case JavaType:
if (operand instanceof AviatorRuntimeJavaType) {
Object val = operand.getValue(null);
if (val == null) {
token = Variable.NIL;
} else if (val instanceof Number) {
token = new NumberToken((Number) val, val.toString(), operatorToken.getLineNo(), operatorToken.getStartIndex());
} else if (val instanceof String || val instanceof Character) {
String s = val.toString();
token = new StringToken(s, operatorToken.getLineNo(), operatorToken.getStartIndex()).withMeta(Constants.INTER_META, s.contains("#"));
} else if (val instanceof Pattern) {
token = new PatternToken(((Pattern) val).pattern(), operatorToken.getLineNo(), operatorToken.getStartIndex());
} else if (val instanceof Boolean) {
token = (boolean) val ? Variable.TRUE : Variable.FALSE;
} else {
throw new CompileExpressionErrorException("Invalid operand:" + operand.desc(null));
}
} else {
throw new CompileExpressionErrorException("Invalid operand:" + operand.desc(null));
}
break;
case Boolean:
token = operand.booleanValue(null) ? Variable.TRUE : Variable.FALSE;
break;
case Nil:
token = Variable.NIL;
break;
case BigInt:
case Decimal:
case Double:
case Long:
final Number value = (Number) operand.getValue(null);
token = new NumberToken(value, value.toString(), operatorToken.getLineNo(), operatorToken.getStartIndex());
break;
case String:
final String str = (String) operand.getValue(null);
token = new StringToken(str, operatorToken.getLineNo(), operatorToken.getStartIndex());
break;
case Pattern:
token = new PatternToken(((AviatorPattern) operand).getPattern().pattern(), operatorToken.getLineNo(), operatorToken.getStartIndex());
break;
}
return token;
}
Aggregations