Skip to content

Instantly share code, notes, and snippets.

@Reconcyl
Last active July 8, 2017 08:52
Show Gist options
  • Save Reconcyl/f5cc6a31be48a8ba9b34a79f50f56c96 to your computer and use it in GitHub Desktop.
Save Reconcyl/f5cc6a31be48a8ba9b34a79f50f56c96 to your computer and use it in GitHub Desktop.

MetaFractran is a version of Fractran in which the entire program is encoded in the program file's metadata - specifically, its file name and path.

Say the program file is located in /home/metafractran/7:3/99:98/13:49/39:35/36:91/10:143/49:13/7:11/1:2/91:1/10.mfc. Only the trailing components of the file path that match the regex \d+:\d+ will be kept, and the rest will be ignored. The file name can have an optional extension, which is ignored. The contents of the file do not matter; however, a compliant implementation must verify that the file exists.

This file corresponds to the program 7/3 99/98 13/49 39/35 36/91 10/143 49/13 7/11 1/2 91/1, with a starting number of 10.

MetaFractran was inspired by Folders and, of course, Fractran.

# An interpreter for the MetaFractran esolang
# In MetaFractran, the contents of the source file do not matter. The metadata of the file (name, path)
# encodes a Fractran program.
# For example, /root/esolangs/metafractran/7:22/11:39/13:35/7.mfc encodes the Fractran program
# 7/22, 11/39, 13/35, with starting number 7.
# At the end of execution, the resulting number is printed.
import sys
import re
import pathlib
import fractions as fr
path = pathlib.Path(sys.argv[1]).resolve()
if not path.is_file():
print("File does not exist")
sys.exit(1)
name, *parts = reversed(path.parts)
is_fraction = re.compile(r"^(\d+):(\d+)$")
fractions = []
for i in parts:
match = is_fraction.match(i)
if not match:
break
num, denom = match.groups()
fractions.append(fr.Fraction(int(num), int(denom)))
fractions = fractions[::-1] # they were added backwards
is_valid_name = re.compile(r"(\d+)(?:\..*)?")
match = is_valid_name.match(name)
if not match:
print("File name is not valid")
sys.exit(1)
num = int(match.group(1))
# run the fractran program
def run_fractran(num, fractions):
while True:
for i in fractions:
product = num * i
if product.denominator == 1:
num = int(product)
break
else:
return num
print(run_fractran(num, fractions))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment