Last active
September 9, 2022 03:48
-
-
Save hornc/ea8711a5803986a855a0d755c2e08092 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Python Portable Minsky Machine | |
Implements a Python version of | |
https://esolangs.org/wiki/Portable_Minsky_Machine_Notation | |
""" | |
counters = {} | |
def inc(counter): | |
counters[counter] = counters.get(counter, 0) + 1 | |
def dec(counter): | |
test = bool(counters[counter]) | |
counters[counter] = max(0, counters.get(counter, 0) - 1) | |
return test | |
## Syntactic sugar | |
def inc_by(counter, amount): | |
counters[counter] = counters.get(counter, 0) + amount | |
def dec_by(counter, amount): | |
test = bool(counters[counter]) | |
counters[counter] = max(0, counters.get(counter, 0) - amount) | |
return test | |
## I/O Extenstion | |
def input(counter): | |
pass | |
def output(counter): | |
if dec(counter): | |
print(chr(counters[counter])) | |
counters[counter] = 0 | |
## Helper methods, not part of the original spec: | |
def show(): | |
"""Shows all set registers.""" | |
print("Counter Value") | |
for k,v in counters.items(): | |
print(k, v) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment