use of edu.princeton.cs.algs4.ST in project compiler by boalang.
the class CodeGeneratingVisitor method visit.
/** {@inheritDoc} */
@Override
public void visit(final FunctionType n) {
final ST st = stg.getInstanceOf("FunctionType");
if (!(n.type instanceof BoaFunction))
throw new TypeCheckException(n, "type " + n.type + " is not a function type");
final BoaFunction funcType = ((BoaFunction) n.type);
final BoaType[] paramTypes = funcType.getFormalParameters();
final List<String> args = new ArrayList<String>();
final List<String> types = new ArrayList<String>();
for (int i = 0; i < paramTypes.length; i++) {
args.add(((BoaName) paramTypes[i]).getId());
types.add(paramTypes[i].toJavaType());
}
st.add("name", 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);
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 VisitorExpression n) {
final ST st = stg.getInstanceOf("Visitor");
this.varDecl.start(n);
st.add("staticDeclarations", this.varDecl.getCode());
final List<String> body = new ArrayList<String>();
for (final Node node : n.getBody().getStatements()) {
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 EmitStatement n) {
final ST st = stg.getInstanceOf("EmitStatement");
if (n.getIndicesSize() > 0) {
final List<String> indices = new ArrayList<String>();
for (final Expression e : n.getIndices()) {
e.accept(this);
indices.add(code.removeLast());
}
st.add("indices", indices);
}
String id = n.getId().getToken();
String prefix = name;
if (id.matches("\\d+_.*")) {
prefix = id.substring(0, id.indexOf('_'));
id = id.substring(id.indexOf('_') + 1);
}
st.add("id", "\"" + id + "\"");
st.add("job", prefix);
n.getValue().accept(this);
st.add("expression", code.removeLast());
if (n.hasWeight()) {
n.getWeight().accept(this);
st.add("weight", code.removeLast());
}
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 TraverseStatement n) {
final ST st = stg.getInstanceOf("TraverseClause");
final BoaFunction funcType = ((BoaFunction) n.type);
final List<String> body = new ArrayList<String>();
String types = "";
traversalNodeIdentifier = "";
if (n.hasWildcard()) {
st.add("name", "defaultPreTraverse");
} else if (n.hasComponent()) {
final Component c = n.getComponent();
traversalNodeIdentifier = "___" + c.getIdentifier().getToken();
n.env.set(traversalNodeIdentifier, c.getType().type);
types = c.getType().type.toJavaType();
st.add("name", "preTraverse");
}
int count = 0;
if (!(funcType.getType() instanceof BoaAny))
st.add("ret", funcType.getType().toBoxedJavaType());
if (n.hasBody()) {
if (n.getBody() instanceof Block) {
for (final Node b : ((Block) n.getBody()).getStatements()) {
b.accept(this);
body.add(code.removeLast());
}
} else {
n.getBody().accept(this);
body.add(code.removeLast());
}
}
st.add("body", body);
st.add("args", traversalNodeIdentifier);
st.add("types", types);
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 Comparison n) {
final ST st = stg.getInstanceOf("Expression");
if (n.hasRhs()) {
final List<String> operators = new ArrayList<String>();
final List<String> operands = new ArrayList<String>();
n.getLhs().accept(this);
n.getRhs().accept(this);
operators.add(n.getOp());
if (n.getOp().equals("==") || n.getOp().equals("!=")) {
// special case string/stack/set/map (in)equality
if (n.getLhs().type instanceof BoaString || n.getLhs().type instanceof BoaStack || n.getLhs().type instanceof BoaSet || n.getLhs().type instanceof BoaMap) {
final String expr = code.removeLast() + ".equals(" + code.removeLast() + ")";
if (n.getOp().equals("!="))
st.add("lhs", "!" + expr);
else
st.add("lhs", expr);
operators.clear();
} else // special case arrays
if (n.getLhs().type instanceof BoaArray) {
final String expr = "boa.functions.BoaIntrinsics.deepEquals(" + code.removeLast() + ", " + code.removeLast() + ")";
if (n.getOp().equals("!="))
st.add("lhs", "!" + expr);
else
st.add("lhs", expr);
operators.clear();
} else // special case AST (in)equality
if (n.getLhs().type instanceof BoaProtoTuple) {
operands.add(code.removeLast() + ".hashCode()");
st.add("lhs", code.removeLast() + ".hashCode()");
} else {
operands.add(code.removeLast());
st.add("lhs", code.removeLast());
}
} else {
operands.add(code.removeLast());
st.add("lhs", code.removeLast());
}
st.add("operators", operators);
st.add("operands", operands);
} else {
n.getLhs().accept(this);
st.add("lhs", code.removeLast());
}
code.add(st.render());
}
Aggregations