Search in sources :

Example 1 with AviatorString

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

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

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

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

the class StringReplaceAllFunction method call.

@Override
public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2, AviatorObject arg3) {
    String target = FunctionUtils.getStringValue(arg1, env);
    if (target == null)
        throw new ExpressionRuntimeException("Could not replace with null string");
    String regex = FunctionUtils.getStringValue(arg2, env);
    String replacement = FunctionUtils.getStringValue(arg3, env);
    return new AviatorString(target.replaceAll(regex, replacement));
}
Also used : ExpressionRuntimeException(com.googlecode.aviator.exception.ExpressionRuntimeException) AviatorString(com.googlecode.aviator.runtime.type.AviatorString) AviatorString(com.googlecode.aviator.runtime.type.AviatorString)

Example 5 with AviatorString

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

the class StringReplaceFirstFunction method call.

@Override
public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2, AviatorObject arg3) {
    String target = FunctionUtils.getStringValue(arg1, env);
    String regex = FunctionUtils.getStringValue(arg2, env);
    String replacement = FunctionUtils.getStringValue(arg3, env);
    return new AviatorString(target.replaceFirst(regex, replacement));
}
Also used : AviatorString(com.googlecode.aviator.runtime.type.AviatorString) AviatorString(com.googlecode.aviator.runtime.type.AviatorString)

Aggregations

AviatorString (com.googlecode.aviator.runtime.type.AviatorString)33 Test (org.junit.Test)19 AviatorObject (com.googlecode.aviator.runtime.type.AviatorObject)14 HashMap (java.util.HashMap)11 AviatorJavaType (com.googlecode.aviator.runtime.type.AviatorJavaType)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 AbstractFunction (com.googlecode.aviator.runtime.function.AbstractFunction)3 Date (java.util.Date)3 List (java.util.List)3 Map (java.util.Map)3 ExpressionRuntimeException (com.googlecode.aviator.exception.ExpressionRuntimeException)2 NumberToken (com.googlecode.aviator.lexer.token.NumberToken)2 AviatorPattern (com.googlecode.aviator.runtime.type.AviatorPattern)2 PrintStream (java.io.PrintStream)2 Collection (java.util.Collection)2 LinkedList (java.util.LinkedList)2 ID (cn.devezhao.persist4j.engine.ID)1 AviatorEvaluator (com.googlecode.aviator.AviatorEvaluator)1 AviatorNil (com.googlecode.aviator.runtime.type.AviatorNil)1 AviatorNumber (com.googlecode.aviator.runtime.type.AviatorNumber)1