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);
}
}
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());
}
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;
}
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;
}
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);
}
}
Aggregations