Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision

Target

Select target project
  • anders_blomdell/labcomm
  • klaren/labcomm
  • tommyo/labcomm
  • erikj/labcomm
  • sven/labcomm
5 results
Select Git revision
  • anders.blomdell
  • compiler-refactoring
  • labcomm2006
  • labcomm2013
  • master
  • pragma
  • typedefs
  • typeref
  • v2006.0
  • v2013.0
  • v2014.0
  • v2014.1
  • v2014.2
  • v2014.3
  • v2014.4
15 results
Show changes
Commits on Source (90)
Showing
with 1131 additions and 950 deletions
...@@ -2,3 +2,4 @@ ...@@ -2,3 +2,4 @@
*.class *.class
*.o *.o
*.pyc *.pyc
examples/dynamic/gen
SUBDIRS=compiler lib test examples packaging SUBDIRS=compiler lib test examples packaging
export LABCOMM_JAR=$(shell pwd)/compiler/labcomm2014_compiler.jar export LABCOMM_JAR=$(shell pwd)/compiler/labcomm2014_compiler.jar
export LABCOMM=java -jar $(LABCOMM_JAR) export LABCOMM=java -jar $(LABCOMM_JAR)
export VALGRIND=valgrind --leak-check=full --error-exitcode=1
UNAME_S=$(shell uname -s) UNAME_S=$(shell uname -s)
......
aspect Annotations {
syn boolean TypeInstance.hasAnnotations() = getAnnotations().getNumAnnotation()>0;
syn boolean TypeInstance.hasIntentions() = ! intentionSet().isEmpty();
syn String Annotations.getName() = new String(lookup(""));
syn byte[] Annotations.lookup(String key) {
for(Annotation a: getAnnotations()) {
byte[] res = a.lookup(key);
if(res != null) return res;
}
return null;
}
syn byte[] Annotation.lookup(String key) = (getKey().equals(key) ? getValue() : null);
syn boolean Annotation.isIntention() = false;
eq Intention.isIntention() = true;
inh TypeInstance Annotation.parentInstance();
eq TypeInstance.getAnnotations().parentInstance() = this;
coll Set<Intention> TypeInstance.intentionSet() [new HashSet<Intention>()] with add;
Intention contributes this
to TypeInstance.intentionSet()
for parentInstance();
}
aspect SigAnnotations {
inh Decl TypeInstance.parentDecl();
coll Set Decl.allAnnotations() [new HashSet()] with add;
TypeInstance contributes getAnnotationString()
to Decl.allAnnotations()
for parentDecl();
// Helper attribute to get the "outermost" intentions for Decls
syn byte[] TypeInstance.intentionBytes() = getIntentionBytes(sortedIntentions());
syn byte[] Decl.getIntentionBytes() = getTypeInstance().intentionBytes();
static Comparator TypeInstance.intentionComp =
new Comparator<Intention>() {
public int compare(Intention i1, Intention i2) {
return i1.getKey().compareTo(i2.getKey());
}
};
syn int Decl.getNumIntentions() = getTypeInstance().sortedIntentions().getNumChild();
syn List<Intention> TypeInstance.sortedIntentions() {
List<Intention> res = new List<Intention>();
//TODO: refactor out creation of sorted list of intentions
java.util.ArrayList<Intention> sorted = new ArrayList(intentionSet());
java.util.Collections.sort(sorted, intentionComp);
for(Intention i : sorted) {
res.add(i);
}
return res;
}
public DocString.DocString(byte[] bs) {
super("DOCSTRING", bs);
}
public DocString.DocString(String s) {
super("DOCSTRING", s.getBytes());
}
public String Intention.toString() {
return "("+getKey() + ":"+new String(getValue())+")";
}
public String DocString.toString() {
return "\""+new String(getValue())+"\"";
}
/// TESTING
syn String Decl.getAnnotationString() {
StringBuilder sb = new StringBuilder();
Iterator<String> iti = allAnnotations().iterator();
while(iti.hasNext()) {
//Annotation i = iti.next();
//sb.append("("+i.getKey()+" : "+i.getValue()+") ");
String i = iti.next();
sb.append(i);
}
return sb.toString();
}
syn int TypeInstance.fooHash() {
List<Annotation> ints = getAnnotations().getAnnotationList();
int result=0;
for(Annotation i : ints) {
if(i.isIntention()) {
result += i.toString().hashCode();
}
}
return result;
}
syn String TypeInstance.getAnnotationString() {
StringBuilder sb = new StringBuilder();
List<Annotation> ints = getAnnotations().getAnnotationList();
for(Annotation i : ints) {
sb.append(i.toString());
}
return sb.toString();
}
public void Decl.debugAnnotations() {
getTypeInstance().debugAnnotations(getName());
}
public void TypeInstance.debugAnnotations(String context) {
if(hasAnnotations()){
System.out.println(context+".annotations: " + fooHash() + " : " + getAnnotationString());
} else {
//System.out.println(context + " : " + fooHash() + " : " + ": NO ANNOTATIONS ");
}
}
// TESTING END
}
...@@ -13,29 +13,29 @@ aspect ArrayRewrite { ...@@ -13,29 +13,29 @@ aspect ArrayRewrite {
when (! getDim(0).isVariable()) when (! getDim(0).isVariable())
to FixedArrayType { to FixedArrayType {
if (getNumDim() == 1) { if (getNumDim() == 1) {
return new FixedArrayType(getType(), return new FixedArrayType(getDataType(),
getDim(0).getExpList()); getDim(0));
} else { } else {
List l = new List(); List l = new List();
for (int i = 1 ; i < getNumDim() ; i++) { for (int i = 1 ; i < getNumDim() ; i++) {
l.add(getDim(i)); l.add(getDim(i));
} }
return new FixedArrayType(new ParseArrayType(getType(), l), return new FixedArrayType(new ParseArrayType(getDataType(), l),
getDim(0).getExpList()); getDim(0));
} }
} }
when (getDim(0).isVariable()) when (getDim(0).isVariable())
to VariableArrayType { to VariableArrayType {
if (getNumDim() == 1) { if (getNumDim() == 1) {
return new VariableArrayType(getType(), return new VariableArrayType(getDataType(),
getDim(0).getExpList()); getDim(0));
} else { } else {
List l = new List(); List l = new List();
for (int i = 1 ; i < getNumDim() ; i++) { for (int i = 1 ; i < getNumDim() ; i++) {
l.add(getDim(i)); l.add(getDim(i));
} }
return new VariableArrayType(new ParseArrayType(getType(), l), return new VariableArrayType(new ParseArrayType(getDataType(), l),
getDim(0).getExpList()); getDim(0));
} }
} }
} }
......
This diff is collapsed.
This diff is collapsed.
aspect DeclNames { aspect DeclNames {
inh String Type.declName(); inh String DataType.declName();
eq Decl.getType().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();
//TODO: aspect should be renamed to parent-something
inh Decl Type.parentDecl(); //TODO: aspect should be renamed to parent-something
inh Decl Field.parentDecl();
eq Decl.getType().parentDecl() = this; inh Decl DataType.parentDecl();
eq StructType.getField(int i).parentDecl() = parentDecl(); inh Decl Field.parentDecl();
eq Decl.getTypeInstance().parentDecl() = this;
eq StructType.getField(int i).parentDecl() = parentDecl();
} }
...@@ -2,30 +2,29 @@ import java.util.Collection; ...@@ -2,30 +2,29 @@ import java.util.Collection;
aspect ErrorCheck { aspect ErrorCheck {
syn int ASTNode.lineNumber() = getLine(getStart()); syn int ASTNode.lineNumber() = getLine(getStart());
protected String ASTNode.errors = null; protected String ASTNode.errors = null;
protected void ASTNode.error(String s) {
s = "Error at " + lineNumber() + ": " + s;
if(errors == null) {
errors = s;
} else {
errors = errors + "\n" + s;
}
}
protected boolean ASTNode.hasErrors() { protected void ASTNode.error(String s) {
return errors != null; s = "Error at " + lineNumber() + ": " + s;
} if(errors == null) {
public void ASTNode.errorCheck(Collection collection) { errors = s;
nameCheck(); } else {
typeCheck(); errors = errors + "\n" + s;
if(hasErrors()) }
collection.add(errors); }
for(int i = 0; i < getNumChild(); i++) {
getChild(i).errorCheck(collection);
}
}
protected boolean ASTNode.hasErrors() {
return errors != null;
}
public void ASTNode.errorCheck(Collection collection) {
nameCheck();
typeCheck();
if(hasErrors())
collection.add(errors);
for(int i = 0; i < getNumChild(); i++) {
getChild(i).errorCheck(collection);
}
}
} }
import java.util.*; import java.util.*;
aspect FlatSignature { aspect NoIntentionForTypeOrSampledefs {
inh boolean TypeInstance.addIntentions();
eq Decl.getTypeInstance().addIntentions() = false;
eq StructType.getField(int i).addIntentions() = true;
}
aspect FlatSignature {
public SignatureList Decl.flatSignature(int version) { public SignatureList Decl.flatSignature(int version) {
SignatureList result = getSignature().getFlatSignatureList(); SignatureList result = getSignature().getFlatSignatureList();
return result; return result;
} }
public void ASTNode.flatSignature(SignatureList list) { public void ASTNode.flatSignature(SignatureList list) {
throw new Error(this.getClass().getName() + throw new Error(this.getClass().getName() +
".flatSignature(SignatureList list)" + ".flatSignature(SignatureList list)" +
" not declared"); " not declared");
} }
public void TypeDecl.flatSignature(SignatureList list) { public void TypeDecl.flatSignature(SignatureList list) {
getType().flatSignature(list); getTypeInstance().flatSignature(list);
} }
public void SampleDecl.flatSignature(SignatureList list) { public void SampleDecl.flatSignature(SignatureList list) {
getType().flatSignature(list); getTypeInstance().flatSignature(list);
} }
// public void SampleRefType.flatSignature(SignatureList list) { // public void SampleRefType.flatSignature(SignatureList list) {
// list.addInt(LABCOMM_SAMPLE_REF, "sample"); // list.addInt(LABCOMM_SAMPLE_REF, "sample");
// } // }
public void TypeInstance.flatSignature(SignatureList list) {
if(addIntentions()) {
debugAnnotations(this.getName()+".TypeInstance.flatSignature");
list.addIntentions(intentions(), "intentions: "+getIntentionString());
}
getDataType().flatSignature(list);
}
public void VoidType.flatSignature(SignatureList list) { public void VoidType.flatSignature(SignatureList list) {
list.addInt(LABCOMM_STRUCT, "void"); list.addInt(LABCOMM_STRUCT, "void");
list.addInt(0, null); list.addInt(0, null);
...@@ -45,7 +59,7 @@ aspect FlatSignature { ...@@ -45,7 +59,7 @@ aspect FlatSignature {
for (int i = 0 ; i < getNumExp() ; i++) { for (int i = 0 ; i < getNumExp() ; i++) {
getExp(i).flatSignature(list); getExp(i).flatSignature(list);
} }
getType().flatSignature(list); getDataType().flatSignature(list);
list.unindent(); list.unindent();
list.add(null, "}"); list.add(null, "}");
} }
...@@ -62,8 +76,10 @@ aspect FlatSignature { ...@@ -62,8 +76,10 @@ aspect FlatSignature {
} }
public void Field.flatSignature(SignatureList list) { public void Field.flatSignature(SignatureList list) {
list.addString(getName(), signatureComment()); debugAnnotations(this.getName()+".Field.flatSignature");
getType().flatSignature(list); list.addIntentions(intentions(), "Field: "+getIntentionString());
// list.addString(getName(), signatureComment());
getDataType().flatSignature(list);
} }
public void IntegerLiteral.flatSignature(SignatureList list) { public void IntegerLiteral.flatSignature(SignatureList list) {
...@@ -78,7 +94,7 @@ aspect FlatSignature { ...@@ -78,7 +94,7 @@ aspect FlatSignature {
StringBuffer result = new StringBuffer("array ["); StringBuffer result = new StringBuffer("array [");
for (int i = 0 ; i < getNumExp() ; i++) { for (int i = 0 ; i < getNumExp() ; i++) {
if (i > 0) { if (i > 0) {
result.append(", "); result.append(", ");
} }
result.append(getExp(i).signatureComment()); result.append(getExp(i).signatureComment());
} }
...@@ -87,13 +103,13 @@ aspect FlatSignature { ...@@ -87,13 +103,13 @@ aspect FlatSignature {
} }
public String ASTNode.signatureComment() { public String ASTNode.signatureComment() {
throw new Error(this.getClass().getName() + throw new Error(this.getClass().getName() +
".signatureComment()" + ".signatureComment()" +
" not declared"); " not declared");
} }
public String Field.signatureComment() { public String Field.signatureComment() {
return getType().signatureComment() + " '" + getName() +"'"; return getDataType().signatureComment() + " '" + getName() +"'";
} }
// public String SampleRefType.signatureComment() { // public String SampleRefType.signatureComment() {
......
aspect TypeDefGen {
public void Specification.generateTypedefs(PrintStream out, int ver) {
for(Decl d : getDecls()) {
d.generateTypedefs(out);
}
}
public void Decl.generateTypedefs(PrintStream out) {
}
public void Decl.generateDepTypedefs(PrintStream out){
Iterator<Decl> it = type_dependencies().iterator();
while(it.hasNext()) {
Decl d = it.next();
d.generateDepTypedefs(out);
}
pp(out);
}
public void SampleDecl.generateTypedefs(PrintStream out){
if(hasDependencies()) {
out.println("sample "+getName()+"_def {");
out.println(" sample sample;");
out.println(" string typedef = <<EOL");
for(Decl d : type_dependencies()) {
d.generateDepTypedefs(out);
}
pp(out);
out.println("EOL;");
}
}
}
This diff is collapsed.
Program ::= Decl*; Specification ::= Decl*;
abstract Decl ::= Type <Name:String> /Signature/; abstract Decl ::= TypeInstance /Signature/;
TypeInstance ::= DataType Annotations;
Annotations ::= Annotation*;
Annotation ::= <Key:String> <Value:byte[]>;
Intention : Annotation;
DocString : Annotation;
TypeDecl : Decl; TypeDecl : Decl;
SampleDecl : Decl; SampleDecl : Decl;
...@@ -8,25 +16,26 @@ SampleDecl : Decl; ...@@ -8,25 +16,26 @@ SampleDecl : Decl;
//Signatures are in the abstract grammar, so that //Signatures are in the abstract grammar, so that
//they can be extended and refined by aspects. //they can be extended and refined by aspects.
Signature ::= SignatureList FlatSignatureList:SignatureList; Signature ::= SignatureList FlatSignatureList:SignatureList;
SignatureList ::= SignatureLine*; SignatureList ::= SignatureLine*;
abstract SignatureLine ::= <Indent:int> <Comment:String>; abstract SignatureLine ::= <Indent:int> <Comment:String>;
abstract DataSignatureLine : SignatureLine; abstract DataSignatureLine : SignatureLine;
ByteArraySignatureLine : DataSignatureLine ::= <Data:byte[]>; ByteArraySignatureLine : DataSignatureLine ::= <Data:byte[]>;
IntSignatureLine : DataSignatureLine ::= <Data:int>; IntSignatureLine : DataSignatureLine ::= <Data:int>;
StringSignatureLine : DataSignatureLine ::= <Data:String>; StringSignatureLine : DataSignatureLine ::= <Data:String>;
IntentionSignatureLine : DataSignatureLine ::= Intention* ;
TypeRefSignatureLine : SignatureLine ::= Decl; TypeRefSignatureLine : SignatureLine ::= Decl;
Field ::= Type <Name:String>; Field : TypeInstance;
abstract Type; abstract DataType;
VoidType : Type; VoidType : DataType;
//SampleRefType : Type; //SampleRefType : DataType;
PrimType : Type ::= <Name:String> <Token:int>; PrimType : DataType ::= <Name:String> <Token:int>;
UserType : Type ::= <Name:String>; UserType : DataType ::= <Name:String>;
StructType : Type ::= Field*; StructType : DataType ::= Field*;
ParseArrayType : Type ::= Type Dim*; ParseArrayType : DataType ::= DataType Dim*;
abstract ArrayType : Type ::= Type Exp*; abstract ArrayType : DataType ::= DataType Dim;
VariableArrayType : ArrayType; VariableArrayType : ArrayType;
FixedArrayType : ArrayType; FixedArrayType : ArrayType;
......
...@@ -36,8 +36,9 @@ public class LabComm { ...@@ -36,8 +36,9 @@ public class LabComm {
println("[ Misc options ]"); println("[ Misc options ]");
println(" --pretty=PFILE Pretty prints to PFILE"); println(" --pretty=PFILE Pretty prints to PFILE");
println(" --typeinfo=TIFILE Generates typeinfo in TIFILE"); println(" --typeinfo=TIFILE Generates typeinfo in TIFILE");
println(" --typedefs=TIFILE Generates typedefs in TIFILE");
} }
/** To be cleaned up. /** To be cleaned up.
*/ */
private static void checkVersion(int v) { private static void checkVersion(int v) {
...@@ -48,12 +49,12 @@ public class LabComm { ...@@ -48,12 +49,12 @@ 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;
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, ver); p.C_genH(out, cIncludes, coreName, prefix, ver);
...@@ -63,7 +64,7 @@ public class LabComm { ...@@ -63,7 +64,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,26 +79,26 @@ public class LabComm { ...@@ -78,26 +79,26 @@ 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);
} 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, 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) {
System.err.println("IOException: " + dirName + " " + System.err.println("IOException: " + dirName + " " +
packageName + " " + e); packageName + " " + e);
} }
} }
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 +112,7 @@ public class LabComm { ...@@ -111,7 +112,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) {
...@@ -119,7 +120,7 @@ public class LabComm { ...@@ -119,7 +120,7 @@ public class LabComm {
} }
} }
/** Helper class to contain command line options /** Helper class to contain command line options
and their associated behaviour and their associated behaviour
**/ **/
private static class Opts { private static class Opts {
...@@ -139,6 +140,7 @@ public class LabComm { ...@@ -139,6 +140,7 @@ public class LabComm {
String pythonFile = null; String pythonFile = null;
String prettyFile = null; String prettyFile = null;
String typeinfoFile = null; String typeinfoFile = null;
String typedefsFile = null;
String rapidFile = null; String rapidFile = null;
String fileName = null; String fileName = null;
...@@ -150,21 +152,21 @@ public class LabComm { ...@@ -150,21 +152,21 @@ public class LabComm {
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());
} }
private static String getFileName(String s) { private static String getFileName(String s) {
return s.substring(s.lastIndexOf('/') + 1, s.length()); return s.substring(s.lastIndexOf('/') + 1, s.length());
} }
private static String getBaseName(String s) { private static String getBaseName(String s) {
s = getFileName(s); s = getFileName(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());
} }
private static String getPrefix(String s) { private static String getPrefix(String s) {
return s.substring(s.lastIndexOf('/') + 1, s.length()); return s.substring(s.lastIndexOf('/') + 1, s.length());
} }
boolean processFilename(){ boolean processFilename(){
// 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++) {
...@@ -180,12 +182,12 @@ public class LabComm { ...@@ -180,12 +182,12 @@ public class LabComm {
} }
return fileName != null; return fileName != null;
} }
void processArgs(){ 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") ||
args[i].equals("-h") || args[i].equals("-h") ||
args[i].equals("--help")) { args[i].equals("--help")) {
print_help(); print_help();
System.exit(0); System.exit(0);
...@@ -223,6 +225,8 @@ public class LabComm { ...@@ -223,6 +225,8 @@ public class LabComm {
prettyFile = args[i].substring(9); prettyFile = args[i].substring(9);
} else if (args[i].startsWith("--typeinfo=")) { } else if (args[i].startsWith("--typeinfo=")) {
typeinfoFile = args[i].substring(11); typeinfoFile = args[i].substring(11);
} else if (args[i].startsWith("--typedefs=")) {
typedefsFile = args[i].substring(11);
} else if (args[i].equals("--rapid")) { } else if (args[i].equals("--rapid")) {
rapidFile = coreName + ".sys"; rapidFile = coreName + ".sys";
} else if (i == args.length - 1) { } else if (i == args.length - 1) {
...@@ -239,17 +243,17 @@ public class LabComm { ...@@ -239,17 +243,17 @@ 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);
if (errors.isEmpty()) { if (errors.isEmpty()) {
ast = p; ast = p;
} else { } else {
...@@ -268,8 +272,8 @@ public class LabComm { ...@@ -268,8 +272,8 @@ 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) {
cIncludes.add(hFile); cIncludes.add(hFile);
...@@ -286,19 +290,19 @@ public class LabComm { ...@@ -286,19 +290,19 @@ 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);
genCS(ast, csFile, csNamespace, ver); genCS(ast, csFile, csNamespace, ver);
wroteFile = true; wroteFile = true;
} }
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);
genJava(ast, javaDir, javaPackage, ver); genJava(ast, javaDir, javaPackage, ver);
...@@ -306,19 +310,19 @@ public class LabComm { ...@@ -306,19 +310,19 @@ 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);
genPython(ast, pythonFile, prefix, ver); genPython(ast, pythonFile, prefix, ver);
wroteFile = true; wroteFile = true;
} }
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);
genRAPID(ast, rapidFile, coreName, ver); genRAPID(ast, rapidFile, coreName, ver);
...@@ -326,10 +330,10 @@ public class LabComm { ...@@ -326,10 +330,10 @@ 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);
try { try {
FileOutputStream f = new FileOutputStream(prettyFile); FileOutputStream f = new FileOutputStream(prettyFile);
PrintStream out = new PrintStream(f); PrintStream out = new PrintStream(f);
...@@ -338,15 +342,15 @@ public class LabComm { ...@@ -338,15 +342,15 @@ public class LabComm {
wroteFile = true; wroteFile = true;
} catch (IOException e) { } catch (IOException e) {
System.err.println("IOException: " + prettyFile + " " + e); System.err.println("IOException: " + prettyFile + " " + e);
} }
} }
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);
try { try {
FileOutputStream f = new FileOutputStream(typeinfoFile); FileOutputStream f = new FileOutputStream(typeinfoFile);
PrintStream out = new PrintStream(f); PrintStream out = new PrintStream(f);
...@@ -361,9 +365,25 @@ public class LabComm { ...@@ -361,9 +365,25 @@ public class LabComm {
return wroteFile; return wroteFile;
} }
boolean generateTypedefs(Specification ast) {
boolean wroteFile = false;
if (typedefsFile != null) {
printStatus("Typedefs: " , typedefsFile);
try {
FileOutputStream f = new FileOutputStream(typedefsFile);
PrintStream out = new PrintStream(f);
ast.generateTypedefs(out, ver);
wroteFile = true;
} catch (IOException e) {
System.err.println("IOException: " + typedefsFile + " " + e);
}
}
return wroteFile;
}
private void printStatus(String kind, String filename){ private void printStatus(String kind, String filename){
if (verbose) { if (verbose) {
System.err.println("Generating "+kind+": " + filename); System.err.println("Generating "+kind+": " + filename);
} }
} }
} }
...@@ -376,10 +396,10 @@ public class LabComm { ...@@ -376,10 +396,10 @@ 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) {
boolean fileWritten = false; boolean fileWritten = false;
fileWritten |= opts.generateC(ast); fileWritten |= opts.generateC(ast);
...@@ -389,6 +409,7 @@ public class LabComm { ...@@ -389,6 +409,7 @@ public class LabComm {
fileWritten |= opts.generateRAPID(ast); fileWritten |= opts.generateRAPID(ast);
fileWritten |= opts.generatePrettyPrint(ast); fileWritten |= opts.generatePrettyPrint(ast);
fileWritten |= opts.generateTypeinfo(ast); fileWritten |= opts.generateTypeinfo(ast);
fileWritten |= opts.generateTypedefs(ast);
// if no output to files, prettyprint on stdout // if no output to files, prettyprint on stdout
if (!fileWritten) { if (!fileWritten) {
...@@ -400,5 +421,5 @@ public class LabComm { ...@@ -400,5 +421,5 @@ public class LabComm {
System.exit(3); System.exit(3);
} }
} }
} }
} }
...@@ -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,26 +54,40 @@ List var_decl_list = ...@@ -54,26 +54,40 @@ 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); :}
; ;
Annotations annotations =
/* empty list */ {: return new Annotations(); :}
| annotation_list.l {: return new Annotations(l); :}
;
List annotation_list =
annotation.i {: return new List().add(i); :}
| annotation_list.l annotation.i {: return l.add(i); :}
;
String key = IDENTIFIER;
String stringliteral = IDENTIFIER | QUOTEDSTRING;
Annotation annotation = intention.i | docstring.d;
Annotation intention = LPAREN key.k COLON stringliteral.v RPAREN {: return new Intention(k,v.getBytes()); :};
Annotation docstring = QUOTEDSTRING.s {: return new DocString(s.substring(1,s.length()-1).getBytes()); :};
TypeInstance type_instance =
annotations.a type.t IDENTIFIER {: return new TypeInstance(t, IDENTIFIER, a); :}
| annotations.a type.t IDENTIFIER dim_list.d
{: return new TypeInstance(new ParseArrayType(t, d), IDENTIFIER, a); :}
;
Field var_decl = Field var_decl =
type.t IDENTIFIER SEMICOLON {: return new Field(t, IDENTIFIER); :} type_instance.t SEMICOLON {: return new Field(t); :}
| type.t IDENTIFIER dim_list.d SEMICOLON
{: return new Field(new ParseArrayType(t, d), IDENTIFIER); :}
; ;
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); :}
;
Type type = DataType type =
prim_type.p {: return p; :} prim_type.p {: return p; :}
| user_type.u {: return u; :} | user_type.u {: return u; :}
| struct_type.s {: return s; :} | struct_type.s {: return s; :}
...@@ -109,8 +123,8 @@ StructType struct_type = ...@@ -109,8 +123,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 =
......
...@@ -6,13 +6,13 @@ import se.lth.control.labcomm2014.compiler.LabCommParser.Terminals; ...@@ -6,13 +6,13 @@ import se.lth.control.labcomm2014.compiler.LabCommParser.Terminals;
%% %%
%public %public
%final %final
%class LabCommScanner %class LabCommScanner
%extends Scanner %extends Scanner
%type Symbol %type Symbol
%function nextToken %function nextToken
%yylexthrow Scanner.Exception %yylexthrow Scanner.Exception
%unicode %unicode
...@@ -44,13 +44,16 @@ Comment = {TraditionalComment} ...@@ -44,13 +44,16 @@ Comment = {TraditionalComment}
TraditionalComment = "/*" [^*] ~"*/" | "/*" "*"+ "/" | "/*" "*"+ [^/*] ~"*/" TraditionalComment = "/*" [^*] ~"*/" | "/*" "*"+ "/" | "/*" "*"+ [^/*] ~"*/"
EndOfLineComment = "//" {InputCharacter}* {LineTerminator}? EndOfLineComment = "//" {InputCharacter}* {LineTerminator}?
Identifier = [:jletter:][:jletterdigit:]* Identifier = [[:letter:]_]([[:letter:]_[:digit:]])*
StringLiteral = [:jletterdigit:]*
DecimalNumeral = 0 | {NonZeroDigit} {Digits}? DecimalNumeral = 0 | {NonZeroDigit} {Digits}?
Digits = {Digit}+ Digits = {Digit}+
Digit = 0 | {NonZeroDigit} Digit = 0 | {NonZeroDigit}
NonZeroDigit = [1-9] NonZeroDigit = [1-9]
QuotedString = "\""~"\""
%% %%
<YYINITIAL> { <YYINITIAL> {
...@@ -76,10 +79,14 @@ NonZeroDigit = [1-9] ...@@ -76,10 +79,14 @@ NonZeroDigit = [1-9]
"}" { return sym(Terminals.RBRACE); } "}" { return sym(Terminals.RBRACE); }
"[" { return sym(Terminals.LBRACK); } "[" { return sym(Terminals.LBRACK); }
"]" { return sym(Terminals.RBRACK); } "]" { return sym(Terminals.RBRACK); }
"(" { return sym(Terminals.LPAREN); }
")" { return sym(Terminals.RPAREN); }
";" { return sym(Terminals.SEMICOLON); } ";" { return sym(Terminals.SEMICOLON); }
":" { return sym(Terminals.COLON); }
"," { return sym(Terminals.COMMA); } "," { return sym(Terminals.COMMA); }
{Identifier} { return sym(Terminals.IDENTIFIER); } {Identifier} { return sym(Terminals.IDENTIFIER); }
{QuotedString} { return sym(Terminals.QUOTEDSTRING); }
} }
// fall through errors // fall through errors
......
...@@ -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)) {
...@@ -34,8 +34,8 @@ aspect NameAnalysis { ...@@ -34,8 +34,8 @@ aspect NameAnalysis {
return null; return null;
} }
syn TypeDecl Type.decl(); syn TypeDecl DataType.decl();
eq Type.decl() = null; eq DataType.decl() = null;
eq UserType.decl() = lookupType(getName()); eq UserType.decl() = lookupType(getName());
eq PrimType.decl() = null; //HERE BE DRAGONS XXX eq PrimType.decl() = null; //HERE BE DRAGONS XXX
......
...@@ -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);
} }
...@@ -27,24 +27,24 @@ aspect PrettyPrint { ...@@ -27,24 +27,24 @@ aspect PrettyPrint {
// Pretty print declarations // Pretty print declarations
public void TypeDecl.pp(PrintStream out) { public void TypeDecl.pp(PrintStream out) {
out.print("typedef "); out.print("typedef ");
getType().ppIdentifier(out, getName()); getDataType().ppIdentifier(out, getName());
out.println(";"); out.println(";");
} }
public void SampleDecl.pp(PrintStream out) { public void SampleDecl.pp(PrintStream out) {
out.print("sample "); out.print("sample ");
getType().ppIdentifier(out, getName()); getDataType().ppIdentifier(out, getName());
out.println(";"); out.println(";");
} }
public void Field.pp(PrintStream out) { public void Field.pp(PrintStream out) {
out.print(pp_indent()); out.print(pp_indent());
getType().ppIdentifier(out, getName()); getDataType().ppIdentifier(out, getName());
out.println(";"); out.println(";");
} }
// Pretty print variable of a given type // Pretty print variable of a given type
public void Type.ppIdentifier(PrintStream out, String id) { public void DataType.ppIdentifier(PrintStream out, String id) {
ppPrefix(out); ppPrefix(out);
out.print(" "); out.print(" ");
out.print(id); out.print(id);
...@@ -58,7 +58,7 @@ aspect PrettyPrint { ...@@ -58,7 +58,7 @@ aspect PrettyPrint {
} }
// PrettyPrint prefix type info // PrettyPrint prefix type info
public void Type.ppPrefix(PrintStream out) { public void DataType.ppPrefix(PrintStream out) {
throw new Error(this.getClass().getName() + throw new Error(this.getClass().getName() +
".ppPrefix(PrintStream out)" + ".ppPrefix(PrintStream out)" +
" not declared"); " not declared");
...@@ -81,7 +81,7 @@ aspect PrettyPrint { ...@@ -81,7 +81,7 @@ aspect PrettyPrint {
} }
public void ArrayType.ppPrefix(PrintStream out) { public void ArrayType.ppPrefix(PrintStream out) {
getType().ppPrefix(out); getDataType().ppPrefix(out);
} }
public void StructType.ppPrefix(PrintStream out) { public void StructType.ppPrefix(PrintStream out) {
...@@ -94,7 +94,7 @@ aspect PrettyPrint { ...@@ -94,7 +94,7 @@ aspect PrettyPrint {
} }
// PrettyPrint suffix type info (array dimensions) // PrettyPrint suffix type info (array dimensions)
public void Type.ppSuffix(PrintStream out) { } public void DataType.ppSuffix(PrintStream out) { }
public void ArrayType.ppSuffix(PrintStream out) { public void ArrayType.ppSuffix(PrintStream out) {
out.print("["); out.print("[");
...@@ -103,7 +103,7 @@ aspect PrettyPrint { ...@@ -103,7 +103,7 @@ aspect PrettyPrint {
getExp(i).pp(out); getExp(i).pp(out);
} }
out.print("]"); out.print("]");
getType().ppSuffix(out); getDataType().ppSuffix(out);
} }
public void IntegerLiteral.pp(PrintStream out) { public void IntegerLiteral.pp(PrintStream out) {
......
...@@ -4,75 +4,23 @@ aspect Python_CodeGenEnv { ...@@ -4,75 +4,23 @@ aspect Python_CodeGenEnv {
// handles qualid nesting, indentation, file writing and // handles qualid nesting, indentation, file writing and
// prefix propagation // prefix propagation
public class Python_env { public class Python_env extends PrintEnv {
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;
}
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) { public Python_env(PrintStream out) {
this.indent = 0; super(out);
this.printer = new Python_printer(out);
}
public void indent() {
indent++;
}
public void unindent() {
indent--;
}
public void print(String s) {
printer.print(this, s);
}
public void println(String s) {
printer.println(this, s);
}
public void println() {
printer.println(this);
} }
} }
} }
aspect Python_CodeGen { aspect Python_CodeGen {
public void Program.Python_gen(PrintStream out, String baseName, int version) { public void Specification.Python_gen(PrintStream out, String baseName, int version) {
Python_env env = new Python_env(out); Python_env env = new Python_env(out);
env.println("#!/usr/bin/python"); env.println("#!/usr/bin/python");
env.println("# Auto generated " + baseName); env.println("# Auto generated " + baseName);
...@@ -99,25 +47,27 @@ aspect Python_CodeGen { ...@@ -99,25 +47,27 @@ aspect Python_CodeGen {
} }
aspect PythonTypes { aspect PythonTypes {
public void Program.Python_genTypes(Python_env env) { public void Specification.Python_genTypes(Python_env env) {
for (int i = 0 ; i < getNumDecl() ; i++) { for (int i = 0 ; i < getNumDecl() ; i++) {
getDecl(i).Python_genSignatureAndTypedef(env); getDecl(i).Python_genSignatureAndTypedef(env);
} }
} }
public void Decl.Python_genSignatureAndTypedef(Python_env env) { public void Decl.Python_genSignatureAndTypedef(Python_env env) {
throw new Error(this.getClass().getName() + throw new Error(this.getClass().getName() +
".Python_genSignatureAndTypedef(Python_env env)" + ".Python_genSignatureAndTypedef(Python_env env)" +
" not declared"); " not declared");
} }
public void TypeDecl.Python_genSignatureAndTypedef(Python_env env) { public void TypeDecl.Python_genSignatureAndTypedef(Python_env env) {
env.println("class " + getName() + "(object):"); env.println("class " + getName() + "(object):");
env.indent(); env.indent();
env.println("typedef = labcomm2014.typedef('" + getName() + "',"); env.print("typedef = labcomm2014.typedef(");
Python_genIntentions(env);
env.println(",");
env.indent(); env.indent();
getType().Python_genTypedef(env); getTypeInstance().Python_genTypedef(env);
env.unindent(); env.unindent();
env.println(")"); env.println(")");
env.unindent(); env.unindent();
...@@ -127,27 +77,51 @@ aspect PythonTypes { ...@@ -127,27 +77,51 @@ aspect PythonTypes {
public void SampleDecl.Python_genSignatureAndTypedef(Python_env env) { public void SampleDecl.Python_genSignatureAndTypedef(Python_env env) {
env.println("class " + getName() + "(object):"); env.println("class " + getName() + "(object):");
env.indent(); env.indent();
env.println("signature = labcomm2014.sample('" + getName() + "', "); env.print("signature = labcomm2014.sample(");
Python_genIntentions(env);
env.println(",");
env.indent(); env.indent();
getType().Python_genSignature(env); getDataType().Python_genSignature(env);
env.unindent(); env.unindent();
env.println(")"); env.println(")");
env.println("typedef = labcomm2014.sample('" + getName() + "', "); env.print("typedef = labcomm2014.sample(");
Python_genIntentions(env);
env.println(",");
env.indent(); env.indent();
getType().Python_genTypedef(env); getTypeInstance().Python_genTypedef(env);
env.unindent(); env.unindent();
env.println(")"); env.println(")");
env.unindent(); env.unindent();
env.println(); env.println();
} }
public void Decl.Python_genIntentions(Python_env env) {
getTypeInstance().Python_genIntentions(env);
}
public void TypeInstance.Python_genIntentions(Python_env env) {
// env.print("{");
// for(Intention i : sortedIntentions()) {
// env.print("'"+i.getKey()+"':'"+new String(i.getValue())+"', ");
// }
// env.print("}");
env.print("tuple((");
for(Intention i : sortedIntentions()) {
env.print("('"+i.getKey()+"','"+new String(i.getValue())+"'), ");
}
env.print("))");
}
public void TypeInstance.Python_genTypedef(Python_env env) {
getDataType().Python_genTypedef(env);
}
public void UserType.Python_genSignature(Python_env env) { public void UserType.Python_genSignature(Python_env env) {
lookupType(getName()).getType().Python_genSignature(env); lookupType(getName()).getDataType().Python_genSignature(env);
} }
public void Type.Python_genSignature(Python_env env) { public void DataType.Python_genSignature(Python_env env) {
throw new Error(this.getClass().getName() + throw new Error(this.getClass().getName() +
".Python_genSignature(Python_env env)" + ".Python_genSignature(Python_env env)" +
" not declared"); " not declared");
} }
...@@ -173,7 +147,7 @@ aspect PythonTypes { ...@@ -173,7 +147,7 @@ aspect PythonTypes {
} }
env.println("],"); env.println("],");
env.indent(); env.indent();
getType().Python_genSignature(env); getDataType().Python_genSignature(env);
env.print(")"); env.print(")");
env.unindent(); env.unindent();
} }
...@@ -194,8 +168,10 @@ aspect PythonTypes { ...@@ -194,8 +168,10 @@ aspect PythonTypes {
} }
public void Field.Python_genSignature(Python_env env) { public void Field.Python_genSignature(Python_env env) {
env.print("('" + getName() + "', "); env.print("(");
getType().Python_genSignature(env); Python_genIntentions(env);
env.print(", ");
getDataType().Python_genSignature(env);
env.print(")"); env.print(")");
} }
...@@ -203,9 +179,9 @@ aspect PythonTypes { ...@@ -203,9 +179,9 @@ aspect PythonTypes {
env.println(getName() + ".typedef"); env.println(getName() + ".typedef");
} }
public void Type.Python_genTypedef(Python_env env) { public void DataType.Python_genTypedef(Python_env env) {
throw new Error(this.getClass().getName() + throw new Error(this.getClass().getName() +
".Python_genTypedef(Python_env env)" + ".Python_genTypedef(Python_env env)" +
" not declared"); " not declared");
} }
...@@ -231,7 +207,7 @@ aspect PythonTypes { ...@@ -231,7 +207,7 @@ aspect PythonTypes {
} }
env.println("],"); env.println("],");
env.indent(); env.indent();
getType().Python_genTypedef(env); getDataType().Python_genTypedef(env);
env.print(")"); env.print(")");
env.unindent(); env.unindent();
} }
...@@ -252,8 +228,10 @@ aspect PythonTypes { ...@@ -252,8 +228,10 @@ aspect PythonTypes {
} }
public void Field.Python_genTypedef(Python_env env) { public void Field.Python_genTypedef(Python_env env) {
env.print("('" + getName() + "', "); env.print("(");
getType().Python_genTypedef(env); Python_genIntentions(env);
env.print(", ");
getDataType().Python_genTypedef(env);
env.print(")"); env.print(")");
} }
...@@ -272,8 +250,8 @@ aspect PythonTypes { ...@@ -272,8 +250,8 @@ aspect PythonTypes {
} }
public String Exp.Python_getValue() { public String Exp.Python_getValue() {
throw new Error(this.getClass().getName() + throw new Error(this.getClass().getName() +
".Python_getValue()" + ".Python_getValue()" +
" not declared"); " not declared");
} }
......
...@@ -77,7 +77,7 @@ aspect RAPID_CodeGen { ...@@ -77,7 +77,7 @@ aspect RAPID_CodeGen {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
public void Program.RAPID_gen(String file, String prefix, int version) public void Specification.RAPID_gen(String file, String prefix, int version)
throws IOException throws IOException
{ {
PrintStream ps = new PrintStream(new FileOutputStream(new File(file))); PrintStream ps = new PrintStream(new FileOutputStream(new File(file)));
...@@ -85,7 +85,7 @@ aspect RAPID_CodeGen { ...@@ -85,7 +85,7 @@ aspect RAPID_CodeGen {
RAPID_gen(env); RAPID_gen(env);
} }
public void Program.RAPID_gen(RAPID_env env) public void Specification.RAPID_gen(RAPID_env env)
{ {
for (int i = 0; i < getNumDecl(); i++) { for (int i = 0; i < getNumDecl(); i++) {
getDecl(i).RAPID_gen(env); getDecl(i).RAPID_gen(env);
...@@ -126,7 +126,7 @@ aspect RAPID_CodeGen { ...@@ -126,7 +126,7 @@ aspect RAPID_CodeGen {
public void SampleDecl.RAPID_gen(RAPID_env env) { public void SampleDecl.RAPID_gen(RAPID_env env) {
// Add type declarations // Add type declarations
String fullName = getType().RAPID_AddType(env, getName()); String fullName = getDataType().RAPID_AddType(env, getName());
// Add signature constants // Add signature constants
String sig_len_name = "signature_len_" + getName(); String sig_len_name = "signature_len_" + getName();
String sig_name = "signature_" + getName(); String sig_name = "signature_" + getName();
...@@ -167,7 +167,7 @@ aspect RAPID_CodeGen { ...@@ -167,7 +167,7 @@ aspect RAPID_CodeGen {
params.add("VAR LabComm_Stream st"); params.add("VAR LabComm_Stream st");
params.add("VAR LabComm_Decoder_Sample s"); params.add("VAR LabComm_Decoder_Sample s");
stmts.add("VAR " + fullName + " tmp;"); stmts.add("VAR " + fullName + " tmp;");
getType().RAPID_AddDecodeInstr(env, stmts, "tmp", "st"); getDataType().RAPID_AddDecodeInstr(env, stmts, "tmp", "st");
stmts.add("% s.handler % tmp;"); stmts.add("% s.handler % tmp;");
env.addProc("Decode_And_Handle_" + getName(), params, stmts); env.addProc("Decode_And_Handle_" + getName(), params, stmts);
...@@ -201,11 +201,11 @@ aspect RAPID_CodeGen { ...@@ -201,11 +201,11 @@ aspect RAPID_CodeGen {
params.add("VAR LabComm_Encoder_Sample s"); params.add("VAR LabComm_Encoder_Sample s");
params.add("VAR " + fullName + " val"); params.add("VAR " + fullName + " val");
stmts.add("Encode_Packed st, s.user_id;"); stmts.add("Encode_Packed st, s.user_id;");
getType().RAPID_AddEncodeInstr(env, stmts, "val", "st"); getDataType().RAPID_AddEncodeInstr(env, stmts, "val", "st");
env.addProc("Encode_" + getName(), params, stmts); env.addProc("Encode_" + getName(), params, stmts);
} }
public String Type.RAPID_AddType(RAPID_env env, String name) { public String DataType.RAPID_AddType(RAPID_env env, String name) {
throw new UnsupportedOperationException("RAPID code generation does (currently) not support "+getClass().getSimpleName()); throw new UnsupportedOperationException("RAPID code generation does (currently) not support "+getClass().getSimpleName());
} }
...@@ -214,7 +214,7 @@ aspect RAPID_CodeGen { ...@@ -214,7 +214,7 @@ aspect RAPID_CodeGen {
for (int i = 0; i < getNumField(); i++) { for (int i = 0; i < getNumField(); i++) {
Field f = getField(i); Field f = getField(i);
components.add( components.add(
f.getType().RAPID_AddType(env, name + "_" + f.getName()) + f.getDataType().RAPID_AddType(env, name + "_" + f.getName()) +
" " + f.getName() + ";"); " " + f.getName() + ";");
} }
String typeName = env.addRecord(name, components); String typeName = env.addRecord(name, components);
...@@ -222,7 +222,7 @@ aspect RAPID_CodeGen { ...@@ -222,7 +222,7 @@ aspect RAPID_CodeGen {
} }
public String FixedArrayType.RAPID_AddType(RAPID_env env, String name) { public String FixedArrayType.RAPID_AddType(RAPID_env env, String name) {
String typeName = getType().RAPID_AddType(env, name + "_e"); String typeName = getDataType().RAPID_AddType(env, name + "_e");
if (getNumExp() > 1) { if (getNumExp() > 1) {
throw new UnsupportedOperationException("RAPID generation only (currently) supports one-dimensional arrays"); throw new UnsupportedOperationException("RAPID generation only (currently) supports one-dimensional arrays");
} }
...@@ -251,7 +251,7 @@ aspect RAPID_CodeGen { ...@@ -251,7 +251,7 @@ aspect RAPID_CodeGen {
throw new UnsupportedOperationException("RAPID code generation does not (currently) support "+getName()); throw new UnsupportedOperationException("RAPID code generation does not (currently) support "+getName());
} }
public void Type.RAPID_AddDecodeInstr(RAPID_env env, public void DataType.RAPID_AddDecodeInstr(RAPID_env env,
java.util.List<String> instrs, java.util.List<String> instrs,
String var_name, String stream_name) { String var_name, String stream_name) {
throw new UnsupportedOperationException("RAPID code generation does not (currently) support "+getClass().getSimpleName()); throw new UnsupportedOperationException("RAPID code generation does not (currently) support "+getClass().getSimpleName());
...@@ -261,7 +261,7 @@ aspect RAPID_CodeGen { ...@@ -261,7 +261,7 @@ aspect RAPID_CodeGen {
java.util.List<String> instrs, java.util.List<String> instrs,
String var_name, String stream_name) { String var_name, String stream_name) {
for (int i = 0; i < getNumField(); i++) { for (int i = 0; i < getNumField(); i++) {
getField(i).getType().RAPID_AddDecodeInstr(env, instrs, getField(i).getDataType().RAPID_AddDecodeInstr(env, instrs,
var_name + "." + getField(i).getName(), stream_name); var_name + "." + getField(i).getName(), stream_name);
} }
} }
...@@ -270,7 +270,7 @@ aspect RAPID_CodeGen { ...@@ -270,7 +270,7 @@ aspect RAPID_CodeGen {
java.util.List<String> instrs, java.util.List<String> instrs,
String var_name, String stream_name) { String var_name, String stream_name) {
for (int i = 1; i <= getExp(0).RAPID_getValue(); i++) { for (int i = 1; i <= getExp(0).RAPID_getValue(); i++) {
getType().RAPID_AddDecodeInstr(env, instrs, getDataType().RAPID_AddDecodeInstr(env, instrs,
var_name + ".e" + i, stream_name); var_name + ".e" + i, stream_name);
} }
} }
...@@ -305,7 +305,7 @@ aspect RAPID_CodeGen { ...@@ -305,7 +305,7 @@ aspect RAPID_CodeGen {
} }
} }
public void Type.RAPID_AddEncodeInstr(RAPID_env env, public void DataType.RAPID_AddEncodeInstr(RAPID_env env,
java.util.List<String> instrs, java.util.List<String> instrs,
String var_name, String stream_name) { String var_name, String stream_name) {
throw new UnsupportedOperationException("RAPID code generation does not (currently) support "+getClass().getSimpleName()); throw new UnsupportedOperationException("RAPID code generation does not (currently) support "+getClass().getSimpleName());
...@@ -315,7 +315,7 @@ aspect RAPID_CodeGen { ...@@ -315,7 +315,7 @@ aspect RAPID_CodeGen {
java.util.List<String> instrs, java.util.List<String> instrs,
String var_name, String stream_name) { String var_name, String stream_name) {
for (int i = 0; i < getNumField(); i++) { for (int i = 0; i < getNumField(); i++) {
getField(i).getType().RAPID_AddEncodeInstr(env, instrs, getField(i).getDataType().RAPID_AddEncodeInstr(env, instrs,
var_name + "." + getField(i).getName(), stream_name); var_name + "." + getField(i).getName(), stream_name);
} }
} }
...@@ -324,7 +324,7 @@ aspect RAPID_CodeGen { ...@@ -324,7 +324,7 @@ aspect RAPID_CodeGen {
java.util.List<String> instrs, java.util.List<String> instrs,
String var_name, String stream_name) { String var_name, String stream_name) {
for (int i = 1; i <= getExp(0).RAPID_getValue(); i++) { for (int i = 1; i <= getExp(0).RAPID_getValue(); i++) {
getType().RAPID_AddEncodeInstr(env, instrs, getDataType().RAPID_AddEncodeInstr(env, instrs,
var_name + ".e" + i, stream_name); var_name + ".e" + i, stream_name);
} }
} }
......
/* Temporary aspect with forwarding methods */
aspect Refactoring {
syn int ArrayType.getNumExp() = getDim().getNumExp();
syn Exp ArrayType.getExp(int i) = getDim().getExp(i);
syn String Decl.getName() = getTypeInstance().getName();
syn DataType Decl.getDataType() = getTypeInstance().getDataType();
syn String TypeInstance.getName() = getAnnotations().getName();
public Annotations Annotations.addName(String n) {
//XXX TODO: check if name already exists
addAnnotation(new Intention("",n.getBytes()));
return this;
}
public Field.Field(TypeInstance t) {
this(t.getDataType(), t.getAnnotations());
}
public TypeInstance.TypeInstance(DataType t, String n, Annotations a) {
this(t, a.addName(n));
}
public TypeInstance.TypeInstance(DataType t, String n) {
this(t, new Annotations().addName(n));
System.out.println("WARNING! TypeInstance(DataType, String) ignoring intention list");
}
syn Annotation TypeInstance.getAnnotation(int i) = getAnnotations().getAnnotation(i);
}