/*
borrowed from https://github.com/paladin-t/b95/blob/master/b95.wren
*/

class Stack is Sequence {
	construct new() {
		_list = [ ]
	}

	count {
		return _list.count
	}
	isEmpty {
		return _list.isEmpty
	}
	clear() {
		_list.clear()
	}

	peek {
		if (_list.isEmpty) {
			Fiber.abort("Peeking from empty stack.")
		}

		return _list[_list.count - 1]
	}
	pop() {
		if (_list.isEmpty) {
			Fiber.abort("Popping from empty stack.")
		}

		var ret = _list[_list.count - 1]
		_list.removeAt(_list.count - 1)

		return ret
	}
	push(val) {
		_list.add(val)

		return this
	}

	// Iterates top-down, but doesn't pop anything.
	iterate(iterator) {
		if (iterator == null) {
			iterator = _list.count
		}
		iterator = iterator - 1
		if (iterator < 0) {
			return null
		}

		return iterator
	}
	iteratorValue(iterator) {
		return _list[iterator]
	}
}

var add = Fn.new { |a,b| a + b }
var mul = Fn.new { |a,b| a * b }
