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