Search in sources :

Example 56 with ST

use of edu.princeton.cs.algs4.ST in project compiler by boalang.

the class CodeGeneratingVisitor method visit.

/** {@inheritDoc} */
@Override
public void visit(final TraversalExpression n) {
    final ST st = stg.getInstanceOf("Traversal");
    this.varDecl.start(n);
    st.add("staticDeclarations", this.varDecl.getCode());
    final List<String> body = new ArrayList<String>();
    for (final Node node : n.getBody().getStatements()) {
        if (node instanceof TraverseStatement) {
            if (!(((BoaFunction) node.type).getType() instanceof BoaAny)) {
                st.add("T", ((BoaFunction) node.type).getType().toBoxedJavaType());
            } else {
                st.add("T", "Object");
            }
        }
        node.accept(this);
        body.add(code.removeLast());
    }
    st.add("body", body);
    code.add(st.render());
}
Also used : ST(org.stringtemplate.v4.ST)

Example 57 with ST

use of edu.princeton.cs.algs4.ST in project compiler by boalang.

the class CodeGeneratingVisitor method visit.

/** {@inheritDoc} */
@Override
public void visit(final Conjunction n) {
    final ST st = stg.getInstanceOf("Expression");
    n.getLhs().accept(this);
    st.add("lhs", code.removeLast());
    if (n.getRhsSize() > 0) {
        final List<String> operators = new ArrayList<String>();
        final List<String> operands = new ArrayList<String>();
        for (final Comparison c : n.getRhs()) {
            operators.add("&&");
            c.accept(this);
            operands.add(code.removeLast());
        }
        st.add("operators", operators);
        st.add("operands", operands);
    }
    code.add(st.render());
}
Also used : ST(org.stringtemplate.v4.ST)

Example 58 with ST

use of edu.princeton.cs.algs4.ST in project compiler by boalang.

the class CodeGeneratingVisitor method visit.

//
// statements
//
/** {@inheritDoc} */
@Override
public void visit(final AssignmentStatement n) {
    final ST st = stg.getInstanceOf("Assignment");
    n.getLhs().accept(this);
    final String lhs = code.removeLast();
    n.getRhs().accept(this);
    String rhs = code.removeLast();
    if (n.getLhs().type instanceof BoaTuple && n.getRhs().type instanceof BoaArray) {
        Operand op = n.getRhs().getLhs().getLhs().getLhs().getLhs().getLhs().getOperand();
        if (op instanceof Composite) {
            List<Expression> exps = ((Composite) op).getExprs();
            if (checkTupleArray(this.check(exps)) == false) {
                final ST stup = stg.getInstanceOf("Tuple");
                stup.add("name", n.getLhs().type.toJavaType());
                visit(exps);
                stup.add("exprlist", code.removeLast());
                rhs = stup.render();
            }
        }
    }
    // FIXME rdyer hack to fix assigning to maps
    if (lhs.contains(".get(")) {
        int idx = lhs.lastIndexOf(')') - 1;
        int parens = 1;
        for (; idx >= 0; idx--) {
            if (lhs.charAt(idx) == '(')
                parens--;
            else if (lhs.charAt(idx) == ')')
                parens++;
            if (parens == 0)
                break;
        }
        idx += 1;
        code.add(lhs.substring(0, idx - ".get(".length()) + ".put(" + lhs.substring(idx, lhs.lastIndexOf(')')) + ", " + rhs + lhs.substring(lhs.lastIndexOf(')')) + ";");
        return;
    }
    st.add("lhs", lhs);
    st.add("rhs", rhs);
    code.add(st.render());
}
Also used : ST(org.stringtemplate.v4.ST)

Example 59 with ST

use of edu.princeton.cs.algs4.ST in project compiler by boalang.

the class CodeGeneratingVisitor method visit.

/** {@inheritDoc} */
@Override
public void visit(final ForeachStatement n) {
    final ST st = stg.getInstanceOf("WhenStatement");
    generateQuantifier(n, n.getVar(), n.getCondition(), n.getBody(), "foreach", st);
}
Also used : ST(org.stringtemplate.v4.ST)

Example 60 with ST

use of edu.princeton.cs.algs4.ST in project compiler by boalang.

the class CodeGeneratingVisitor method visit.

/** {@inheritDoc} */
@Override
public void visit(final PostfixStatement n) {
    final ST st = stg.getInstanceOf("ExprStatement");
    n.getExpr().accept(this);
    st.add("expression", code.removeLast());
    st.add("operator", n.getOp());
    code.add(st.render());
}
Also used : ST(org.stringtemplate.v4.ST)

Aggregations

ST (org.stringtemplate.v4.ST)197 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)37 STGroup (org.stringtemplate.v4.STGroup)24 File (java.io.File)19 ArrayList (java.util.ArrayList)16 IOException (java.io.IOException)12 STGroupFile (org.stringtemplate.v4.STGroupFile)12 Path (java.nio.file.Path)10 Test (org.junit.Test)10 ATNFactory (org.antlr.v4.automata.ATNFactory)9 LexerATNFactory (org.antlr.v4.automata.LexerATNFactory)9 ParserATNFactory (org.antlr.v4.automata.ParserATNFactory)9 CodeGenerator (org.antlr.v4.codegen.CodeGenerator)9 SemanticPipeline (org.antlr.v4.semantics.SemanticPipeline)9 Grammar (org.antlr.v4.tool.Grammar)9 LexerGrammar (org.antlr.v4.tool.LexerGrammar)9 STGroupString (org.stringtemplate.v4.STGroupString)9 LinkedHashMap (java.util.LinkedHashMap)7 URL (java.net.URL)6 Map (java.util.Map)6