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

added void and sampleRef types to typedef parser

parent 62a2866a
......@@ -16,12 +16,14 @@ public class Encoder
throws Exception
{
encoder = new EncoderChannel(out);
doavoid.register(encoder);
theTwoInts.register(encoder);
IntString.register(encoder);
TwoArrays.register(encoder);
}
public void doEncode() throws java.io.IOException {
TwoInts x = new TwoInts();
x.a = 17;
x.b = 42;
......@@ -35,6 +37,9 @@ public class Encoder
// ta.variable = new int[][] {{1,2},{0x11,0x12},{0x21,0x22},{0x31,0x32}};
ta.variable = new int[][] {{1,2, 3, 4},{0x21,0x22,0x23,0x24}};
System.out.println("Encoding doavoid");
doavoid.encode(encoder);
System.out.println("Encoding theTwoInts, a="+x.a+", b="+x.b);
theTwoInts.encode(encoder, x);
......
......@@ -35,6 +35,7 @@ public class Constant {
public static final int FLOAT = 0x25;
public static final int DOUBLE = 0x26;
public static final int STRING = 0x27;
public static final int SAMPLE = 0x28;
/*
......
......@@ -211,6 +211,7 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler {
void visit(SampleSymbol s);
void visit(NameSymbol s);
void visit(PrimitiveType t);
void visit(SampleRefType t);
void visit(ParsedStructType t);
void visit(ParsedField t);
void visit(ArrayType t);
......@@ -257,6 +258,15 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler {
//public abstract Type makeNode();
}
public class SampleRefType extends ParsedType {
public void accept(ParsedSymbolVisitor v) {
v.visit(this);
}
public String toString() {
return "sample";}
}
public class PrimitiveType extends ParsedType {
private final String name;
private int tag;
......@@ -323,15 +333,23 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler {
fields[idx] = f;
}
public boolean isVoid() {
return fields.length == 0;
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("struct {\n");
for(ParsedField f : fields) {
sb.append(f.toString());
sb.append(";\n");
if(isVoid()) { //HERE BE DRAGONS: void type is empty struct
return "void";
} else {
StringBuilder sb = new StringBuilder();
sb.append("struct {\n");
for(ParsedField f : fields) {
sb.append(f.toString());
sb.append(";\n");
}
sb.append("}");
return sb.toString();
}
sb.append("}");
return sb.toString();
}
public void accept(ParsedSymbolVisitor v) {
......@@ -656,6 +674,8 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler {
TypeDef td = typeDefs.get(tag);
result = new ParsedUserType(td.getName());
in.pushType(tag);
} else if(tag == Constant.SAMPLE) {
result = new SampleRefType();
} else {
result = new PrimitiveType(tag);
}
......
......@@ -26,8 +26,8 @@ import se.lth.control.labcomm2014.compiler.Decl;
import se.lth.control.labcomm2014.compiler.TypeDecl;
import se.lth.control.labcomm2014.compiler.SampleDecl;
import se.lth.control.labcomm2014.compiler.Type;
//import se.lth.control.labcomm2014.compiler.VoidType;
//import se.lth.control.labcomm2014.compiler.SampleRefType;
import se.lth.control.labcomm2014.compiler.VoidType;
import se.lth.control.labcomm2014.compiler.SampleRefType;
import se.lth.control.labcomm2014.compiler.PrimType;
import se.lth.control.labcomm2014.compiler.UserType;
import se.lth.control.labcomm2014.compiler.StructType;
......@@ -70,13 +70,22 @@ public class TypeDefVisitor implements TypeDefParser.ParsedSymbolVisitor {
public void visit(TypeDefParser.PrimitiveType t){
typeStack.push(new PrimType(t.getName(), t.getTag()));
}
public void visit(TypeDefParser.SampleRefType t){
typeStack.push(new SampleRefType());
}
public void visit(TypeDefParser.ParsedStructType t){
List<Field> tmpF = new List<Field>();
for( TypeDefParser.ParsedField f : t.getFields()) {
f.accept(this);
tmpF.add(fieldStack.pop());
if(t.isVoid()) {
typeStack.push(new VoidType());
} else {
List<Field> tmpF = new List<Field>();
for( TypeDefParser.ParsedField f : t.getFields()) {
f.accept(this);
tmpF.add(fieldStack.pop());
}
typeStack.push(new StructType(tmpF));
}
typeStack.push(new StructType(tmpF));
}
public void visit(TypeDefParser.ParsedField t){
t.getType().accept(this);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment