Search in sources :

Example 26 with TypeCheckException

use of boa.compiler.TypeCheckException in project compiler by boalang.

the class TypeCheckingVisitor method visit.

/** {@inheritDoc} */
@Override
public void visit(final ReturnStatement n, final SymbolTable env) {
    if (env.getIsVisitor())
        throw new TypeCheckException(n, "return statement not allowed inside visitors");
    n.env = env;
    if (n.hasExpr()) {
        n.getExpr().accept(this, env);
        n.type = n.getExpr().type;
    } else {
        n.type = new BoaAny();
    }
}
Also used : TypeCheckException(boa.compiler.TypeCheckException)

Example 27 with TypeCheckException

use of boa.compiler.TypeCheckException in project compiler by boalang.

the class TypeCheckingVisitor method visit.

/** {@inheritDoc} */
@Override
public void visit(final TraverseStatement n, final SymbolTable env) {
    SymbolTable st;
    try {
        st = env.cloneNonLocals();
    } catch (final IOException e) {
        throw new RuntimeException(e.getClass().getSimpleName() + " caught", e);
    }
    st.setIsTraverse(true);
    BoaType ret = new BoaAny();
    if (n.getReturnType() != null) {
        n.getReturnType().accept(this, env);
        ret = n.getReturnType().type;
    //System.out.println("type   "+ret);
    }
    n.type = new BoaFunction(ret, new BoaType[] {});
    lastRetType = ret;
    n.env = st;
    if (n.hasComponent()) {
        n.getComponent().accept(this, st);
        if (n.getComponent().type instanceof BoaName)
            n.getComponent().type = n.getComponent().getType().type;
    } else if (!n.hasWildcard())
        for (final Identifier id : n.getIdList()) {
            if (SymbolTable.getType(id.getToken()) == null)
                throw new TypeCheckException(id, "Invalid type '" + id.getToken() + "'");
            id.accept(this, st);
        }
    for (final IfStatement ifStatement : n.getIfStatements()) {
        ifStatement.accept(this, st);
    }
    if (n.hasCondition()) {
        n.getCondition().accept(this, st);
    }
    if (n.hasBody()) {
        st.setIsVisitor(false);
        n.getBody().accept(this, st);
        st.unsetIsVisitor();
    }
}
Also used : SymbolTable(boa.compiler.SymbolTable) IOException(java.io.IOException) TypeCheckException(boa.compiler.TypeCheckException)

Example 28 with TypeCheckException

use of boa.compiler.TypeCheckException in project compiler by boalang.

the class TypeCheckingVisitor method visit.

/** {@inheritDoc} */
@Override
public void visit(final Conjunction n, final SymbolTable env) {
    n.env = env;
    n.getLhs().accept(this, env);
    final BoaType ltype = n.getLhs().type;
    n.type = ltype;
    if (n.getRhsSize() > 0) {
        if (!(ltype instanceof BoaBool))
            throw new TypeCheckException(n.getLhs(), "incompatible types for conjunction: required 'bool', found '" + ltype + "'");
        for (final Comparison c : n.getRhs()) {
            c.accept(this, env);
            if (!(c.type instanceof BoaBool))
                throw new TypeCheckException(c, "incompatible types for conjunction: required 'bool', found '" + c.type + "'");
        }
        n.type = new BoaBool();
    }
}
Also used : TypeCheckException(boa.compiler.TypeCheckException)

Aggregations

TypeCheckException (boa.compiler.TypeCheckException)28 IOException (java.io.IOException)7 ST (org.stringtemplate.v4.ST)5 SymbolTable (boa.compiler.SymbolTable)4 AggregatorSpec (boa.aggregators.AggregatorSpec)2 VisitorDesugar (boa.compiler.transforms.VisitorDesugar)1 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1