Search in sources :

Example 1 with AviatorPattern

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

Example 2 with AviatorPattern

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

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

Aggregations

NumberToken (com.googlecode.aviator.lexer.token.NumberToken)3 AviatorObject (com.googlecode.aviator.runtime.type.AviatorObject)3 AviatorPattern (com.googlecode.aviator.runtime.type.AviatorPattern)3 AviatorString (com.googlecode.aviator.runtime.type.AviatorString)3 AviatorNumber (com.googlecode.aviator.runtime.type.AviatorNumber)2 CompileExpressionErrorException (com.googlecode.aviator.exception.CompileExpressionErrorException)1 ExpressionRuntimeException (com.googlecode.aviator.exception.ExpressionRuntimeException)1 PatternToken (com.googlecode.aviator.lexer.token.PatternToken)1 StringToken (com.googlecode.aviator.lexer.token.StringToken)1 AviatorBoolean (com.googlecode.aviator.runtime.type.AviatorBoolean)1 AviatorJavaType (com.googlecode.aviator.runtime.type.AviatorJavaType)1 AviatorRuntimeJavaType (com.googlecode.aviator.runtime.type.AviatorRuntimeJavaType)1 Pattern (java.util.regex.Pattern)1