Commit 25ad39c4 authored by Sven Gestegård Robertz's avatar Sven Gestegård Robertz
Browse files

started adding type hierarchy to Java API and unifying that with

TypeDefParser/ASTbuilder
parent 26636eec
......@@ -2,3 +2,4 @@
*.class
*.o
*.pyc
examples/dynamic/gen
......@@ -14,28 +14,28 @@ aspect ArrayRewrite {
to FixedArrayType {
if (getNumDim() == 1) {
return new FixedArrayType(getDataType(),
getDim(0).getExpList());
getDim(0));
} else {
List l = new List();
for (int i = 1 ; i < getNumDim() ; i++) {
l.add(getDim(i));
}
return new FixedArrayType(new ParseArrayType(getDataType(), l),
getDim(0).getExpList());
getDim(0));
}
}
when (getDim(0).isVariable())
to VariableArrayType {
if (getNumDim() == 1) {
return new VariableArrayType(getDataType(),
getDim(0).getExpList());
getDim(0));
} else {
List l = new List();
for (int i = 1 ; i < getNumDim() ; i++) {
l.add(getDim(i));
}
return new VariableArrayType(new ParseArrayType(getDataType(), l),
getDim(0).getExpList());
getDim(0));
}
}
}
......
......@@ -170,6 +170,13 @@ aspect Java_Class {
private void Decl.Java_declareUserTypeDeps(Java_env env) {
int numDeps = type_dependencies().size();
int i=0;
env.println("private static DataType dataType;");
env.println("static {");
env.indent();
Java_emitTypeTree(env);
env.unindent();
env.println("}");
env.println("private Set<SampleDispatcher> typeDependencies = new HashSet<SampleDispatcher>();");
env.println("public Dispatcher(){");
......@@ -183,6 +190,12 @@ aspect Java_Class {
env.unindent();
env.println("}");
env.println("public DataType getDataType(){");
env.indent();
env.println("return dataType;");
env.unindent();
env.println("}");
env.println("public Iterator<SampleDispatcher> getDependencyIterator(){");
env.indent();
env.println("return typeDependencies.iterator();");
......@@ -274,6 +287,12 @@ aspect Java_Class {
env.unindent();
env.println("}");
env.println();
env.println("public static DataType getDataType(){");
env.indent();
env.println("return dispatcher.getDataType();");
env.unindent();
env.println("}");
env.println();
}
public void TypeDecl.Java_emitClass(Java_env env, String pack) {
......@@ -284,6 +303,7 @@ aspect Java_Class {
env.println("import se.lth.control.labcomm"+env.verStr+".Constant;");
env.println("import se.lth.control.labcomm"+env.verStr+".SampleType;");
env.println("import se.lth.control.labcomm"+env.verStr+".*;");
if (getDataType().Java_needInstance() || hasDependencies() || isReferenced()) {
env.println("import se.lth.control.labcomm"+env.verStr+".Encoder;");
......@@ -340,6 +360,7 @@ aspect Java_Class {
env.println("import se.lth.control.labcomm"+env.verStr+".Encoder;");
env.println("import se.lth.control.labcomm"+env.verStr+".SampleHandler;");
env.println("import se.lth.control.labcomm"+env.verStr+".Sample;");
env.println("import se.lth.control.labcomm"+env.verStr+".*;");
env.println("import java.util.Set;");
env.println("import java.util.HashSet;");
env.println("import java.util.Iterator;");
......@@ -451,6 +472,98 @@ aspect Java_Class {
}
}
public void TypeInstance.Java_emitTypeTree(Java_env env, String varName) {
getDataType().Java_emitTypeTree(env, varName);
}
public void TypeInstance.Java_emitTypeField(Java_env env, String struct, String name) {
getDataType().Java_emitTypeField(env, struct, name);
}
public abstract void DataType.Java_emitTypeTree(Java_env env, String name);
public abstract void DataType.Java_emitTypeField(Java_env env, String struct, String name);
public void PrimType.Java_emitTypeTree(Java_env env, String name) {
env.println("// "+name+"="+getName() + " : " + getToken());
env.println(name+" = new SigPrimitiveType("+getToken()+");");
}
public void PrimType.Java_emitTypeField(Java_env env, String struct, String name) {
//env.println("// "+struct+".addField("+name+ ", "+getName()+");");
env.println(struct+".addField(\""+name+ "\", new SigPrimitiveType("+getToken()+"));");
}
public void VoidType.Java_emitTypeTree(Java_env env, String name) {
env.println(name+"= new VoidType();");
}
public void VoidType.Java_emitTypeField(Java_env env, String struct, String name) {
throw new Error("Void may not be field");
}
public void ParseArrayType.Java_emitTypeTree(Java_env env, String name) {
String elementType = name + "_element";
env.println("DataType "+elementType+";");
getDataType().Java_emitTypeTree(env, elementType);
env.println("// "+name+"= array("+elementType+");");
}
syn String Exp.dimArrayPart();
eq IntegerLiteral.dimArrayPart() = getValue();
eq VariableSize.dimArrayPart() = "-1";
syn String Dim.dimArrayString() {
StringBuilder sb = new StringBuilder();
sb.append("new int[] {");
for(int i=0; i<getNumExp(); i++) {
sb.append(getExp(i).dimArrayPart());
if(i < getNumExp()-1) {
sb.append(", ");
}
}
sb.append("}");
return sb.toString();
}
public void ArrayType.Java_emitTypeTree(Java_env env, String name) {
String elementType = name + "_element";
StringBuilder dim = new StringBuilder();
dim.append("new int[] {");
env.println("DataType "+elementType+";");
getDataType().Java_emitTypeTree(env, elementType);
env.println(name+"= new SigArrayType("+elementType+","+getDim().dimArrayString()+");");
}
public void ParseArrayType.Java_emitTypeField(Java_env env, String struct, String name) {
env.println("// field "+name+ " = array("+getDataType().Java_getTypeName()); }
public void ArrayType.Java_emitTypeField(Java_env env, String struct, String name) {
String innerName = struct+"_"+name;
env.println("DataType "+innerName+";");
Java_emitTypeTree(env, innerName);
env.println(struct+".addField(\""+name+ "\", "+innerName+");");
//env.println(struct+".addField(\""+name+ "\", new UserType(\""+getName()+"\"));");
}
public void StructType.Java_emitTypeTree(Java_env env, String name) {
env.println(name+" = new SigStructType();");
for(Field f : getFields()) {
f.Java_emitTypeField(env, name, f.getName());
}
}
public void StructType.Java_emitTypeField(Java_env env, String struct, String name) {
String innerName = struct+"_"+name;
env.println("SigStructType "+innerName+" = new SigStructType();");
for(Field f : getFields()) {
f.Java_emitTypeField(env, innerName, f.getName());
}
env.println(struct+".addField(\""+name+ "\", "+innerName+");");
}
public void UserType.Java_emitTypeTree(Java_env env, String name) {
env.println(name+" = new SigUserType(\""+getName()+"\");");
}
public void UserType.Java_emitTypeField(Java_env env, String struct, String name) {
env.println(struct+".addField(\""+name+ "\", new SigUserType(\""+getName()+"\"));");
}
public void Decl.Java_emitTypeTree(Java_env env) {
getTypeInstance().Java_emitTypeTree(env, "dataType");
}
//XXX TODO: refactor: split into a static class ("TypeDefSingleton"?)and a (smaller) dispatcher
public void Decl.Java_emitDispatcher(Java_env env, boolean isSample) {
// String genericStr = ""; //env.versionHasMetaData()?"<"+getName()+">":"";
......@@ -628,6 +741,39 @@ aspect Java_Class {
public void VoidType.Java_emitEncoder(Java_env env, String name) {
}
public abstract String DataType.Java_getTypeName();
public String UserType.Java_getTypeName() {
return "UserType";
}
public String VoidType.Java_getTypeName() {
return "VoidType";
}
public String ArrayType.Java_getTypeName() {
return "SigArrayType";
}
public String ParseArrayType.Java_getTypeName() {
throw new Error("should not be called");
//return "ParseArrayType";
}
public String StructType.Java_getTypeName() {
return "SigStructType";
}
public String PrimType.Java_getTypeName() {
switch (getToken()) {
case LABCOMM_BOOLEAN: { return "BooleanType"; }
case LABCOMM_BYTE: { return "ByteType"; }
case LABCOMM_SHORT: { return "ShortType"; }
case LABCOMM_INT: { return "IntType"; }
case LABCOMM_LONG: { return "LongType"; }
case LABCOMM_FLOAT: { return "FloatType"; }
case LABCOMM_DOUBLE: { return "DoubleType"; }
case LABCOMM_STRING: { return "StringType"; }
case LABCOMM_SAMPLE: { return "SampleRefType"; }
}
throw new Error( "unknown primitive type ("+getToken()+")");
}
public void PrimType.Java_emitEncoder(Java_env env, String name) {
switch (getToken()) {
case LABCOMM_BOOLEAN: { env.print("e.encodeBoolean"); } break;
......
......@@ -35,7 +35,7 @@ PrimType : DataType ::= <Name:String> <Token:int>;
UserType : DataType ::= <Name:String>;
StructType : DataType ::= Field*;
ParseArrayType : DataType ::= DataType Dim*;
abstract ArrayType : DataType ::= DataType Exp*;
abstract ArrayType : DataType ::= DataType Dim;
VariableArrayType : ArrayType;
FixedArrayType : ArrayType;
......
/* 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();
......
......@@ -6,6 +6,7 @@ import java.io.IOException;
import se.lth.control.labcomm2014.DecoderChannel;
import se.lth.control.labcomm2014.TypeDef;
import se.lth.control.labcomm2014.TypeDefParser;
import se.lth.control.labcomm2014.SigTypeDef;
//import se.lth.control.labcomm2014.TypeBinding;
public class Decoder
......@@ -65,7 +66,7 @@ public class Decoder
// System.out.println("Got TypeBinding: "+d.getSampleIndex()+" --> "+d.getTypeIndex()+"");
// }
public void onTypeDef(TypeDefParser.ParsedTypeDef d) {
public void onTypeDef(SigTypeDef d) {
System.out.println("ontype_def: ");
if(d != null) {
System.out.print((d.isSampleDef()?"sample ":"typedef ")+d);
......
......@@ -4,6 +4,7 @@ LCLJAR=${LCDIR}/lib/java/labcomm2014.jar# the LabComm library
EXECUTABLES=example_encoder example_decoder \
Encoder.class Decoder.class TDDecoder.class \
TestDataType.class \
ExampleEncoder.exe ExampleDecoder.exe
include ${LCDIR}/lib/c/os_compat.mk
......@@ -51,6 +52,7 @@ build :
javac -cp ${LCDIR}/lib/java/labcomm2014.jar:. ${GENDIR}/*.java Encoder.java Decoder.java
javac -cp ${LCDIR}/lib/java/labcomm2014.jar:${LCCJAR}:${GENDIR}:. TDDecoder.java
javac -cp ${LCDIR}/lib/java/labcomm2014.jar:${LCCJAR}:${GENDIR}:. TestDataType.java
${CC} ${CFLAGS} ${LDFLAGS} -Wall -Werror -Wno-unused-function \
-I. -I${LCDIR}/lib/c/2014 -L${LCDIR}/lib/c \
......@@ -183,6 +185,15 @@ runjastadd: cleanbuild
@echo "************ running Java TypeDefdecoder: *****************"
java -cp .:${LCDIR}/lib/java/labcomm2014.jar:${LCCJAR}:${GENDIR} TDDecoder encoded_data_j
rundatatype: cleanbuild
@echo
@echo "********************************************"
@echo "*************** running datatype example ***"
@echo "********************************************"
@echo
@java -cp .:${LCDIR}/lib/java/labcomm2014.jar:${GENDIR} TestDataType
clean:
rm -rf ${GENDIR}
......
......@@ -6,8 +6,11 @@ import java.io.IOException;
import se.lth.control.labcomm2014.DecoderChannel;
import se.lth.control.labcomm2014.TypeDef;
import se.lth.control.labcomm2014.TypeDefParser;
import se.lth.control.labcomm2014.SigTypeDef;
import se.lth.control.labcomm2014.ParsedSampleDef;
import se.lth.control.labcomm2014.ASTbuilder;
//import se.lth.control.labcomm2014.TypeBinding;
import se.lth.control.labcomm2014.DataType;
import se.lth.control.labcomm2014.compiler.Specification;
import java.io.FileOutputStream;
......@@ -75,18 +78,29 @@ public class TDDecoder
// System.out.println("Got TypeBinding: "+d.getSampleIndex()+" --> "+d.getTypeIndex()+"");
// }
public void onTypeDef(TypeDefParser.ParsedTypeDef d) {
public void onTypeDef(SigTypeDef d) {
if(d != null && d.isSampleDef()){
System.out.println("onTypeDef (sample): ");
//------------
try {
System.out.println("==================== DataType ======");
DataType.printDataType(System.out, d);
System.out.println();
System.out.println("==================== end ======");
} catch (IOException ioe) {
ioe.printStackTrace();
}
ASTbuilder v = new ASTbuilder();
Specification p = v.makeSpecification((TypeDefParser.ParsedSampleDef) d);
Specification p = v.makeSpecification((ParsedSampleDef) d);
try {
//FileOutputStream f = new FileOutputStream("/tmp/foopp"+d.getName()+".txt");
//PrintStream out = new PrintStream(f);
FileOutputStream f = new FileOutputStream("/tmp/foopp"+d.getName()+".txt");
PrintStream out = new PrintStream(f);
p.pp(System.out);
//p.C_genC(System.out, new Vector(), "lcname", "prefix", 2014);
//p.J_gen(out, "testpackage", 2014);
//out.close();
p.J_gen(out, "testpackage", 2014);
out.close();
} catch (Throwable e) {
System.err.println("Exception: " + e);
e.printStackTrace();
......
import java.io.PrintStream;
import se.lth.control.labcomm2014.*;
/**
* Test data type tree
*/
public class TestDataType
{
private PrintStream out;
public TestDataType(PrintStream out)
throws Exception
{
this.out = out;
}
public void doTest() throws java.io.IOException {
twoLines x = new twoLines();
DataType.printDataType(out, x.getDispatcher());
twoStructsAndInt y = new twoStructsAndInt();
DataType.printDataType(out, y.getDispatcher());
theFirstInt z = new theFirstInt();
DataType.printDataType(out, z.getDispatcher());
doavoid a = new doavoid();
DataType.printDataType(out, a.getDispatcher());
}
public static void main(String[] arg) throws Exception {
TestDataType example = new TestDataType(System.out);
example.doTest();
}
}
......@@ -41,3 +41,19 @@ sample struct {
sample anInt theFirstInt;
sample anInt theSecondInt;
sample struct {
struct {
int x;
int y;
} s1;
struct {
int a;
int b;
} s2;
int i;
double double_array[2,3,_][3][_];
} twoStructsAndInt;
......@@ -15,8 +15,27 @@ MODULES=Constant \
TypeBinding \
ASTbuilder \
TypeDefParser \
SigTypeDef \
SigSampleDef \
ParsedTypeDef \
ParsedSampleDef \
Writer \
WriterWrapper
WriterWrapper \
DataType \
VoidType \
SignatureSymbolVisitor \
SignatureSymbol \
TypeSymbol \
SampleSymbol \
NameSymbol \
SigPrimitiveType \
SigStructType \
SigField \
SigArrayType \
SigUserType
.PHONY: all
all: labcomm2014.jar
......
......@@ -10,9 +10,6 @@ import java.io.DataInputStream;
import java.io.IOException;
import java.io.EOFException;
import se.lth.control.labcomm2014.TypeDef;
import se.lth.control.labcomm2014.TypeDefParser;
import se.lth.control.labcomm2014.compiler.LabComm;
import se.lth.control.labcomm2014.compiler.LabCommParser;
......@@ -40,7 +37,7 @@ import se.lth.control.labcomm2014.compiler.VariableSize;
/** A class for building a JastAdd AST from the parsed types
* created by a TypeDefParser. This class depends on the LabComm compiler.
*/
public class ASTbuilder implements TypeDefParser.ParsedSymbolVisitor {
public class ASTbuilder implements SignatureSymbolVisitor {
private LinkedList<DataType> typeStack;
private LinkedList<Field> fieldStack;
......@@ -59,19 +56,19 @@ public class ASTbuilder implements TypeDefParser.ParsedSymbolVisitor {
}
}
public void visit(TypeDefParser.TypeSymbol s){
public void visit(TypeSymbol s){
throw new Error("not implemented? needed?");
}
public void visit(TypeDefParser.SampleSymbol s){
public void visit(SampleSymbol s){
throw new Error("not implemented? needed?");
}
public void visit(TypeDefParser.NameSymbol s){
public void visit(NameSymbol s){
throw new Error("not implemented? needed?");
}
public void visit(TypeDefParser.PrimitiveType t){
public void visit(SigPrimitiveType t){
typeStack.push(new PrimType(t.getName(), t.getTag()));
}
......@@ -81,24 +78,24 @@ public class ASTbuilder implements TypeDefParser.ParsedSymbolVisitor {
// typeStack.push(new SampleRefType());
// }
public void visit(TypeDefParser.ParsedStructType t){
public void visit(SigStructType t){
if(t.isVoid()) {
typeStack.push(new VoidType());
} else {
List<Field> tmpF = new List<Field>();
for( TypeDefParser.ParsedField f : t.getFields()) {
for( SigField f : t.getFields()) {
f.accept(this);
tmpF.add(fieldStack.pop());
}
typeStack.push(new StructType(tmpF));
}
}
public void visit(TypeDefParser.ParsedField t){
public void visit(SigField t){
t.getType().accept(this);
fieldStack.push(new Field(new TypeInstance(typeStack.pop(),t.getName())));
}
public void visit(TypeDefParser.ArrayType t){
public void visit(SigArrayType t){
boolean isFixed = true;
List<Exp> dim = new List<Exp>();
for(int i : t.getIdx()) {
......@@ -111,18 +108,18 @@ public class ASTbuilder implements TypeDefParser.ParsedSymbolVisitor {
}
t.getType().accept(this);
if(isFixed) {
typeStack.push(new FixedArrayType(typeStack.pop(), dim));
typeStack.push(new FixedArrayType(typeStack.pop(), new Dim(dim)));
} else {
typeStack.push(new VariableArrayType(typeStack.pop(), dim));
typeStack.push(new VariableArrayType(typeStack.pop(), new Dim(dim)));
}
}
public void visit(TypeDefParser.ParsedUserType t){
public void visit(SigUserType t){
typeStack.push(new UserType(t.getName()));
}
public Decl makeDecl(TypeDefParser.ParsedTypeDef d) {
public Decl makeDecl(SigTypeDef d) {
d.getType().accept(this);
Decl result = new TypeDecl(new TypeInstance(typeStack.pop(), d.getName()));
return result;
......@@ -146,7 +143,7 @@ public class ASTbuilder implements TypeDefParser.ParsedSymbolVisitor {
}
}
public Specification makeSpecification(TypeDefParser.ParsedTypeDef d) {
public Specification makeSpecification(SigTypeDef d) {
assertStacksEmpty();
List<Decl> ds = new List<Decl>();
......@@ -155,19 +152,26 @@ public class ASTbuilder implements TypeDefParser.ParsedSymbolVisitor {
return createAndCheckSpecification(ds);
}
public Decl makeDecl(TypeDefParser.ParsedSampleDef d) {
public Decl makeDecl(SigSampleDef d) {
d.getType().accept(this);
Decl result = new SampleDecl(new TypeInstance(typeStack.pop(), d.getName()));
return result;
}
public Specification makeSpecification(TypeDefParser.ParsedSampleDef d) {
assertStacksEmpty();
List<Decl> ds = new List<Decl>();
Iterator<TypeDefParser.ParsedTypeDef> it = d.getDepIterator();
private void addAllDeps(List<Decl> ds, SigTypeDef d) {
Iterator<SigTypeDef> it = d.getDepIterator();
while(it.hasNext()){
ds.add(makeDecl(it.next()));
SigTypeDef dd = it.next();
addAllDeps(ds,dd);
ds.add(makeDecl(dd));
}
}
public Specification makeSpecification(ParsedSampleDef d) {
assertStacksEmpty();
List<Decl> ds = new List<Decl>();
addAllDeps(ds, d);
ds.add(makeDecl(d));
......
package se.lth.control.labcomm2014;
import java.io.PrintStream;
import java.util.Iterator;
public abstract class DataType implements SignatureSymbol{
private final String name;
private final int typeTag;
public static String tagName(int typeTag) {
switch(typeTag) {
case Constant.BOOLEAN : return "boolean";
case Constant.BYTE : return "byte";
case Constant.SHORT : return "short";
case Constant.INT : return "int";
case Constant.LONG : return "long";
case Constant.FLOAT : return "float";
case Constant.DOUBLE : return "double";
case Constant.STRING : return "string";
case Constant.SAMPLE : return "sample";
}
throw new Error("not primitive type tag : " +Integer.toHexString(typeTag));
}
protected DataType(String name, int typeTag) {
this.name = name;
this.typeTag = typeTag;
}
// protected DataType(int typeTag) {
// this(tagName(typeTag), typeTag);
// }
//
public String getName() {
return name;
}
public int getTag() {
return typeTag;
}
public boolean isArray() {
return false;
}
public boolean isStruct(){
return false;
}
public boolean isUserType(){
return false;
}