Search in sources :

Example 21 with TypeCheckException

use of boa.compiler.TypeCheckException 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 22 with TypeCheckException

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

the class TypeCheckingVisitor method visit.

/** {@inheritDoc} */
@Override
public void visit(final VisitorExpression n, final SymbolTable env) {
    n.env = env;
    n.getType().accept(this, env);
    for (final Statement s : n.getBody().getStatements()) if (!(s instanceof VisitStatement))
        throw new TypeCheckException(s, "only 'before' or 'after' visit statements are allowed inside visitor bodies");
    visitorChecker.start(n);
    n.getBody().accept(this, env);
    n.type = n.getType().type;
    final VisitorDesugar desugar = new VisitorDesugar();
    desugar.start(n);
}
Also used : VisitorDesugar(boa.compiler.transforms.VisitorDesugar) TypeCheckException(boa.compiler.TypeCheckException)

Example 23 with TypeCheckException

use of boa.compiler.TypeCheckException 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 24 with TypeCheckException

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

the class TypeCheckingVisitor method visit.

/** {@inheritDoc} */
@Override
public void visit(final Term n, final SymbolTable env) {
    n.env = env;
    n.getLhs().accept(this, env);
    final BoaType accepts = n.getLhs().type;
    n.type = accepts;
    if (n.getRhsSize() > 0) {
        BoaScalar type;
        if (accepts instanceof BoaFunction)
            type = (BoaScalar) ((BoaFunction) accepts).getType();
        else
            type = (BoaScalar) accepts;
        for (int i = 0; i < n.getRhsSize(); i++) {
            final Factor f = n.getRhs(i);
            f.accept(this, env);
            try {
                type = type.arithmetics(f.type);
            } catch (final Exception e) {
                throw new TypeCheckException(f, "type '" + f.type + "' does not support the '" + n.getOp(i) + "' operator", e);
            }
        }
        n.type = type;
    }
}
Also used : TypeCheckException(boa.compiler.TypeCheckException) IOException(java.io.IOException) TypeCheckException(boa.compiler.TypeCheckException)

Example 25 with TypeCheckException

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

the class TypeCheckingVisitor method visit.

//
// statements
//
/** {@inheritDoc} */
@Override
public void visit(final AssignmentStatement n, final SymbolTable env) {
    n.env = env;
    n.getLhs().accept(this, env);
    n.getRhs().accept(this, env);
    if (!(n.getLhs().type instanceof BoaArray && n.getRhs().type instanceof BoaTuple))
        if (!n.getLhs().type.assigns(n.getRhs().type))
            throw new TypeCheckException(n.getRhs(), "incompatible types for assignment: required '" + n.getLhs().type + "', found '" + n.getRhs().type + "'");
    if (n.getLhs().getOperand().type instanceof BoaProtoTuple && n.getLhs().getOpsSize() > 0)
        throw new TypeCheckException(n.getLhs(), "assignment not allowed to input-derived type '" + n.getLhs().getOperand().type + "'");
    final Factor f = n.getRhs().getLhs().getLhs().getLhs().getLhs().getLhs();
    if (f.getOperand() instanceof Identifier && f.getOpsSize() == 0 && env.hasType(((Identifier) f.getOperand()).getToken()))
        throw new TypeCheckException(n.getRhs(), "type '" + f.getOperand().type + "' is not a value and can not be assigned");
    n.type = n.getLhs().type;
}
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