Talk:Your stack may be eaten

From Esolang
Jump to navigation Jump to search

Implementation

Implementation in java

import java.util.Scanner;
import java.util.ArrayList;

public class YourStackMayBeEaten {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		ArrayList<Character> stack = new ArrayList<>();
		ArrayList<Character> code = new ArrayList<>();
		String[] line = scan.nextLine().split("!");
		for (int i = 0; i < line[0].length(); i++)
			code.add(line[0].charAt(i));
		for (int i = line[1].length(); i > 0; i--)
			stack.add(line[1].charAt(i));
		int pos = 0;
		int pointer = 0;
		while (!stack.isEmpty()) {
			switch (code.get(pos)) {
			case 'v':
				stack.add(stack.get(pointer));
				break;
			case '^':
				stack.remove(pointer);
				break;
			case '$':
				stack.remove(stack.size());
				if (pointer >= stack.size())
					pointer = stack.size() - 1;
				break;
			case ':':
				stack.add(pointer, stack.get(pointer));
				pointer++;
				break;
			case '/':
			{
				Character arg1 = stack.get(pointer);
				Character arg2 = stack.get(pointer - 1);
				stack.set(pointer, arg2);
				stack.set(pointer - 1, arg1)
				break;
			}
			case '{':
				pointer++;
				if (pointer == stack.size()) 
					pointer--;
				break;
			case '}':
				pointer--;
				if (pointer == -1) 
					pointer++;
				break;
			case '_':
				if (pointer == 0) {
					stack.add(0, scan.nextLine().charAt(1));
					pointer++;
				}
				else
					stack.add(pointer - 1, scan.nextLine().charAt(1));
				break;
			case '@':
				System.out.print(stack.get(pointer));
				stack.remove(pointer);
				break;
			case '[':
			{
				int length = stack.size();
				for (int i = 0; i < length; i++) {
					stack.add(stack.get(i));
				}
			}
			}
			pos++;
			code.add(stack.get(stack.size()));
			stack.remove(stack.size());
		}
	}
}

BoundedBeans (talk) 02:40, 12 July 2022 (UTC)