Search in sources :

Example 16 with TypeCheckException

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

the class CodeGeneratingVisitor method visit.

/** {@inheritDoc} */
@Override
public void visit(final TimeLiteral n) {
    final String lit = n.getLiteral();
    // time lit is a string, convert to an int
    if (lit.startsWith("T")) {
        final String s = lit.substring(2, lit.length() - 1);
        // first try a standard format
        try {
            final DateFormat df = new SimpleDateFormat("EEE MMM dd kk:mm:ss zzz yyyy");
            code.add(formatDate(df.parse(s)));
            return;
        } catch (Exception e) {
        }
        // then try every possible combination of built in formats
        final int[] formats = new int[] { DateFormat.DEFAULT, DateFormat.FULL, DateFormat.SHORT, DateFormat.LONG, DateFormat.MEDIUM };
        for (final int f : formats) for (final int f2 : formats) try {
            final DateFormat df = DateFormat.getDateTimeInstance(f, f2);
            code.add(formatDate(df.parse(s)));
            return;
        } catch (Exception e) {
        }
        throw new TypeCheckException(n, "Invalid time literal '" + s + "'");
    }
    code.add(lit.substring(0, lit.length() - 1));
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) TypeCheckException(boa.compiler.TypeCheckException) IOException(java.io.IOException) TypeCheckException(boa.compiler.TypeCheckException)

Example 17 with TypeCheckException

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

the class CodeGeneratingVisitor method visit.

/** {@inheritDoc} */
@Override
public void visit(final FunctionExpression n) {
    final ST st = stg.getInstanceOf("Function");
    if (!(n.getType().type instanceof BoaFunction))
        throw new TypeCheckException(n, "type " + n.getType().type + " is not a function type");
    final BoaFunction funcType = ((BoaFunction) n.getType().type);
    final BoaType[] params = funcType.getFormalParameters();
    final List<String> args = new ArrayList<String>();
    final List<String> types = new ArrayList<String>();
    for (final BoaType c : params) {
        if (!(c instanceof BoaName))
            continue;
        args.add(((BoaName) c).getId());
        types.add(((BoaName) c).getType().toJavaType());
    }
    this.varDecl.start(n);
    st.add("staticDeclarations", this.varDecl.getCode());
    st.add("type", 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);
    n.getBody().accept(this);
    st.add("body", code.removeLast());
    code.add(st.render());
}
Also used : ST(org.stringtemplate.v4.ST) TypeCheckException(boa.compiler.TypeCheckException)

Example 18 with TypeCheckException

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

the class CodeGeneratingVisitor method visit.

/** {@inheritDoc} */
@Override
public void visit(final TupleType n) {
    final ST st = stg.getInstanceOf("TupleType");
    if (!(n.type instanceof BoaTuple))
        throw new TypeCheckException(n, "type " + n.type + " is not a tuple type");
    final BoaTuple tupType = ((BoaTuple) n.type);
    final List<Component> members = n.getMembers();
    final List<String> fields = new ArrayList<String>();
    final List<String> types = new ArrayList<String>();
    int fieldCount = 0;
    for (final Component c : members) {
        if (c.hasIdentifier()) {
            fields.add(c.getIdentifier().getToken());
        } else {
            fields.add("id" + fieldCount++);
        }
        types.add(c.getType().type.toJavaType());
    }
    st.add("name", tupType.toBoxedJavaType());
    st.add("fields", fields);
    st.add("types", types);
    code.add(st.render());
}
Also used : ST(org.stringtemplate.v4.ST) TypeCheckException(boa.compiler.TypeCheckException)

Example 19 with TypeCheckException

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

the class TypeCheckingVisitor method visit.

/** {@inheritDoc} */
@Override
public void visit(final Index n, final SymbolTable env) {
    n.env = env;
    n.getStart().accept(this, env);
    n.type = n.getStart().type;
    if (n.getStart().type == null)
        throw new RuntimeException();
    if (n.hasEnd()) {
        if (!(n.getStart().type instanceof BoaInt))
            throw new TypeCheckException(n.getStart(), "invalid type '" + n.getStart().type + "' for slice expression");
        n.getEnd().accept(this, env);
        if (!(n.getEnd().type instanceof BoaInt))
            throw new TypeCheckException(n.getEnd(), "invalid type '" + n.getEnd().type + "' for slice expression");
    }
}
Also used : TypeCheckException(boa.compiler.TypeCheckException)

Example 20 with TypeCheckException

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

the class TypeCheckingVisitor method visit.

/** {@inheritDoc} */
@Override
public void visit(final PostfixStatement n, final SymbolTable env) {
    n.env = env;
    n.getExpr().accept(this, env);
    if (!(n.getExpr().type instanceof BoaInt))
        throw new TypeCheckException(n.getExpr(), "incompatible types for operator '" + n.getOp() + "': required 'int', found '" + n.getExpr().type + "'");
    n.type = n.getExpr().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