package dk.aau.sw402F15.CodeGenerator;

import dk.aau.sw402F15.parser.analysis.DepthFirstAdapter;
import dk.aau.sw402F15.parser.node.*;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by Mikkel on 22-04-2015.
 */
public class ASTSimplify extends DepthFirstAdapter {

    @Override
    public void outAForStatement(AForStatement node) {
        List<PStatement> statementList = new ArrayList<PStatement>();
        List<PStatement> statements = new ArrayList<PStatement>();

        statements.add(node.getStatement());
        for (PExpr expr : node.getIterator())
            statements.add(new AExprStatement(expr));

        for (PExpr expr : node.getInitilizer())
            statements.add(new AExprStatement(expr));
        statementList.add(new AWhileStatement(node.getCondition(), new AScopeStatement(statements)));

        node.replaceBy(new AScopeStatement(statementList));

        super.outAForStatement(node);
    }

}
@@ -162,37 +162,8 @@
 st;%0A
-import java.util.LinkedList;%0A
 impo
@@ -850,103 +850,2561 @@
     
-node.replaceBy(new AScopeStatement(statementList));%0A%0A        super.outAForStatement(node);%0A    %7D%0A
+Node newNode = new AScopeStatement(statementList);%0A%0A        // Apply newnode%0A        newNode.apply(this);%0A%0A        // Replace node%0A        node.replaceBy(newNode);%0A    %7D%0A%0A    @Override%0A    public void caseAModExpr(AModExpr node) %7B%0A        // 3 %25 2 = std.mod(3, 2) // STDLIB%0A        List%3CPExpr%3E argumentList = new ArrayList%3CPExpr%3E();%0A        argumentList.add(node.getLeft());%0A        argumentList.add(node.getRight());%0A%0A        Node newNode =%0A                new AMemberExpr(%0A                        new AFunctionCallExpr(new TIdentifier(%22std%22), new ArrayList%3CObject%3E()),%0A                        new AFunctionCallExpr(new TIdentifier(%22mod%22), argumentList%0A                        )%0A                )%0A        ;%0A%0A        // Apply newnode%0A        newNode.apply(this);%0A%0A        // Replace node%0A        node.replaceBy(newNode);%0A    %7D%0A%0A    @Override%0A    public void caseACompoundAddExpr(ACompoundAddExpr node) %7B%0A        Node newNode = new AAssignmentExpr(%0A                node.getLeft(),%0A                new AAddExpr(node.getLeft(), node.getRight())%0A        );%0A%0A        // Apply newnode%0A        newNode.apply(this);%0A%0A        // Replace node%0A        node.replaceBy(newNode);%0A    %7D%0A%0A    @Override%0A    public void caseACompoundSubExpr(ACompoundSubExpr node) %7B%0A        Node newNode = new AAssignmentExpr(%0A                node.getLeft(),%0A                new ASubExpr(node.getLeft(), node.getRight())%0A        );%0A%0A        // Apply newnode%0A        newNode.apply(this);%0A%0A        // Replace node%0A        node.replaceBy(newNode);%0A    %7D%0A%0A    @Override%0A    public void caseACompoundMultExpr(ACompoundMultExpr node) %7B%0A        Node newNode = new AAssignmentExpr(%0A                node.getLeft(),%0A                new AMultiExpr(node.getLeft(), node.getRight())%0A        );%0A%0A        // Apply newnode%0A        newNode.apply(this);%0A%0A        // Replace node%0A        node.replaceBy(newNode);%0A    %7D%0A%0A    @Override%0A    public void caseACompoundDivExpr(ACompoundDivExpr node) %7B%0A        Node newNode = new AAssignmentExpr(%0A                node.getLeft(),%0A                new ADivExpr(node.getLeft(), node.getRight())%0A        );%0A%0A        // Apply newnode%0A        newNode.apply(this);%0A%0A        // Replace node%0A        node.replaceBy(newNode);%0A    %7D%0A%0A    @Override%0A    public void caseACompoundModExpr(ACompoundModExpr node) %7B%0A        Node newNode = new AAssignmentExpr(%0A                node.getLeft(),%0A                new AModExpr(node.getLeft(), node.getRight())%0A        );%0A%0A        // Apply newnode%0A        newNode.apply(this);%0A%0A        // Replace node%0A        node.replaceBy(newNode);%0A    %7D
 %0A%7D

Simplify compound and mod rewrite