Package webit.script.core.ast.expressions

Source Code of webit.script.core.ast.expressions.FunctionDeclarePart

// 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<String> 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<String>();
        varmgr.push();
        start = varmgr.assignVariant("arguments", line, column);
    }

    public FunctionDeclarePart appendArgs(List<Identifer> 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<LoopInfo> loopInfos = StatementUtil.collectPossibleLoopsInfo(statements);
        if (loopInfos != null) {
            for (Iterator<LoopInfo> 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);
    }
}
TOP

Related Classes of webit.script.core.ast.expressions.FunctionDeclarePart

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.