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