Search in sources :

Example 11 with AviatorObject

use of com.googlecode.aviator.runtime.type.AviatorObject in project aviatorscript by killme2008.

the class IfCallccFunction method call.

@Override
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1, final AviatorObject arg2) {
    if (arg1 instanceof ReducerResult) {
        return arg1;
    } else {
        final Object nextClauseVal = arg2.getValue(env);
        if (nextClauseVal == Constants.REDUCER_EMPTY) {
            return arg1;
        }
        AviatorFunction otherClausesFn = (AviatorFunction) nextClauseVal;
        AviatorObject result = otherClausesFn.call(env);
        // No remaining statements, return the if statement result.
        if (result == Constants.REDUCER_EMPTY) {
            return arg1;
        }
        return result;
    }
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AviatorFunction(com.googlecode.aviator.runtime.type.AviatorFunction) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject)

Example 12 with AviatorObject

use of com.googlecode.aviator.runtime.type.AviatorObject in project aviatorscript by killme2008.

the class NewInstanceFunction method variadicCall.

@Override
public AviatorObject variadicCall(final Map<String, Object> env, final AviatorObject... args) {
    if (args == null || args.length == 0) {
        throw new IllegalArgumentException("Missing className for new");
    }
    AviatorObject firstArg = args[0];
    if (firstArg.getAviatorType() != AviatorType.JavaType) {
        throw new IllegalArgumentException("Invalid class name: " + firstArg.desc(env));
    }
    String className = ((AviatorJavaType) firstArg).getName();
    try {
        assert (env instanceof Env);
        Class<?> clazz = ((Env) env).resolveClassSymbol(className);
        Constructor<?>[] constructors = clazz.getConstructors();
        final Object[] constructArgs = new Object[args.length - 1];
        for (int i = 1; i < args.length; i++) {
            constructArgs[i - 1] = args[i].getValue(env);
        }
        Constructor<?> bestMatch = null;
        for (Constructor<?> constructor : constructors) {
            final Class<?>[] pTypes = constructor.getParameterTypes();
            if (pTypes.length == constructArgs.length) {
                if (Reflector.isCongruent(pTypes, constructArgs)) {
                    bestMatch = constructor;
                    for (int i = 0; i < constructArgs.length; i++) {
                        constructArgs[i] = Reflector.boxArg(pTypes[i], constructArgs[i]);
                    }
                    break;
                }
            }
        }
        if (bestMatch == null) {
            throw new IllegalStateException("Could not find constructor for class " + className + " with arguments: " + Arrays.toString(constructArgs));
        }
        return AviatorRuntimeJavaType.valueOf(bestMatch.newInstance(constructArgs));
    } catch (Throwable t) {
        throw Reflector.sneakyThrow(t);
    }
}
Also used : Constructor(java.lang.reflect.Constructor) Env(com.googlecode.aviator.utils.Env) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AviatorJavaType(com.googlecode.aviator.runtime.type.AviatorJavaType) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject)

Example 13 with AviatorObject

use of com.googlecode.aviator.runtime.type.AviatorObject in project aviatorscript by killme2008.

the class ReducerFunction method call.

