Search in sources :

Example 1 with AggregatorSpec

use of boa.aggregators.AggregatorSpec 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 2 with AggregatorSpec

use of boa.aggregators.AggregatorSpec 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 3 with AggregatorSpec

use of boa.aggregators.AggregatorSpec in project compiler by boalang.

the class SymbolTable method importAggregator.

private static void importAggregator(final Class<?> clazz) {
    if (!clazz.isAnnotationPresent(AggregatorSpec.class))
        return;
    final AggregatorSpec annotation = clazz.getAnnotation(AggregatorSpec.class);
    if (annotation == null)
        return;
    final String type = annotation.type();
    if (type.equals("any"))
        aggregators.put(annotation.name(), clazz);
    else
        aggregators.put(annotation.name() + ":" + type, clazz);
}
Also used : AggregatorSpec(boa.aggregators.AggregatorSpec)

Aggregations

AggregatorSpec (boa.aggregators.AggregatorSpec)3 TypeCheckException (boa.compiler.TypeCheckException)2 ST (org.stringtemplate.v4.ST)1