Commit b3ccaa4f authored by Sven Gestegård Robertz's avatar Sven Gestegård Robertz
Browse files

refactored environments in compiler code generation

parent ac6acf60
......@@ -6,145 +6,43 @@ aspect CS_CodeGenEnv {
// Environment wrapper for CS-code generation
// handles indentation, file writing,
public class CS_env {
public class CS_env extends PrintEnv {
public final int version;
public final String verStr;
private int indent;
private int depth;
private CS_printer printer;
private HashMap unique = new HashMap();
final private static class CS_printer {
private boolean newline = true;
private File file;
private PrintStream out;
private IOException exception;
public CS_printer(File f) {
file = f;
File parentFile = f.getParentFile();
if(parentFile != null) {
parentFile.mkdirs();
}
}
final private static class CS_printer extends PrintEnv.FilePrinter {
public CS_printer(File f) {
super(f);
}
public CS_printer(PrintStream out) {
this.out = out;
}
public void close() throws IOException {
if (out != null) {
out.close();
}
if (exception != null) {
throw exception;
}
}
public PrintStream getPrintStream() {
return(out);
}
public void checkOpen() {
if (out == null && exception == null) {
try {
out = new PrintStream(new FileOutputStream(file));
} catch (IOException e) {
exception = e;
}
}
}
public void print(CS_env env, String s) {
checkOpen();
if (newline) {
newline = false;
for (int i = 0 ; i < env.indent ; i++) {
out.print(" ");
}
}
out.print(s);
}
public void println(CS_env env, String s) {
checkOpen();
print(env, s);
out.println();
newline = true;
}
}
super(out);
}
private CS_env(int indent, CS_printer printer, int version) {
this.version = version;
this.indent = indent;
this.printer = printer;
this.verStr = LabCommVersion.versionString(version);
}
public CS_env(File f, int version) {
this(0, new CS_printer(f), version);
super(0, new CS_printer(f), version);
}
public CS_env(PrintStream out, int version) {
this(0, new CS_printer(out), version);
}
public void close() throws IOException {
printer.close();
super(0, new CS_printer(out), version);
}
public PrintStream getPrintStream() {
return printer.getPrintStream();
}
public void indent(int amount) {
indent += amount;
}
public void indent() {
indent(1);
}
public void unindent(int amount) {
indent -= amount;
if (indent < 0) {
throw new Error("Negative indent level");
}
}
public void unindent() {
unindent(1);
}
public void print(String s) {
printer.print(this, s);
}
public void println(String s) {
printer.println(this, s);
}
public void println() {
printer.println(this, "");
}
public int getDepth() {
return depth;
}
public String print_for_begin(String limit) {
int depth = getDepth();
print("for (int i_" + depth + " = 0 ; ");
print("i_" + depth + " < " + limit + " ; ");
println("i_" + depth + "++) {");
indent();
depth++;
return "i_" + (depth - 1);
incDepth();
return "i_" + (depth);
}
public void print_for_end() {
depth--;
decDepth();
unindent();
println("}");
}
......
This diff is collapsed.
......@@ -6,148 +6,40 @@ aspect Java_CodeGenEnv {
// Environment wrapper for Java-code generation
// handles indentation, file writing,
public class Java_env {
public class Java_env extends PrintEnv {
public final int version; //labcomm version to generate code for
public final String verStr;
private int indent;
private int depth;
private Java_printer printer;
private HashMap unique = new HashMap();
final private class Java_printer {
private boolean newline = true;
private File file;
private PrintStream out;
private IOException exception;
public Java_printer(File f) {
file = f;
File parentFile = f.getParentFile();
if(parentFile != null) {
parentFile.mkdirs();
}
}
public Java_printer(PrintStream out) {
this.out = out;
}
public void close() throws IOException {
if (out != null) {
out.close();
}
if (exception != null) {
throw exception;
}
final private static class Java_printer extends PrintEnv.FilePrinter {
public Java_printer(PrintStream out) {
super(out);
}
public PrintStream getPrintStream() {
return(out);
}
public void checkOpen() {
if (out == null && exception == null) {
try {
out = new PrintStream(new FileOutputStream(file));
} catch (IOException e) {
exception = e;
}
}
}
public void print(Java_env env, String s) {
checkOpen();
if (newline) {
newline = false;
for (int i = 0 ; i < env.indent ; i++) {
out.print(" ");
}
}
out.print(s);
}
public void println(Java_env env, String s) {
checkOpen();
print(env, s);
out.println();
newline = true;
public Java_printer(File f) {
super(f);
}
}
private Java_env(int version, int indent) {
this.version = version;
this.verStr = "2014";
this.indent = indent;
}
private Java_env(int version, Java_printer printer) {
this(version, 0);
this.printer = printer;
}
public Java_env(int version, File f) {
this(version, 0);
this.printer = new Java_printer(f);
super(0, new Java_printer(f), version);
}
public Java_env(int version, PrintStream out) {
this(version, 0);
this.printer = new Java_printer(out);
}
public void close() throws IOException {
printer.close();
}
public PrintStream getPrintStream() {
return printer.getPrintStream();
}
public void indent(int amount) {
indent += amount;
}
public void indent() {
indent(1);
}
public void unindent(int amount) {
indent -= amount;
}
public void unindent() {
unindent(1);
}
public void print(String s) {
printer.print(this, s);
}
public void println(String s) {
printer.println(this, s);
}
public void println() {
printer.println(this, "");
}
public int getDepth() {
return depth;
super(0, new Java_printer(out), version);
}
public String print_for_begin(String limit) {
int depth = getDepth();
print("for (int i_" + depth + " = 0 ; ");
print("i_" + depth + " < " + limit + " ; ");
println("i_" + depth + "++) {");
indent();
depth++;
return "[i_" + (depth - 1) + "]";
incDepth();
return "[i_" + (depth) + "]";
}
public void print_for_end() {
depth--;
decDepth();
unindent();
println("}");
}
......
......@@ -4,70 +4,18 @@ aspect Python_CodeGenEnv {
// handles qualid nesting, indentation, file writing and
// prefix propagation
public class Python_env {
final private class Python_printer {
private boolean newline = true;
private PrintStream out;
public Python_printer(PrintStream out) {
this.out = out;
}
public void print(Python_env env, String s) {
if (newline) {
newline = false;
for (int i = 0 ; i < env.indent ; i++) {
out.print(" ");
}
}
out.print(s);
}
public void println(Python_env env, String s) {
print(env, s);
out.println();
newline = true;
}
public void println(Python_env env) {
out.println();
newline = true;
}
public class Python_env extends PrintEnv {
final private class Python_printer extends PrintEnv.Printer {
// public C_printer(PrintStream out) {
// super(out, " ");
// }
}
private int indent;
private Python_printer printer;
public Python_env(PrintStream out) {
this.indent = 0;
this.printer = new Python_printer(out);
}
public void indent() {
indent++;
}
public void unindent() {
indent--;
}
public void print(String s) {
printer.print(this, s);
super(out);
}
public void println(String s) {
printer.println(this, s);
}
public void println() {
printer.println(this);
}
}
}
aspect Python_CodeGen {
......
......@@ -37,3 +37,197 @@ aspect Encoding {
}
}
aspect PrintEnv {
public abstract class PrintEnv {
protected static class Printer {
private final String indentString = " ";
private boolean newline = true; // last print ended with newline
protected PrintStream out;
private Printer printer;
/** dummy constructor motivated by the FilePrinter subclass */
protected Printer() {
this.out = null;
}
public Printer(PrintStream out) {
this.out = out;
}
public void print(PrintEnv env, String s) {
if (newline) {
newline = false;
for (int i = 0 ; i < env.getIndent() ; i++) {
out.print(indentString);
}
}
out.print(s);
}
public void println(PrintEnv env, String s) {
print(env, s);
out.println();
newline = true;
}
public void println(PrintEnv env) {
out.println();
newline = true;
}
public PrintStream getPrintStream() {
return(out);
}
public void close() throws IOException {
//do nothing
}
}
protected static class FilePrinter extends Printer {
private File file;
private IOException exception;
public FilePrinter(PrintStream out) {
super(out);
}
public FilePrinter(File f) {
file = f;
File parentFile = f.getParentFile();
if(parentFile != null) {
parentFile.mkdirs();
}
}
public void close() throws IOException {
if (out != null) {
out.close();
}
if (exception != null) {
throw exception;
}
}
public void checkOpen() {
if (out == null && exception == null) {
try {
out = new PrintStream(new FileOutputStream(file));
} catch (IOException e) {
exception = e;
}
}
}
public void print(PrintEnv env, String s) {
checkOpen();
super.print(env,s);
}
public void println(PrintEnv env, String s) {
checkOpen();
super.println(env, s);
}
}
public final int version; //labcomm version (2006 or 2014)
public final String verStr; // version suffix to append (currently _2006 and empty string)
private Printer printer;
private int indent;
private int depth;
protected PrintEnv(PrintStream out) {
this(new Printer(out));
}
protected PrintEnv(Printer printer) {
this(printer, 2014);
}
protected PrintEnv(Printer printer, int version) {
this(0, printer, version);
}
protected PrintEnv(int indent, Printer printer, int version) {
this(indent, printer, version, 0);
}
protected PrintEnv(int indent, Printer printer, int version, int depth) {
this.version = version;
this.indent = indent;
this.printer = printer;
this.verStr = LabCommVersion.versionString(version);
this.depth = depth;
}
public void close() throws IOException {
printer.close();
}
public PrintStream getPrintStream() {
return printer.getPrintStream();
}
public void indent(int amount) {
indent += amount;
}
public void indent() {
indent(1);
}
public void unindent(int amount) {
indent -= amount;
if (indent < 0) {
throw new Error("Negative indent level");
}
}
public void unindent() {
unindent(1);
}
public void print(String s) {
printer.print(this, s);
}
public void println(String s) {
printer.println(this, s);
}
public void println() {
printer.println(this, "");
}
public void incDepth() {
depth++;
}
public void decDepth() {
if(depth<=0) {
throw new RuntimeException("decDepth() called when depth = "+depth);
}
depth--;
}
public int getDepth() {
return depth;
}
public int getVersion() {
return version;
}
public int getIndent() {
return indent;
}
public Printer getPrinter() {
return printer;
}
public boolean versionHasMetaData() {
return version != 2006;
}
}
}
......@@ -5,8 +5,15 @@ aspect Version {
*/
class LabCommVersion {
public static String versionString(int version) {
return (version == 2006) ? "2006" : "";
}
switch(version) {
case 2006:
return "2006";
case 2014:
return "2014";
default:
throw new Error("no versionString for version "+version);
}
}
public static boolean versionHasPragma(int version) {
return version != 2006;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment