Search in sources :

Example 6 with AviatorObject

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

the class OptimizeCodeGenerator method getAviatorObjectFromToken.

private AviatorObject getAviatorObjectFromToken(final Token<?> lookhead) {
    AviatorObject result = null;
    switch(lookhead.getType()) {
        case Number:
            // load numbers
            NumberToken numberToken = (NumberToken) lookhead;
            Number num = numberToken.getNumber();
            result = AviatorNumber.valueOf(num);
            break;
        case String:
            // load string
            result = new AviatorString((String) lookhead.getValue(null), true, true, lookhead.getLineNo());
            break;
        case Pattern:
            // load pattern
            result = new AviatorPattern((String) lookhead.getValue(null));
            break;
        case Variable:
            if (lookhead == Variable.TRUE) {
                result = AviatorBoolean.TRUE;
            } else if (lookhead == Variable.FALSE) {
                result = AviatorBoolean.FALSE;
            } else if (lookhead == Variable.NIL) {
                result = AviatorNil.NIL;
            }
            break;
        case Char:
            result = new AviatorString(String.valueOf(lookhead.getValue(null)), true, true, lookhead.getLineNo());
            break;
    }
    return result;
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AviatorPattern(com.googlecode.aviator.runtime.type.AviatorPattern) AviatorNumber(com.googlecode.aviator.runtime.type.AviatorNumber) AviatorString(com.googlecode.aviator.runtime.type.AviatorString) NumberToken(com.googlecode.aviator.lexer.token.NumberToken) AviatorString(com.googlecode.aviator.runtime.type.AviatorString)

Example 7 with AviatorObject

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

the class LoadIR method evalWithoutDispatch.

public void evalWithoutDispatch(final InterpretContext context) {
    if (this.token == null) {
        return;
    }
    if (this.inConstantPool) {
        final AviatorObject constant = context.loadConstant(this.token);
        assert (constant != null);
        context.push(constant);
        return;
    }
    // load token to stack
    switch(this.token.getType()) {
        case Number:
            // load numbers
            NumberToken numberToken = (NumberToken) this.token;
            Number number = numberToken.getNumber();
            if (TypeUtils.isBigInt(number)) {
                context.push(AviatorBigInt.valueOf(this.token.getLexeme()));
            } else if (TypeUtils.isDecimal(number)) {
                context.push(AviatorDecimal.valueOf(context.getEnv(), this.token.getLexeme()));
            } else if (TypeUtils.isDouble(number)) {
                context.push(AviatorDouble.valueOf(number.doubleValue()));
            } else {
                context.push(AviatorLong.valueOf(number.longValue()));
            }
            break;
        case String:
            context.push(new AviatorString((String) this.token.getValue(null), true, this.token.getMeta(Constants.INTER_META, true), this.token.getLineNo()));
            break;
        case Pattern:
            context.push(new AviatorPattern((String) this.token.getValue(null)));
            break;
        case Variable:
            if (this.token == Variable.TRUE) {
                context.push(AviatorBoolean.TRUE);
            } else if (this.token == Variable.FALSE) {
                context.push(AviatorBoolean.FALSE);
            } else if (this.token == Variable.NIL) {
                context.push(AviatorNil.NIL);
            } else {
                AviatorJavaType var;
                if (this.meta != null) {
                    var = context.loadVar(this.meta);
                    assert (var != null);
                } else {
                    var = new AviatorJavaType(this.token.getLexeme());
                }
                context.push(var);
            }
            break;
        default:
            throw new ExpressionRuntimeException("Can't load " + this.token);
    }
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AviatorPattern(com.googlecode.aviator.runtime.type.AviatorPattern) ExpressionRuntimeException(com.googlecode.aviator.exception.ExpressionRuntimeException) AviatorString(com.googlecode.aviator.runtime.type.AviatorString) NumberToken(com.googlecode.aviator.lexer.token.NumberToken) AviatorJavaType(com.googlecode.aviator.runtime.type.AviatorJavaType) AviatorString(com.googlecode.aviator.runtime.type.AviatorString)

Example 8 with AviatorObject

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

the class AviatorEvaluatorInstanceUnitTest method testIssue245.

@Test
public void testIssue245() {
    // test EQ
    this.instance.addOpFunction(OperatorType.EQ, new AbstractFunction() {

        @Override
        public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1, final AviatorObject arg2) {
            if (!arg1.getValue(env).equals(arg2.getValue(env))) {
                return AviatorBoolean.TRUE;
            }
            return AviatorBoolean.FALSE;
        }

        @Override
        public String getName() {
            return OperatorType.EQ.getToken();
        }
    });
    assertFalse((boolean) this.instance.execute("1==1"));
    assertTrue((boolean) this.instance.execute("0==1"));
    assertFalse((boolean) this.instance.execute("a==b", AviatorEvaluator.newEnv("a", 1, "b", 1)));
    assertTrue((boolean) this.instance.execute("a==b", AviatorEvaluator.newEnv("a", 1, "b", 0)));
    // test Assignment
    assertEquals(1, this.instance.execute("a=b", AviatorEvaluator.newEnv("a", 1, "b", 1)));
    assertEquals(2, this.instance.execute("a=b", AviatorEvaluator.newEnv("a", 1, "b", 2)));
    this.instance.addOpFunction(OperatorType.ASSIGNMENT, new AbstractFunction() {

        @Override
        public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1, final AviatorObject arg2) {
            int c = arg1.compare(arg2, env);
            return AviatorBoolean.valueOf(c == 0);
        }

        @Override
        public String getName() {
            return OperatorType.ASSIGNMENT.getToken();
        }
    });
    assertTrue((boolean) this.instance.execute("1=1"));
    assertFalse((boolean) this.instance.execute("0=1"));
    assertTrue((boolean) this.instance.execute("a=b", AviatorEvaluator.newEnv("a", 1, "b", 1)));
    assertFalse((boolean) this.instance.execute("a=b", AviatorEvaluator.newEnv("a", 1, "b", 0)));
    assertFalse((boolean) this.instance.execute("a=b", AviatorEvaluator.newEnv("a", 1, "b", 2)));
    assertFalse((boolean) this.instance.execute("a=b", AviatorEvaluator.newEnv("a", "hello", "b", "world")));
    assertTrue((boolean) this.instance.execute("a=b", AviatorEvaluator.newEnv("a", "hello", "b", "hello")));
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AbstractFunction(com.googlecode.aviator.runtime.function.AbstractFunction) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AviatorString(com.googlecode.aviator.runtime.type.AviatorString) Test(org.junit.Test)

Example 9 with AviatorObject

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

the class TryCatchFunction method call.

@SuppressWarnings("unchecked")
@Override
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1, final AviatorObject arg2, final AviatorObject arg3, final AviatorObject arg4) {
    AviatorFunction tryBody = (AviatorFunction) arg1;
    List<CatchHandler> catchHandlers = (List<CatchHandler>) arg2.getValue(env);
    AviatorFunction finallyBody = arg3 != AviatorNil.NIL ? (AviatorFunction) arg3 : null;
    AviatorObject result = null;
    try {
        result = tryBody.call(env);
    } catch (Throwable t) {
        boolean handle = false;
        if (catchHandlers != null) {
            for (CatchHandler handler : catchHandlers) {
                if (handler.isMatch(t.getClass())) {
                    AviatorObject ret = handler.getFunc().call(env, AviatorRuntimeJavaType.valueOf(t));
                    result = chooseResult(result, ret);
                    handle = true;
                    break;
                }
            }
        }
        if (!handle) {
            throw Reflector.sneakyThrow(t);
        }
    } finally {
        if (finallyBody != null) {
            AviatorObject ret = finallyBody.call(env);
            result = chooseResult(result, ret);
        }
    }
    if (isReturnResult(result)) {
        return result;
    }
    Object val = arg4.getValue(env);
    if (val == Constants.REDUCER_EMPTY) {
        return result;
    }
    AviatorFunction continueFn = (AviatorFunction) val;
    AviatorObject contResult = continueFn.call(env);
    if (contResult == Constants.REDUCER_EMPTY) {
        return result;
    } else {
        return contResult;
    }
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AviatorFunction(com.googlecode.aviator.runtime.type.AviatorFunction) List(java.util.List) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject)

