webit.script.core.ast.expressions.FunctionDeclarePart Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of webit-script Show documentation
Show all versions of webit-script Show documentation
a template-like script and engine, all writen with Java.
The newest version!
// Copyright (c) 2013-2014, Webit Team. All Rights Reserved.
package webit.script.core.ast.expressions;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import webit.script.core.LoopInfo;
import webit.script.core.VariantManager;
import webit.script.core.ast.Expression;
import webit.script.core.ast.Identifer;
import webit.script.core.ast.Statement;
import webit.script.core.ast.StatementList;
import webit.script.core.ast.operators.Assign;
import webit.script.core.ast.statements.Return;
import webit.script.exceptions.ParseException;
import webit.script.util.StatementUtil;
import webit.script.util.StringUtil;
/**
*
* @author Zqq
*/
public class FunctionDeclarePart {
protected final int line;
protected final int column;
private int argsCount = 0;
private final int assignToIndex;
private final int start;
private final VariantManager varmgr;
private final List args;
public FunctionDeclarePart(String assignTo, VariantManager varmgr, int line, int column) {
this(varmgr.assignVariant(assignTo, line, column), varmgr, line, column);
}
public FunctionDeclarePart(VariantManager varmgr, int line, int column) {
this(-1, varmgr, line, column);
}
protected FunctionDeclarePart(int assignToIndex, VariantManager varmgr, int line, int column) {
this.line = line;
this.column = column;
this.varmgr = varmgr;
this.assignToIndex = assignToIndex;
this.args = new ArrayList();
varmgr.push();
start = varmgr.assignVariant("arguments", line, column);
}
public FunctionDeclarePart appendArgs(List identiferList) {
for (Identifer identifer : identiferList) {
appendArg(identifer.name, identifer.line, identifer.column);
}
return this;
}
public FunctionDeclarePart appendArg(String name, int line, int column) {
if (varmgr.assignVariant(name, line, column) != (start + (++this.argsCount))) {
throw new ParseException("Failed to assign vars!");
}
this.args.add(name);
return this;
}
public String getArg(int index) {
return args.get(index);
}
public Expression pop(Expression expr) {
return pop(toStatementList(expr));
}
public FunctionDeclare popFunctionDeclare(Expression expr) {
return popFunctionDeclare(toStatementList(expr));
}
private static StatementList toStatementList(Expression expr) {
StatementList statementList = new StatementList();
statementList.add(new Return(expr, expr.line, expr.column));
return statementList;
}
public Expression pop(StatementList list) {
final Expression expr = popFunctionDeclare(list);
if (this.assignToIndex >= 0) {
return new Assign(new ContextValue(this.assignToIndex, line, column), expr, line, column);
}
return expr;
}
public FunctionDeclare popFunctionDeclare(StatementList list) {
final Statement[] statements = list.toInvertArray();
int varIndexer = varmgr.pop();
boolean hasReturnLoops = false;
List loopInfos = StatementUtil.collectPossibleLoopsInfo(statements);
if (loopInfos != null) {
for (Iterator it = loopInfos.iterator(); it.hasNext();) {
LoopInfo loopInfo = it.next();
if (loopInfo.type == LoopInfo.RETURN) {
hasReturnLoops = true;
it.remove();
}
}
if (!loopInfos.isEmpty()) {
throw new ParseException("Loops overflow in function body: ".concat(StringUtil.join(loopInfos, ',')));
}
}
return new FunctionDeclare(argsCount,
varIndexer,
statements,
start,
hasReturnLoops,
line, column);
}
}