package absyn;

import env.Env;
import javaslang.collection.Tree;
import parse.Loc;
import semantic.SemanticHelper;
import types.Type;

public class DecVar extends Dec {

   public final String name;
   public final String typeName;
   public final Exp init;

   public DecVar(Loc loc, String name, String typeName, Exp init) {
      super(loc);
      this.name = name;
      this.typeName = typeName;
      this.init = init;
   }

   @Override
   public Tree.Node<String> toTree() {
      return Tree.of("DecVar",
                     Tree.of(name),
                     Tree.of(typeName),
                     init.toTree());
   }

   @Override
   public void semantic(Env env) {
      Type t_init = init.semantic(env);
      Type t_var = t_init;
      if (typeName != null) {
         Type t_typeName = env.tenv.get(typeName);
         if (t_typeName == null)
            throw SemanticHelper.undefined(loc, "type", typeName);
         if (!t_init.is(t_typeName))
            throw SemanticHelper.typeMismatch(init.loc, t_init, t_typeName);
         t_var = t_typeName;
      }
      env.venv.put(name, t_var);
   }
}

@@ -25,16 +25,50 @@
 nv.Env;%0A
+import javaslang.collection.List;%0A
 import j
@@ -521,54 +521,51 @@
     
-return Tree.of(%22DecVar%22,%0A                     
+List%3CTree.Node%3CString%3E%3E children = List.of(
 Tree
@@ -573,39 +573,83 @@
 of(name)
-,
+);
 %0A      
-               
+if (typeName != null)%0A         children = children.append(
 Tree.of(
@@ -661,44 +661,99 @@
 ame)
-,
+);
 %0A      
-               init.toTree()
+children = children.append(init.toTree());%0A      return Tree.of(%22DecVar%22, children
 );%0A 

Fix toTree method for variable declarations