From e7b1f0ec2b94644f6fcde90637fdc76e810371c7 Mon Sep 17 00:00:00 2001
From: Sven Gestegard Robertz <sven.robertz@cs.lth.se>
Date: Mon, 9 Feb 2015 22:29:38 +0100
Subject: [PATCH] added void and sampleRef types to typedef parser

---
 examples/simple/Encoder.java                  |  5 +++
 lib/java/se/lth/control/labcomm/Constant.java |  1 +
 .../se/lth/control/labcomm/TypeDefParser.java | 34 +++++++++++++++----
 .../lth/control/labcomm/TypeDefVisitor.java   | 23 +++++++++----
 4 files changed, 49 insertions(+), 14 deletions(-)

diff --git a/examples/simple/Encoder.java b/examples/simple/Encoder.java
index d70384c..15e4858 100644
--- a/examples/simple/Encoder.java
+++ b/examples/simple/Encoder.java
@@ -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);
 
diff --git a/lib/java/se/lth/control/labcomm/Constant.java b/lib/java/se/lth/control/labcomm/Constant.java
index 1198172..e109b30 100644
--- a/lib/java/se/lth/control/labcomm/Constant.java
+++ b/lib/java/se/lth/control/labcomm/Constant.java
@@ -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;
 
 
   /*
diff --git a/lib/java/se/lth/control/labcomm/TypeDefParser.java b/lib/java/se/lth/control/labcomm/TypeDefParser.java
index 6765978..2fb07e3 100644
--- a/lib/java/se/lth/control/labcomm/TypeDefParser.java
+++ b/lib/java/se/lth/control/labcomm/TypeDefParser.java
@@ -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);
         }
diff --git a/lib/java/se/lth/control/labcomm/TypeDefVisitor.java b/lib/java/se/lth/control/labcomm/TypeDefVisitor.java
index 4ce48c6..835d20b 100644
--- a/lib/java/se/lth/control/labcomm/TypeDefVisitor.java
+++ b/lib/java/se/lth/control/labcomm/TypeDefVisitor.java
@@ -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);
-- 
GitLab