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");
}
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);
}
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);
}
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;
}
}
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;
}
Aggregations