Example 10 with AviatorObject

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

the class UnpackingArgsFunction method variadicCall.

@Override
public AviatorObject variadicCall(final Map<String, Object> env, final AviatorObject... args) {
    assert (args.length > 0);
    List<AviatorObject> realArgList = new ArrayList<AviatorObject>(args.length + 10);
    for (AviatorObject arg : args) {
        if (arg.meta(Constants.UNPACK_ARGS) != null) {
            for (Object obj : RuntimeUtils.seq(arg.getValue(env), env)) {
                realArgList.add(AviatorRuntimeJavaType.valueOf(obj));
            }
        } else {
            realArgList.add(arg);
        }
    }
    AviatorObject[] realArgs = new AviatorObject[realArgList.size()];
    realArgs = realArgList.toArray(realArgs);
    switch(realArgs.length) {
        case 0:
            return this.fn.call(env);
        case 1:
            return this.fn.call(env, realArgs[0]);
        case 2:
            return this.fn.call(env, realArgs[0], realArgs[1]);
        case 3:
            return this.fn.call(env, realArgs[0], realArgs[1], realArgs[2]);
        case 4:
            return this.fn.call(env, realArgs[0], realArgs[1], realArgs[2], realArgs[3]);
        case 5:
            return this.fn.call(env, realArgs[0], realArgs[1], realArgs[2], realArgs[3], realArgs[4]);
        case 6:
            return this.fn.call(env, realArgs[0], realArgs[1], realArgs[2], realArgs[3], realArgs[4], realArgs[5]);
        case 7:
            return this.fn.call(env, realArgs[0], realArgs[1], realArgs[2], realArgs[3], realArgs[4], realArgs[5], realArgs[6]);
        case 8:
            return this.fn.call(env, realArgs[0], realArgs[1], realArgs[2], realArgs[3], realArgs[4], realArgs[5], realArgs[6], realArgs[7]);
        case 9:
            return this.fn.call(env, realArgs[0], realArgs[1], realArgs[2], realArgs[3], realArgs[4], realArgs[5], realArgs[6], realArgs[7], realArgs[8]);
        case 10:
            return this.fn.call(env, realArgs[0], realArgs[1], realArgs[2], realArgs[3], realArgs[4], realArgs[5], realArgs[6], realArgs[7], realArgs[8], realArgs[9]);
        case 11:
            return this.fn.call(env, realArgs[0], realArgs[1], realArgs[2], realArgs[3], realArgs[4], realArgs[5], realArgs[6], realArgs[7], realArgs[8], realArgs[9], realArgs[10]);
        case 12:
            return this.fn.call(env, realArgs[0], realArgs[1], realArgs[2], realArgs[3], realArgs[4], realArgs[5], realArgs[6], realArgs[7], realArgs[8], realArgs[9], realArgs[10], realArgs[11]);
        case 13:
            return this.fn.call(env, realArgs[0], realArgs[1], realArgs[2], realArgs[3], realArgs[4], realArgs[5], realArgs[6], realArgs[7], realArgs[8], realArgs[9], realArgs[10], realArgs[11], realArgs[12]);
        case 14:
            return this.fn.call(env, realArgs[0], realArgs[1], realArgs[2], realArgs[3], realArgs[4], realArgs[5], realArgs[6], realArgs[7], realArgs[8], realArgs[9], realArgs[10], realArgs[11], realArgs[12], realArgs[13]);
        case 15:
            return this.fn.call(env, realArgs[0], realArgs[1], realArgs[2], realArgs[3], realArgs[4], realArgs[5], realArgs[6], realArgs[7], realArgs[8], realArgs[9], realArgs[10], realArgs[11], realArgs[12], realArgs[13], realArgs[14]);
        case 16:
            return this.fn.call(env, realArgs[0], realArgs[1], realArgs[2], realArgs[3], realArgs[4], realArgs[5], realArgs[6], realArgs[7], realArgs[8], realArgs[9], realArgs[10], realArgs[11], realArgs[12], realArgs[13], realArgs[14], realArgs[15]);
        case 17:
            return this.fn.call(env, realArgs[0], realArgs[1], realArgs[2], realArgs[3], realArgs[4], realArgs[5], realArgs[6], realArgs[7], realArgs[8], realArgs[9], realArgs[10], realArgs[11], realArgs[12], realArgs[13], realArgs[14], realArgs[15], realArgs[16]);
        case 18:
            return this.fn.call(env, realArgs[0], realArgs[1], realArgs[2], realArgs[3], realArgs[4], realArgs[5], realArgs[6], realArgs[7], realArgs[8], realArgs[9], realArgs[10], realArgs[11], realArgs[12], realArgs[13], realArgs[14], realArgs[15], realArgs[16], realArgs[17]);
        case 19:
            return this.fn.call(env, realArgs[0], realArgs[1], realArgs[2], realArgs[3], realArgs[4], realArgs[5], realArgs[6], realArgs[7], realArgs[8], realArgs[9], realArgs[10], realArgs[11], realArgs[12], realArgs[13], realArgs[14], realArgs[15], realArgs[16], realArgs[17], realArgs[18]);
        case 20:
            return this.fn.call(env, realArgs[0], realArgs[1], realArgs[2], realArgs[3], realArgs[4], realArgs[5], realArgs[6], realArgs[7], realArgs[8], realArgs[9], realArgs[10], realArgs[11], realArgs[12], realArgs[13], realArgs[14], realArgs[15], realArgs[16], realArgs[17], realArgs[18], realArgs[19]);
        default:
            assert (realArgs.length >= 20);
            AviatorObject[] remainingArgs = new AviatorObject[realArgs.length - 20];
            System.arraycopy(realArgs, 20, remainingArgs, 0, remainingArgs.length);
            return this.fn.call(env, realArgs[0], realArgs[1], realArgs[2], realArgs[3], realArgs[4], realArgs[5], realArgs[6], realArgs[7], realArgs[8], realArgs[9], realArgs[10], realArgs[11], realArgs[12], realArgs[13], realArgs[14], realArgs[15], realArgs[16], realArgs[17], realArgs[18], realArgs[19], remainingArgs);
    }
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) ArrayList(java.util.ArrayList) 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