Skip to content

Instantly share code, notes, and snippets.

@AMiller42
Created October 8, 2021 17:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AMiller42/a39d903b0e38c78385566750367058ff to your computer and use it in GitHub Desktop.
Save AMiller42/a39d903b0e38c78385566750367058ff to your computer and use it in GitHub Desktop.
import sys
import re
plumb2plum = {
"[]": "v",
"][": "^",
"] ": "+",
" [": "+",
"[ ": "-",
" ]": "-",
"==": "=",
"= ": "1",
" =": "0",
"=]": "]",
"[=": "[",
"=[": "}",
"]=": "{",
"[[": "/",
"]]": "\\",
" ": " "
}
plum2plumb = {
"v": "[]",
"^": "][",
"+": "] ",
"-": "[ ",
"=": "==",
"1": "= ",
"0": " =",
"]": "=]",
"[": "[=",
"}": "=[",
"{": "]=",
"/": "[[",
"\\": "]]",
" ": " "
}
def to_plum(code):
new_code = ""
# replace all invalid characters with spaces and split
code = re.sub(r"[^\n\r[\]=]", " ", code)
code = code.split("\n")
# append space on any odd length lines
for x in range(len(code)):
if len(code[x]) % 2 == 1:
code[x] += " "
# group lines into units
code[x] = re.findall('..', code[x])
# convert each unit to its plum equivalent
for line in range(len(code)):
for unit in range(len(code[line])):
new_code += plumb2plum[code[line][unit]]
new_code += "\n"
# remove extra newline
new_code = new_code[:-1]
return new_code
def to_plumber(code):
new_code = ""
# convert each unit to its plumber equivalent
for unit in code:
new_code += "\n" if unit == "\n" else plum2plumb[unit]
# remove trailing whitespace
temp = ""
for line in new_code.split("\n"):
temp += line.rstrip() + "\n"
# remove extra newline
new_code = temp[:-1]
return new_code
if __name__ == "__main__":
file_location = ""
flag = ""
if len(sys.argv) > 1:
file_location = sys.argv[1]
if len(sys.argv) > 2:
flag = sys.argv[2]
if flag != 'b':
exit(f"Error: Unrecognized flag - {flag}")
if not file_location:
exit(f"Error: A file is required to translate, e.g. $ python {sys.argv[0]} /path/to/file")
code = open(file_location, "r").read()
if code:
if flag == 'b':
print(to_plumber(code), end="")
else:
print(to_plum(code), end="")
else:
exit(f"Error: File {file_location} is empty; nothing to translate")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment