Search in sources :

Example 6 with TypeCheckException

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

the class TypeCheckingVisitor method visit.

/** {@inheritDoc} */
@Override
public void visit(final SimpleExpr n, final SymbolTable env) {
    n.env = env;
    n.getLhs().accept(this, env);
    BoaType type = n.getLhs().type;
    // only allow '+' (concat) on arrays
    if (type instanceof BoaArray) {
        for (final String s : n.getOps()) if (!s.equals("+"))
            throw new TypeCheckException(n, "arrays do not support the '" + s + "' arithmetic operator, perhaps you meant '+'?");
        final BoaType valType = ((BoaArray) type).getType();
        for (final Term t : n.getRhs()) {
            t.accept(this, env);
            if (!(t.type instanceof BoaArray) || !valType.assigns(((BoaArray) t.type).getType()))
                throw new TypeCheckException(t, "invalid array concatenation, found: " + t.type + " expected: " + type);
        }
    // only allow '+' (concat) on strings
    } else if (type instanceof BoaString) {
        for (final String s : n.getOps()) if (!s.equals("+"))
            throw new TypeCheckException(n, "strings do not support the '" + s + "' arithmetic operator, perhaps you meant '+'?");
        for (final Term t : n.getRhs()) {
            t.accept(this, env);
            if (!(t.type instanceof BoaString))
                throw new TypeCheckException(t, "invalid string concatenation, found: " + t.type + " expected: string");
        }
    } else
        for (int i = 0; i < n.getRhsSize(); i++) {
            final Term t = n.getRhs(i);
            t.accept(this, env);
            try {
                type = type.arithmetics(t.type);
            } catch (final Exception e) {
                throw new TypeCheckException(t, "type '" + t.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 7 with TypeCheckException

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

the class CodeGeneratingVisitor method visit.

/** {@inheritDoc} */
@Override
public void visit(final FunctionType n) {
    final ST st = stg.getInstanceOf("FunctionType");
    if (!(n.type instanceof BoaFunction))
        throw new TypeCheckException(n, "type " + n.type + " is not a function type");
    final BoaFunction funcType = ((BoaFunction) n.type);
    final BoaType[] paramTypes = funcType.getFormalParameters();
    final List<String> args = new ArrayList<String>();
    final List<String> types = new ArrayList<String>();
    for (int i = 0; i < paramTypes.length; i++) {
        args.add(((BoaName) paramTypes[i]).getId());
        types.add(paramTypes[i].toJavaType());
    }
    st.add("name", funcType.toJavaType());
    if (funcType.getType() instanceof BoaAny)
        st.add("ret", "void");
    else
        st.add("ret", funcType.getType().toBoxedJavaType());
    st.add("args", args);
    st.add("types", types);
    code.add(st.render());
}
Also used : ST(org.stringtemplate.v4.ST) TypeCheckException(boa.compiler.TypeCheckException)

Example 8 with TypeCheckException

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

the class TypeCheckingVisitor method visit.

/** {@inheritDoc} */
@Override
public void visit(final EmitStatement n, final SymbolTable env) {
    hasEmit = true;
    n.env = env;
    n.getId().accept(this, env);
    final String id = n.getId().getToken();
    final BoaType type = n.getId().type;
    if (type == null)
        throw new TypeCheckException(n.getId(), "emitting to undeclared output variable '" + id + "'");
    if (!(type instanceof BoaTable))
        throw new TypeCheckException(n.getId(), "emitting to non-output variable '" + id + "'");
    final BoaTable t = (BoaTable) type;
    if (n.getIndicesSize() != t.countIndices())
        throw new TypeCheckException(n.getId(), "output variable '" + id + "': incorrect number of indices for '" + id + "': required " + t.countIndices() + ", found " + n.getIndicesSize());
    if (n.getIndicesSize() > 0)
        for (int i = 0; i < n.getIndicesSize() && i < t.countIndices(); i++) {
            n.getIndice(i).accept(this, env);
            if (!t.getIndex(i).assigns(n.getIndice(i).type))
                throw new TypeCheckException(n.getIndice(i), "output variable '" + id + "': incompatible types for index '" + i + "': required '" + t.getIndex(i) + "', found '" + n.getIndice(i).type + "'");
        }
    n.getValue().accept(this, env);
    if (!t.accepts(n.getValue().type))
        throw new TypeCheckException(n.getValue(), "output variable '" + id + "': incompatible emit value types: required '" + t.getType() + "', found '" + n.getValue().type + "'");
    if (n.hasWeight()) {
        if (t.getWeightType() == null)
            throw new TypeCheckException(n.getWeight(), "output variable '" + id + "': emit contains a weight, but variable not declared with a weight");
        n.getWeight().accept(this, env);
        if (!t.acceptsWeight(n.getWeight().type))
            throw new TypeCheckException(n.getWeight(), "output variable '" + id + "': incompatible types for weight: required '" + t.getWeightType() + "', found '" + n.getWeight().type + "'");
    } else if (t.getWeightType() != null && !t.canOmitWeight())
        throw new TypeCheckException(n, "output variable '" + id + "': emit must specify a weight");
}
Also used : TypeCheckException(boa.compiler.TypeCheckException)

Example 9 with TypeCheckException

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

the class TypeCheckingVisitor method visit.

/** {@inheritDoc} */
@Override
public void visit(final OutputType n, final SymbolTable env) {
    n.env = env;
    List<BoaScalar> indexTypes = null;
    if (n.getIndicesSize() > 0) {
        indexTypes = new ArrayList<BoaScalar>();
        for (final Component c : n.getIndices()) {
            c.accept(this, env);
            if (!(c.type instanceof BoaScalar))
                throw new TypeCheckException(c, "incorrect type '" + c.type + "' for index");
            indexTypes.add((BoaScalar) c.type);
        }
    }
    n.getType().accept(this, env);
    final BoaType type = n.getType().type;
    final AggregatorSpec annotation;
    try {
        annotation = env.getAggregators(n.getId().getToken(), type).get(0).getAnnotation(AggregatorSpec.class);
    } catch (final RuntimeException e) {
        throw new TypeCheckException(n, e.getMessage(), e);
    }
    BoaScalar tweight = null;
    if (n.hasWeight()) {
        if (annotation.weightType().equals("none"))
            throw new TypeCheckException(n.getWeight(), "output aggregator '" + n.getId().getToken() + "' does not expect a weight");
        final BoaType aweight = SymbolTable.getType(annotation.weightType());
        n.getWeight().accept(this, env);
        tweight = (BoaScalar) n.getWeight().type;
        if (!aweight.assigns(tweight))
            throw new TypeCheckException(n.getWeight(), "invalid weight type, found: " + tweight + " expected: " + aweight);
    } else if (!annotation.weightType().equals("none") && !annotation.weightType().equals("any"))
        throw new TypeCheckException(n, "output aggregator expects a weight type");
    if (n.getArgsSize() > 0 && annotation.formalParameters().length == 0)
        throw new TypeCheckException(n.getArgs(), "output aggregator '" + n.getId().getToken() + "' takes no arguments");
    n.type = new BoaTable(type, indexTypes, tweight, annotation.canOmitWeight());
    env.set(n.getId().getToken(), n.type);
    n.getId().accept(this, env);
}
Also used : AggregatorSpec(boa.aggregators.AggregatorSpec) TypeCheckException(boa.compiler.TypeCheckException)

Example 10 with TypeCheckException

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

the class TypeCheckingVisitor method visit.

/** {@inheritDoc} */
@Override
public void visit(final VisitStatement 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.hasComponent()) {
        st.setShadowing(true);
        n.getComponent().accept(this, st);
        if (n.getComponent().type instanceof BoaName)
            n.getComponent().type = n.getComponent().getType().type;
        st.setShadowing(false);
    } 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);
        }
    st.setIsVisitor(true);
    n.getBody().accept(this, st);
    st.unsetIsVisitor();
}
Also used : SymbolTable(boa.compiler.SymbolTable) IOException(java.io.IOException) 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