Search in sources :

Example 11 with SymbolTable

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

the class TypeCheckingVisitor method visit.

/** {@inheritDoc} */
@Override
public void visit(final Program n, final SymbolTable env) {
    SymbolTable st;
    try {
        st = env.cloneNonLocals();
    } catch (final IOException e) {
        throw new RuntimeException(e.getClass().getSimpleName() + " caught", e);
    }
    n.env = st;
    for (final Statement s : n.getStatements()) s.accept(this, env);
}
Also used : SymbolTable(boa.compiler.SymbolTable) IOException(java.io.IOException)

Example 12 with SymbolTable

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

the class TypeCheckingVisitor method visit.

/** {@inheritDoc} */
@Override
public void visit(final SwitchStatement n, final SymbolTable env) {
    SymbolTable st;
    try {
        st = env.cloneNonLocals();
    } catch (final IOException e) {
        throw new RuntimeException(e.getClass().getSimpleName() + " caught", e);
    }
    n.env = st;
    n.getCondition().accept(this, st);
    final BoaType expr = n.getCondition().type;
    if (!(expr instanceof BoaInt) && !(expr instanceof BoaProtoMap) && !(expr instanceof BoaEnum))
        throw new TypeCheckException(n.getCondition(), "incompatible types for switch expression: required 'int' or 'enum', found: " + expr);
    for (final SwitchCase sc : n.getCases()) {
        sc.accept(this, st);
        for (final Expression e : sc.getCases()) if (!expr.assigns(e.type))
            throw new TypeCheckException(e, "incompatible types for case expression: required '" + expr + "', found '" + e.type + "'");
    }
    n.getDefault().accept(this, st);
}
Also used : SymbolTable(boa.compiler.SymbolTable) IOException(java.io.IOException) TypeCheckException(boa.compiler.TypeCheckException)

Example 13 with SymbolTable

use of boa.compiler.SymbolTable 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)

Aggregations

SymbolTable (boa.compiler.SymbolTable)13 IOException (java.io.IOException)12 TypeCheckException (boa.compiler.TypeCheckException)4 TypeCheckingVisitor (boa.compiler.visitors.TypeCheckingVisitor)1 StartContext (boa.parser.BoaParser.StartContext)1 RecognitionException (org.antlr.v4.runtime.RecognitionException)1 ParseCancellationException (org.antlr.v4.runtime.misc.ParseCancellationException)1