<commit_before>package stackprogramvisualiser;

import stackprogramvisualiser.exceptions.InvalidInstructionException;
import stackprogramvisualiser.exceptions.InvalidLabelException;
import stackprogramvisualiser.exceptions.ProgramExitException;

import java.util.EmptyStackException;

public class Instruction {

	private Command command;
	private Integer intArg = null;
	private String strArg = null;

	public Instruction(String command) throws InvalidInstructionException {
		setCommand(command);
	}

	public Instruction(String command, int intArg) throws InvalidInstructionException {
		setCommand(command);
		this.intArg = intArg;
	}

	public Instruction(String command, String strArg) throws InvalidInstructionException {
		setCommand(command);
		this.strArg = strArg;
	}

	private void setCommand(String command) throws InvalidInstructionException {
		try {
			this.command = Command.valueOf(command.toUpperCase());
		} catch (IllegalArgumentException iae) {
			throw new InvalidInstructionException(command);
		}
	}

	public void execute() throws NullPointerException, EmptyStackException, ProgramExitException, InvalidLabelException {
		// temporary holders
		Integer a1, a2;

		// switch on the command we're executing
		switch (command) {
			case INT:
				if (intArg == null) throw new NullPointerException();
				push(intArg);
				break;

			case ADD:
				a1 = pop();
				a2 = pop();
				push(a1 + a2);
				break;

			case SUB:
				a1 = pop();
				a2 = pop();
				push(a2 - a1);
				break;

			case JGE:
				a1 = peek();
				if (a1 >= 0) {
					// which arg to move to?
					if (intArg != null) {
						pcSet(intArg);
					} else {
						int newPC = StackProgramVisualiser.parsedCode.getPositionForLabel(strArg);
						if (newPC < 0) {
							throw new InvalidLabelException(strArg);
						}
						pcSet(newPC);
					}
					return;
				}
				break;

			case JEQ:
				a1 = peek();
				if (a1 == 0) {
					// which arg to move to?
					if (intArg != null) {
						pcSet(intArg);
					} else {
						int newPC = StackProgramVisualiser.parsedCode.getPositionForLabel(strArg);
						if (newPC < 0) {
							throw new InvalidLabelException(strArg);
						}
						pcSet(newPC);
					}
					return;
				}
				break;

			case EXIT:
				throw new ProgramExitException();

			case PRINT:
				StackProgramVisualiser.gui.outputTerminalMessage(peek().toString());
				break;
		}

		// move on!
		pcAdvance();
	}

	/* shortcut methods */

	private void push(Integer i) {
		StackProgramVisualiser.dataStack.push(i);
	}

	private Integer pop() throws EmptyStackException {
		return StackProgramVisualiser.dataStack.pop();
	}

	private Integer peek() throws EmptyStackException {
		return StackProgramVisualiser.dataStack.peek();
	}

	private void pcAdvance() {
		StackProgramVisualiser.programCounter++;
	}

	private void pcSet(int pc) {
		StackProgramVisualiser.programCounter = pc;
	}

	public enum Command {
		// basic inputs
		INT,

		// arithmetic
		ADD,
		SUB,

		// control statements
		JGE,
		JEQ,
		EXIT,

		// output
		PRINT,

		SWAP,
		CALL,
		RET,
		DUP,
		POP,
	}

}<commit_msg>Implement SWAP, DUP and POP<commit_after>package stackprogramvisualiser;

import stackprogramvisualiser.exceptions.InvalidInstructionException;
import stackprogramvisualiser.exceptions.InvalidLabelException;
import stackprogramvisualiser.exceptions.ProgramExitException;

import java.util.EmptyStackException;

public class Instruction {

	private Command command;
	private Integer intArg = null;
	private String strArg = null;

	public Instruction(String command) throws InvalidInstructionException {
		setCommand(command);
	}

	public Instruction(String command, int intArg) throws InvalidInstructionException {
		setCommand(command);
		this.intArg = intArg;
	}

	public Instruction(String command, String strArg) throws InvalidInstructionException {
		setCommand(command);
		this.strArg = strArg;
	}

	private void setCommand(String command) throws InvalidInstructionException {
		try {
			this.command = Command.valueOf(command.toUpperCase());
		} catch (IllegalArgumentException iae) {
			throw new InvalidInstructionException(command);
		}
	}

	public void execute() throws NullPointerException, EmptyStackException, ProgramExitException, InvalidLabelException {
		// temporary holders
		Integer a1, a2;

		// switch on the command we're executing
		switch (command) {
			case INT:
				if (intArg == null) throw new NullPointerException();
				push(intArg);
				break;

			case ADD:
				a1 = pop();
				a2 = pop();
				push(a1 + a2);
				break;

			case SUB:
				a1 = pop();
				a2 = pop();
				push(a2 - a1);
				break;

			case JGE:
				a1 = peek();
				if (a1 >= 0) {
					// which arg to move to?
					if (intArg != null) {
						pcSet(intArg);
					} else {
						int newPC = StackProgramVisualiser.parsedCode.getPositionForLabel(strArg);
						if (newPC < 0) {
							throw new InvalidLabelException(strArg);
						}
						pcSet(newPC);
					}
					return;
				}
				break;

			case JEQ:
				a1 = peek();
				if (a1 == 0) {
					// which arg to move to?
					if (intArg != null) {
						pcSet(intArg);
					} else {
						int newPC = StackProgramVisualiser.parsedCode.getPositionForLabel(strArg);
						if (newPC < 0) {
							throw new InvalidLabelException(strArg);
						}
						pcSet(newPC);
					}
					return;
				}
				break;

			case EXIT:
				throw new ProgramExitException();

			case SWAP:
				a1 = pop();
				a2 = pop();
				push(a1);
				push(a2);
				break;

			case DUP:
				push(peek());
				break;

			case POP:
				pop();
				break;

			case PRINT:
				StackProgramVisualiser.gui.outputTerminalMessage(peek().toString());
				break;
		}

		// move on!
		pcAdvance();
	}

	/* shortcut methods */

	private void push(Integer i) {
		StackProgramVisualiser.dataStack.push(i);
	}

	private Integer pop() throws EmptyStackException {
		return StackProgramVisualiser.dataStack.pop();
	}

	private Integer peek() throws EmptyStackException {
		return StackProgramVisualiser.dataStack.peek();
	}

	private void pcAdvance() {
		StackProgramVisualiser.programCounter++;
	}

	private void pcSet(int pc) {
		StackProgramVisualiser.programCounter = pc;
	}

	public enum Command {
		// basic inputs
		INT,

		// arithmetic
		ADD,
		SUB,

		// control statements
		JGE,
		JEQ,
		EXIT,

		// stack manipulation
		SWAP,
		DUP,
		POP,

		// output
		PRINT,

		// not implemented yet
		CALL,
		RET,
	}

}
{'fraction_non_alphanumeric': 0.1020471948741991, 'fraction_numerical': 0.006250976715111736, 'mean_word_length': 3.1343669250645996, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '967092', 'n_tokens_mistral': 2545, 'n_tokens_neox': 1974, 'n_words': 498}