This is the seconds version of an interpreter yesterday. It's supposed to be used for code golf, but it currently can't do much.
import re, functools, time
stack = [""] * 9
def run(file):
f = open(file, 'r')
code = f.read()
f.close()
parse(code)
def prompt():
"""Lets the user input commands and feeds them to parse()"""
while True:
userInput = input("> ")
if len(userInput) > 0:
return userInput
print("Please enter a command.")
def parse_numbers(userInput, operator):
"""Parses math like 100+100"""
numbers = userInput.split(operator)
try:
return map(int, numbers)
except ValueError as error:
print(error.message)
def add(command):
"""Adds two or more numbers together"""
numbers = parse_numbers(command, '+')
return sum(numbers)
def subtract(command):
"""Subtracts two or more numbers"""
numbers = parse_numbers(command, '-')
return functools.reduce(lambda x, y: x - y, numbers)
def multiply(command):
"""Multiplies two or more numbers"""
numbers = parse_numbers(command, '*')
return functools.reduce(lambda x, y: x * y, numbers)
def divide(command):
"""Divides two or more numbers"""
numbers = parse_numbers(command, '/')
return functools.reduce(lambda x, y: x / y, numbers)
def modulus(command):
"""Prints the remainder of two numbers"""
numbers = parse_numbers(command, '%')
return functools.reduce(lambda x, y: x % y, numbers)
def quine(command):
"""Prints itself"""
print(command)
def print_command(command):
"""Prints whatever is after the command"""
print(command[2:])
def var(command):
command = command.split('=')
command[0].replace(" ", "")
command[1].replace(" ", "")
stack[command[0]] = command[1]
def loop(command, amount):
"""Loops (a) command(s) for multiple times"""
for i in range(0, int(amount)):
parse(command)
def putOnStack(text, num):
stack[int(num)] = text
def parse(command):
"""Parses the commands."""
if ';' in command:
commands = command.split(";")
for i in commands:
parse(i)
if '\n' in command:
commands = command.split('\n')
for i in commands:
parse(i)
elif command.startswith("q"):
quine(command)
elif command.startswith("p "):
print_command(command)
elif command.startswith("l "):
try:
loopAmount = re.sub("\D", "", command)
lst = command.split(loopAmount)
strCommand = lst[1]
strCommand = strCommand[1:]
loop(strCommand, loopAmount)
except IndexError as error:
print("Error: Can't put numbers in loop")
elif '+' in command:
print (add(command))
elif '-' in command:
print (subtract(command))
elif '*' in command:
print (multiply(command))
elif '/' in command:
print (divide(command))
elif '%' in command:
print (modulus(command))
elif '=' in command:
lst = command.split('=')
lst[0].replace(" ", "")
lst[1].replace(" ", "")
stackNum = ''.join(lst[1])
putOnStack(stackNum, lst[0])
elif command.startswith("run "):
command = command.replace(" ", "")
command = command.split("run")
run(command[1])
elif command.startswith('#'):
pass
elif command.startswith('? '):
stackNum = command[2]
text = input("input> ")
putOnStack(text, stackNum)
elif command.startswith('@ '):
stackNum = command[2]
print(''.join(stack[int(stackNum)]))
elif command.startswith("."):
time.sleep(2)
else:
print("Invalid command")
while True:
userInput = prompt()
parse(userInput)
Here's a few examples of programs using it:
p What's your name?;? 1;p Hello,;@ 1
l 5 p Hello, world!
Commands:
p (text)
: Prints textq (text)
: Creates a quinel (x) (command)
: Loops over commands+, -, *, /**
: Arithmetic operators? (x)
: Asks for user input and puts it on stack@ (x)
: Reads from stack.
: Sleeps for 2 seconds# (text)
: A simple comment
There's a few bad things about this, namely:
You can't use numbers in loops. Check the
loop()
function to see why.You can't use variables in print statements, math, etc.
You can't get
p
to print on one line instead of adding newlines after the text.
So, what do you guys think about it?