Search in sources :

Example 6 with SymbolTable

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

the class TypeCheckingVisitor method visit.

/** {@inheritDoc} */
@Override
public void visit(final WhileStatement 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);
    n.getBody().accept(this, st);
}
Also used : SymbolTable(boa.compiler.SymbolTable) IOException(java.io.IOException)

Example 7 with SymbolTable

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

the class TypeCheckingVisitor method visit.

/** {@inheritDoc} */
@Override
public void visit(final FixPStatement n, final SymbolTable env) {
    SymbolTable st;
    try {
        st = env.cloneNonLocals();
    } catch (final IOException e) {
        throw new RuntimeException(e.getClass().getSimpleName() + " caught", e);
    }
    BoaType ret = new BoaAny();
    if (n.getReturnType() != null) {
        n.getReturnType().accept(this, env);
        ret = n.getReturnType().type;
    }
    n.type = new BoaFunction(ret, new BoaType[] {});
    //System.out.println(st.locals);
    n.env = st;
    n.getParam1().accept(this, st);
    if (n.getParam1().type instanceof BoaName)
        n.getParam1().type = n.getParam1().getType().type;
    n.getParam2().accept(this, st);
    if (n.getParam2().type instanceof BoaName)
        n.getParam2().type = n.getParam2().getType().type;
    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)

Example 8 with SymbolTable

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

the class TypeCheckingVisitor method visit.

/** {@inheritDoc} */
@Override
public void visit(final ForStatement 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;
    if (n.hasInit())
        n.getInit().accept(this, st);
    if (n.hasCondition())
        n.getCondition().accept(this, st);
    if (n.hasUpdate())
        n.getUpdate().accept(this, st);
    n.getBody().accept(this, st);
}
Also used : SymbolTable(boa.compiler.SymbolTable) IOException(java.io.IOException)

Example 9 with SymbolTable

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

the class TypeCheckingVisitor method visit.

/** {@inheritDoc} */
@Override
public void visit(final FunctionExpression 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.getType().accept(this, st);
    if (!(n.getType().type instanceof BoaFunction))
        throw new TypeCheckException(n.getType(), "the identifier '" + n.getType() + "' must be a function type");
    final BoaFunction t = (BoaFunction) n.getType().type;
    n.type = t;
    st.setIsVisitor(false);
    n.getBody().accept(this, st);
    st.unsetIsVisitor();
    returnFinder.initialize(t.getType());
    returnFinder.start(n.getBody());
    if (!(t.getType() instanceof BoaAny) && (n.getBody().getStatementsSize() == 0 || !(n.getBody().getStatement(n.getBody().getStatementsSize() - 1) instanceof ReturnStatement)))
        throw new TypeCheckException(n.getBody(), "missing return statement");
}
Also used : SymbolTable(boa.compiler.SymbolTable) IOException(java.io.IOException) TypeCheckException(boa.compiler.TypeCheckException)

Example 10 with SymbolTable

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

the class BaseTest method typecheck.

protected StartContext typecheck(final String input, final String error) throws IOException {
    final StartContext ctx = parse(input);
    try {
        new TypeCheckingVisitor().start(ctx.ast, new SymbolTable());
        if (error != null)
            fail("expected error: " + error);
    } catch (final Exception e) {
        if (error == null)
            fail("found unexpected error: " + e.getMessage());
        else
            assertEquals(error, e.getMessage());
    }
    return ctx;
}
Also used : StartContext(boa.parser.BoaParser.StartContext) SymbolTable(boa.compiler.SymbolTable) TypeCheckingVisitor(boa.compiler.visitors.TypeCheckingVisitor) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) IOException(java.io.IOException) RecognitionException(org.antlr.v4.runtime.RecognitionException)

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