package net.jloop.rejoice;

import java.util.ArrayList;
import java.util.Iterator;

public class Stack implements Iterable<Atom> {

    private final ArrayList<Atom> atoms;

    public Stack() {
        this.atoms = new ArrayList<>();
    }

    public Stack(ArrayList<Atom> atoms) {
        this.atoms = atoms;
    }

    private int lastIndex() {
        return atoms.size() - 1;
    }

    public <V extends Atom> V peek(Class<V> type) {
        Atom atom = atoms.get(lastIndex());
        if (!type.isInstance(atom)) {
            throw new RuntimeException("Expecting type " + type + " but found " + atom.getClass());
        } else {
            return type.cast(atom);
        }
    }

    public <V extends Atom> V consume(Class<V> type) {
        V atom = peek(type);
        this.pop();
        return atom;
    }

    public Stack push(Atom atom) {
        atoms.add(atom);
        return this;
    }

    public Stack pushAll(Stack stack) {
        for (Atom atom : stack) {
            atoms.add(atom);
        }
        return this;
    }

    public Stack pop() {
        atoms.remove(lastIndex());
        return this;
    }

    public void print() {
        for (Atom atom : atoms) {
            System.out.println(atom.print());
        }
    }

    public Stack copy() {
        // This is a shallow copy
        return new Stack(new ArrayList<>(atoms));
    }

    @Override
    public Iterator<Atom> iterator() {
        return atoms.iterator();
    }
}

@@ -49,16 +49,46 @@
 ayList;%0A
+import java.util.Collections;%0A
 import j
@@ -1187,24 +1187,114 @@
 d print() %7B%0A
+        ArrayList%3CAtom%3E list = new ArrayList%3C%3E(atoms);%0A        Collections.reverse(list);%0A
         for 
@@ -1306,21 +1306,20 @@
  atom : 
-atoms
+list
 ) %7B%0A    

Print the stack with the latest atom at the top