package de.dhbwstuttgart.parser.SyntaxTreeGenerator;

import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.syntaxtree.statement.*;

import java.util.List;

public class SyntacticSugar {

    public static List<Statement> addTrailingReturn(List<Statement> statements){
        if(statements.size()!=0) {
            Statement lastStmt = statements.get(statements.size() - 1);
            if (lastStmt instanceof Return) return statements;
            if (lastStmt instanceof WhileStmt) {
                if (hasReturn(((WhileStmt) lastStmt).loop_block)) return statements;
            } else if (lastStmt instanceof IfStmt) {
                if (hasReturn(((IfStmt) lastStmt).then_block)
                        && hasReturn(((IfStmt) lastStmt).else_block)) return statements;
            } else if (lastStmt instanceof ForStmt) {
                if (hasReturn(((ForStmt) lastStmt).body_Loop_block)) return statements;
            } else {
            }
        }
        statements.add(new ReturnVoid(new NullToken()));
        return statements;
    }

    private static boolean hasReturn(Block block){
        for(Statement s : block.getStatements())
            if(s instanceof Return)return true;
        return false;
    }

}
