Search in sources :

Example 1 with TypeCheckException

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

the class CodeGeneratingVisitor method visit.

/** {@inheritDoc} */
@Override
public void visit(final EnumType n) {
    final ST st = stg.getInstanceOf("EnumType");
    if (!(n.type instanceof BoaEnum))
        throw new TypeCheckException(n, "type " + n.type + " is not a enum type");
    final BoaEnum enumType = ((BoaEnum) n.type);
    final BoaType fieldType = enumType.getType();
    final List<String> fields = new ArrayList<String>();
    final List<String> values = new ArrayList<String>();
    for (final EnumBodyDeclaration c : n.getMembers()) {
        Factor f = c.getExp().getLhs().getLhs().getLhs().getLhs().getLhs();
        if (f.getOperand() instanceof ILiteral) {
            code.add(((ILiteral) (f.getOperand())).getLiteral());
            fields.add(c.getIdentifier().getToken());
            values.add(code.removeLast());
        }
    }
    st.add("ename", enumType.toJavaType());
    st.add("fields", fields);
    st.add("values", values);
    st.add("fname", fieldType.toJavaType());
    code.add(st.render());
}
Also used : ST(org.stringtemplate.v4.ST) TypeCheckException(boa.compiler.TypeCheckException)

Example 2 with TypeCheckException

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

the class CodeGeneratingVisitor method visit.

/** {@inheritDoc} */
@Override
public void visit(final Program n) {
    final ST st = stg.getInstanceOf("Job");
    st.add("name", this.name);
    this.varDecl.start(n);
    this.functionDeclarator.start(n);
    this.tupleDeclarator.start(n);
    this.enumDeclarator.start(n);
    if (this.functionDeclarator.hasCode())
        st.add("staticDeclarations", this.varDecl.getCode() + "\n" + this.functionDeclarator.getCode());
    else
        st.add("staticDeclarations", this.varDecl.getCode());
    if (this.tupleDeclarator.hasCode())
        st.add("staticDeclarations", "\n" + this.tupleDeclarator.getCode());
    if (this.enumDeclarator.hasCode())
        st.add("staticDeclarations", "\n" + this.enumDeclarator.getCode());
    this.staticInitialization.start(n);
    if (this.staticInitialization.hasCode())
        st.add("staticStatements", this.staticInitialization.getCode());
    final List<String> statements = new ArrayList<String>();
    for (final Statement s : n.getStatements()) {
        s.accept(this);
        final String statement = code.removeLast();
        if (!statement.isEmpty())
            statements.add(statement);
    }
    st.add("statements", statements);
    if (this.aggregators.size() == 0)
        throw new TypeCheckException(n, "No output variables were declared - must declare at least one output variable");
    for (final Entry<String, AggregatorDescription> entry : this.aggregators.entrySet()) {
        String id = entry.getKey();
        String prefix = name;
        if (id.matches("\\d+_.*")) {
            prefix = id.substring(0, id.indexOf('_'));
            id = id.substring(id.indexOf('_') + 1);
        }
        final AggregatorDescription description = entry.getValue();
        final String parameters = description.getParameters() == null ? "" : description.getParameters().get(0);
        final BoaType type = description.getType();
        final StringBuilder src = new StringBuilder();
        boolean combines = false;
        for (final Class<?> c : n.env.getAggregators(description.getAggregator(), type)) {
            src.append(", new " + c.getCanonicalName() + "(" + parameters + ")");
            try {
                final AggregatorSpec annotation = c.getAnnotation(AggregatorSpec.class);
                if (annotation.canCombine())
                    combines = true;
            } catch (final RuntimeException e) {
                throw new TypeCheckException(n, e.getMessage(), e);
            }
        }
        if (combines)
            combineAggregatorStrings.add("this.aggregators.put(\"" + prefix + "::" + id + "\", " + src.toString().substring(2) + ");");
        reduceAggregatorStrings.add("this.aggregators.put(\"" + prefix + "::" + id + "\", " + src.toString().substring(2) + ");");
    }
    code.add(st.render());
}
Also used : ST(org.stringtemplate.v4.ST) TypeCheckException(boa.compiler.TypeCheckException) AggregatorSpec(boa.aggregators.AggregatorSpec)

Example 3 with TypeCheckException

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

the class TypeCheckingVisitor method checkPairs.

protected BoaType checkPairs(final List<Pair> pl, final SymbolTable env) {
    pl.get(0).accept(this, env);
    final BoaMap boaMap = (BoaMap) pl.get(0).type;
    for (final Pair p : pl) {
        p.accept(this, env);
        if (!boaMap.assigns(p.type))
            throw new TypeCheckException(p, "incompatible types: required '" + boaMap + "', found '" + p.type + "'");
    }
    return boaMap;
}
Also used : TypeCheckException(boa.compiler.TypeCheckException)

Example 4 with TypeCheckException

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

the class TypeCheckingVisitor method visit.

