<commit_before>package com.thesis;

import com.thesis.expression.*;

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

public class LoopEnhancer implements StackEnhancer {

	@Override
	public void enhance(ExpressionStack expressionStack, Stack<StackItem> stack) {
		for (int i = 0; i < expressionStack.size(); i++) {
			Expression exp = expressionStack.get(i);
			if (exp instanceof ConditionalExpression) {
				updateDoLoops((ConditionalExpression) exp, i, expressionStack, stack);
			}
		}

		for (int i = 0; i < stack.size(); i++) {
			Expression exp = stack.get(i).getExpression();
			if (exp instanceof ConditionalExpression) {
				int loopStartLabel = stack.get(i).getLabelId();
				int nextLabel = i < stack.size() - 1 ? stack.get(i+1).getLabelId() : -2;
				fixLoopDetection((ConditionalExpression) exp, loopStartLabel, stack.get(i));
				improveBreakAndContinue((ConditionalExpression) exp, nextLabel, (ConditionalExpression) exp);
			}
		}
	}

	private void updateDoLoops(ConditionalExpression exp, int currentStackPosition, ExpressionStack expressionStack, Stack<StackItem> stack) {
		if ((exp.getThenBranch() != null && !exp.getThenBranch().isEmpty()) || (exp.getElseBranch() != null && !exp.getElseBranch().isEmpty())) return;
		for (int stackPosition = 0; stackPosition < currentStackPosition; stackPosition++) {
			if (stack.get(stackPosition).getLabelId() == exp.getJumpDestination()) {
				exp.setLoopType(ConditionalExpression.LoopType.DO_WHILE);
				exp.setStartFrameLocation(exp.getJumpDestination());
				exp.getThenBranch().addAll(expressionStack.substack(stackPosition, currentStackPosition));
				exp.negate();
				break;
			}
		}
	}

	private void fixLoopDetection(ConditionalExpression topLevelExp, int loopStartLabel, StackItem stackItem) {
		if (!(stackItem.getExpression() instanceof ConditionalExpression) || ((ConditionalExpression) stackItem.getExpression()).getThenBranch() == null) {
			return;
		}
		ConditionalExpression expression = ((ConditionalExpression) stackItem.getExpression());
		for (int i = 0; i < expression.getThenBranch().getAll().size(); i++) {
			StackItem item = expression.getThenBranch().getAll().get(i);
			fixLoopDetection(topLevelExp, loopStartLabel, item);
			if (item.getExpression() instanceof ConditionalExpression && ((ConditionalExpression) item.getExpression()).getJumpDestination() == loopStartLabel) {
				topLevelExp.setElseBranchEnd(((ConditionalExpression) item.getExpression()).getJumpDestination());
			}
		}

		if (((ConditionalExpression) stackItem.getExpression()).getElseBranch() != null) {
			for (int i = 0; i < expression.getElseBranch().getAll().size(); i++) {
				StackItem item = expression.getElseBranch().getAll().get(i);
				fixLoopDetection(topLevelExp, loopStartLabel, item);
				if (item.getExpression() instanceof ConditionalExpression && ((ConditionalExpression) item.getExpression()).getJumpDestination() == loopStartLabel) {
					topLevelExp.setElseBranchEnd(((ConditionalExpression) item.getExpression()).getJumpDestination());
				}
			}
		}
	}

