From cc8f66ade3be590fe1bc6d2d39b177e6c6f3746b Mon Sep 17 00:00:00 2001
From: Sven Gestegard Robertz <sven.robertz@cs.lth.se>
Date: Wed, 11 Feb 2015 18:02:39 +0100
Subject: [PATCH] rename and cleanup

---
 examples/user_types/TDDecoder.java            | 17 ++-------
 lib/java/Makefile                             |  2 +-
 .../se/lth/control/labcomm/ASTbuilder.java    | 38 +++++++++++++++++--
 .../lth/control/labcomm2006/ASTbuilder.java   |  2 +-
 4 files changed, 40 insertions(+), 19 deletions(-)

diff --git a/examples/user_types/TDDecoder.java b/examples/user_types/TDDecoder.java
index be6e7eb..6bfdb3c 100644
--- a/examples/user_types/TDDecoder.java
+++ b/examples/user_types/TDDecoder.java
@@ -6,7 +6,7 @@ import java.io.IOException;
 import se.lth.control.labcomm.DecoderChannel;
 import se.lth.control.labcomm.TypeDef;
 import se.lth.control.labcomm.TypeDefParser;
-import se.lth.control.labcomm.TypeDefVisitor;
+import se.lth.control.labcomm.ASTbuilder;
 //import se.lth.control.labcomm.TypeBinding;
 
 import se.lth.control.labcomm2014.compiler.Program;
@@ -78,27 +78,18 @@ public class TDDecoder
   public void onTypeDef(TypeDefParser.ParsedTypeDef d) {
     if(d.isSampleDef()){
         System.out.println("onTypeDef (sample): ");
-        TypeDefVisitor v = new TypeDefVisitor();
+        ASTbuilder v = new ASTbuilder();
         Program p = v.makeProgram((TypeDefParser.ParsedSampleDef) d);
-        LinkedList errors = new LinkedList();
-        p.errorCheck(errors);
-        if(errors.isEmpty()) {
-            try {
+        try {
                 //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();
-            } catch (Throwable e) {
+        } catch (Throwable e) {
                 System.err.println("Exception: " + e);
                 e.printStackTrace();
-            }
-        } else {
-            for (Iterator iter = errors.iterator(); iter.hasNext(); ) {
-                String s = (String)iter.next();
-                System.out.println(s);
-            }
         }
     }
     //System.out.println(" "+d.getName()+";");
diff --git a/lib/java/Makefile b/lib/java/Makefile
index 3c65578..fc0daff 100644
--- a/lib/java/Makefile
+++ b/lib/java/Makefile
@@ -13,7 +13,7 @@ MODULES=Constant \
 	BuiltinType \
 	TypeDef \
 	TypeBinding \
-	TypeDefVisitor \
+	ASTbuilder \
 	TypeDefParser \
 	Writer \
 	WriterWrapper
diff --git a/lib/java/se/lth/control/labcomm/ASTbuilder.java b/lib/java/se/lth/control/labcomm/ASTbuilder.java
index 96a0991..f05a3c5 100644
--- a/lib/java/se/lth/control/labcomm/ASTbuilder.java
+++ b/lib/java/se/lth/control/labcomm/ASTbuilder.java
@@ -40,7 +40,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 TypeDefVisitor implements TypeDefParser.ParsedSymbolVisitor {
+public class ASTbuilder implements TypeDefParser.ParsedSymbolVisitor {
 
 ///// tree building
 //
@@ -48,11 +48,20 @@ public class TypeDefVisitor implements TypeDefParser.ParsedSymbolVisitor {
         private LinkedList<Type> typeStack;
         private LinkedList<Field> fieldStack;
 
-        public TypeDefVisitor() {
+        public ASTbuilder() {
             this.typeStack = new LinkedList<Type>();
             this.fieldStack = new LinkedList<Field>();
         }
 
+        private void assertStacksEmpty() throws RuntimeException {
+            if(!typeStack.isEmpty()) {
+               throw new RuntimeException("Error: type stack not empty"); 
+            }
+            if(!fieldStack.isEmpty()) {
+               throw new RuntimeException("Error: field stack not empty"); 
+            }
+        }
+
         public void visit(TypeDefParser.TypeSymbol s){
             throw new Error("not implemented? needed?");
 
@@ -119,12 +128,31 @@ public class TypeDefVisitor implements TypeDefParser.ParsedSymbolVisitor {
            Decl result = new TypeDecl(typeStack.pop(), d.getName());
            return result;
        }
+
+       private Program createAndCheckProgram(List<Decl> ds) {
+            Program p = new Program(ds);
+            LinkedList errors = new LinkedList();
+            p.errorCheck(errors);
+            if(errors.isEmpty()) {
+                return p;
+            } else {
+                //XXX temporary debug printout
+                for (Iterator iter = errors.iterator(); iter.hasNext(); ) {
+                    String s = (String)iter.next();
+                    System.err.println(s);
+                }
+                // This should not happen
+                throw new RuntimeException("Internal error: parsed labcomm declaration has errors");
+            }
+       }
        
        public Program makeProgram(TypeDefParser.ParsedTypeDef d) {
+           assertStacksEmpty();
            List<Decl> ds = new List<Decl>();
 
            ds.add(makeDecl(d));
-           return new Program(ds);
+           assertStacksEmpty();
+           return createAndCheckProgram(ds);
        }
 
        public Decl makeDecl(TypeDefParser.ParsedSampleDef d) {
@@ -133,6 +161,7 @@ public class TypeDefVisitor implements TypeDefParser.ParsedSymbolVisitor {
            return result;
        }
        public Program makeProgram(TypeDefParser.ParsedSampleDef d) {
+           assertStacksEmpty();
            List<Decl> ds = new List<Decl>();
 
            Iterator<TypeDefParser.ParsedTypeDef> it = d.getDepIterator();
@@ -142,7 +171,8 @@ public class TypeDefVisitor implements TypeDefParser.ParsedSymbolVisitor {
 
            ds.add(makeDecl(d));
 
-           return new Program(ds);
+           assertStacksEmpty();
+           return createAndCheckProgram(ds);
        }
     }
 
diff --git a/lib/java/se/lth/control/labcomm2006/ASTbuilder.java b/lib/java/se/lth/control/labcomm2006/ASTbuilder.java
index 33c054d..a65e887 100644
--- a/lib/java/se/lth/control/labcomm2006/ASTbuilder.java
+++ b/lib/java/se/lth/control/labcomm2006/ASTbuilder.java
@@ -1,4 +1,4 @@
 package se.lth.control.labcomm2006;
 
-public class TypeDefVisitor {
+public class ASTbuilder {
 }
-- 
GitLab