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

refactored grammar: added TypeInstance :: DataType Name and renamed Program...

refactored grammar: added TypeInstance :: DataType Name  and renamed Program to Specification (based on cherry-picked (parts of) 9d2ca1)
parent e2a0f191
...@@ -176,11 +176,11 @@ aspect CS_StructName { ...@@ -176,11 +176,11 @@ aspect CS_StructName {
inh int Decl.CS_Depth(); inh int Decl.CS_Depth();
inh int DataType.CS_Depth(); inh int DataType.CS_Depth();
eq Program.getDecl(int i).CS_Depth() = 0; eq Specification.getDecl(int i).CS_Depth() = 0;
eq StructType.getField(int i).CS_Depth() = CS_Depth() + 1; eq StructType.getField(int i).CS_Depth() = CS_Depth() + 1;
inh String DataType.CS_structName(); inh String DataType.CS_structName();
eq Program.getDecl(int i).CS_structName() = getDecl(i).getName(); eq Specification.getDecl(int i).CS_structName() = getDecl(i).getName();
eq StructType.getField(int i).CS_structName() { eq StructType.getField(int i).CS_structName() {
if (CS_Depth() == 0) { if (CS_Depth() == 0) {
return "struct_" + getField(i).getName(); return "struct_" + getField(i).getName();
...@@ -201,7 +201,7 @@ aspect CS_Void { ...@@ -201,7 +201,7 @@ aspect CS_Void {
aspect CS_CodeGen { aspect CS_CodeGen {
public void Program.CS_gen(String file, public void Specification.CS_gen(String file,
String namespace, int version) throws IOException { String namespace, int version) throws IOException {
// Registration class // Registration class
CS_env env = new CS_env(new File(file), version); CS_env env = new CS_env(new File(file), version);
...@@ -230,7 +230,7 @@ aspect CS_CodeGen { ...@@ -230,7 +230,7 @@ aspect CS_CodeGen {
aspect CS_Register { aspect CS_Register {
public void Program.CS_emitTypeRegister(CS_env env) { public void Specification.CS_emitTypeRegister(CS_env env) {
} }
public void Decl.CS_emitTypeRegister(CS_env env) { public void Decl.CS_emitTypeRegister(CS_env env) {
...@@ -935,7 +935,7 @@ aspect CS_Signature { ...@@ -935,7 +935,7 @@ aspect CS_Signature {
aspect CS_Info { aspect CS_Info {
public void Program.CS_info(PrintStream out, String namespace, int version) { public void Specification.CS_info(PrintStream out, String namespace, int version) {
CS_env env = new CS_env(out, version); CS_env env = new CS_env(out, version);
if (namespace == null) { if (namespace == null) {
namespace = ""; namespace = "";
......
...@@ -177,7 +177,7 @@ aspect C_IsDynamic { ...@@ -177,7 +177,7 @@ aspect C_IsDynamic {
aspect C_CodeGen { aspect C_CodeGen {
public void Program.C_genH(PrintStream out, Vector includes, public void Specification.C_genH(PrintStream out, Vector includes,
String lcName, String prefix, int version) { String lcName, String prefix, int version) {
C_env env = new C_env("", lcName, prefix, out, version); C_env env = new C_env("", lcName, prefix, out, version);
...@@ -203,7 +203,7 @@ aspect C_CodeGen { ...@@ -203,7 +203,7 @@ aspect C_CodeGen {
env.println("#endif"); env.println("#endif");
} }
public void Program.C_genC(PrintStream out, Vector includes, public void Specification.C_genC(PrintStream out, Vector includes,
String lcName, String prefix, int version) { String lcName, String prefix, int version) {
C_env env = new C_env("", lcName, prefix, out, version); C_env env = new C_env("", lcName, prefix, out, version);
...@@ -219,7 +219,7 @@ aspect C_CodeGen { ...@@ -219,7 +219,7 @@ aspect C_CodeGen {
C_emitC(env); C_emitC(env);
} }
public void Program.C_emitH(C_env env) { public void Specification.C_emitH(C_env env) {
for (int i = 0; i < getNumDecl(); i++) { for (int i = 0; i < getNumDecl(); i++) {
getDecl(i).C_emitType(env); getDecl(i).C_emitType(env);
getDecl(i).C_emitDecoderDeclaration(env); getDecl(i).C_emitDecoderDeclaration(env);
...@@ -233,7 +233,7 @@ aspect C_CodeGen { ...@@ -233,7 +233,7 @@ aspect C_CodeGen {
C_emitForAll(env); C_emitForAll(env);
} }
public void Program.C_emitC(C_env env) { public void Specification.C_emitC(C_env env) {
for (int i = 0; i < getNumDecl(); i++) { for (int i = 0; i < getNumDecl(); i++) {
getDecl(i).C_emitSignature(env); getDecl(i).C_emitSignature(env);
getDecl(i).C_emitDecoder(env); getDecl(i).C_emitDecoder(env);
...@@ -1462,7 +1462,7 @@ aspect C_Constructor { ...@@ -1462,7 +1462,7 @@ aspect C_Constructor {
" not declared"); " not declared");
} }
public void Program.C_emitConstructor(C_env env) { public void Specification.C_emitConstructor(C_env env) {
env.println("LABCOMM_CONSTRUCTOR void init_" + env.println("LABCOMM_CONSTRUCTOR void init_" +
env.prefix + "_signatures(void)"); env.prefix + "_signatures(void)");
env.println("{"); env.println("{");
...@@ -1499,7 +1499,7 @@ aspect C_Constructor { ...@@ -1499,7 +1499,7 @@ aspect C_Constructor {
" not declared"); " not declared");
} }
public void Program.C_emitConstructorDeclaration(C_env env) { public void Specification.C_emitConstructorDeclaration(C_env env) {
env.println("void init_" + env.prefix + "_signatures(void);"); env.println("void init_" + env.prefix + "_signatures(void);");
} }
...@@ -1688,7 +1688,7 @@ aspect C_Sizeof { ...@@ -1688,7 +1688,7 @@ aspect C_Sizeof {
aspect C_forAll { aspect C_forAll {
public void Program.C_emitForAll(C_env env) { public void Specification.C_emitForAll(C_env env) {
env.print("#define LABCOMM_FORALL_SAMPLES_" + env.lcName + env.print("#define LABCOMM_FORALL_SAMPLES_" + env.lcName +
"(func, sep)"); "(func, sep)");
env.indent(); env.indent();
...@@ -1718,7 +1718,7 @@ aspect C_forAll { ...@@ -1718,7 +1718,7 @@ aspect C_forAll {
aspect C_Info { aspect C_Info {
public void Program.C_info(PrintStream out, String prefix, int version) { public void Specification.C_info(PrintStream out, String prefix, int version) {
C_env env = new C_env("", "", prefix, out, version); 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);
......
aspect DeclNames { aspect DeclNames {
inh String DataType.declName(); inh String DataType.declName();
eq Decl.DataType().declName() = getName(); eq Decl.getTypeInstance().declName() = getName();
inh String Field.declName(); inh String Field.declName();
eq StructType.getField(int i).declName() = declName(); eq StructType.getField(int i).declName() = declName();
...@@ -9,6 +9,6 @@ aspect DeclNames { ...@@ -9,6 +9,6 @@ aspect DeclNames {
inh Decl DataType.parentDecl(); inh Decl DataType.parentDecl();
inh Decl Field.parentDecl(); inh Decl Field.parentDecl();
eq Decl.getDataType().parentDecl() = this; eq Decl.getTypeInstance().parentDecl() = this;
eq StructType.getField(int i).parentDecl() = parentDecl(); eq StructType.getField(int i).parentDecl() = parentDecl();
} }
...@@ -179,11 +179,11 @@ aspect Java_StructName { ...@@ -179,11 +179,11 @@ aspect Java_StructName {
inh int Decl.Java_Depth(); inh int Decl.Java_Depth();
inh int DataType.Java_Depth(); inh int DataType.Java_Depth();
eq Program.getDecl(int i).Java_Depth() = 0; eq Specification.getDecl(int i).Java_Depth() = 0;
eq StructType.getField(int i).Java_Depth() = Java_Depth() + 1; eq StructType.getField(int i).Java_Depth() = Java_Depth() + 1;
inh String DataType.Java_structName(); inh String DataType.Java_structName();
eq Program.getDecl(int i).Java_structName() = getDecl(i).getName(); eq Specification.getDecl(int i).Java_structName() = getDecl(i).getName();
eq StructType.getField(int i).Java_structName() { eq StructType.getField(int i).Java_structName() {
if (Java_Depth() == 0) { if (Java_Depth() == 0) {
return "struct_" + getField(i).getName(); return "struct_" + getField(i).getName();
...@@ -204,7 +204,7 @@ aspect Java_Void { ...@@ -204,7 +204,7 @@ aspect Java_Void {
aspect Java_CodeGen { aspect Java_CodeGen {
public void Program.J_gen(PrintStream ps, String pack, int version) throws IOException { public void Specification.J_gen(PrintStream ps, String pack, int version) throws IOException {
Java_env env; Java_env env;
env = new Java_env(version, ps); env = new Java_env(version, ps);
for (int i = 0; i < getNumDecl(); i++) { for (int i = 0; i < getNumDecl(); i++) {
...@@ -219,7 +219,7 @@ aspect Java_CodeGen { ...@@ -219,7 +219,7 @@ aspect Java_CodeGen {
env.close(); env.close();
} }
public void Program.J_gen(String dir, String pack, int version) throws IOException { public void Specification.J_gen(String dir, String pack, int version) throws IOException {
Java_env env; Java_env env;
for (int i = 0; i < getNumDecl(); i++) { for (int i = 0; i < getNumDecl(); i++) {
Decl d = getDecl(i); Decl d = getDecl(i);
...@@ -236,7 +236,7 @@ aspect Java_CodeGen { ...@@ -236,7 +236,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, int version) throws IOException { public void Specification.J_gen(Map<String,String> src, String pack, int version) throws IOException {
Java_env env; Java_env env;
for (int i = 0; i < getNumDecl(); i++) { for (int i = 0; i < getNumDecl(); i++) {
Decl d = getDecl(i); Decl d = getDecl(i);
...@@ -1032,7 +1032,7 @@ aspect Java_Signature { ...@@ -1032,7 +1032,7 @@ aspect Java_Signature {
aspect Java_Info { aspect Java_Info {
public void Program.Java_info(PrintStream out, int version) { public void Specification.Java_info(PrintStream out, int version) {
Java_env env = new Java_env(version, 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);
......
Program ::= Decl*; Specification ::= Decl*;
abstract Decl ::= DataType <Name:String> /Signature/; abstract Decl ::= TypeInstance /Signature/;
TypeInstance ::= DataType <Name:String>;
TypeDecl : Decl; TypeDecl : Decl;
SampleDecl : Decl; SampleDecl : Decl;
...@@ -17,7 +19,7 @@ IntSignatureLine : DataSignatureLine ::= <Data:int>; ...@@ -17,7 +19,7 @@ IntSignatureLine : DataSignatureLine ::= <Data:int>;
StringSignatureLine : DataSignatureLine ::= <Data:String>; StringSignatureLine : DataSignatureLine ::= <Data:String>;
TypeRefSignatureLine : SignatureLine ::= Decl; TypeRefSignatureLine : SignatureLine ::= Decl;
Field ::= DataType <Name:String>; Field : TypeInstance;
abstract DataType; abstract DataType;
VoidType : DataType; VoidType : DataType;
......
...@@ -48,7 +48,7 @@ public class LabComm { ...@@ -48,7 +48,7 @@ public class LabComm {
} }
} }
private static void genH(Program p, String hName, private static void genH(Specification p, String hName,
Vector cIncludes, String coreName, String prefix, int ver) { Vector cIncludes, String coreName, String prefix, int ver) {
try { try {
FileOutputStream f; FileOutputStream f;
...@@ -63,7 +63,7 @@ public class LabComm { ...@@ -63,7 +63,7 @@ public class LabComm {
} }
} }
private static void genC(Program p, String cName, private static void genC(Specification p, String cName,
Vector cIncludes, String coreName, String prefix, int ver) { Vector cIncludes, String coreName, String prefix, int ver) {
try { try {
FileOutputStream f; FileOutputStream f;
...@@ -78,7 +78,7 @@ public class LabComm { ...@@ -78,7 +78,7 @@ public class LabComm {
} }
} }
private static void genCS(Program p, String csName, String csNamespace, int ver) { private static void genCS(Specification p, String csName, String csNamespace, int ver) {
// throw new Error("C# generation currently disabled"); // throw new Error("C# generation currently disabled");
try { try {
p.CS_gen(csName, csNamespace, ver); p.CS_gen(csName, csNamespace, ver);
...@@ -88,7 +88,7 @@ public class LabComm { ...@@ -88,7 +88,7 @@ public class LabComm {
} }
} }
private static void genJava(Program p, String dirName, String packageName, int ver) { private static void genJava(Specification p, String dirName, String packageName, int ver) {
try { try {
p.J_gen(dirName, packageName, ver); p.J_gen(dirName, packageName, ver);
} catch (IOException e) { } catch (IOException e) {
...@@ -97,7 +97,7 @@ public class LabComm { ...@@ -97,7 +97,7 @@ public class LabComm {
} }
} }
private static void genPython(Program p, String filename, String prefix, int ver) { private static void genPython(Specification p, String filename, String prefix, int ver) {
try { try {
FileOutputStream f; FileOutputStream f;
PrintStream out; PrintStream out;
...@@ -111,7 +111,7 @@ public class LabComm { ...@@ -111,7 +111,7 @@ public class LabComm {
} }
} }
private static void genRAPID(Program p, String filename, String prefix, int ver) { private static void genRAPID(Specification p, String filename, String prefix, int ver) {
try { try {
p.RAPID_gen(filename, prefix, ver); p.RAPID_gen(filename, prefix, ver);
} catch (IOException e) { } catch (IOException e) {
...@@ -239,14 +239,14 @@ public class LabComm { ...@@ -239,14 +239,14 @@ public class LabComm {
} }
} }
Program parseFile(){ Specification parseFile(){
Program ast = null; Specification ast = null;
try { try {
// Check for errors // Check for errors
LabCommScanner scanner = new LabCommScanner( LabCommScanner scanner = new LabCommScanner(
new FileReader(fileName)); new FileReader(fileName));
LabCommParser parser = new LabCommParser(); LabCommParser parser = new LabCommParser();
Program p = (Program)parser.parse(scanner); Specification p = (Specification)parser.parse(scanner);
Collection errors = new LinkedList(); Collection errors = new LinkedList();
p.errorCheck(errors); p.errorCheck(errors);
...@@ -268,7 +268,7 @@ public class LabComm { ...@@ -268,7 +268,7 @@ public class LabComm {
return ast; return ast;
} }
boolean generateC(Program ast) { boolean generateC(Specification ast) {
boolean wroteFile = false; boolean wroteFile = false;
Vector hIncludes = new Vector(cIncludes); Vector hIncludes = new Vector(cIncludes);
if (hFile != null) { if (hFile != null) {
...@@ -287,7 +287,7 @@ public class LabComm { ...@@ -287,7 +287,7 @@ public class LabComm {
return wroteFile; return wroteFile;
} }
boolean generateCS(Program ast) { boolean generateCS(Specification ast) {
boolean wroteFile = false; boolean wroteFile = false;
if (csFile != null) { if (csFile != null) {
printStatus("C#: " , csFile); printStatus("C#: " , csFile);
...@@ -297,7 +297,7 @@ public class LabComm { ...@@ -297,7 +297,7 @@ public class LabComm {
return wroteFile; return wroteFile;
} }
boolean generateJava(Program ast) { boolean generateJava(Specification ast) {
boolean wroteFile = false; boolean wroteFile = false;
if (javaDir != null) { if (javaDir != null) {
printStatus("Java: " , javaDir); printStatus("Java: " , javaDir);
...@@ -307,7 +307,7 @@ public class LabComm { ...@@ -307,7 +307,7 @@ public class LabComm {
return wroteFile; return wroteFile;
} }
boolean generatePython(Program ast) { boolean generatePython(Specification ast) {
boolean wroteFile = false; boolean wroteFile = false;
if (pythonFile != null) { if (pythonFile != null) {
printStatus("Python: " , pythonFile); printStatus("Python: " , pythonFile);
...@@ -317,7 +317,7 @@ public class LabComm { ...@@ -317,7 +317,7 @@ public class LabComm {
return wroteFile; return wroteFile;
} }
boolean generateRAPID(Program ast) { boolean generateRAPID(Specification ast) {
boolean wroteFile = false; boolean wroteFile = false;
if (rapidFile != null) { if (rapidFile != null) {
printStatus("RAPID: " , rapidFile); printStatus("RAPID: " , rapidFile);
...@@ -326,7 +326,7 @@ public class LabComm { ...@@ -326,7 +326,7 @@ public class LabComm {
} }
return wroteFile; return wroteFile;
} }
boolean generatePrettyPrint(Program ast) { boolean generatePrettyPrint(Specification ast) {
boolean wroteFile = false; boolean wroteFile = false;
if (prettyFile != null) { if (prettyFile != null) {
printStatus("Pretty: " , prettyFile); printStatus("Pretty: " , prettyFile);
...@@ -343,7 +343,7 @@ public class LabComm { ...@@ -343,7 +343,7 @@ public class LabComm {
return wroteFile; return wroteFile;
} }
boolean generateTypeinfo(Program ast) { boolean generateTypeinfo(Specification ast) {
boolean wroteFile = false; boolean wroteFile = false;
if (typeinfoFile != null) { if (typeinfoFile != null) {
printStatus("TypeInfo: " , typeinfoFile); printStatus("TypeInfo: " , typeinfoFile);
...@@ -376,7 +376,7 @@ public class LabComm { ...@@ -376,7 +376,7 @@ public class LabComm {
System.exit(1); System.exit(1);
} else { } else {
opts.processArgs(); opts.processArgs();
Program ast = opts.parseFile(); Specification ast = opts.parseFile();
if (ast != null) { if (ast != null) {
......
...@@ -34,9 +34,9 @@ ...@@ -34,9 +34,9 @@
} }
:}; :};
Program goal = Specification goal =
/* Empty program */ {: return new Program(); :} /* Empty program */ {: return new Specification(); :}
| decl_list.l {: return new Program(l); :} | decl_list.l {: return new Specification(l); :}
; ;
List decl_list = List decl_list =
...@@ -54,6 +54,12 @@ List var_decl_list = ...@@ -54,6 +54,12 @@ List var_decl_list =
| var_decl_list.l var_decl.v {: return l.add(v); :} | var_decl_list.l var_decl.v {: return l.add(v); :}
; ;
TypeInstance type_instance =
type.t IDENTIFIER {: return new TypeInstance(t, IDENTIFIER); :}
| type.t IDENTIFIER dim_list.d
{: return new TypeInstance(new ParseArrayType(t, d), IDENTIFIER); :}
;
Field var_decl = Field var_decl =
type.t IDENTIFIER SEMICOLON {: return new Field(t, IDENTIFIER); :} type.t IDENTIFIER SEMICOLON {: return new Field(t, IDENTIFIER); :}
| type.t IDENTIFIER dim_list.d SEMICOLON | type.t IDENTIFIER dim_list.d SEMICOLON
...@@ -61,17 +67,10 @@ Field var_decl = ...@@ -61,17 +67,10 @@ Field var_decl =
; ;
TypeDecl type_decl = TypeDecl type_decl =
TYPEDEF type.t IDENTIFIER SEMICOLON {: return new TypeDecl(t, IDENTIFIER); :} TYPEDEF type_instance.t SEMICOLON {: return new TypeDecl(t); :} ;
| TYPEDEF type.t IDENTIFIER dim_list.d SEMICOLON
{: return new TypeDecl(new ParseArrayType(t, d), IDENTIFIER); :}
;
SampleDecl sample_decl = SampleDecl sample_decl =
SAMPLE type.t IDENTIFIER SEMICOLON SAMPLE type_instance.t SEMICOLON {: return new SampleDecl(t); :} ;
{: return new SampleDecl(t, IDENTIFIER); :}
| SAMPLE type.t IDENTIFIER dim_list.d SEMICOLON
{: return new SampleDecl(new ParseArrayType(t, d), IDENTIFIER); :}
;
DataType type = DataType type =
prim_type.p {: return p; :} prim_type.p {: return p; :}
...@@ -109,8 +108,8 @@ StructType struct_type = ...@@ -109,8 +108,8 @@ StructType struct_type =
STRUCT LBRACE var_decl_list.l RBRACE {: return new StructType(l); :} STRUCT LBRACE var_decl_list.l RBRACE {: return new StructType(l); :}
; ;
VoidType void_type = VoidType void_type =
VOID {: return new VoidType(); :} VOID {: return new VoidType(); :}
; ;
List dim_list = List dim_list =
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
aspect NameAnalysis { aspect NameAnalysis {
inh String Decl.lookupName(String name); inh String Decl.lookupName(String name);
eq Program.getDecl(int index).lookupName(String name) { eq Specification.getDecl(int index).lookupName(String name) {
for (int i = 0; i < index; i++) { for (int i = 0; i < index; i++) {
String s = getDecl(i).getName(); String s = getDecl(i).getName();
if (s.equals(name)) { if (s.equals(name)) {
...@@ -24,7 +24,7 @@ aspect NameAnalysis { ...@@ -24,7 +24,7 @@ aspect NameAnalysis {
inh TypeDecl Decl.lookupType(String name); inh TypeDecl Decl.lookupType(String name);
inh TypeDecl UserType.lookupType(String name); inh TypeDecl UserType.lookupType(String name);
eq Program.getDecl(int index).lookupType(String name) { eq Specification.getDecl(int index).lookupType(String name) {
for(int i = 0; i < index; i++) { for(int i = 0; i < index; i++) {
Decl d = getDecl(i); Decl d = getDecl(i);
if(d instanceof TypeDecl && d.getName().equals(name)) { if(d instanceof TypeDecl && d.getName().equals(name)) {
......
...@@ -6,7 +6,7 @@ aspect PPIndentation { ...@@ -6,7 +6,7 @@ aspect PPIndentation {
inh String Field.pp_indent(); inh String Field.pp_indent();
inh String StructType.pp_indent(); inh String StructType.pp_indent();
eq StructType.getField(int index).pp_indent() = pp_indent() + " "; eq StructType.getField(int index).pp_indent() = pp_indent() + " ";
eq Program.getDecl(int index).pp_indent() = ""; eq Specification.getDecl(int index).pp_indent() = "";
} }
...@@ -18,7 +18,7 @@ aspect PrettyPrint { ...@@ -18,7 +18,7 @@ aspect PrettyPrint {
" not declared"); " not declared");
} }
public void Program.pp(PrintStream out) { public void Specification.pp(PrintStream out) {
for(int i = 0; i < getNumDecl(); i++) { for(int i = 0; i < getNumDecl(); i++) {
getDecl(i).pp(out); getDecl(i).pp(out);
}