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
Show changes
Commits on Source (90)
Showing
with 1131 additions and 950 deletions
......@@ -2,3 +2,4 @@
*.class
*.o
*.pyc
examples/dynamic/gen
SUBDIRS=compiler lib test examples packaging
export LABCOMM_JAR=$(shell pwd)/compiler/labcomm2014_compiler.jar
export LABCOMM=java -jar $(LABCOMM_JAR)
export VALGRIND=valgrind --leak-check=full --error-exitcode=1
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 {
when (! getDim(0).isVariable())
to FixedArrayType {
if (getNumDim() == 1) {
return new FixedArrayType(getType(),
getDim(0).getExpList());
return new FixedArrayType(getDataType(),
getDim(0));
} else {
List l = new List();
for (int i = 1 ; i < getNumDim() ; i++) {
l.add(getDim(i));
}
return new FixedArrayType(new ParseArrayType(getType(), l),
getDim(0).getExpList());
return new FixedArrayType(new ParseArrayType(getDataType(), l),
getDim(0));
}
}
when (getDim(0).isVariable())
to VariableArrayType {
if (getNumDim() == 1) {
return new VariableArrayType(getType(),
getDim(0).getExpList());
return new VariableArrayType(getDataType(),
getDim(0));
} else {
List l = new List();
for (int i = 1 ; i < getNumDim() ; i++) {
l.add(getDim(i));
}
return new VariableArrayType(new ParseArrayType(getType(), l),
getDim(0).getExpList());
return new VariableArrayType(new ParseArrayType(getDataType(), l),
getDim(0));
}
}
}
......
This diff is collapsed.
This diff is collapsed.
aspect DeclNames {
inh String Type.declName();
eq Decl.getType().declName() = getName();
inh String DataType.declName();
eq Decl.getTypeInstance().declName() = getName();
inh String Field.declName();
eq StructType.getField(int i).declName() = declName();
//TODO: aspect should be renamed to parent-something
inh Decl Type.parentDecl();
inh Decl Field.parentDecl();
eq Decl.getType().parentDecl() = this;
eq StructType.getField(int i).parentDecl() = parentDecl();
//TODO: aspect should be renamed to parent-something
inh Decl DataType.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;
aspect ErrorCheck {
syn int ASTNode.lineNumber() = getLine(getStart());
syn int ASTNode.lineNumber() = getLine(getStart());
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 String ASTNode.errors = null;
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);
}
}
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() {
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.*;
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) {
SignatureList result = getSignature().getFlatSignatureList();
return result;
}
public void ASTNode.flatSignature(SignatureList list) {
throw new Error(this.getClass().getName() +
".flatSignature(SignatureList list)" +
throw new Error(this.getClass().getName() +
".flatSignature(SignatureList list)" +
" not declared");
}
public void TypeDecl.flatSignature(SignatureList list) {
getType().flatSignature(list);
getTypeInstance().flatSignature(list);
}
public void SampleDecl.flatSignature(SignatureList list) {
getType().flatSignature(list);
getTypeInstance().flatSignature(list);
}
// public void SampleRefType.flatSignature(SignatureList list) {
// 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) {
list.addInt(LABCOMM_STRUCT, "void");
list.addInt(0, null);
......@@ -45,7 +59,7 @@ aspect FlatSignature {
for (int i = 0 ; i < getNumExp() ; i++) {
getExp(i).flatSignature(list);
}
getType().flatSignature(list);
getDataType().flatSignature(list);
list.unindent();
list.add(null, "}");
}
......@@ -62,8 +76,10 @@ aspect FlatSignature {
}
public void Field.flatSignature(SignatureList list) {
list.addString(getName(), signatureComment());
getType().flatSignature(list);
debugAnnotations(this.getName()+".Field.flatSignature");
list.addIntentions(intentions(), "Field: "+getIntentionString());
// list.addString(getName(), signatureComment());
getDataType().flatSignature(list);
}
public void IntegerLiteral.flatSignature(SignatureList list) {
......@@ -78,7 +94,7 @@ aspect FlatSignature {
StringBuffer result = new StringBuffer("array [");
for (int i = 0 ; i < getNumExp() ; i++) {
if (i > 0) {
result.append(", ");
result.append(", ");
}
result.append(getExp(i).signatureComment());
}
......@@ -87,13 +103,13 @@ aspect FlatSignature {
}
public String ASTNode.signatureComment() {
throw new Error(this.getClass().getName() +
".signatureComment()" +
throw new Error(this.getClass().getName() +
".signatureComment()" +
" not declared");
}
public String Field.signatureComment() {
return getType().signatureComment() + " '" + getName() +"'";
return getDataType().signatureComment() + " '" + getName() +"'";
}
// 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;
SampleDecl : Decl;
......@@ -8,25 +16,26 @@ SampleDecl : Decl;
//Signatures are in the abstract grammar, so that
//they can be extended and refined by aspects.
Signature ::= SignatureList FlatSignatureList:SignatureList;
Signature ::= SignatureList FlatSignatureList:SignatureList;
SignatureList ::= SignatureLine*;
abstract SignatureLine ::= <Indent:int> <Comment:String>;
abstract SignatureLine ::= <Indent:int> <Comment:String>;
abstract DataSignatureLine : SignatureLine;
ByteArraySignatureLine : DataSignatureLine ::= <Data:byte[]>;
IntSignatureLine : DataSignatureLine ::= <Data:int>;
StringSignatureLine : DataSignatureLine ::= <Data:String>;
IntentionSignatureLine : DataSignatureLine ::= Intention* ;
TypeRefSignatureLine : SignatureLine ::= Decl;
Field ::= Type <Name:String>;
Field : TypeInstance;
abstract Type;
VoidType : Type;
//SampleRefType : Type;
PrimType : Type ::= <Name:String> <Token:int>;
UserType : Type ::= <Name:String>;
StructType : Type ::= Field*;
ParseArrayType : Type ::= Type Dim*;
abstract ArrayType : Type ::= Type Exp*;
abstract DataType;
VoidType : DataType;
//SampleRefType : DataType;
PrimType : DataType ::= <Name:String> <Token:int>;
UserType : DataType ::= <Name:String>;
StructType : DataType ::= Field*;
ParseArrayType : DataType ::= DataType Dim*;
abstract ArrayType : DataType ::= DataType Dim;
VariableArrayType : ArrayType;
FixedArrayType : ArrayType;
......
......@@ -36,8 +36,9 @@ public class LabComm {
println("[ Misc options ]");
println(" --pretty=PFILE Pretty prints to PFILE");
println(" --typeinfo=TIFILE Generates typeinfo in TIFILE");
println(" --typedefs=TIFILE Generates typedefs in TIFILE");
}
/** To be cleaned up.
*/
private static void checkVersion(int v) {
......@@ -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) {
try {
FileOutputStream f;
PrintStream out;
f = new FileOutputStream(hName);
out = new PrintStream(f);
p.C_genH(out, cIncludes, coreName, prefix, ver);
......@@ -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) {
try {
FileOutputStream f;
......@@ -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");
try {
p.CS_gen(csName, csNamespace, ver);
} catch (IOException e) {
System.err.println("IOException: " + csName + " " +
System.err.println("IOException: " + csName + " " +
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 {
p.J_gen(dirName, packageName, ver);
} catch (IOException e) {
System.err.println("IOException: " + dirName + " " +
System.err.println("IOException: " + dirName + " " +
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 {
FileOutputStream f;
PrintStream out;
......@@ -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 {
p.RAPID_gen(filename, prefix, ver);
} catch (IOException e) {
......@@ -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
**/
private static class Opts {
......@@ -139,6 +140,7 @@ public class LabComm {
String pythonFile = null;
String prettyFile = null;
String typeinfoFile = null;
String typedefsFile = null;
String rapidFile = null;
String fileName = null;
......@@ -150,21 +152,21 @@ public class LabComm {
int i = s.lastIndexOf('.');
return s.substring(0, i > 0 ? i : s.length());
}
private static String getFileName(String s) {
return s.substring(s.lastIndexOf('/') + 1, s.length());
}
private static String getBaseName(String s) {
s = getFileName(s);
int i = s.lastIndexOf('.');
return s.substring(0, i > 0 ? i : s.length());
}
private static String getPrefix(String s) {
return s.substring(s.lastIndexOf('/') + 1, s.length());
}
boolean processFilename(){
// Scan for first non-option
for (int i = 0 ; i < args.length ; i++) {
......@@ -180,12 +182,12 @@ public class LabComm {
}
return fileName != null;
}
void processArgs(){
for (int i = 0 ; i < args.length ; i++) {
if (fileName == null ||
args[i].equals("-help") ||
args[i].equals("-h") ||
args[i].equals("-help") ||
args[i].equals("-h") ||
args[i].equals("--help")) {
print_help();
System.exit(0);
......@@ -223,6 +225,8 @@ public class LabComm {
prettyFile = args[i].substring(9);
} else if (args[i].startsWith("--typeinfo=")) {
typeinfoFile = args[i].substring(11);
} else if (args[i].startsWith("--typedefs=")) {
typedefsFile = args[i].substring(11);
} else if (args[i].equals("--rapid")) {
rapidFile = coreName + ".sys";
} else if (i == args.length - 1) {
......@@ -239,17 +243,17 @@ public class LabComm {
}
}
Program parseFile(){
Program ast = null;
Specification parseFile(){
Specification ast = null;
try {
// Check for errors
LabCommScanner scanner = new LabCommScanner(
new FileReader(fileName));
LabCommParser parser = new LabCommParser();
Program p = (Program)parser.parse(scanner);
Specification p = (Specification)parser.parse(scanner);
Collection errors = new LinkedList();
p.errorCheck(errors);
if (errors.isEmpty()) {
ast = p;
} else {
......@@ -268,8 +272,8 @@ public class LabComm {
return ast;
}
boolean generateC(Program ast) {
boolean wroteFile = false;
boolean generateC(Specification ast) {
boolean wroteFile = false;
Vector hIncludes = new Vector(cIncludes);
if (hFile != null) {
cIncludes.add(hFile);
......@@ -286,19 +290,19 @@ public class LabComm {
}
return wroteFile;
}
boolean generateCS(Program ast) {
boolean wroteFile = false;
boolean generateCS(Specification ast) {
boolean wroteFile = false;
if (csFile != null) {
printStatus("C#: " , csFile);
printStatus("C#: " , csFile);
genCS(ast, csFile, csNamespace, ver);
wroteFile = true;
}
return wroteFile;
}
boolean generateJava(Program ast) {
boolean wroteFile = false;
boolean generateJava(Specification ast) {
boolean wroteFile = false;
if (javaDir != null) {
printStatus("Java: " , javaDir);
genJava(ast, javaDir, javaPackage, ver);
......@@ -306,19 +310,19 @@ public class LabComm {
}
return wroteFile;
}
boolean generatePython(Program ast) {
boolean wroteFile = false;
boolean generatePython(Specification ast) {
boolean wroteFile = false;
if (pythonFile != null) {
printStatus("Python: " , pythonFile);
printStatus("Python: " , pythonFile);
genPython(ast, pythonFile, prefix, ver);
wroteFile = true;
}
return wroteFile;
}
boolean generateRAPID(Program ast) {
boolean wroteFile = false;
boolean generateRAPID(Specification ast) {
boolean wroteFile = false;
if (rapidFile != null) {
printStatus("RAPID: " , rapidFile);
genRAPID(ast, rapidFile, coreName, ver);
......@@ -326,10 +330,10 @@ public class LabComm {
}
return wroteFile;
}
boolean generatePrettyPrint(Program ast) {
boolean wroteFile = false;
boolean generatePrettyPrint(Specification ast) {
boolean wroteFile = false;
if (prettyFile != null) {
printStatus("Pretty: " , prettyFile);
printStatus("Pretty: " , prettyFile);
try {
FileOutputStream f = new FileOutputStream(prettyFile);
PrintStream out = new PrintStream(f);
......@@ -338,15 +342,15 @@ public class LabComm {
wroteFile = true;
} catch (IOException e) {
System.err.println("IOException: " + prettyFile + " " + e);
}
}
}
return wroteFile;
}
boolean generateTypeinfo(Program ast) {
boolean wroteFile = false;
boolean generateTypeinfo(Specification ast) {
boolean wroteFile = false;
if (typeinfoFile != null) {
printStatus("TypeInfo: " , typeinfoFile);
printStatus("TypeInfo: " , typeinfoFile);
try {
FileOutputStream f = new FileOutputStream(typeinfoFile);
PrintStream out = new PrintStream(f);
......@@ -361,9 +365,25 @@ public class LabComm {
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){
if (verbose) {
System.err.println("Generating "+kind+": " + filename);
if (verbose) {
System.err.println("Generating "+kind+": " + filename);
}
}
}
......@@ -376,10 +396,10 @@ public class LabComm {
System.exit(1);
} else {
opts.processArgs();
Program ast = opts.parseFile();
Specification ast = opts.parseFile();
if (ast != null) {
boolean fileWritten = false;
fileWritten |= opts.generateC(ast);
......@@ -389,6 +409,7 @@ public class LabComm {
fileWritten |= opts.generateRAPID(ast);
fileWritten |= opts.generatePrettyPrint(ast);
fileWritten |= opts.generateTypeinfo(ast);
fileWritten |= opts.generateTypedefs(ast);
// if no output to files, prettyprint on stdout
if (!fileWritten) {
......@@ -400,5 +421,5 @@ public class LabComm {
System.exit(3);
}
}
}
}
}
......@@ -34,9 +34,9 @@
}
:};
Program goal =
/* Empty program */ {: return new Program(); :}
| decl_list.l {: return new Program(l); :}
Specification goal =
/* Empty program */ {: return new Specification(); :}
| decl_list.l {: return new Specification(l); :}
;
List decl_list =
......@@ -54,26 +54,40 @@ List var_decl_list =
| 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 =
type.t IDENTIFIER SEMICOLON {: return new Field(t, IDENTIFIER); :}
| type.t IDENTIFIER dim_list.d SEMICOLON
{: return new Field(new ParseArrayType(t, d), IDENTIFIER); :}
type_instance.t SEMICOLON {: return new Field(t); :}
;
TypeDecl type_decl =
TYPEDEF type.t IDENTIFIER SEMICOLON {: return new TypeDecl(t, IDENTIFIER); :}
| TYPEDEF type.t IDENTIFIER dim_list.d SEMICOLON
{: return new TypeDecl(new ParseArrayType(t, d), IDENTIFIER); :}
;
TYPEDEF type_instance.t SEMICOLON {: return new TypeDecl(t); :} ;
SampleDecl sample_decl =
SAMPLE type.t IDENTIFIER SEMICOLON
{: return new SampleDecl(t, IDENTIFIER); :}
| SAMPLE type.t IDENTIFIER dim_list.d SEMICOLON
{: return new SampleDecl(new ParseArrayType(t, d), IDENTIFIER); :}
;
SAMPLE type_instance.t SEMICOLON {: return new SampleDecl(t); :} ;
Type type =
DataType type =
prim_type.p {: return p; :}
| user_type.u {: return u; :}
| struct_type.s {: return s; :}
......@@ -109,8 +123,8 @@ StructType struct_type =
STRUCT LBRACE var_decl_list.l RBRACE {: return new StructType(l); :}
;
VoidType void_type =
VOID {: return new VoidType(); :}
VoidType void_type =
VOID {: return new VoidType(); :}
;
List dim_list =
......
......@@ -6,13 +6,13 @@ import se.lth.control.labcomm2014.compiler.LabCommParser.Terminals;
%%
%public
%final
%public
%final
%class LabCommScanner
%extends Scanner
%type Symbol
%function nextToken
%type Symbol
%function nextToken
%yylexthrow Scanner.Exception
%unicode
......@@ -44,13 +44,16 @@ Comment = {TraditionalComment}
TraditionalComment = "/*" [^*] ~"*/" | "/*" "*"+ "/" | "/*" "*"+ [^/*] ~"*/"
EndOfLineComment = "//" {InputCharacter}* {LineTerminator}?
Identifier = [:jletter:][:jletterdigit:]*
Identifier = [[:letter:]_]([[:letter:]_[:digit:]])*
StringLiteral = [:jletterdigit:]*
DecimalNumeral = 0 | {NonZeroDigit} {Digits}?
DecimalNumeral = 0 | {NonZeroDigit} {Digits}?
Digits = {Digit}+
Digit = 0 | {NonZeroDigit}
NonZeroDigit = [1-9]
QuotedString = "\""~"\""
%%
<YYINITIAL> {
......@@ -76,10 +79,14 @@ NonZeroDigit = [1-9]
"}" { return sym(Terminals.RBRACE); }
"[" { return sym(Terminals.LBRACK); }
"]" { return sym(Terminals.RBRACK); }
"(" { return sym(Terminals.LPAREN); }
")" { return sym(Terminals.RPAREN); }
";" { return sym(Terminals.SEMICOLON); }
":" { return sym(Terminals.COLON); }
"," { return sym(Terminals.COMMA); }
{Identifier} { return sym(Terminals.IDENTIFIER); }
{QuotedString} { return sym(Terminals.QUOTEDSTRING); }
}
// fall through errors
......
......@@ -2,7 +2,7 @@
aspect NameAnalysis {
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++) {
String s = getDecl(i).getName();
if (s.equals(name)) {
......@@ -24,7 +24,7 @@ aspect NameAnalysis {
inh TypeDecl Decl.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++) {
Decl d = getDecl(i);
if(d instanceof TypeDecl && d.getName().equals(name)) {
......@@ -34,8 +34,8 @@ aspect NameAnalysis {
return null;
}
syn TypeDecl Type.decl();
eq Type.decl() = null;
syn TypeDecl DataType.decl();
eq DataType.decl() = null;
eq UserType.decl() = lookupType(getName());
eq PrimType.decl() = null; //HERE BE DRAGONS XXX
......
......@@ -6,7 +6,7 @@ aspect PPIndentation {
inh String Field.pp_indent();
inh String StructType.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 {
" not declared");
}
public void Program.pp(PrintStream out) {
public void Specification.pp(PrintStream out) {
for(int i = 0; i < getNumDecl(); i++) {
getDecl(i).pp(out);
}
......@@ -27,24 +27,24 @@ aspect PrettyPrint {
// Pretty print declarations
public void TypeDecl.pp(PrintStream out) {
out.print("typedef ");
getType().ppIdentifier(out, getName());
getDataType().ppIdentifier(out, getName());
out.println(";");
}
public void SampleDecl.pp(PrintStream out) {
out.print("sample ");
getType().ppIdentifier(out, getName());
getDataType().ppIdentifier(out, getName());
out.println(";");
}
public void Field.pp(PrintStream out) {
out.print(pp_indent());
getType().ppIdentifier(out, getName());
getDataType().ppIdentifier(out, getName());
out.println(";");
}
// 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);
out.print(" ");
out.print(id);
......@@ -58,7 +58,7 @@ aspect PrettyPrint {
}
// PrettyPrint prefix type info
public void Type.ppPrefix(PrintStream out) {
public void DataType.ppPrefix(PrintStream out) {
throw new Error(this.getClass().getName() +
".ppPrefix(PrintStream out)" +
" not declared");
......@@ -81,7 +81,7 @@ aspect PrettyPrint {
}
public void ArrayType.ppPrefix(PrintStream out) {
getType().ppPrefix(out);
getDataType().ppPrefix(out);
}
public void StructType.ppPrefix(PrintStream out) {
......@@ -94,7 +94,7 @@ aspect PrettyPrint {
}
// PrettyPrint suffix type info (array dimensions)
public void Type.ppSuffix(PrintStream out) { }
public void DataType.ppSuffix(PrintStream out) { }
public void ArrayType.ppSuffix(PrintStream out) {
out.print("[");
......@@ -103,7 +103,7 @@ aspect PrettyPrint {
getExp(i).pp(out);
}
out.print("]");
getType().ppSuffix(out);
getDataType().ppSuffix(out);
}
public void IntegerLiteral.pp(PrintStream out) {
......
......@@ -4,75 +4,23 @@ aspect Python_CodeGenEnv {
// handles qualid nesting, indentation, file writing and
// prefix propagation
public class Python_env {
final private class Python_printer {
private boolean newline = true;
private PrintStream out;
public Python_printer(PrintStream out) {
this.out = out;
}
public void print(Python_env env, String s) {
if (newline) {
newline = false;
for (int i = 0 ; i < env.indent ; i++) {
out.print(" ");
}
}
out.print(s);
}
public void println(Python_env env, String s) {
print(env, s);
out.println();
newline = true;
}
public void println(Python_env env) {
out.println();
newline = true;
}
public class Python_env extends PrintEnv {
final private class Python_printer extends PrintEnv.Printer {
// public C_printer(PrintStream out) {
// super(out, " ");
// }
}
private int indent;
private Python_printer printer;
public Python_env(PrintStream out) {
this.indent = 0;
this.printer = new Python_printer(out);
}
public void indent() {
indent++;
}
public void unindent() {
indent--;
}
public void print(String s) {
printer.print(this, s);
}
public void println(String s) {
printer.println(this, s);
}
public void println() {
printer.println(this);
super(out);
}
}
}
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);
env.println("#!/usr/bin/python");
env.println("# Auto generated " + baseName);
......@@ -99,25 +47,27 @@ aspect Python_CodeGen {
}
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++) {
getDecl(i).Python_genSignatureAndTypedef(env);
}
}
public void Decl.Python_genSignatureAndTypedef(Python_env env) {
throw new Error(this.getClass().getName() +
".Python_genSignatureAndTypedef(Python_env env)" +
throw new Error(this.getClass().getName() +
".Python_genSignatureAndTypedef(Python_env env)" +
" not declared");
}
public void TypeDecl.Python_genSignatureAndTypedef(Python_env env) {
env.println("class " + getName() + "(object):");
env.indent();
env.println("typedef = labcomm2014.typedef('" + getName() + "',");
env.print("typedef = labcomm2014.typedef(");
Python_genIntentions(env);
env.println(",");
env.indent();
getType().Python_genTypedef(env);
getTypeInstance().Python_genTypedef(env);
env.unindent();
env.println(")");
env.unindent();
......@@ -127,27 +77,51 @@ aspect PythonTypes {
public void SampleDecl.Python_genSignatureAndTypedef(Python_env env) {
env.println("class " + getName() + "(object):");
env.indent();
env.println("signature = labcomm2014.sample('" + getName() + "', ");
env.print("signature = labcomm2014.sample(");
Python_genIntentions(env);
env.println(",");
env.indent();
getType().Python_genSignature(env);
getDataType().Python_genSignature(env);
env.unindent();
env.println(")");
env.println("typedef = labcomm2014.sample('" + getName() + "', ");
env.print("typedef = labcomm2014.sample(");
Python_genIntentions(env);
env.println(",");
env.indent();
getType().Python_genTypedef(env);
getTypeInstance().Python_genTypedef(env);
env.unindent();
env.println(")");
env.unindent();
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) {
lookupType(getName()).getType().Python_genSignature(env);
lookupType(getName()).getDataType().Python_genSignature(env);
}
public void Type.Python_genSignature(Python_env env) {
throw new Error(this.getClass().getName() +
".Python_genSignature(Python_env env)" +
public void DataType.Python_genSignature(Python_env env) {
throw new Error(this.getClass().getName() +
".Python_genSignature(Python_env env)" +
" not declared");
}
......@@ -173,7 +147,7 @@ aspect PythonTypes {
}
env.println("],");
env.indent();
getType().Python_genSignature(env);
getDataType().Python_genSignature(env);
env.print(")");
env.unindent();
}
......@@ -194,8 +168,10 @@ aspect PythonTypes {
}
public void Field.Python_genSignature(Python_env env) {
env.print("('" + getName() + "', ");
getType().Python_genSignature(env);
env.print("(");
Python_genIntentions(env);
env.print(", ");
getDataType().Python_genSignature(env);
env.print(")");
}
......@@ -203,9 +179,9 @@ aspect PythonTypes {
env.println(getName() + ".typedef");
}
public void Type.Python_genTypedef(Python_env env) {
throw new Error(this.getClass().getName() +
".Python_genTypedef(Python_env env)" +
public void DataType.Python_genTypedef(Python_env env) {
throw new Error(this.getClass().getName() +
".Python_genTypedef(Python_env env)" +
" not declared");
}
......@@ -231,7 +207,7 @@ aspect PythonTypes {
}
env.println("],");
env.indent();
getType().Python_genTypedef(env);
getDataType().Python_genTypedef(env);
env.print(")");
env.unindent();
}
......@@ -252,8 +228,10 @@ aspect PythonTypes {
}
public void Field.Python_genTypedef(Python_env env) {
env.print("('" + getName() + "', ");
getType().Python_genTypedef(env);
env.print("(");
Python_genIntentions(env);
env.print(", ");
getDataType().Python_genTypedef(env);
env.print(")");
}
......@@ -272,8 +250,8 @@ aspect PythonTypes {
}
public String Exp.Python_getValue() {
throw new Error(this.getClass().getName() +
".Python_getValue()" +
throw new Error(this.getClass().getName() +
".Python_getValue()" +
" not declared");
}
......
......@@ -77,7 +77,7 @@ aspect RAPID_CodeGen {
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
{
PrintStream ps = new PrintStream(new FileOutputStream(new File(file)));
......@@ -85,7 +85,7 @@ aspect RAPID_CodeGen {
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++) {
getDecl(i).RAPID_gen(env);
......@@ -126,7 +126,7 @@ aspect RAPID_CodeGen {
public void SampleDecl.RAPID_gen(RAPID_env env) {
// Add type declarations
String fullName = getType().RAPID_AddType(env, getName());
String fullName = getDataType().RAPID_AddType(env, getName());
// Add signature constants
String sig_len_name = "signature_len_" + getName();
String sig_name = "signature_" + getName();
......@@ -167,7 +167,7 @@ aspect RAPID_CodeGen {
params.add("VAR LabComm_Stream st");
params.add("VAR LabComm_Decoder_Sample s");
stmts.add("VAR " + fullName + " tmp;");
getType().RAPID_AddDecodeInstr(env, stmts, "tmp", "st");
getDataType().RAPID_AddDecodeInstr(env, stmts, "tmp", "st");
stmts.add("% s.handler % tmp;");
env.addProc("Decode_And_Handle_" + getName(), params, stmts);
......@@ -201,11 +201,11 @@ aspect RAPID_CodeGen {
params.add("VAR LabComm_Encoder_Sample s");
params.add("VAR " + fullName + " val");
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);
}
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());
}
......@@ -214,7 +214,7 @@ aspect RAPID_CodeGen {
for (int i = 0; i < getNumField(); i++) {
Field f = getField(i);
components.add(
f.getType().RAPID_AddType(env, name + "_" + f.getName()) +
f.getDataType().RAPID_AddType(env, name + "_" + f.getName()) +
" " + f.getName() + ";");
}
String typeName = env.addRecord(name, components);
......@@ -222,7 +222,7 @@ aspect RAPID_CodeGen {
}
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) {
throw new UnsupportedOperationException("RAPID generation only (currently) supports one-dimensional arrays");
}
......@@ -251,7 +251,7 @@ aspect RAPID_CodeGen {
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,
String var_name, String stream_name) {
throw new UnsupportedOperationException("RAPID code generation does not (currently) support "+getClass().getSimpleName());
......@@ -261,7 +261,7 @@ aspect RAPID_CodeGen {
java.util.List<String> instrs,
String var_name, String stream_name) {
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);
}
}
......@@ -270,7 +270,7 @@ aspect RAPID_CodeGen {
java.util.List<String> instrs,
String var_name, String stream_name) {
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);
}
}
......@@ -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,
String var_name, String stream_name) {
throw new UnsupportedOperationException("RAPID code generation does not (currently) support "+getClass().getSimpleName());
......@@ -315,7 +315,7 @@ aspect RAPID_CodeGen {
java.util.List<String> instrs,
String var_name, String stream_name) {
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);
}
}
......@@ -324,7 +324,7 @@ aspect RAPID_CodeGen {
java.util.List<String> instrs,
String var_name, String stream_name) {
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);
}
}
......
/* 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);
}