/** {@inheritDoc} */
@Override
public void visit(final VarDeclStatement n, final SymbolTable env) {
    n.env = env;
    final String id = n.getId().getToken();
    if (env.hasGlobal(id))
        throw new TypeCheckException(n.getId(), "name conflict: constant '" + id + "' already exists");
    if (env.hasLocal(id))
        throw new TypeCheckException(n.getId(), "variable '" + id + "' already declared as '" + env.get(id) + "'");
    BoaType rhs = null;
    if (n.hasInitializer()) {
        n.getInitializer().accept(this, env);
        rhs = n.getInitializer().type;
        final Factor f = n.getInitializer().getLhs().getLhs().getLhs().getLhs().getLhs();
        if (f.getOperand() instanceof Identifier && f.getOpsSize() == 0 && env.hasType(((Identifier) f.getOperand()).getToken()))
            throw new TypeCheckException(n.getInitializer(), "type '" + f.getOperand().type + "' is not a value and can not be assigned");
        // then its a call so the lhs type is the return type
        if (rhs instanceof BoaFunction) {
            final IsFunctionVisitor v = new IsFunctionVisitor();
            v.start(n.getInitializer());
            if (!v.isFunction())
                rhs = ((BoaFunction) rhs).getType();
        }
    }
    BoaType lhs;
    if (n.hasType()) {
        if (n.getType() instanceof Identifier && !env.hasType(((Identifier) n.getType()).getToken()))
            throw new TypeCheckException(n.getType(), "type '" + ((Identifier) n.getType()).getToken() + "' undefined");
        n.getType().accept(this, env);
        lhs = n.getType().type;
        if (lhs instanceof BoaArray && rhs instanceof BoaTuple)
            rhs = new BoaArray(((BoaTuple) rhs).getMember(0));
        if (rhs != null && !lhs.assigns(rhs) && !env.hasCast(rhs, lhs))
            throw new TypeCheckException(n.getInitializer(), "incorrect type '" + rhs + "' for assignment to '" + id + ": " + lhs + "'");
    } else {
        if (rhs == null)
            throw new TypeCheckException(n, "variable declaration requires an explicit type or an initializer");
        lhs = rhs;
    }
    if (lhs instanceof BoaFunction && (env.hasGlobalFunction(id) || env.hasLocalFunction(id)))
        throw new TypeCheckException(n.getId(), "name conflict: a function '" + id + "' already exists");
    env.set(id, lhs);
    n.type = lhs;
    n.getId().accept(this, env);
}
Also used : TypeCheckException(boa.compiler.TypeCheckException)

Example 5 with TypeCheckException

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

the class TypeCheckingVisitor method visit.

/** {@inheritDoc} */
@Override
public void visit(final Factor n, final SymbolTable env) {
    n.env = env;
    BoaType type = null;
    if (n.getOpsSize() > 0) {
        for (final Node node : n.getOps()) {
            if (node instanceof Selector) {
                if (type == null) {
                    n.getOperand().accept(this, env);
                    type = n.getOperand().type;
                }
                if (type instanceof BoaName)
                    type = ((BoaName) type).getType();
                env.setOperandType(type);
                node.accept(this, env);
                type = node.type;
            } else if (node instanceof Index) {
                if (type == null) {
                    n.getOperand().accept(this, env);
                    type = n.getOperand().type;
                }
                node.accept(this, env);
                final BoaType index = node.type;
                if (type instanceof BoaArray) {
                    if (!(index instanceof BoaInt))
                        throw new TypeCheckException(node, "invalid index type '" + index + "' for indexing into '" + type + "'");
                    type = ((BoaArray) type).getType();
                } else if (type instanceof BoaProtoList) {
                    if (!(index instanceof BoaInt))
                        throw new TypeCheckException(node, "invalid index type '" + index + "' for indexing into '" + type + "'");
                    type = ((BoaProtoList) type).getType();
                } else if (type instanceof BoaMap) {
                    if (!((BoaMap) type).getIndexType().assigns(index))
                        throw new TypeCheckException(node, "invalid index type '" + index + "' for indexing into '" + type + "'");
                    type = ((BoaMap) type).getType();
                } else {
                    throw new TypeCheckException(node, "type '" + type + "' does not allow index operations");
                }
            } else {
                node.accept(this, env);
                n.getOperand().env = env;
                final List<BoaType> formalParameters = this.check((Call) node, env);
                final FunctionFindingVisitor v = new FunctionFindingVisitor(formalParameters);
                try {
                    v.start((Identifier) n.getOperand(), env);
                } catch (final ClassCastException e) {
                    throw new TypeCheckException(n.getOperand(), "Function declarations must be assigned to a variable and can not be used anonymously", e);
                } catch (final RuntimeException e) {
                    throw new TypeCheckException(n.getOperand(), e.getMessage(), e);
                }
                type = v.getFunction().erase(formalParameters);
                if (((Identifier) n.getOperand()).getToken().equals("getvalue")) {
                    if (formalParameters.size() == 1) {
                        type = lastRetType;
                    }
                }
            }
            node.type = type;
        }
    } else {
        n.getOperand().accept(this, env);
        type = n.getOperand().type;
        if (type instanceof BoaFunction && n.getOperand() instanceof Identifier)
            throw new TypeCheckException(n, "expected a call to function '" + n.getOperand() + "'");
    }
    n.type = 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