Search in sources :

Example 6 with AviatorFunction

use of com.googlecode.aviator.runtime.type.AviatorFunction 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 7 with AviatorFunction

use of com.googlecode.aviator.runtime.type.AviatorFunction 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)

Example 8 with AviatorFunction

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

the class SeqEveryFunction method call.

@Override
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1, final AviatorObject arg2) {
    Object first = arg1.getValue(env);
    AviatorFunction fun = FunctionUtils.getFunction(arg2, env, 1);
    if (fun == null) {
        throw new FunctionNotFoundException("There is no function named " + ((AviatorJavaType) arg2).getName());
    }
    if (first == null) {
        return AviatorBoolean.TRUE;
    }
    for (Object obj : RuntimeUtils.seq(first, env)) {
        if (!fun.call(env, AviatorRuntimeJavaType.valueOf(obj)).booleanValue(env)) {
            return AviatorBoolean.FALSE;
        }
    }
    return AviatorBoolean.TRUE;
}
Also used : FunctionNotFoundException(com.googlecode.aviator.exception.FunctionNotFoundException) AviatorFunction(com.googlecode.aviator.runtime.type.AviatorFunction) AviatorJavaType(com.googlecode.aviator.runtime.type.AviatorJavaType) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject)

Example 9 with AviatorFunction

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

the class OperationRuntime method eval.

/**
 * Eval with arguments array.
 *
 * @param args
 * @param opType
 * @return
 */
public static AviatorObject eval(final Map<String, Object> env, final AviatorObject[] args, final OperatorType opType) {
    AviatorFunction func = RuntimeUtils.getInstance(env).getOpFunction(opType);
    AviatorObject ret = eval0(env, args, opType, func);
    if (RuntimeUtils.isTracedEval(env)) {
        trace(env, opType, ret, args);
    }
    return ret;
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AviatorFunction(com.googlecode.aviator.runtime.type.AviatorFunction)

Example 10 with AviatorFunction

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

the class SeqMakePredicateFunctionUnitTest method testMakePredicate.

@Test
public void testMakePredicate() {
    SeqMakePredicateFunFunction fun = new SeqMakePredicateFunFunction("eq", OperatorType.EQ);
    Map<String, Object> env = new HashMap<String, Object>();
    AviatorObject predicateName = fun.call(env, AviatorRuntimeJavaType.valueOf("hello"));
    assertNotNull(predicateName);
    AviatorFunction predicate = (AviatorFunction) predicateName.getValue(env);
    assertNotNull(predicate);
    AviatorObject result = predicate.call(null, AviatorRuntimeJavaType.valueOf("hello"));
    // equals self
    assertTrue(result.booleanValue(null));
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) HashMap(java.util.HashMap) AviatorFunction(com.googlecode.aviator.runtime.type.AviatorFunction) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) Test(org.junit.Test)

Aggregations

AviatorFunction (com.googlecode.aviator.runtime.type.AviatorFunction)29 AviatorObject (com.googlecode.aviator.runtime.type.AviatorObject)16 AviatorJavaType (com.googlecode.aviator.runtime.type.AviatorJavaType)10 FunctionNotFoundException (com.googlecode.aviator.exception.FunctionNotFoundException)6 HashMap (java.util.HashMap)6 Test (org.junit.Test)6 Map (java.util.Map)5 BinaryFunction (com.googlecode.aviator.runtime.function.system.BinaryFunction)2 Collector (com.googlecode.aviator.runtime.type.Collector)2 Sequence (com.googlecode.aviator.runtime.type.Sequence)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 IdentityHashMap (java.util.IdentityHashMap)2 FunctionMap (org.casbin.jcasbin.model.FunctionMap)2 AviatorEvaluatorInstance (com.googlecode.aviator.AviatorEvaluatorInstance)1 Expression (com.googlecode.aviator.Expression)1 Value (com.googlecode.aviator.Options.Value)1 OperatorIR (com.googlecode.aviator.code.interpreter.ir.OperatorIR)1 ExpressionRuntimeException (com.googlecode.aviator.exception.ExpressionRuntimeException)1 OperatorType (com.googlecode.aviator.lexer.token.OperatorType)1