Skip to content
Snippets Groups Projects
Commit 69cb93b4 authored by Sven Gestegård Robertz's avatar Sven Gestegård Robertz
Browse files

refactored compiler main method into separate methods

parent df2a18ef
No related branches found
No related tags found
No related merge requests found
...@@ -56,7 +56,10 @@ aspect C_CodeGenEnv { ...@@ -56,7 +56,10 @@ aspect C_CodeGenEnv {
this.qualid = qualid; this.qualid = qualid;
this.lcName = lcName; this.lcName = lcName;
this.rawPrefix = rawPrefix; this.rawPrefix = rawPrefix;
if (rawPrefix.equals("")) { if (rawPrefix == null) {
System.err.println("WARNING: prefix==null");
this.prefix = "";
} else if (rawPrefix.equals("")) {
this.prefix = rawPrefix; this.prefix = rawPrefix;
} else { } else {
this.prefix = rawPrefix + "_"; this.prefix = rawPrefix + "_";
......
...@@ -37,6 +37,113 @@ public class LabComm { ...@@ -37,6 +37,113 @@ public class LabComm {
println(" --typeinfo=TIFILE Generates typeinfo in TIFILE"); println(" --typeinfo=TIFILE Generates typeinfo in TIFILE");
} }
/** 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);
}
}
private static void genH(Program p, String hName,
Vector cIncludes, String coreName, String prefix, int ver) {
try {
FileOutputStream f;
PrintStream out;
f = new FileOutputStream(hName);
out = new PrintStream(f);
p.C_genH(out, cIncludes, coreName, prefix, ver);
out.close();
} catch (IOException e) {
System.err.println("IOException: " + hName + " " + e);
}
}
private static void genC(Program p, String cName,
Vector cIncludes, String coreName, String prefix, int ver) {
try {
FileOutputStream f;
PrintStream out;
f = new FileOutputStream(cName);
out = new PrintStream(f);
p.C_genC(out, cIncludes, coreName, prefix, ver);
out.close();
} catch (IOException e) {
System.err.println("IOException: " + cName + " " + e);
}
}
private static void genCS(Program p, String csName, String csNamespace, int ver) {
try {
p.CS_gen(csName, csNamespace, ver);
} catch (IOException e) {
System.err.println("IOException: " + csName + " " +
csNamespace + " " + e);
}
}
private static void genJava(Program p, String dirName, String packageName, int ver) {
try {
p.J_gen(dirName, packageName, ver);
} catch (IOException e) {
System.err.println("IOException: " + dirName + " " +
packageName + " " + e);
}
}
private static void genPython(Program p, String filename, String prefix, int ver) {
try {
FileOutputStream f;
PrintStream out;
f = new FileOutputStream(filename);
out = new PrintStream(f);
p.Python_gen(out, prefix, ver);
out.close();
} catch (IOException e) {
System.err.println("IOException: " + filename + " " + e);
}
}
private static void genRAPID(Program p, String filename, String prefix, int ver) {
try {
p.RAPID_gen(filename, prefix, ver);
} catch (IOException e) {
System.err.println("IOException: " + filename + " " + e);
}
}
/** Helper class to contain command line options
and their associated behaviour
**/
private static class Opts {
final String[] args;
String coreName = null;
String prefix = null;
boolean verbose = false;
int ver = 2013; //Version 2013 as default
String cFile = null;
String hFile = null;
Vector cIncludes = new Vector();
String cPrefix; // gets default value (prefix) in processFilename
String csFile = null;
String csNamespace = null;
String javaDir = null;
String javaPackage = "";
String pythonFile = null;
String prettyFile = null;
String typeinfoFile = null;
String rapidFile = null;
String fileName = null;
Opts(String[] args) {
this.args = args;
}
private static String getCoreName(String s) { private static String getCoreName(String s) {
int i = s.lastIndexOf('.'); int i = s.lastIndexOf('.');
return s.substring(0, i > 0 ? i : s.length()); return s.substring(0, i > 0 ? i : s.length());
...@@ -56,18 +163,7 @@ public class LabComm { ...@@ -56,18 +163,7 @@ public class LabComm {
return s.substring(s.lastIndexOf('/') + 1, s.length()); return s.substring(s.lastIndexOf('/') + 1, s.length());
} }
/** To be cleaned up. boolean processFilename(){
*/
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) {
String fileName = null;
// Scan for first non-option // Scan for first non-option
for (int i = 0 ; i < args.length ; i++) { for (int i = 0 ; i < args.length ; i++) {
if (! args[i].startsWith("-")) { if (! args[i].startsWith("-")) {
...@@ -75,27 +171,15 @@ public class LabComm { ...@@ -75,27 +171,15 @@ public class LabComm {
break; break;
} }
} }
String coreName = null;
String prefix = null;
if (fileName != null) { if (fileName != null) {
coreName = getBaseName(fileName); coreName = getBaseName(fileName);
prefix = getPrefix(coreName); prefix = getPrefix(coreName);
cPrefix = prefix;
}
return fileName != null;
} }
boolean verbose = false;
int ver = 2013; //Version 2013 as default
String cFile = null;
String hFile = null;
Vector cIncludes = new Vector();
String cPrefix = prefix;
String csFile = null;
String csNamespace = null;
String javaDir = null;
String javaPackage = "";
String pythonFile = null;
String prettyFile = null;
String typeinfoFile = null;
String rapidFile = null;
void processArgs(){
for (int i = 0 ; i < args.length ; i++) { for (int i = 0 ; i < args.length ; i++) {
if (fileName == null || if (fileName == null ||
args[i].equals("-help") || args[i].equals("-help") ||
...@@ -147,16 +231,18 @@ public class LabComm { ...@@ -147,16 +231,18 @@ public class LabComm {
System.exit(2); System.exit(2);
} }
} }
if (fileName == null) { if(prefix==null){
print_help(); System.err.println(" WARNING! prefix==null");
System.exit(1); prefix="";
} else { }
}
Program parseFile(){
Program ast = null; Program ast = null;
try { try {
// Check for errors // Check for errors
LabCommScanner scanner = new LabCommScanner(new FileReader(fileName)); LabCommScanner scanner = new LabCommScanner(
new FileReader(fileName));
LabCommParser parser = new LabCommParser(); LabCommParser parser = new LabCommParser();
Program p = (Program)parser.parse(scanner); Program p = (Program)parser.parse(scanner);
Collection errors = new LinkedList(); Collection errors = new LinkedList();
...@@ -177,48 +263,73 @@ public class LabComm { ...@@ -177,48 +263,73 @@ public class LabComm {
} catch (beaver.Parser.Exception e) { } catch (beaver.Parser.Exception e) {
System.err.println(e.getMessage()); System.err.println(e.getMessage());
} }
if (ast != null) { return ast;
}
boolean generateC(Program ast) {
boolean wroteFile = false;
Vector hIncludes = new Vector(cIncludes); Vector hIncludes = new Vector(cIncludes);
if (hFile != null) { if (hFile != null) {
cIncludes.add(hFile); cIncludes.add(hFile);
} }
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, ver); genC(ast, cFile, cIncludes, coreName, cPrefix, ver);
prettyOnStdout = false; wroteFile = true;
} }
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, ver); genH(ast, hFile, hIncludes, coreName, cPrefix, ver);
prettyOnStdout = false; wroteFile = true;
}
return wroteFile;
} }
boolean generateCS(Program ast) {
boolean wroteFile = 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, ver); genCS(ast, csFile, csNamespace, ver);
prettyOnStdout = false; wroteFile = true;
}
return wroteFile;
} }
boolean generateJava(Program ast) {
boolean wroteFile = 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, ver); genJava(ast, javaDir, javaPackage, ver);
prettyOnStdout = false; wroteFile = true;
} }
return wroteFile;
}
boolean generatePython(Program ast) {
boolean wroteFile = 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, ver); genPython(ast, pythonFile, prefix, ver);
prettyOnStdout = false; wroteFile = true;
} }
return wroteFile;
}
boolean generateRAPID(Program ast) {
boolean wroteFile = 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, ver); genRAPID(ast, rapidFile, coreName, ver);
prettyOnStdout = false; wroteFile = true;
} }
return wroteFile;
}
boolean generatePrettyPrint(Program ast) {
boolean wroteFile = false;
if (prettyFile != null) { if (prettyFile != null) {
if (verbose) { if (verbose) {
System.err.println("Generating Pretty: " + prettyFile); System.err.println("Generating Pretty: " + prettyFile);
...@@ -228,11 +339,16 @@ public class LabComm { ...@@ -228,11 +339,16 @@ public class LabComm {
PrintStream out = new PrintStream(f); PrintStream out = new PrintStream(f);
ast.pp(out); ast.pp(out);
out.close(); out.close();
prettyOnStdout = false; wroteFile = true;
} catch (IOException e) { } catch (IOException e) {
System.err.println("IOException: " + prettyFile + " " + e); System.err.println("IOException: " + prettyFile + " " + e);
} }
} }
return wroteFile;
}
boolean generateTypeinfo(Program ast) {
boolean wroteFile = false;
if (typeinfoFile != null) { if (typeinfoFile != null) {
if (verbose) { if (verbose) {
System.err.println("Generating TypeInfo: " + typeinfoFile); System.err.println("Generating TypeInfo: " + typeinfoFile);
...@@ -243,89 +359,46 @@ public class LabComm { ...@@ -243,89 +359,46 @@ public class LabComm {
ast.C_info(out, cPrefix, ver); ast.C_info(out, cPrefix, ver);
ast.Java_info(out, ver); ast.Java_info(out, ver);
ast.CS_info(out, csNamespace, ver); ast.CS_info(out, csNamespace, ver);
prettyOnStdout = false; wroteFile = true;
} catch (IOException e) { } catch (IOException e) {
System.err.println("IOException: " + typeinfoFile + " " + e); System.err.println("IOException: " + typeinfoFile + " " + e);
} }
} }
if (prettyOnStdout) { return wroteFile;
ast.pp(System.out);
}
} else {
// Catch-all for compilation errors
System.err.println("Error in specification");
System.exit(3);
}
}
}
private static void genH(Program p, String hName,
Vector cIncludes, String coreName, String prefix, int ver) {
try {
FileOutputStream f;
PrintStream out;
f = new FileOutputStream(hName);
out = new PrintStream(f);
p.C_genH(out, cIncludes, coreName, prefix, ver);
out.close();
} catch (IOException e) {
System.err.println("IOException: " + hName + " " + e);
} }
} }
private static void genC(Program p, String cName,
Vector cIncludes, String coreName, String prefix, int ver) {
try {
FileOutputStream f;
PrintStream out;
f = new FileOutputStream(cName); public static void main(String[] args) {
out = new PrintStream(f); Opts opts = new Opts(args);
p.C_genC(out, cIncludes, coreName, prefix, ver); if(!opts.processFilename()) {
out.close(); print_help();
} catch (IOException e) { System.exit(1);
System.err.println("IOException: " + cName + " " + e); } else {
} opts.processArgs();
} Program ast = opts.parseFile();
private static void genCS(Program p, String csName, String csNamespace, int ver) { if (ast != null) {
try {
p.CS_gen(csName, csNamespace, ver);
} catch (IOException e) {
System.err.println("IOException: " + csName + " " +
csNamespace + " " + e);
}
}
private static void genJava(Program p, String dirName, String packageName, int ver) { boolean fileWritten = false;
try {
p.J_gen(dirName, packageName, ver);
} catch (IOException e) {
System.err.println("IOException: " + dirName + " " +
packageName + " " + e);
}
}
private static void genPython(Program p, String filename, String prefix, int ver) { fileWritten |= opts.generateC(ast);
try { fileWritten |= opts.generateCS(ast);
FileOutputStream f; fileWritten |= opts.generateJava(ast);
PrintStream out; fileWritten |= opts.generatePython(ast);
fileWritten |= opts.generateRAPID(ast);
fileWritten |= opts.generatePrettyPrint(ast);
fileWritten |= opts.generateTypeinfo(ast);
f = new FileOutputStream(filename); // if no output to files, prettyprint on stdout
out = new PrintStream(f); if (!fileWritten) {
p.Python_gen(out, prefix, ver); ast.pp(System.out);
out.close();
} catch (IOException e) {
System.err.println("IOException: " + filename + " " + e);
} }
} else {
// Catch-all for compilation errors
System.err.println("Error in specification");
System.exit(3);
} }
private static void genRAPID(Program p, String filename, String prefix, int ver) {
try {
p.RAPID_gen(filename, prefix, ver);
} catch (IOException e) {
System.err.println("IOException: " + filename + " " + e);
} }
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment