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());
}
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());
}
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());
}
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);
}
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());
}
Aggregations