![]() |
|
The Programming Language Challenge - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Coding (https://sinister.ly/Forum-Coding) +--- Forum: Python (https://sinister.ly/Forum-Python) +--- Thread: The Programming Language Challenge (/Thread-The-Programming-Language-Challenge) |
The Programming Language Challenge - m0dem - 11-26-2015 So, in this challenge, you must create a programming language (defined in just a moment) using the Python programming language. Whoever has the shortest code "wins" (while their code is still the shortest); but that is not the main point, just have fun and learn! Your programming language must include:
What I hope you learn is that making a programming language (a simple one) is not all that hard. Let the games begin! Shoutout to @Shebang - the programming language making machine
RE: The Programming Language Challenge - Rick - 11-30-2015 Can I do this at a very basic level? Eg.Your conditions don't say I can't simulate everything in base 2 RE: The Programming Language Challenge - m0dem - 11-30-2015 (11-30-2015, 09:27 AM)Rick Wrote: Can I do this at a very basic level? Sure, go ahead! I would like to see anything you can come up with!
RE: The Programming Language Challenge - m0dem - 12-15-2015 Well, I've had this lying around for awhile, but I was too lazy to post until now. ![]() Code: from __future__ import print_function
def tokenize(code):
return code.split()
def evaluate(code, stack, operators):
tokens = tokenize(code)
for token in tokens:
if token in operators:
if token in ["+", "-", "*", "/"]:
while len(stack) < 2:
stack.append(0)
ret = operators[token](stack)
if ret != None:
stack.append(ret)
else:
# push
stack.append(int(token))
stack = []
operators = {
"+": lambda stack: stack.pop() + stack.pop(),
"-": lambda stack: stack.pop() - stack.pop(),
"*": lambda stack: stack.pop() * stack.pop(),
"/": lambda stack: stack.pop() / stack.pop(),
"|": lambda stack: print(stack[-1]),
"=": lambda stack: int(stack.pop() == stack.pop()),
"!": lambda stack: int(stack.pop() != stack.pop()),
">": lambda stack: int(stack.pop() > stack.pop()),
"<": lambda stack: int(stack.pop() < stack.pop())
}
print("CTRL-D or CTRL-C to close REPL")
while True:
try:
code = raw_input(">>> ")
evaluate(code, stack, operators)
except KeyboardInterrupt, EOFError:
break
print("\nthe stack is: " + repr(stack)) |