I’m making an esoteric programming language and when I try to add a loop(a loop is made with an ‘o’ then the amount of times after that. For example, o2hello* this will get hello 2 times and print it with the star(*).) onto the stack by putting ‘a’ at the start. It adds ‘o2hello’ instead of ‘hello 2 times’ as I programmed it as.
stack = []
line = 1
def print_text(text, times):
if times == -1:
while True:
print(f'>> {text}')
else:
for _ in range(times):
print(f'>> {text}')
while True:
command = input(f'{line} ')
if command.lower().startswith('a'):
if command.lower()[1] != 'o':
if command.endswith('*'):
stack.append(command[1:-1].replace('*', ''))
print(f'>> {command[1:-1].replace("*", "")}')
else:
stack.append(command[1:].replace('*', ''))
line += 1
elif command.lower().startswith('b'):
if command.endswith('*'):
if stack:
print(f'>> {stack[0]}')
else:
print("Stack is empty")
line += 1
elif command.lower().startswith('t'):
if command.endswith('*'):
if stack:
print(f'>> {stack[-1]}')
else:
print("Stack is empty")
line += 1
elif command.lower().startswith('o'):
if command.endswith('*'):
if command[1] == '!':
print_text(command[3:], -1)
else:
print_text(command[2:], int(command[1]))
line += 1
if command.lower().startswith('ao'):
stack.append(f'{command[2:]} {command[1]} times')
elif command.startswith('>'):
if command[-1] == '*':
user_input = input('')
if command.lower().startswith('a'):
stack.append(user_input)
print(f'>> {user_input}')
line += 1
elif command.lower() == 'd' or command.lower().startswith('delete'):
stack = []
line += 1
elif command.lower().startswith('r'):
if command.endswith('*'):
stack.reverse()
print(f'>> {stack}')
else:
stack.reverse()
print('>>', stack)
line += 1
elif command.startswith('#'):
if command.endswith('*'):
print(f'>> {stack}')
elif command.endswith('.'):
if len(command) == 2:
try:
stack.pop()
except IndexError:
print('Stack is empty')
else:
try:
index = int(command[2:])
stack.pop(index)
except ValueError:
print('Indexing should be an integer')
except IndexError:
print('Index out of range')
line += 1
else:
print("Invalid command")