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