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

version 2013 still works, version 2006 untested

parent 45b8e760
...@@ -8,12 +8,13 @@ aspect CS_CodeGenEnv { ...@@ -8,12 +8,13 @@ aspect CS_CodeGenEnv {
public class CS_env { public class CS_env {
public final int version;
private int indent; private int indent;
private int depth; private int depth;
private CS_printer printer; private CS_printer printer;
private HashMap unique = new HashMap(); private HashMap unique = new HashMap();
final private class CS_printer { final private static class CS_printer {
private boolean newline = true; private boolean newline = true;
private File file; private File file;
...@@ -75,19 +76,18 @@ aspect CS_CodeGenEnv { ...@@ -75,19 +76,18 @@ aspect CS_CodeGenEnv {
} }
} }
private CS_env(int indent, CS_printer printer) { private CS_env(int indent, CS_printer printer, int version) {
this.version = version;
this.indent = indent; this.indent = indent;
this.printer = printer; this.printer = printer;
} }
public CS_env(File f) { public CS_env(File f, int version) {
this.indent = 0; this(0, new CS_printer(f), version);
this.printer = new CS_printer(f);
} }
public CS_env(PrintStream out) { public CS_env(PrintStream out, int version) {
this.indent = 0; this(0, new CS_printer(out), version);
this.printer = new CS_printer(out);
} }
public void close() throws IOException { public void close() throws IOException {
...@@ -196,9 +196,9 @@ aspect CS_Void { ...@@ -196,9 +196,9 @@ aspect CS_Void {
aspect CS_CodeGen { aspect CS_CodeGen {
public void Program.CS_gen(String file, public void Program.CS_gen(String file,
String namespace) throws IOException { String namespace, int version) throws IOException {
// Registration class // Registration class
CS_env env = new CS_env(new File(file)); CS_env env = new CS_env(new File(file), version);
if (namespace != null && namespace.length() > 0) { if (namespace != null && namespace.length() > 0) {
env.println("namespace " + namespace + "{"); env.println("namespace " + namespace + "{");
env.indent(); env.indent();
...@@ -351,7 +351,7 @@ aspect CS_Class { ...@@ -351,7 +351,7 @@ aspect CS_Class {
CS_emitDecoder(env); CS_emitDecoder(env);
env.println("private static byte[] signature = new byte[] {"); env.println("private static byte[] signature = new byte[] {");
env.indent(); env.indent();
SignatureList signature = signature(); SignatureList signature = signature(env.version);
for (int i = 0 ; i < signature.size() ; i++) { for (int i = 0 ; i < signature.size() ; i++) {
String comment = signature.getComment(i); String comment = signature.getComment(i);
if (comment != null) { if (comment != null) {
...@@ -752,8 +752,8 @@ aspect CS_Class { ...@@ -752,8 +752,8 @@ aspect CS_Class {
aspect CS_Info { aspect CS_Info {
public void Program.CS_info(PrintStream out, String namespace) { public void Program.CS_info(PrintStream out, String namespace, int version) {
CS_env env = new CS_env(out); CS_env env = new CS_env(out, version);
if (namespace == null) { if (namespace == null) {
namespace = ""; namespace = "";
} else { } else {
......
...@@ -8,7 +8,7 @@ aspect C_CodeGenEnv { ...@@ -8,7 +8,7 @@ aspect C_CodeGenEnv {
public class C_env { public class C_env {
final private class C_printer { final private static class C_printer {
private boolean newline = true; private boolean newline = true;
private PrintStream out; private PrintStream out;
...@@ -33,6 +33,8 @@ aspect C_CodeGenEnv { ...@@ -33,6 +33,8 @@ aspect C_CodeGenEnv {
} }
} }
public final int version; //labcomm version (2006 or 2013)
public final String qualid; public final String qualid;
public final String lcName; public final String lcName;
public final String rawPrefix; public final String rawPrefix;
...@@ -42,7 +44,8 @@ aspect C_CodeGenEnv { ...@@ -42,7 +44,8 @@ aspect C_CodeGenEnv {
private C_printer printer; private C_printer printer;
private C_env(String qualid, String lcName, String rawPrefix, private C_env(String qualid, String lcName, String rawPrefix,
int indent, int depth, C_printer printer) { int indent, int depth, C_printer printer, int version) {
this.version = version;
this.qualid = qualid; this.qualid = qualid;
this.lcName = lcName; this.lcName = lcName;
this.rawPrefix = rawPrefix; this.rawPrefix = rawPrefix;
...@@ -57,28 +60,18 @@ aspect C_CodeGenEnv { ...@@ -57,28 +60,18 @@ aspect C_CodeGenEnv {
} }
public C_env(String qualid, String lcName, String rawPrefix, public C_env(String qualid, String lcName, String rawPrefix,
PrintStream out) { PrintStream out, int version) {
this.qualid = qualid; this(qualid, lcName, rawPrefix, 0, 0, new C_printer(out), version);
this.lcName = lcName;
this.rawPrefix = rawPrefix;
if (rawPrefix.equals("")) {
this.prefix = rawPrefix;
} else {
this.prefix = rawPrefix + "_";
}
this.depth = 0;
this.indent = 0;
this.printer = new C_printer(out);
} }
public C_env nestArray(String suffix) { public C_env nestArray(String suffix) {
return new C_env(qualid + suffix, lcName, rawPrefix, return new C_env(qualid + suffix, lcName, rawPrefix,
indent, depth + 1, printer); indent, depth + 1, printer, version);
} }
public C_env nestStruct(String suffix) { public C_env nestStruct(String suffix) {
return new C_env(qualid + suffix, lcName, rawPrefix, return new C_env(qualid + suffix, lcName, rawPrefix,
indent, depth, printer); indent, depth, printer, version);
} }
public void indent() { public void indent() {
...@@ -147,8 +140,8 @@ aspect C_IsDynamic { ...@@ -147,8 +140,8 @@ aspect C_IsDynamic {
aspect C_CodeGen { aspect C_CodeGen {
public void Program.C_genH(PrintStream out, Vector includes, public void Program.C_genH(PrintStream out, Vector includes,
String lcName, String prefix) { String lcName, String prefix, int version) {
C_env env = new C_env("", lcName, prefix, out); C_env env = new C_env("", lcName, prefix, out, version);
// Hackish prettyprint preamble // Hackish prettyprint preamble
out.println("/* LabComm declarations:"); out.println("/* LabComm declarations:");
...@@ -174,8 +167,8 @@ aspect C_CodeGen { ...@@ -174,8 +167,8 @@ aspect C_CodeGen {
} }
public void Program.C_genC(PrintStream out, Vector includes, public void Program.C_genC(PrintStream out, Vector includes,
String lcName, String prefix) { String lcName, String prefix, int version) {
C_env env = new C_env("", lcName, prefix, out); C_env env = new C_env("", lcName, prefix, out, version);
// Include // Include
env.println("#include \"labcomm.h\""); env.println("#include \"labcomm.h\"");
...@@ -882,7 +875,7 @@ aspect C_Signature { ...@@ -882,7 +875,7 @@ aspect C_Signature {
public void SampleDecl.C_emitSignature(C_env env) { public void SampleDecl.C_emitSignature(C_env env) {
env.println("static unsigned char signature_bytes_" + env.println("static unsigned char signature_bytes_" +
env.prefix + getName() + "[] = {"); env.prefix + getName() + "[] = {");
SignatureList signature = signature(); SignatureList signature = signature(env.version);
for (int i = 0 ; i < signature.size() ; i++) { for (int i = 0 ; i < signature.size() ; i++) {
String comment = signature.getComment(i); String comment = signature.getComment(i);
if (comment != null) { if (comment != null) {
...@@ -1146,8 +1139,8 @@ aspect C_forAll { ...@@ -1146,8 +1139,8 @@ aspect C_forAll {
aspect C_Info { aspect C_Info {
public void Program.C_info(PrintStream out, String prefix) { public void Program.C_info(PrintStream out, String prefix, int version) {
C_env env = new C_env("", "", prefix, out); C_env env = new C_env("", "", prefix, out, version);
for (int i = 0; i < getNumDecl(); i++) { for (int i = 0; i < getNumDecl(); i++) {
getDecl(i).C_info(env); getDecl(i).C_info(env);
} }
......
...@@ -8,6 +8,7 @@ aspect Java_CodeGenEnv { ...@@ -8,6 +8,7 @@ aspect Java_CodeGenEnv {
public class Java_env { public class Java_env {
public final int version; //labcomm version to generate code for
private int indent; private int indent;
private int depth; private int depth;
private Java_printer printer; private Java_printer printer;
...@@ -75,18 +76,23 @@ aspect Java_CodeGenEnv { ...@@ -75,18 +76,23 @@ aspect Java_CodeGenEnv {
} }
} }
private Java_env(int indent, Java_printer printer) { private Java_env(int version, int indent) {
this.version = version;
this.indent = indent; this.indent = indent;
}
private Java_env(int version, Java_printer printer) {
this(version, 0);
this.printer = printer; this.printer = printer;
} }
public Java_env(File f) { public Java_env(int version, File f) {
this.indent = 0; this(version, 0);
this.printer = new Java_printer(f); this.printer = new Java_printer(f);
} }
public Java_env(PrintStream out) { public Java_env(int version, PrintStream out) {
this.indent = 0; this(version, 0);
this.printer = new Java_printer(out); this.printer = new Java_printer(out);
} }
...@@ -195,11 +201,11 @@ aspect Java_Void { ...@@ -195,11 +201,11 @@ aspect Java_Void {
aspect Java_CodeGen { aspect Java_CodeGen {
public void Program.J_gen(PrintStream ps, String pack) throws IOException { public void Program.J_gen(PrintStream ps, String pack, int version) throws IOException {
Java_env env; Java_env env;
/* /*
// Registration class // Registration class
env = new Java_env(ps); env = new Java_env(version, ps);
if (pack != null && pack.length() > 0) { if (pack != null && pack.length() > 0) {
env.println("package " + pack + ";"); env.println("package " + pack + ";");
} }
...@@ -212,7 +218,7 @@ aspect Java_CodeGen { ...@@ -212,7 +218,7 @@ aspect Java_CodeGen {
env.println("}"); env.println("}");
// env.close(); // env.close();
*/ */
env = new Java_env(ps); env = new Java_env(version, ps);
for (int i = 0; i < getNumDecl(); i++) { for (int i = 0; i < getNumDecl(); i++) {
Decl d = getDecl(i); Decl d = getDecl(i);
try { try {
...@@ -225,11 +231,11 @@ aspect Java_CodeGen { ...@@ -225,11 +231,11 @@ aspect Java_CodeGen {
env.close(); env.close();
} }
public void Program.J_gen(String dir, String pack) throws IOException { public void Program.J_gen(String dir, String pack, int version) throws IOException {
Java_env env; Java_env env;
/* /*
// Registration class // Registration class
env = new Java_env(new File(dir, "LabCommRegister.java")); env = new Java_env(version, new File(dir, "LabCommRegister.java"));
if (pack != null && pack.length() > 0) { if (pack != null && pack.length() > 0) {
env.println("package " + pack + ";"); env.println("package " + pack + ";");
} }
...@@ -245,7 +251,7 @@ aspect Java_CodeGen { ...@@ -245,7 +251,7 @@ aspect Java_CodeGen {
for (int i = 0; i < getNumDecl(); i++) { for (int i = 0; i < getNumDecl(); i++) {
Decl d = getDecl(i); Decl d = getDecl(i);
try { try {
env = new Java_env(new File(dir, d.getName() + ".java")); env = new Java_env(version, new File(dir, d.getName() + ".java"));
d.Java_emitClass(env, pack); d.Java_emitClass(env, pack);
env.close(); env.close();
} catch (Error e) { } catch (Error e) {
...@@ -257,7 +263,7 @@ aspect Java_CodeGen { ...@@ -257,7 +263,7 @@ aspect Java_CodeGen {
/** Experimental method for generating code to a map <classname, source> /** Experimental method for generating code to a map <classname, source>
*/ */
public void Program.J_gen(Map<String,String> src, String pack) throws IOException { public void Program.J_gen(Map<String,String> src, String pack, int version) throws IOException {
Java_env env; Java_env env;
/* /*
// Registration class was commented out, so got removed in this copy // Registration class was commented out, so got removed in this copy
...@@ -267,7 +273,7 @@ aspect Java_CodeGen { ...@@ -267,7 +273,7 @@ aspect Java_CodeGen {
try { try {
ByteArrayOutputStream bs = new ByteArrayOutputStream(); ByteArrayOutputStream bs = new ByteArrayOutputStream();
PrintStream out = new PrintStream(bs); PrintStream out = new PrintStream(bs);
env = new Java_env(out); env = new Java_env(version, out);
d.Java_emitClass(env, pack); d.Java_emitClass(env, pack);
env.close(); env.close();
src.put(d.getName(), bs.toString()); src.put(d.getName(), bs.toString());
...@@ -449,7 +455,7 @@ aspect Java_Class { ...@@ -449,7 +455,7 @@ aspect Java_Class {
Java_emitDecoder(env); Java_emitDecoder(env);
env.println("private static byte[] signature = new byte[] {"); env.println("private static byte[] signature = new byte[] {");
env.indent(); env.indent();
SignatureList signature = signature(); SignatureList signature = signature(env.version);
for (int i = 0 ; i < signature.size() ; i++) { for (int i = 0 ; i < signature.size() ; i++) {
String comment = signature.getComment(i); String comment = signature.getComment(i);
if (comment != null) { if (comment != null) {
...@@ -841,8 +847,8 @@ aspect Java_Class { ...@@ -841,8 +847,8 @@ aspect Java_Class {
aspect Java_Info { aspect Java_Info {
public void Program.Java_info(PrintStream out) { public void Program.Java_info(PrintStream out, int version) {
Java_env env = new Java_env(out); Java_env env = new Java_env(version, out);
for (int i = 0; i < getNumDecl(); i++) { for (int i = 0; i < getNumDecl(); i++) {
getDecl(i).Java_info(env); getDecl(i).Java_info(env);
} }
......
...@@ -13,6 +13,7 @@ public class LabComm { ...@@ -13,6 +13,7 @@ public class LabComm {
println(""); println("");
println(" --help Shows this help text"); println(" --help Shows this help text");
println(" -v Be verbose"); println(" -v Be verbose");
println(" --ver=VERSION Generate code for labcomm VERSION (=2006 or 2013)");
println("[ C options ]"); println("[ C options ]");
println(" -C Generates C/H code in FILE.[ch]"); println(" -C Generates C/H code in FILE.[ch]");
println(" --cprefix=PREFIX Prefixes C types with PREFIX"); println(" --cprefix=PREFIX Prefixes C types with PREFIX");
...@@ -53,6 +54,16 @@ public class LabComm { ...@@ -53,6 +54,16 @@ public class LabComm {
return s.substring(s.lastIndexOf('/') + 1, s.length()); return s.substring(s.lastIndexOf('/') + 1, s.length());
} }
/** To be cleaned up.
*/
private static void checkVersion(int v) {
if(! (v == 2006 || v == 2013) ) {
System.err.println(" Unknown version: " + v);
System.err.println(" Supported versions: 2006, 2013 ");
System.exit(2);
}
}
public static void main(String[] args) { public static void main(String[] args) {
String fileName = null; String fileName = null;
// Scan for first non-option // Scan for first non-option
...@@ -69,6 +80,7 @@ public class LabComm { ...@@ -69,6 +80,7 @@ public class LabComm {
prefix = getPrefix(coreName); prefix = getPrefix(coreName);
} }
boolean verbose = false; boolean verbose = false;
int ver = 2013; //Version 2013 as default
String cFile = null; String cFile = null;
String hFile = null; String hFile = null;
Vector cIncludes = new Vector(); Vector cIncludes = new Vector();
...@@ -91,6 +103,9 @@ public class LabComm { ...@@ -91,6 +103,9 @@ public class LabComm {
System.exit(0); System.exit(0);
} else if (args[i].equals("-v")) { } else if (args[i].equals("-v")) {
verbose=true; verbose=true;
} else if (args[i].startsWith("--ver=")) {
ver = Integer.parseInt(args[i].substring(6));
checkVersion(ver);
} else if (args[i].equals("-C")) { } else if (args[i].equals("-C")) {
cFile = coreName + ".c"; cFile = coreName + ".c";
hFile = coreName + ".h"; hFile = coreName + ".h";
...@@ -170,36 +185,36 @@ public class LabComm { ...@@ -170,36 +185,36 @@ public class LabComm {
boolean prettyOnStdout = true; boolean prettyOnStdout = true;
if (cFile != null) { if (cFile != null) {
if (verbose) { System.err.println("Generating C: " + cFile); } if (verbose) { System.err.println("Generating C: " + cFile); }
genC(ast, cFile, cIncludes, coreName, cPrefix); genC(ast, cFile, cIncludes, coreName, cPrefix, ver);
prettyOnStdout = false; prettyOnStdout = false;
} }
if (hFile != null) { if (hFile != null) {
if (verbose) { System.err.println("Generating H: " + hFile); } if (verbose) { System.err.println("Generating H: " + hFile); }
genH(ast, hFile, hIncludes, coreName, cPrefix); genH(ast, hFile, hIncludes, coreName, cPrefix, ver);
prettyOnStdout = false; prettyOnStdout = false;
} }
if (csFile != null) { if (csFile != null) {
if (verbose) { System.err.println("Generating C#: " + csFile); } if (verbose) { System.err.println("Generating C#: " + csFile); }
genCS(ast, csFile, csNamespace); genCS(ast, csFile, csNamespace, ver);
prettyOnStdout = false; prettyOnStdout = false;
} }
if (javaDir != null) { if (javaDir != null) {
if (verbose) { System.err.println("Generating Java: " + javaDir); } if (verbose) { System.err.println("Generating Java: " + javaDir); }
genJava(ast, javaDir, javaPackage); genJava(ast, javaDir, javaPackage, ver);
prettyOnStdout = false; prettyOnStdout = false;
} }
if (pythonFile != null) { if (pythonFile != null) {
if (verbose) { if (verbose) {
System.err.println("Generating Python: " + pythonFile); System.err.println("Generating Python: " + pythonFile);
} }
genPython(ast, pythonFile, prefix); genPython(ast, pythonFile, prefix, ver);
prettyOnStdout = false; prettyOnStdout = false;
} }
if (rapidFile != null) { if (rapidFile != null) {
if (verbose) { if (verbose) {
System.err.println("Generating RAPID: " + rapidFile); System.err.println("Generating RAPID: " + rapidFile);
} }
genRAPID(ast, rapidFile, coreName); genRAPID(ast, rapidFile, coreName, ver);
prettyOnStdout = false; prettyOnStdout = false;
} }
if (prettyFile != null) { if (prettyFile != null) {
...@@ -223,9 +238,9 @@ public class LabComm { ...@@ -223,9 +238,9 @@ public class LabComm {
try { try {
FileOutputStream f = new FileOutputStream(typeinfoFile); FileOutputStream f = new FileOutputStream(typeinfoFile);
PrintStream out = new PrintStream(f); PrintStream out = new PrintStream(f);
ast.C_info(out, cPrefix); ast.C_info(out, cPrefix, ver);
ast.Java_info(out); ast.Java_info(out, ver);
ast.CS_info(out, csNamespace); ast.CS_info(out, csNamespace, ver);
prettyOnStdout = false; prettyOnStdout = false;
} catch (IOException e) { } catch (IOException e) {
System.err.println("IOException: " + typeinfoFile + " " + e); System.err.println("IOException: " + typeinfoFile + " " + e);
...@@ -239,14 +254,14 @@ public class LabComm { ...@@ -239,14 +254,14 @@ public class LabComm {
} }
private static void genH(Program p, String hName, private static void genH(Program p, String hName,
Vector cIncludes, String coreName, String prefix) { Vector cIncludes, String coreName, String prefix, int ver) {
try { try {
FileOutputStream f; FileOutputStream f;
PrintStream out; PrintStream out;
f = new FileOutputStream(hName); f = new FileOutputStream(hName);
out = new PrintStream(f); out = new PrintStream(f);
p.C_genH(out, cIncludes, coreName, prefix); p.C_genH(out, cIncludes, coreName, prefix, ver);
out.close(); out.close();
} catch (IOException e) { } catch (IOException e) {
System.err.println("IOException: " + hName + " " + e); System.err.println("IOException: " + hName + " " + e);
...@@ -254,59 +269,57 @@ public class LabComm { ...@@ -254,59 +269,57 @@ public class LabComm {
} }
private static void genC(Program p, String cName, private static void genC(Program p, String cName,
Vector cIncludes, String coreName, String prefix) { Vector cIncludes, String coreName, String prefix, int ver) {
try { try {
FileOutputStream f; FileOutputStream f;
PrintStream out; PrintStream out;
f = new FileOutputStream(cName); f = new FileOutputStream(cName);
out = new PrintStream(f); out = new PrintStream(f);
p.C_genC(out, cIncludes, coreName, prefix); p.C_genC(out, cIncludes, coreName, prefix, ver);
out.close(); out.close();
} catch (IOException e) { } catch (IOException e) {
System.err.println("IOException: " + cName + " " + e); System.err.println("IOException: " + cName + " " + e);
} }
} }
private static void genCS(Program p, String csName, String csNamespace) { private static void genCS(Program p, String csName, String csNamespace, int ver) {
try { try {
p.CS_gen(csName, csNamespace); p.CS_gen(csName, csNamespace, ver);
} catch (IOException e) { } catch (IOException e) {
System.err.println("IOException: " + csName + " " + System.err.println("IOException: " + csName + " " +
csNamespace + " " + e); csNamespace + " " + e);
} }
} }
private static void genJava(Program p, String dirName, String packageName) { private static void genJava(Program p, String dirName, String packageName, int ver) {
try { try {
p.J_gen(dirName, packageName); p.J_gen(dirName, packageName, ver);
} catch (IOException e) {