# Task
Make a custom esolang interpreter for the language Stick. Stick is a simple, stack-based esoteric programming language with only 7 commands.
# Commands

* `^`: Pop the stack.

* `!`: Add new element to stack with the value of 0.

* `+`: Increment element. 255+1=0.

* `-`: Decrement element. 0-1=255.

* `*`: Add ascii value of **top** element to the output stream.

* `[`: Skip past **next** `]` if element value is 0.

* `]`: Jump back to the command after **previous** `[` if element value is nonzero.

# Syntax and other info

* You don't need to add support for nested brackets.
* Non-command characters should be ignored.
* Code will always have all brackets closed.
* Note the highlighted **next** and **previous** in the commands reference.
* Program begins with the top element having the value of 0 and being the only element in the stack.
* Program ends when command executor reaches the end.

# Examples

## Hello, World!

```python
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*!++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*!++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*!++++++++++++++++++++++++++++++++++++++++++++*!++++++++++++++++++++++++++++++++*!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*!++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*!++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*!++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*!+++++++++++++++++++++++++++++++++*!'
```

# Notes

* If this kata is a duplicate, this is not an issue.
* If this kata uses incorrect styling, it is also not an issue.
* Feel free to comment in the discource area.
* Swift versions must throw an error (conforming to the Error Protocol) when abnormal conditions occur.
* Javascript versions should throw when abnormal conditions occur.

Source: codewars | Difficulty: EASY
Tags: Data structures

Starter code: def interpreter(tape):
	

Solutions:
def interpreter(tape):
	(ptr, stack, output) = (0, [0], [])
	while ptr < len(tape):
		command = tape[ptr]
		if command == '^':
			stack.pop()
		elif command == '!':
			stack.append(0)
		elif command == '+':
			stack[-1] = (stack[-1] + 1) % 256
		elif command == '-':
			stack[-1] = (stack[-1] - 1) % 256
		elif command == '*':
			output.append(chr(stack[-1]))
		elif command == '[' and stack[-1] == 0:
			ptr = tape.find(']', ptr)
		elif command == ']' and stack[-1] != 0:
			ptr = tape.rfind('[', ptr)
		ptr += 1
	return ''.join(output)


def interpreter(s):
	(find, find_, d) = ([], [], {})
	for (i, j) in enumerate(s):
		if j == '[':
			find.append(i)
		if j == ']':
			find_.append(i)
	for (i, j) in zip(find, find_):
		d[i] = j
		d[j] = i
	(memory, output, j) = ([0], [], 0)
	while j < len(s):
		i = s[j]
		if i == '+':
			memory[-1] = (memory[-1] + 1) % 256
		if i == '-':
			memory[-1] = (memory[-1] - 1) % 256
		if i == '*':
			output.append(chr(memory[-1]))
		if i == '!':
			memory.append(0)
		if i == '[':
			if memory[-1] == 0:
				j = d[j]
		if i == ']':
			if memory[-1] != 0:
				j = d[j]
		if i == '^':
			memory.pop(-1)
		j += 1
	return ''.join(output)


def pop(tape, pc, out, stack):
	pc += 1
	stack.pop()
	return (pc, out, stack)

def zstack(tape, pc, out, stack):
	pc += 1
	stack.append(0)
	return (pc, out, stack)

def inc(tape, pc, out, stack):
	pc += 1
	stack[-1] += 1
	if stack[-1] > 255:
		stack[-1] = 0
	return (pc, out, stack)

def dec(tape, pc, out, stack):
	pc += 1
	stack[-1] -= 1
	if stack[-1] < 0:
		stack[-1] = 255
	return (pc, out, stack)

def ascii(tape, pc, out, stack):
	pc += 1
	out += chr(stack[-1])
	return (pc, out, stack)

def skip(tape, pc, out, stack):
	pc += 1
	if stack[-1] == 0:
		c = tape[pc]
		while c != ']':
			pc += 1
			c = tape[pc]
		pc += 1
	return (pc, out, stack)

def back(tape, pc, out, stack):
	if stack[-1] != 0:
		pc -= 1
		c = tape[pc]
		while c != '[':
			pc -= 1
			c = tape[pc]
		pc += 1
	else:
		pc += 1
	return (pc, out, stack)
dispatch = {'^': pop, '!': zstack, '+': inc, '-': dec, '*': ascii, '[': skip, ']': back}

def interpreter(tape):
	out = ''
	stack = [0]
	pc = 0
	c = tape[pc]
	while c:
		if c in dispatch:
			(pc, out, stack) = dispatch[c](tape, pc, out, stack)
		else:
			pc += 1
		c = tape[pc:pc + 1]
	return out


