use of com.googlecode.aviator.runtime.type.AviatorRuntimeJavaType 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;
}
use of com.googlecode.aviator.runtime.type.AviatorRuntimeJavaType in project aviatorscript by killme2008.
the class FunctionUtils method getFunction.
/**
* Get a function from env in follow orders:
* <ul>
* <li>arg value</li>
* <li>env</li>
* <li>current evaluator instance.</li>
* </ul>
*
* @param arg
* @param env
* @param arity
* @return
*/
public static AviatorFunction getFunction(final AviatorObject arg, final Map<String, Object> env, final int arity) {
if (arg.getAviatorType() != AviatorType.JavaType && arg.getAviatorType() != AviatorType.Lambda) {
throw new ClassCastException(arg.desc(env) + " is not a function");
}
// Runtime type.
Object val = null;
if (arg instanceof AviatorFunction) {
return (AviatorFunction) arg;
}
if (arg instanceof AviatorRuntimeJavaType && (val = arg.getValue(env)) instanceof AviatorFunction) {
return (AviatorFunction) val;
}
// resolve by name.
// special processing for "-" operator
String name = ((AviatorJavaType) arg).getName();
if (name.equals("-")) {
if (arity == 2) {
name = "-sub";
} else {
name = "-neg";
}
}
AviatorFunction rt = null;
if (env != null) {
rt = (AviatorFunction) env.get(name);
}
if (rt == null) {
AviatorEvaluatorInstance instance = RuntimeUtils.getInstance(env);
rt = instance.getFunction(name);
}
return rt;
}
Aggregations