Search in sources :

Example 61 with AviatorObject

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

the class AviatorEvaluatorInstanceUnitTest method testFunctionMissing.

@Test
public void testFunctionMissing() {
    this.instance.setFunctionMissing(new FunctionMissing() {

        @Override
        public AviatorObject onFunctionMissing(final String name, final Map<String, Object> env, final AviatorObject... args) {
            // Returns the missing function name.
            return new AviatorString(name);
        }
    });
    assertEquals("test", this.instance.execute("test()"));
    assertEquals("abc", this.instance.execute("abc(1,2)"));
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AviatorString(com.googlecode.aviator.runtime.type.AviatorString) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AviatorString(com.googlecode.aviator.runtime.type.AviatorString) Test(org.junit.Test)

Example 62 with AviatorObject

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

the class CustomDivideExample method main.

public static void main(final String[] args) {
    AviatorEvaluator.getInstance().addOpFunction(OperatorType.DIV, new AbstractFunction() {

        @Override
        public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1, final AviatorObject arg2) {
            if (arg1.getAviatorType() == AviatorType.Long && arg2.getAviatorType() == AviatorType.Long) {
                // If arg1 and arg2 are all long type.
                // Cast arg2 into double and divided by arg1.
                double d = FunctionUtils.getNumberValue(arg1, env).longValue() / FunctionUtils.getNumberValue(arg2, env).doubleValue();
                return AviatorDouble.valueOf(d);
            } else {
                // Otherwise, call aviatorscript's div function.
                return arg1.div(arg2, env);
            }
        }

        @Override
        public String getName() {
            return OperatorType.DIV.getToken();
        }
    });
    System.out.println(AviatorEvaluator.execute("1/2"));
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AbstractFunction(com.googlecode.aviator.runtime.function.AbstractFunction) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject)

Example 63 with AviatorObject

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

the class OperationRuntime method eval.

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

Example 64 with AviatorObject

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

the class OptimizeCodeGenerator method executeOperator.

private int executeOperator(final Map<Integer, DelegateTokenType> index2DelegateType, final Token<?> operatorToken, final OperatorType operatorType, final int operatorIndex, int operandCount) {
    Token<?> token = null;
    operandCount += index2DelegateType.size();
    // check if literal expression can be executed
    boolean canExecute = true;
    // operand count
    int count = 0;
    // operand start index
    int operandStartIndex = -1;
    for (int j = operatorIndex - 1; j >= 0; j--) {
        token = this.tokenList.get(j);
        if (token == null) {
            // we must compact token list and retry executing
            return -1;
        }
        final TokenType tokenType = token.getType();
        // Check if operand is a literal operand
        if (!isLiteralOperand(token, tokenType, count + 1, index2DelegateType)) {
            canExecute = false;
            break;
        }
        count++;
        if (count == operandCount) {
            operandStartIndex = j;
            break;
        }
    }
    // if we can execute it on compile
    if (canExecute) {
        // arguments
        AviatorObject[] args = new AviatorObject[operandCount];
        int index = 0;
        for (int j = operandStartIndex; j < operatorIndex; j++) {
            token = this.tokenList.get(j);
            if (token.getType() == TokenType.Delegate) {
                this.tokenList.set(j, null);
                continue;
            }
            args[index++] = getAviatorObjectFromToken(token);
            // set argument token to null
            this.tokenList.set(j, null);
        }
        AviatorObject result = OperationRuntime.eval(getCompileEnv(), args, operatorType);
        // set result as token to tokenList for next executing
        this.tokenList.set(operatorIndex, getTokenFromOperand(operatorToken, result));
        return 1;
    }
    return 0;
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) TokenType(com.googlecode.aviator.lexer.token.Token.TokenType) DelegateTokenType(com.googlecode.aviator.lexer.token.DelegateToken.DelegateTokenType)

Example 65 with AviatorObject

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

the class BranchIfIR method eval.

@Override
public void eval(final InterpretContext context) {
    AviatorObject top = context.peek();
    if (top.booleanValue(context.getEnv())) {
        context.jumpTo(this.pc);
        context.dispatch(false);
    } else {
        context.dispatch();
    }
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject)

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