package bf_mips.back_end;

import bf_mips.structs.ASMCode;
import bf_mips.structs.AbstractSyntaxTree.AST;
import bf_mips.structs.AbstractSyntaxTree.Node;

import java.util.List;
import java.util.Stack;

public class CodeGenerator {
    private AST syntaxTree;

    public CodeGenerator(AST syntaxTree) {
        this.syntaxTree = syntaxTree;
    }

    public String generate() {
        ASMCode MIPSCode = new ASMCode();
        Stack<String> loops = new Stack<String>();
        return traverse(syntaxTree.getRoot(), MIPSCode, loops, 0);
    }

    private String traverse(Node startNode, ASMCode MIPSCode, Stack<String> loops, int count) {
        List<Node> children = startNode.getChildren();
        for (Node child: children) {
            switch (child.getToken()) {
                case '+':
                    MIPSCode.incrementValue();
                    break;
                case '-':
                    MIPSCode.decrementValue();
                    break;
                case '>':
                    MIPSCode.incrementPt();
                    break;
                case '<':
                    MIPSCode.decrementPt();
                    break;
                case '[':
                    String label = Integer.toString(count);
                    MIPSCode.beginLoop(Integer.toString(count));
                    count++;
                    loops.push(label);
                    break;
                case '.':
                    MIPSCode.output();
                    break;
                case ',':
                    MIPSCode.input();
                    break;
            }

            if (child.hasChildren()) {
                traverse(child, MIPSCode, loops, count);
            }
        }

        if (! loops.isEmpty()) {
           MIPSCode.endLoop(loops.pop());
        }

        return MIPSCode.toString();
    }
}
@@ -174,32 +174,8 @@
 ist;
-%0Aimport java.util.Stack;
 %0A%0Apu
@@ -395,59 +395,8 @@
 ();%0A
-        Stack%3CString%3E loops = new Stack%3CString%3E();%0A
     
@@ -447,21 +447,17 @@
 PSCode, 
-loops
+0
 , 0);%0A  
@@ -527,26 +527,17 @@
 de, 
-Stack%3CString%3E loop
+int level
 s, i
@@ -599,24 +599,56 @@
 Children();%0A
+        int curr_label = count;%0A
         for 
@@ -668,24 +668,224 @@
 children) %7B%0A
+            if (child.hasChildren()) %7B%0A                count++;%0A                MIPSCode.beginLoop(Integer.toString(count));%0A                traverse(child, MIPSCode, levels+1, count);%0A            %7D%0A%0A
             
@@ -1310,254 +1310,8 @@
 ak;%0A
-                case '%5B':%0A                    String label = Integer.toString(count);%0A                    MIPSCode.beginLoop(Integer.toString(count));%0A                    count++;%0A                    loops.push(label);%0A                    break;%0A
     
@@ -1506,119 +1506,8 @@
    %7D
-%0A%0A            if (child.hasChildren()) %7B%0A                traverse(child, MIPSCode, loops, count);%0A            %7D
 %0A   
@@ -1530,29 +1530,23 @@
 if (
-! loops.isEmpty()
+levels %3E 0
 ) %7B%0A
+ 
     
@@ -1573,18 +1573,35 @@
 oop(
-loops.pop(
+Integer.toString(curr_label
 ));%0A

change the tree traversal to postorder so we can scrap the stack