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