Search in sources :

Example 1 with AviatorJavaType

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

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

the class ASMCodeGeneratorUnitTest method testOnLambdaDefine.

@Test
public void testOnLambdaDefine() throws Exception {
    this.codeGenerator.setParser(new Parser() {

        @Override
        public void setCodeGenerator(final CodeGenerator codeGenerator) {
        }

        @Override
        public void restoreScope(final ScopeInfo info) {
        }

        @Override
        public SymbolTable getSymbolTable() {
            return null;
        }

        @Override
        public CodeGenerator getCodeGenerator() {
            return ASMCodeGeneratorUnitTest.this.codeGenerator;
        }

        @Override
        public ScopeInfo enterScope(final boolean inForLoop) {
            return null;
        }
    });
    assertNull(this.codeGenerator.getLambdaGenerator());
    this.codeGenerator.onLambdaDefineStart(new Variable("lambda", 0, 0));
    LambdaGenerator lambdaGenerator = this.codeGenerator.getLambdaGenerator();
    assertNotNull(lambdaGenerator);
    this.codeGenerator.onLambdaArgument(new Variable("x", 0, 1), new FunctionParam(0, "x", false));
    this.codeGenerator.onLambdaArgument(new Variable("y", 0, 2), new FunctionParam(1, "y", false));
    this.codeGenerator.onLambdaBodyStart(new Variable(">", 0, 3));
    lambdaGenerator.onConstant(new Variable("x", 0, 4));
    lambdaGenerator.onConstant(new Variable("y", 0, 5));
    lambdaGenerator.onAdd(null);
    this.codeGenerator.onLambdaBodyEnd(new Variable("end", 0, 7));
    HashMap<String, Object> env = new HashMap<String, Object>();
    env.put("x", 2);
    env.put("y", 3);
    Object result = eval(env);
    assertTrue(result instanceof LambdaFunction);
    assertEquals(5, ((LambdaFunction) result).call(env, new AviatorJavaType("x"), new AviatorJavaType("y")).getValue(env));
    assertNull(this.codeGenerator.getLambdaGenerator());
}
Also used : LambdaGenerator(com.googlecode.aviator.code.LambdaGenerator) Variable(com.googlecode.aviator.lexer.token.Variable) HashMap(java.util.HashMap) SymbolTable(com.googlecode.aviator.lexer.SymbolTable) ScopeInfo(com.googlecode.aviator.parser.ScopeInfo) LambdaFunction(com.googlecode.aviator.runtime.function.LambdaFunction) CodeGenerator(com.googlecode.aviator.code.CodeGenerator) Parser(com.googlecode.aviator.parser.Parser) AviatorJavaType(com.googlecode.aviator.runtime.type.AviatorJavaType) FunctionParam(com.googlecode.aviator.runtime.FunctionParam) Test(org.junit.Test)

Example 3 with AviatorJavaType

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

the class UseFunction method variadicCall.

/**
 * use package.{class1, class2};
 */
@Override
public AviatorObject variadicCall(final Map<String, Object> env, final AviatorObject... args) {
    if (args.length < 3) {
        throw new IllegalArgumentException("Wrong arguments(" + args.length + ") passed to __use variadicCall");
    }
    if (args[0].getAviatorType() != AviatorType.JavaType) {
        throw new IllegalArgumentException("Can't use other aviator type except varaible");
    }
    final String packageSym = ((AviatorJavaType) args[0]).getName();
    assert (env instanceof Env);
    final Env theEnv = (Env) env;
    for (int i = 1; i < args.length; i++) {
        if (args[i].getAviatorType() != AviatorType.JavaType) {
            throw new IllegalArgumentException("Can't use other aviator type except varaible");
        }
        final String name = ((AviatorJavaType) args[i]).getName();
        addSym(theEnv, packageSym, name);
    }
    return AviatorNil.NIL;
}
Also used : AviatorJavaType(com.googlecode.aviator.runtime.type.AviatorJavaType) Env(com.googlecode.aviator.utils.Env)

Example 4 with AviatorJavaType

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

the class UseFunction method call.

/**
 * use package.* or use.package.{class};
 */
