Search in sources :

Example 1 with AviatorRuntimeJavaType

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;
}
Also used : AviatorPattern(com.googlecode.aviator.runtime.type.AviatorPattern) Pattern(java.util.regex.Pattern) PatternToken(com.googlecode.aviator.lexer.token.PatternToken) AviatorNumber(com.googlecode.aviator.runtime.type.AviatorNumber) CompileExpressionErrorException(com.googlecode.aviator.exception.CompileExpressionErrorException) AviatorRuntimeJavaType(com.googlecode.aviator.runtime.type.AviatorRuntimeJavaType) NumberToken(com.googlecode.aviator.lexer.token.NumberToken) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AviatorString(com.googlecode.aviator.runtime.type.AviatorString) AviatorBoolean(com.googlecode.aviator.runtime.type.AviatorBoolean) StringToken(com.googlecode.aviator.lexer.token.StringToken)

Example 2 with AviatorRuntimeJavaType

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;
}
Also used : AviatorEvaluatorInstance(com.googlecode.aviator.AviatorEvaluatorInstance) AviatorRuntimeJavaType(com.googlecode.aviator.runtime.type.AviatorRuntimeJavaType) AviatorFunction(com.googlecode.aviator.runtime.type.AviatorFunction) AviatorJavaType(com.googlecode.aviator.runtime.type.AviatorJavaType) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AviatorString(com.googlecode.aviator.runtime.type.AviatorString)

Aggregations

AviatorObject (com.googlecode.aviator.runtime.type.AviatorObject)2 AviatorRuntimeJavaType (com.googlecode.aviator.runtime.type.AviatorRuntimeJavaType)2 AviatorString (com.googlecode.aviator.runtime.type.AviatorString)2 AviatorEvaluatorInstance (com.googlecode.aviator.AviatorEvaluatorInstance)1 CompileExpressionErrorException (com.googlecode.aviator.exception.CompileExpressionErrorException)1 NumberToken (com.googlecode.aviator.lexer.token.NumberToken)1 PatternToken (com.googlecode.aviator.lexer.token.PatternToken)1 StringToken (com.googlecode.aviator.lexer.token.StringToken)1 AviatorBoolean (com.googlecode.aviator.runtime.type.AviatorBoolean)1 AviatorFunction (com.googlecode.aviator.runtime.type.AviatorFunction)1 AviatorJavaType (com.googlecode.aviator.runtime.type.AviatorJavaType)1 AviatorNumber (com.googlecode.aviator.runtime.type.AviatorNumber)1 AviatorPattern (com.googlecode.aviator.runtime.type.AviatorPattern)1 Pattern (java.util.regex.Pattern)1