Talk:PDAsephone

From Esolang
Jump to navigation Jump to search

Implementation

An implementation in Java:

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayDeque; 
import java.util.HashMap;

public class PDAsephone {
	public static ArrayDeque<Character> charstack = new ArrayDeque<>();
	public static ArrayDeque<PDA> pdastack = new ArrayDeque<>();
	public static ArrayDeque<Character> inputbuffer = new ArrayDeque<>();

	public static class PDA {

		public HashMap<String, Transition> t = new HashMap<>();
		public ArrayDeque<Character> stack = new ArrayDeque<>();
		public char state = '0';

		public static class Transition {

			public boolean pop;
			public char push;
			public char state;

			public Transition(boolean p1, char p2, char s) {
				pop = p1;
				push = p2;
				state = s;
			}
		}

		public PDA() {
			this.t = new HashMap<>();
			this.stack = new ArrayDeque<>();
			this.state = '0';
		}

		public PDA(PDA p) {
			this.t = new HashMap<>(p.t);
			this.stack = new ArrayDeque<>(p.stack);
			this.state = p.state;
		}

		public void transition(char input) {
			String s = "" + input + state + (stack.isEmpty() ? '\n' : stack.peekLast());
			Transition tr = t.containsKey(s) ? t.get(s) : new Transition(false, '\n', state);
 			state = tr.state;
			if (tr.pop)
				stack.removeLast();
			if (tr.push != '\n')
				stack.addLast(tr.push);
		}
	}

	public static void main(String[] args) throws FileNotFoundException {
		Scanner in = new Scanner(System.in);
		File f = new File(in.nextLine());
		Scanner fin = new Scanner(f);
		String code = "";
		while (fin.hasNextLine()) {
			String line = fin.nextLine();
			if (line.length() >= 4 ? !(line.substring(0, 4).equals(">>>>")) : true) {
				code += line;
			}
		}
		for (int i = 0; i < code.length(); i++) {
			switch (code.charAt(i)) {
			case '@':
				pdastack.addLast(new PDA());
				break;
			case '%':
				if (true) {
					char c1 = charstack.removeLast();
					char c2 = charstack.removeLast();
					char c3 = charstack.removeLast();
					char c4 = charstack.removeLast();
					char c5 = charstack.removeLast();
					char c6 = charstack.removeLast();
					pdastack.peekLast().t.put("" + c1 + c2 + c3, new PDA.Transition(c4 != '0', c5, c6));
				}
				break;
			case '!':
				pdastack.peekLast().transition(charstack.removeLast());
				break;
			case '^':
				charstack.addLast(pdastack.peekLast().stack.isEmpty() ? '\n' : pdastack.peekLast().stack.removeLast());
				break;
			case '.':
				System.out.print(charstack.removeLast());
				break;
			case ',':
				if (inputbuffer.isEmpty()) {
					String s1 = in.nextLine();
					for (int j = 0; j < s1.length(); j++)
					inputbuffer.addLast(s1.charAt(j));
				}
				charstack.addLast(inputbuffer.removeFirst());
				break;
			case '"':
				i++;
				charstack.addLast(code.charAt(i));
				break;
			case '_':
				charstack.addLast('\n');
				break;
			case 'v':
				if (charstack.peekLast().charValue() != '\n')
					pdastack.peekLast().stack.addLast(charstack.removeLast().charValue());
				else {
					charstack.removeLast();
					pdastack.peekLast().stack.addLast(' ');
				}
				break;
			case ':':
				charstack.addLast(charstack.peekLast().charValue());
				break;
			case ';':
				pdastack.addLast(new PDA(pdastack.peekLast()));
				break;
			case '/':
				if (true) {
					char c1 = charstack.removeLast();
					char c2 = charstack.removeLast();
					charstack.addLast(c1);
					charstack.addLast(c2);
				}
				break;
			case '\\':
				if (true) {
					PDA p1 = pdastack.removeLast();
					PDA p2 = pdastack.removeLast();
					pdastack.addLast(p1);
					pdastack.addLast(p2);
				}
				break;
			case '$':
				charstack.removeLast();
				break;
			case '#':
				pdastack.removeLast();
				break;
			case '|':
				if (true) {
					char c1 = charstack.removeLast();
					if ("ABCDEFGHIJKLMNOPQRST".contains("" + c1)) {
						while (!(code.charAt(i) == c1 && (i != 0 ? code.charAt(i - 1) != '"' : true))) {
							i++;
						}
					}
					else if ("abcdefghijklmnopqrst".contains("" + c1)) {
						while (!(code.charAt(i) == c1 && (i != 0 ? code.charAt(i - 1) != '"' : true))) {
							i--;
						}
					}
				}
				break;
			}
		}
	}
 }
Realized just now that I forgot to sign the entry. This implementation/talk page entry was by BoundedBeans. BoundedBeans (talk) 18:47, 25 December 2022 (UTC)