@Override
public final AviatorObject call(final Map<String, Object> env, final AviatorObject arg1, final AviatorObject arg2, final AviatorObject arg3) {
    Object coll = arg1.getValue(env);
    AviatorFunction iteratorFn = (AviatorFunction) arg2;
    int maxLoopCount = RuntimeUtils.getInstance(env).getOptionValue(Options.MAX_LOOP_COUNT).number;
    AviatorObject result = AviatorNil.NIL;
    long c = 0;
    if (coll != Range.LOOP) {
        long arities = (long) arg2.meta(Constants.ARITIES_META);
        long index = 0;
        boolean unboxEntry = arities == 2 && coll != null && Map.class.isAssignableFrom(coll.getClass());
        // for..in loop
        for (Object obj : RuntimeUtils.seq(coll, env)) {
            if (arities == 1) {
                result = iteratorFn.call(env, AviatorRuntimeJavaType.valueOf(obj));
            } else {
                if (unboxEntry) {
                    Map.Entry<?, ?> entry = (Map.Entry<?, ?>) obj;
                    result = iteratorFn.call(env, AviatorRuntimeJavaType.valueOf(entry.getKey()), AviatorRuntimeJavaType.valueOf(entry.getValue()));
                } else {
                    result = iteratorFn.call(env, AviatorLong.valueOf(index++), AviatorRuntimeJavaType.valueOf(obj));
                }
            }
            if (!(result instanceof ReducerResult)) {
                continue;
            }
            boolean breakOut = false;
            ReducerResult midResult = (ReducerResult) result;
            result = midResult.obj;
            if (midResult.state == ReducerState.Empty) {
                continue;
            }
            switch(midResult.state) {
                case Break:
                    breakOut = true;
                    break;
                case Return:
                    return midResult;
                default:
                    break;
            }
            if (breakOut) {
                break;
            }
        }
    } else {
        // while statement
        while (true) {
            if (maxLoopCount > 0 && ++c > maxLoopCount) {
                throw new ExpressionRuntimeException("Overflow max loop count: " + maxLoopCount);
            }
            result = iteratorFn.call(env);
            if (!(result instanceof ReducerResult)) {
                continue;
            }
            boolean breakOut = false;
            ReducerResult midResult = (ReducerResult) result;
            result = midResult.obj;
            if (midResult.state == ReducerState.Empty) {
                continue;
            }
            switch(midResult.state) {
                case Break:
                    breakOut = true;
                    break;
                case Return:
                    return midResult;
                default:
                    break;
            }
            if (breakOut) {
                break;
            }
        }
    }
    Object contObj = arg3.getValue(env);
    if (contObj == Constants.REDUCER_EMPTY) {
        return result;
    }
    AviatorObject contResult = ((AviatorFunction) contObj).call(env);
    if (contResult == Constants.REDUCER_EMPTY) {
        // empty continuation, return current result.
        return result;
    } else {
        return contResult;
    }
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) ExpressionRuntimeException(com.googlecode.aviator.exception.ExpressionRuntimeException) AviatorFunction(com.googlecode.aviator.runtime.type.AviatorFunction) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) Map(java.util.Map)

Example 14 with AviatorObject

use of com.googlecode.aviator.runtime.type.AviatorObject in project aviatorscript by killme2008.

the class AbstractSeqMinMaxFunction method call.

@SuppressWarnings("rawtypes")
@Override
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1) {
    Object first = arg1.getValue(env);
    if (first == null) {
        return AviatorNil.NIL;
    }
    Sequence seq = RuntimeUtils.seq(first, env);
    boolean wasFirst = true;
    Object result = null;
    for (Object obj : seq) {
        result = compareObjects(result, obj, wasFirst);
        if (wasFirst) {
            wasFirst = false;
        }
        if (getOp() == Op.Min && result == null) {
            break;
        }
    }
    return AviatorRuntimeJavaType.valueOf(result);
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) Sequence(com.googlecode.aviator.runtime.type.Sequence)

Example 15 with AviatorObject

use of com.googlecode.aviator.runtime.type.AviatorObject 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)110 Test (org.junit.Test)51 AviatorJavaType (com.googlecode.aviator.runtime.type.AviatorJavaType)25 AviatorString (com.googlecode.aviator.runtime.type.AviatorString)22 AviatorFunction (com.googlecode.aviator.runtime.type.AviatorFunction)16 HashMap (java.util.HashMap)12 Map (java.util.Map)11 List (java.util.List)10 AbstractFunction (com.googlecode.aviator.runtime.function.AbstractFunction)9 FunctionNotFoundException (com.googlecode.aviator.exception.FunctionNotFoundException)7 Env (com.googlecode.aviator.utils.Env)6 ExpressionRuntimeException (com.googlecode.aviator.exception.ExpressionRuntimeException)5 LinkedList (java.util.LinkedList)5 Collection (java.util.Collection)4 NumberToken (com.googlecode.aviator.lexer.token.NumberToken)3 AviatorPattern (com.googlecode.aviator.runtime.type.AviatorPattern)3 AviatorRuntimeJavaType (com.googlecode.aviator.runtime.type.AviatorRuntimeJavaType)3 Sequence (com.googlecode.aviator.runtime.type.Sequence)3 Date (java.util.Date)3 HashSet (java.util.HashSet)3