@Override
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1, final AviatorObject arg2) {
    if (arg1.getAviatorType() != AviatorType.JavaType) {
        throw new IllegalArgumentException("Can't use other aviator type except varaible");
    }
    if (arg2.getAviatorType() != AviatorType.JavaType) {
        throw new IllegalArgumentException("Can't use other aviator type except varaible");
    }
    final String packageSym = ((AviatorJavaType) arg1).getName();
    final String name = ((AviatorJavaType) arg2).getName();
    assert (env instanceof Env);
    final Env theEnv = (Env) env;
    addSym(theEnv, packageSym, name);
    return AviatorNil.NIL;
}
Also used : AviatorJavaType(com.googlecode.aviator.runtime.type.AviatorJavaType) Env(com.googlecode.aviator.utils.Env)

Example 5 with AviatorJavaType

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

the class NewInstanceFunction method variadicCall.

@Override
public AviatorObject variadicCall(final Map<String, Object> env, final AviatorObject... args) {
    if (args == null || args.length == 0) {
        throw new IllegalArgumentException("Missing className for new");
    }
    AviatorObject firstArg = args[0];
    if (firstArg.getAviatorType() != AviatorType.JavaType) {
        throw new IllegalArgumentException("Invalid class name: " + firstArg.desc(env));
    }
    String className = ((AviatorJavaType) firstArg).getName();
    try {
        assert (env instanceof Env);
        Class<?> clazz = ((Env) env).resolveClassSymbol(className);
        Constructor<?>[] constructors = clazz.getConstructors();
        final Object[] constructArgs = new Object[args.length - 1];
        for (int i = 1; i < args.length; i++) {
            constructArgs[i - 1] = args[i].getValue(env);
        }
        Constructor<?> bestMatch = null;
        for (Constructor<?> constructor : constructors) {
            final Class<?>[] pTypes = constructor.getParameterTypes();
            if (pTypes.length == constructArgs.length) {
                if (Reflector.isCongruent(pTypes, constructArgs)) {
                    bestMatch = constructor;
                    for (int i = 0; i < constructArgs.length; i++) {
                        constructArgs[i] = Reflector.boxArg(pTypes[i], constructArgs[i]);
                    }
                    break;
                }
            }
        }
        if (bestMatch == null) {
            throw new IllegalStateException("Could not find constructor for class " + className + " with arguments: " + Arrays.toString(constructArgs));
        }
        return AviatorRuntimeJavaType.valueOf(bestMatch.newInstance(constructArgs));
    } catch (Throwable t) {
        throw Reflector.sneakyThrow(t);
    }
}
Also used : Constructor(java.lang.reflect.Constructor) Env(com.googlecode.aviator.utils.Env) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AviatorJavaType(com.googlecode.aviator.runtime.type.AviatorJavaType) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject)

Aggregations

AviatorJavaType (com.googlecode.aviator.runtime.type.AviatorJavaType)45 Test (org.junit.Test)29 AviatorObject (com.googlecode.aviator.runtime.type.AviatorObject)25 HashMap (java.util.HashMap)16 AviatorString (com.googlecode.aviator.runtime.type.AviatorString)11 AviatorFunction (com.googlecode.aviator.runtime.type.AviatorFunction)10 Env (com.googlecode.aviator.utils.Env)10 FunctionNotFoundException (com.googlecode.aviator.exception.FunctionNotFoundException)6 LinkedList (java.util.LinkedList)4 BigDecimal (java.math.BigDecimal)3 BigInteger (java.math.BigInteger)3 FunctionParam (com.googlecode.aviator.runtime.FunctionParam)2 BinaryFunction (com.googlecode.aviator.runtime.function.system.BinaryFunction)2 Collector (com.googlecode.aviator.runtime.type.Collector)2 Sequence (com.googlecode.aviator.runtime.type.Sequence)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 LinkedHashSet (java.util.LinkedHashSet)2 List (java.util.List)2 AviatorEvaluatorInstance (com.googlecode.aviator.AviatorEvaluatorInstance)1 CodeGenerator (com.googlecode.aviator.code.CodeGenerator)1