use of com.googlecode.aviator.lexer.token.Variable in project aviatorscript by killme2008.
the class ASMCodeGeneratorUnitTest method testOnConstant_Variable.
@Test
public void testOnConstant_Variable() throws Exception {
this.codeGenerator.onConstant(new Variable("a", 0, 0));
Expression exp = this.codeGenerator.getResult(true);
HashMap<String, Object> env = new HashMap<String, Object>();
long now = System.currentTimeMillis();
env.put("a", now);
Object result = exp.execute(env);
assertEquals(now, result);
}
use of com.googlecode.aviator.lexer.token.Variable in project aviatorscript by killme2008.
the class ASMCodeGeneratorUnitTest method doBitOpTests.
public void doBitOpTests(final OperatorType operatorType) throws Exception {
NumberToken a = new NumberToken(99L, "3");
NumberToken b = new NumberToken(2, "2");
Map<String, Object> env = new HashMap<String, Object>();
env.put("c", 9L);
this.codeGenerator.onConstant(new Variable("c", 0, 7));
this.codeGenerator.onConstant(a);
this.codeGenerator.onConstant(b);
switch(operatorType) {
case BIT_OR:
this.codeGenerator.onBitOr(null);
this.codeGenerator.onBitOr(null);
Object result = eval(env);
assertEquals(7 | 3 | 2, result);
break;
case BIT_AND:
this.codeGenerator.onBitAnd(null);
this.codeGenerator.onBitAnd(null);
result = eval(env);
assertEquals(7 & 3 & 2, result);
break;
case BIT_XOR:
this.codeGenerator.onBitXor(null);
this.codeGenerator.onBitXor(null);
result = eval(env);
assertEquals(7 ^ 3 ^ 2, result);
break;
case SHIFT_LEFT:
this.codeGenerator.onShiftLeft(null);
this.codeGenerator.onShiftLeft(null);
result = eval(env);
assertEquals(7 << 3 << 2, result);
break;
case SHIFT_RIGHT:
this.codeGenerator.onShiftRight(null);
this.codeGenerator.onShiftRight(null);
result = eval(env);
assertEquals(7 >> 3 >> 2, result);
break;
case U_SHIFT_RIGHT:
this.codeGenerator.onUnsignedShiftRight(null);
this.codeGenerator.onUnsignedShiftRight(null);
result = eval(env);
assertEquals(7 >>> 3 >>> 2, result);
break;
}
}
use of com.googlecode.aviator.lexer.token.Variable in project aviatorscript by killme2008.
the class ASMCodeGeneratorUnitTest method doLogicOpTest.
public void doLogicOpTest(final OperatorType operatorType) throws Exception {
NumberToken a = new NumberToken(3L, "3");
NumberToken b = new NumberToken(3L, "3");
Map<String, Object> env = new HashMap<String, Object>();
env.put("c", 9L);
switch(operatorType) {
case EQ:
this.codeGenerator.onConstant(a);
this.codeGenerator.onConstant(b);
this.codeGenerator.onEq(null);
Object result = eval(env);
assertEquals(Boolean.TRUE, result);
break;
case NEQ:
this.codeGenerator.onConstant(a);
this.codeGenerator.onConstant(b);
this.codeGenerator.onNeq(null);
result = eval(env);
assertEquals(Boolean.FALSE, result);
break;
case GT:
this.codeGenerator.onConstant(a);
this.codeGenerator.onConstant(b);
this.codeGenerator.onGt(null);
result = eval(env);
assertEquals(Boolean.FALSE, result);
break;
case GE:
this.codeGenerator.onConstant(a);
this.codeGenerator.onConstant(b);
this.codeGenerator.onGe(null);
result = eval(env);
assertEquals(Boolean.TRUE, result);
break;
case LT:
this.codeGenerator.onConstant(a);
this.codeGenerator.onConstant(new Variable("c", 0, 0));
this.codeGenerator.onLt(null);
result = eval(env);
assertEquals(Boolean.TRUE, result);
break;
case LE:
this.codeGenerator.onConstant(a);
this.codeGenerator.onConstant(b);
this.codeGenerator.onLe(null);
result = eval(env);
assertEquals(Boolean.TRUE, result);
break;
}
}
use of com.googlecode.aviator.lexer.token.Variable in project aviatorscript by killme2008.
the class ExpressionParser method factor0.
private boolean factor0() {
if (this.lookhead == null) {
reportSyntaxError("illegal token");
}
if (this.lookhead == Variable.END) {
return false;
}
if (expectChar('(')) {
move(true);
this.scope.enterParen();
ternary();
if (expectChar(')')) {
move(true);
this.scope.leaveParen();
}
} else if (this.lookhead.getType() == TokenType.Number || this.lookhead.getType() == TokenType.String || this.lookhead.getType() == TokenType.Variable || this.lookhead == Variable.TRUE || this.lookhead == Variable.FALSE || isOPVariable(this.lookhead)) {
if (this.lookhead.getType() == TokenType.Variable) {
checkVariableName(this.lookhead);
}
// binary operation as variable for seq functions
if (this.lookhead.getType() == TokenType.Char) {
CharToken charToken = (CharToken) this.lookhead;
if (!ExpressionLexer.isBinaryOP(charToken.getCh())) {
reportSyntaxError("unexpect char '" + charToken.getCh() + "'");
}
// make it as variable
this.lookhead = this.lexer.getSymbolTable().reserve(new Variable(charToken.getLexeme(), charToken.getLineNo(), charToken.getStartIndex()));
}
move(true);
// function
Token<?> prev = getPrevToken();
if (prev.getType() == TokenType.Variable && expectChar('(')) {
if (prev == Variable.LAMBDA) {
lambda(false);
} else if (prev == Variable.FN) {
lambda(true);
} else {
method(prev);
}
} else if (prev.getType() == TokenType.Variable) {
if (!arrayAccess()) {
getCodeGeneratorWithTimes().onConstant(prev);
}
} else {
getCodeGeneratorWithTimes().onConstant(prev);
}
} else if (expectChar('/')) {
pattern();
} else if (expectChar('}')) {
return false;
} else {
reportSyntaxError("invalid token");
}
return true;
}
use of com.googlecode.aviator.lexer.token.Variable in project aviatorscript by killme2008.
the class ASMCodeGeneratorUnitTest method doArithOpTest.
public void doArithOpTest(final OperatorType operatorType) throws Exception {
NumberToken a = new NumberToken(3L, "3");
NumberToken b = new NumberToken(3.5d, "3.5");
Map<String, Object> env = new HashMap<String, Object>();
env.put("c", 9L);
this.codeGenerator.onConstant(new Variable("c", 0, 0));
this.codeGenerator.onConstant(a);
this.codeGenerator.onConstant(b);
switch(operatorType) {
case ADD:
this.codeGenerator.onAdd(null);
this.codeGenerator.onAdd(null);
Object result = eval(env);
assertEquals(15.5, (Double) result, 0.001);
break;
case SUB:
this.codeGenerator.onSub(null);
this.codeGenerator.onSub(null);
result = eval(env);
assertEquals(9.5, (Double) result, 0.001);
break;
case MULT:
this.codeGenerator.onMult(null);
this.codeGenerator.onMult(null);
result = eval(env);
assertEquals(94.5, (Double) result, 0.001);
break;
case DIV:
this.codeGenerator.onDiv(null);
this.codeGenerator.onDiv(null);
result = eval(env);
assertEquals(10.50, (Double) result, 0.001);
break;
case MOD:
this.codeGenerator.onMod(null);
this.codeGenerator.onMod(null);
result = eval(env);
assertEquals(0.0, (Double) result, 0.001);
break;
}
}
Aggregations