use of edu.princeton.cs.algs4.ST in project compiler by boalang.
the class CodeGeneratingVisitor method visit.
//
// expressions
//
/** {@inheritDoc} */
@Override
public void visit(final Expression 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 Conjunction 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.
/** {@inheritDoc} */
@Override
public void visit(final DoStatement n) {
final ST st = stg.getInstanceOf("DoWhile");
n.getCondition().accept(this);
st.add("condition", code.removeLast());
n.getBody().accept(this);
st.add("stmt", 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 Composite n) {
final ST st = stg.getInstanceOf("Composite");
if (n.getPairsSize() > 0) {
String s = "\t{\n";
for (final Pair p : n.getPairs()) {
visit(p);
s += "\t\t" + code.removeLast() + "\n";
}
s += "\t}";
st.add("exprlist", s);
st.add("type", n.type.toBoxedJavaType() + "()");
} else if (n.getExprsSize() > 0) {
// FIXME rdyer
BoaType t = n.type;
if (t instanceof BoaTuple) {
final ST stup = stg.getInstanceOf("Tuple");
stup.add("name", t.toJavaType());
visit(n.getExprs());
stup.add("exprlist", code.removeLast());
code.add(stup.render());
return;
}
visit(n.getExprs());
if (t instanceof BoaArray) {
if (((BoaArray) t).getType() instanceof BoaEnum) {
st.add("exprlist", code.removeLast());
st.add("type", "Object[] ");
code.add(st.render());
return;
}
}
st.add("exprlist", code.removeLast());
st.add("type", t.toJavaType());
}
code.add(st.render());
}
use of edu.princeton.cs.algs4.ST in project bender by Nextdoor.
the class BenderConfig method swapEnvironmentVariables.
/**
* Parses an input String and replaces instances of {@literal <XXX>}" with the value of the XXX OS
* Environment Variable. This is used as a pre-parser for the Config files, allowing environment
* variables to be swapped at run-time.
*
* @param raw A raw string (not necessarily valid configuration data)
* @return A parsed string with OS variables swapped in
* @throws ConfigurationException If any discovered {@literal <WRAPPED_VALUES>} are not found in
* System.getenv().
*/
public static String swapEnvironmentVariables(String raw) throws ConfigurationException {
ErrorBuffer errors = new ErrorBuffer();
ST template = new ST(raw);
STGroup g = template.groupThatCreatedThisInstance;
g.setListener(errors);
Map<String, String> env = System.getenv();
for (String envName : env.keySet()) {
template.add(envName, env.get(envName));
}
String parsed = template.render();
if (errors.errors.size() > 0) {
throw new ConfigurationException(errors.toString());
}
return parsed;
}
use of edu.princeton.cs.algs4.ST in project bookish by parrt.
the class Tex2SVG method tex2svg.
public Triple<String, Float, Float> tex2svg(String latex, LatexType type, int fontsize) {
try {
latex = latex.trim();
String tmpdir = new File(outputDir).getAbsolutePath();
if (!Files.exists(Paths.get(tmpdir))) {
Files.createDirectories(Paths.get(tmpdir));
}
if (!Files.exists(Paths.get(tmpdir + "/images"))) {
Files.createSymbolicLink(Paths.get(tmpdir + "/images"), Paths.get(outputDir + "/images"));
}
String texfilename = tmpdir + "/temp.tex";
ST template = null;
switch(type) {
case EQN:
template = templates.getInstanceOf("eqntex");
break;
case BLOCKEQN:
template = templates.getInstanceOf("blockeqntex");
break;
case LATEX:
template = templates.getInstanceOf("blocktex");
break;
}
template.add("text", latex);
template.add("fontsize", fontsize);
Files.write(Paths.get(texfilename), template.render().getBytes());
// System.out.println("wrote "+texfilename);
String[] results = execInDir(tmpdir, "xelatex", "-shell-escape", "-interaction=nonstopmode", "temp.tex");
// println(results.a)
String metricsRegex = "// bookish metrics: ([0-9]*[.][0-9]+)pt, ([0-9]*[.][0-9]+)pt";
Pattern metricsPattern = Pattern.compile("metricsRegex");
String log = ParrtIO.load(tmpdir + "/temp.log");
float height = 0, depth = 0;
for (String line : results[0].split("\n")) {
String prefix = "// bookish metrics: ";
if (line.startsWith(prefix)) {
int first = prefix.length();
int comma = line.indexOf(',');
String heightS = line.substring(first, comma - "pt".length());
String depthS = line.substring(comma + 1, line.indexOf('p', comma));
height = Float.parseFloat(heightS);
depth = Float.parseFloat(depthS);
}
if (line.startsWith("!") || line.startsWith("l.")) {
System.err.println(line);
System.err.println(latex);
}
}
if (results[1].length() > 0) {
System.err.println(results[1]);
}
results = execInDir(tmpdir, "pdfcrop", "temp.pdf");
if (results[1].length() > 0) {
System.err.println(results[1]);
}
results = execInDir(tmpdir, "pdf2svg", "temp-crop.pdf", "temp.svg");
if (results[1].length() > 0) {
System.err.println(results[1]);
}
String svgfilename = tmpdir + "/temp.svg";
return new Triple<>(new String(Files.readAllBytes(Paths.get(svgfilename))), height, depth);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
Aggregations