use of com.googlecode.aviator.lexer.token.Token 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.Token in project aviatorscript by killme2008.
the class OptimizeCodeGenerator method getResult.
@Override
public Expression getResult(final boolean unboxObject) {
// execute literal expression
while (execute() > 0) {
;
}
Map<String, VariableMeta> /* metadata */
variables = new LinkedHashMap<String, VariableMeta>();
Map<String, Integer> /* counter */
methods = new HashMap<String, Integer>();
Set<Token<?>> constants = new HashSet<>();
for (Token<?> token : this.tokenList) {
if (ExpressionParser.isConstant(token, this.instance)) {
constants.add(token);
}
switch(token.getType()) {
case Variable:
if (SymbolTable.isReservedKeyword((Variable) token)) {
continue;
}
String varName = token.getLexeme();
VariableMeta meta = variables.get(varName);
if (meta == null) {
meta = new VariableMeta((CompileTypes) token.getMeta(Constants.TYPE_META), varName, token.getMeta(Constants.INIT_META, false), token.getStartIndex());
variables.put(varName, meta);
} else {
meta.add(token);
}
break;
case Delegate:
DelegateToken delegateToken = (DelegateToken) token;
if (delegateToken.getDelegateTokenType() == DelegateTokenType.Method_Name) {
Token<?> realToken = delegateToken.getToken();
if (realToken == null) {
continue;
}
if (realToken.getType() == TokenType.Variable) {
String methodName = token.getLexeme();
if (!methods.containsKey(methodName)) {
methods.put(methodName, 1);
} else {
methods.put(methodName, methods.get(methodName) + 1);
}
}
} else if (delegateToken.getDelegateTokenType() == DelegateTokenType.Array) {
Token<?> realToken = delegateToken.getToken();
if (realToken.getType() == TokenType.Variable) {
varName = token.getLexeme();
VariableMeta varMeta = variables.get(varName);
if (varMeta == null) {
varMeta = new VariableMeta((CompileTypes) realToken.getMeta(Constants.TYPE_META), varName, realToken.getMeta(Constants.INIT_META, false), realToken.getStartIndex());
variables.put(varName, varMeta);
} else {
varMeta.add(realToken);
}
}
}
break;
}
}
Expression exp = null;
// Last token is a literal token,then return a LiteralExpression
if (this.tokenList.size() <= 1) {
if (this.tokenList.isEmpty()) {
exp = new LiteralExpression(this.instance, null, new ArrayList<>(variables.values()));
} else {
final Token<?> lastToken = this.tokenList.get(0);
if (ExpressionParser.isLiteralToken(lastToken, this.instance)) {
exp = new LiteralExpression(this.instance, getAviatorObjectFromToken(lastToken).getValue(getCompileEnv()), new ArrayList<>(variables.values()));
}
}
}
if (exp == null) {
// call asm to generate byte codes
callASM(variables, methods, constants);
// get result from asm
exp = this.codeGen.getResult(unboxObject);
}
if (exp instanceof BaseExpression) {
((BaseExpression) exp).setCompileEnv(getCompileEnv());
((BaseExpression) exp).setSourceFile(this.sourceFile);
}
return exp;
}
Aggregations