Search in sources :

Example 1 with AviatorObject

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

the class FunctionTest method testOverloadLogicOperator.

@Test
public void testOverloadLogicOperator() {
    // instance.setOption(Options.TRACE_EVAL, true);
    this.instance.addOpFunction(OperatorType.AND, new AbstractFunction() {

        @Override
        public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1, final AviatorObject arg2) {
            return arg1.add(arg2, env);
        }

        @Override
        public String getName() {
            return "&&";
        }
    });
    this.instance.addOpFunction(OperatorType.OR, new AbstractFunction() {

        @Override
        public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1, final AviatorObject arg2) {
            return arg1.sub(arg2, env);
        }

        @Override
        public String getName() {
            return "||";
        }
    });
    assertEquals(3, this.instance.execute("1 && 2"));
    assertEquals(6, this.instance.execute("1 && 2 && 3"));
    assertEquals(0, this.instance.execute("1 && 2 || 3"));
    assertEquals(-4, this.instance.execute("1 || 2 || 3"));
    this.instance.removeOpFunction(OperatorType.AND);
    this.instance.removeOpFunction(OperatorType.OR);
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AbstractFunction(com.googlecode.aviator.runtime.function.AbstractFunction) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) Test(org.junit.Test)

Example 2 with AviatorObject

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

the class GrammarUnitTest method testReturnNullCustomFunction.

@Test
public void testReturnNullCustomFunction() {
    this.instance.addFunction(new AbstractFunction() {

        @Override
        public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1) {
            try {
                return AviatorLong.valueOf(FunctionUtils.getJavaObject(arg1, env));
            } catch (Exception e) {
                return null;
            }
        }

        @Override
        public String getName() {
            return "SafeCastLong";
        }
    });
    assertNull(this.instance.execute("SafeCastLong('a')"));
}
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) CompareNotSupportedException(com.googlecode.aviator.exception.CompareNotSupportedException) CompileExpressionErrorException(com.googlecode.aviator.exception.CompileExpressionErrorException) ExpressionSyntaxErrorException(com.googlecode.aviator.exception.ExpressionSyntaxErrorException) ExpressionRuntimeException(com.googlecode.aviator.exception.ExpressionRuntimeException) Test(org.junit.Test)

Example 3 with AviatorObject

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

the class OperatorFunctionTest method testCustomUnaryOperatorFunction.

@Test
public void testCustomUnaryOperatorFunction() {
    try {
        assertEquals("3", this.instance.exec("!3"));
        fail();
    } catch (Exception e) {
    }
    this.instance.addOpFunction(OperatorType.NOT, new AbstractFunction() {

        @Override
        public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1) {
            return new AviatorString(arg1.getValue(env).toString());
        }

        @Override
        public String getName() {
            return "!";
        }
    });
    assertEquals("3", this.instance.exec("!3"));
    assertEquals("4", this.instance.exec("!a", 4));
    assertEquals("3.2", this.instance.exec("!3.2"));
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AbstractFunction(com.googlecode.aviator.runtime.function.AbstractFunction) 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 4 with AviatorObject

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

the class InterpretContext method descOperandsStack.

public String descOperandsStack() {
    StringBuilder sb = new StringBuilder("<Stack, [");
    int i = this.operands.size();
    for (AviatorObject obj : this.operands) {
        sb.append(obj.desc(this.env));
        if (--i > 0) {
            sb.append(", ");
        }
    }
    sb.append("]>");
    return sb.toString();
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject)

Example 5 with AviatorObject

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

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