	private void improveBreakAndContinue(ConditionalExpression exp, int nextLabel, ConditionalExpression topLevelExpression) {
		List<StackItem> thenBranch;
		List<StackItem> elseBranch;

		if (exp.getThenBranch() != null) {
			thenBranch = exp.getThenBranch().getAll();
			for (StackItem innerExp : thenBranch) {
				if (innerExp.getExpression() instanceof ConditionalExpression) {
					improveBreakAndContinue((ConditionalExpression) innerExp.getExpression(), nextLabel, topLevelExpression);
				}
			}
			addBreak(nextLabel, topLevelExpression, thenBranch);
			addContinue(exp, topLevelExpression, thenBranch);
		}
<commit_msg>for loop replaced with for each in loop enhancer<commit_after>package com.thesis;

import com.thesis.expression.*;

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

public class LoopEnhancer implements StackEnhancer {

	@Override
	public void enhance(ExpressionStack expressionStack, Stack<StackItem> stack) {
		for (int i = 0; i < expressionStack.size(); i++) {
			Expression exp = expressionStack.get(i);
			if (exp instanceof ConditionalExpression) {
				updateDoLoops((ConditionalExpression) exp, i, expressionStack, stack);
			}
		}

		for (int i = 0; i < stack.size(); i++) {
			Expression exp = stack.get(i).getExpression();
			if (exp instanceof ConditionalExpression) {
				int loopStartLabel = stack.get(i).getLabelId();
				int nextLabel = i < stack.size() - 1 ? stack.get(i+1).getLabelId() : -2;
				fixLoopDetection((ConditionalExpression) exp, loopStartLabel, stack.get(i));
				improveBreakAndContinue((ConditionalExpression) exp, nextLabel, (ConditionalExpression) exp);
			}
		}
	}

	private void updateDoLoops(ConditionalExpression exp, int currentStackPosition, ExpressionStack expressionStack, Stack<StackItem> stack) {
		if ((exp.getThenBranch() != null && !exp.getThenBranch().isEmpty()) || (exp.getElseBranch() != null && !exp.getElseBranch().isEmpty())) return;
		for (int stackPosition = 0; stackPosition < currentStackPosition; stackPosition++) {
			if (stack.get(stackPosition).getLabelId() == exp.getJumpDestination()) {
				exp.setLoopType(ConditionalExpression.LoopType.DO_WHILE);
				exp.setStartFrameLocation(exp.getJumpDestination());
				exp.getThenBranch().addAll(expressionStack.substack(stackPosition, currentStackPosition));
				exp.negate();
				break;
			}
		}
	}

	private void fixLoopDetection(ConditionalExpression topLevelExp, int loopStartLabel, StackItem stackItem) {
		if (!(stackItem.getExpression() instanceof ConditionalExpression) || ((ConditionalExpression) stackItem.getExpression()).getThenBranch() == null) {
			return;
		}
		ConditionalExpression expression = ((ConditionalExpression) stackItem.getExpression());
		for (StackItem item : expression.getThenBranch().getAll()) {
			fixLoopDetection(topLevelExp, loopStartLabel, item);
			if (item.getExpression() instanceof ConditionalExpression && ((ConditionalExpression) item.getExpression()).getJumpDestination() == loopStartLabel) {
				topLevelExp.setElseBranchEnd(((ConditionalExpression) item.getExpression()).getJumpDestination());
			}
		}

		if (((ConditionalExpression) stackItem.getExpression()).getElseBranch() != null) {
			for (StackItem item : expression.getElseBranch().getAll()) {
				fixLoopDetection(topLevelExp, loopStartLabel, item);
				if (item.getExpression() instanceof ConditionalExpression && ((ConditionalExpression) item.getExpression()).getJumpDestination() == loopStartLabel) {
					topLevelExp.setElseBranchEnd(((ConditionalExpression) item.getExpression()).getJumpDestination());
				}
			}
		}
	}

	private void improveBreakAndContinue(ConditionalExpression exp, int nextLabel, ConditionalExpression topLevelExpression) {
		List<StackItem> thenBranch;
		List<StackItem> elseBranch;

		if (exp.getThenBranch() != null) {
			thenBranch = exp.getThenBranch().getAll();
			for (StackItem innerExp : thenBranch) {
				if (innerExp.getExpression() instanceof ConditionalExpression) {
					improveBreakAndContinue((ConditionalExpression) innerExp.getExpression(), nextLabel, topLevelExpression);
				}
			}
			addBreak(nextLabel, topLevelExpression, thenBranch);
			addContinue(exp, topLevelExpression, thenBranch);
		}

{'fraction_non_alphanumeric': 0.12777161862527717, 'fraction_numerical': 0.0019401330376940134, 'mean_word_length': 5.694805194805195, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4018855', 'n_tokens_mistral': 2298, 'n_tokens_neox': 2062, 'n_words': 414}