package vaporize.library; import cs132.vapor.ast.*; import java.util.ArrayList; /** * This class contains various generic methods for * assembling common-use vaporm strings and managing * the original input program. * * Robert Martin cries */ class Kettle { ArrayList vapor; protected Kettle(ArrayList vapor) { this.vapor = vapor; } protected String get(Node n) { /** * Given the source position of a Node, returns the original line. */ return this.vapor.get(this.indexOf(n)); } protected void replaceFunctionDeclare(VFunction prev, int in, int out, int local) { this.set(prev, String.format("func %s [in %d, out %d, local %d]", this.parseFuncName(this.get(prev)), in, out, local)); } protected String spill() { return null; } protected String backup() { return null; } protected String dump() { return String.join("\n", this.vapor); } private int indexOf(Node n) { return n.sourcePos.line-1; } private void set(Node n, String s) { /** * Sets the position of Node n in the original * file to String s. */ this.vapor.set(this.indexOf(n), s); } private static String parseFuncName(String func) { /** * "func A_foo(this t.1)" -> "A_foo" */ int openParenIndex = func.indexOf('('); return func.substring(5, openParenIndex); } }