def interpreter(tape):
	out = []
	stack = [0]
	ip = 0
	while ip < len(tape):
		cmd = tape[ip]
		if cmd == '^':
			stack.pop()
		elif cmd == '!':
			stack.append(0)
		elif cmd == '+':
			stack[-1] = (stack[-1] + 1) % 256
		elif cmd == '-':
			stack[-1] = (stack[-1] - 1) % 256
		elif cmd == '*':
			out.append(chr(stack[-1]))
		elif cmd == '[':
			if stack[-1] == 0:
				while tape[ip] != ']':
					ip += 1
		elif cmd == ']':
			if stack[-1] != 0:
				while tape[ip] != '[':
					ip -= 1
		ip += 1
	return ''.join(out)


def interpreter(tape):
	(ptr, stack, output) = (0, [0], '')
	n = len(tape)
	while ptr < n:
		cmd = tape[ptr]
		if cmd == '^':
			stack.pop()
		elif cmd == '!':
			stack.append(0)
		elif cmd == '+':
			stack[-1] = (stack[-1] + 1) % 256
		elif cmd == '-':
			stack[-1] = (stack[-1] + 255) % 256
		elif cmd == '*':
			output += chr(stack[-1])
		elif cmd == '[' and stack[-1] == 0:
			while tape[ptr] != ']':
				ptr += 1
		elif cmd == ']' and stack[-1] != 0:
			while tape[ptr] != '[':
				ptr -= 1
		ptr += 1
	return output


def interpreter(tape):
	(stack, output) = ([0], '')
	(i, n) = (0, len(tape))
	while i < n:
		cmd = tape[i]
		if cmd == '^':
			stack.pop()
		elif cmd == '!':
			stack.append(0)
		elif cmd == '+':
			stack[0] = 0 if stack[0] == 255 else stack[0] + 1
		elif cmd == '-':
			stack[0] = 255 if stack[0] == 0 else stack[0] - 1
		elif cmd == '*':
			output += chr(stack.pop(0))
		elif cmd == '[' and stack[0] == 0:
			while tape[i] != ']':
				i += 1
		elif cmd == ']' and stack[0] != 0:
			while tape[i] != '[':
				i -= 1
		i += 1
	return output.replace('\n', '\x02')


def cleanTape(tape):
	a = [x for x in tape if x in '^!+-*[]']
	return ''.join(a)

def interpreter(tape):
	tape = cleanTape(tape)
	stack = [0]
	index = 0
	output = ''
	while index < len(tape):
		if tape[index] == '^':
			stack.pop()
		if tape[index] == '!':
			stack.append(0)
		if tape[index] == '+':
			stack[-1] += 1
		if tape[index] == '-':
			stack[-1] -= 1
		stack[-1] %= 256
		if tape[index] == '*':
			output += chr(stack[-1])
		if tape[index] == '[':
			if stack[-1] == 0:
				i = index + 1
				while True:
					if tape[i] == ']':
						index = i + 1
						break
					i += 1
				continue
		if tape[index] == ']':
			if stack[-1] != 0:
				i = index - 1
				while True:
					if tape[i] == '[':
						index = i - 1
						break
					i -= 1
				continue
		index += 1
	return output


def interpreter(tape):
	stack = [0]
	ip = 0
	output = ''
	while ip < len(tape):
		op = tape[ip]
		if op == '^':
			stack.pop()
		elif op == '!':
			stack.append(0)
		elif op == '+':
			stack[-1] = (stack[-1] + 1) % 256
		elif op == '-':
			stack[-1] = (stack[-1] + 255) % 256
		elif op == '*':
			output += chr(stack[-1])
		elif op == '[' and stack[-1] == 0:
			ip = tape.find(']', ip + 1)
		elif op == ']' and stack[-1] != 0:
			ip = tape.rfind('[', 0, ip)
		ip += 1
	return output


def interpreter(tape):
	(a, stack, out) = (0, [0], [])
	while a < len(tape):
		command = tape[a]
		if command == '^':
			stack.pop()
		elif command == '!':
			stack.append(0)
		elif command == '+':
			stack[-1] = (stack[-1] + 1) % 256
		elif command == '-':
			stack[-1] = (stack[-1] - 1) % 256
		elif command == '*':
			out.append(chr(stack[-1]))
		elif command == '[' and stack[-1] == 0:
			a = tape.find(']', a)
		elif command == ']' and stack[-1] != 0:
			a = tape.rfind('[', a)
		a += 1
	return ''.join(out)


def interpreter(tape):
	(j, k) = ({}, [])
	for i in range(len(tape)):
		if tape[i] == '[':
			k.append(i)
		elif tape[i] == ']':
			j[i] = k[-1]
			j[k.pop()] = i
	(m, r, i, l) = ([0], '', 0, len(tape))
	while i < l:
		if tape[i] == '^':
			m.pop(0)
		elif tape[i] == '!':
			m.append(0)
		elif tape[i] == '+':
			m[-1] += 1
		elif tape[i] == '-':
			m[-1] -= 1
		elif tape[i] == '*':
			r += chr(m.pop(0) % 256)
		elif tape[i] == '[' and (not m[-1] % 256) or (tape[i] == ']' and m[-1] % 256):
			i = j[i]
		i += 1
	return r