├── .gitignore ├── LICENSE ├── README.md ├── compilers ├── assembly │ ├── bfstack.py │ ├── home-row.py │ ├── jaune.py │ ├── suffolk.py │ └── unsquare.py └── c │ ├── RAM0.c │ ├── bf-pda.c │ ├── bfstack.c │ └── excon.c ├── extra ├── assembly │ ├── 123.asm │ ├── 2b1b.asm │ ├── brainpocalypse.asm │ ├── nocomment.asm │ └── stun-step.asm ├── c++ │ ├── %^2^-1.cpp │ ├── 2dFish.cpp │ ├── basicfuck.cpp │ ├── dimensional.cpp │ ├── forþ.cpp │ ├── kak.cpp │ ├── painfuck.cpp │ └── trash.cpp ├── lean │ ├── 74.lean │ ├── albabet.lean │ ├── bf-pda.lean │ └── excon.lean ├── r │ └── excon.r ├── ruby │ ├── 3x.rb │ ├── 74.rb │ ├── bit.rb │ └── unsquare.rb └── rust │ ├── laserfuck.rs │ └── unsquare.rs ├── miscellaneous ├── ascii-art.txt ├── binary.py ├── generate.py ├── laserfuck.txt ├── lightlang.txt ├── mammalian.txt └── suffolk.txt ├── other ├── bitdeque.py ├── clockwise.py ├── container.py ├── keys.py ├── nevermind.py └── ztoalc.py ├── register-based ├── RAM0.py ├── WII2D.py ├── bio.py ├── dig.py ├── dotlang.py ├── dsdlai.py ├── huf.py ├── lightlang.py ├── minsky-swap.py ├── movesum.py ├── polynomial.py ├── qoibl.py └── sophie.py ├── stack-based ├── bfstack.py ├── eval.py ├── modulous.py └── temporary.py └── tape-based ├── 6-5.py ├── ascii-art.py ├── back.py ├── brainif.py ├── circlefuck.py ├── excon.py ├── mammalian.py ├── minifuck.py └── suffolk.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Test files 2 | output.asm 3 | test.txt 4 | output.c 5 | a.exe 6 | 7 | # Byte-compiled / optimized / DLL files 8 | __pycache__/ 9 | *.py[cod] 10 | *$py.class 11 | 12 | # C extensions 13 | *.so 14 | 15 | # Distribution / packaging 16 | .Python 17 | build/ 18 | develop-eggs/ 19 | dist/ 20 | downloads/ 21 | eggs/ 22 | .eggs/ 23 | lib/ 24 | lib64/ 25 | parts/ 26 | sdist/ 27 | var/ 28 | wheels/ 29 | pip-wheel-metadata/ 30 | share/python-wheels/ 31 | *.egg-info/ 32 | .installed.cfg 33 | *.egg 34 | MANIFEST 35 | 36 | # PyInstaller 37 | # Usually these files are written by a python script from a template 38 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 39 | *.manifest 40 | *.spec 41 | 42 | # Installer logs 43 | pip-log.txt 44 | pip-delete-this-directory.txt 45 | 46 | # Unit test / coverage reports 47 | htmlcov/ 48 | .tox/ 49 | .nox/ 50 | .coverage 51 | .coverage.* 52 | .cache 53 | nosetests.xml 54 | coverage.xml 55 | *.cover 56 | *.py,cover 57 | .hypothesis/ 58 | .pytest_cache/ 59 | 60 | # Translations 61 | *.mo 62 | *.pot 63 | 64 | # Django stuff: 65 | *.log 66 | local_settings.py 67 | db.sqlite3 68 | db.sqlite3-journal 69 | 70 | # Flask stuff: 71 | instance/ 72 | .webassets-cache 73 | 74 | # Scrapy stuff: 75 | .scrapy 76 | 77 | # Sphinx documentation 78 | docs/_build/ 79 | 80 | # PyBuilder 81 | target/ 82 | 83 | # Jupyter Notebook 84 | .ipynb_checkpoints 85 | 86 | # IPython 87 | profile_default/ 88 | ipython_config.py 89 | 90 | # pyenv 91 | .python-version 92 | 93 | # pipenv 94 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 95 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 96 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 97 | # install all needed dependencies. 98 | #Pipfile.lock 99 | 100 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 101 | __pypackages__/ 102 | 103 | # Celery stuff 104 | celerybeat-schedule 105 | celerybeat.pid 106 | 107 | # SageMath parsed files 108 | *.sage.py 109 | 110 | # Environments 111 | .env 112 | .venv 113 | env/ 114 | venv/ 115 | ENV/ 116 | env.bak/ 117 | venv.bak/ 118 | 119 | # Spyder project settings 120 | .spyderproject 121 | .spyproject 122 | 123 | # Rope project settings 124 | .ropeproject 125 | 126 | # mkdocs documentation 127 | /site 128 | 129 | # mypy 130 | .mypy_cache/ 131 | .dmypy.json 132 | dmypy.json 133 | 134 | # Pyre type checker 135 | .pyre/ 136 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Esolang Interpreters 2 | [![Language grade: Python](https://img.shields.io/lgtm/grade/python/g/bangyen/esolangs.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/bangyen/esolangs/context:python) 3 | [![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0) \ 4 | Implementations of different esoteric programming languages. Most of the interpreters/compilers work by reading the file specified by the first command line argument. 5 | 6 | ## Implemented 7 | | Register-based | Register-based (cont.) | Tape-based | Tape-based (cont.) | Stack-based | Other | 8 | |------------------------------------------------------|----------------------------------------------------|----------------------------------------------------|--------------------------------------------------------------------|----------------------------------------------------------------------|--------------------------------------------------| 9 | | [BIO](https://esolangs.org/wiki/BIO) | [Movesum](https://esolangs.org/wiki/Movesum) | [ASCII art](https://esolangs.org/wiki/ASCII_art) | [SLOW ACV MAMMALIAN](https://esolangs.org/wiki/SLOW_ACV_MAMMALIAN) | [BFStack](https://esolangs.org/wiki/BFStack) | [Bitdeque](https://esolangs.org/wiki/Bitdeque) | 10 | | [Dig](https://esolangs.org/wiki/Dig) | [Polynomial](https://esolangs.org/wiki/Polynomial) | [Back](https://esolangs.org/wiki/Back) | [Suffolk](https://esolangs.org/wiki/Suffolk) | [Eval](https://esolangs.org/wiki/Eval) | [Clockwise](https://esolangs.org/wiki/Clockwise) | 11 | | [dotlang](https://esolangs.org/wiki/Dotlang) | [Qoibl](https://esolangs.org/wiki/Qoibl) | [BrainIf](https://esolangs.org/wiki/BrainIf) | [6-5](https://esolangs.org/wiki/6-5) | [Modulous](https://esolangs.org/wiki/Modulous) | [Container](https://esolangs.org/wiki/Container) | 12 | | [DSDLAI](https://esolangs.org/wiki/DSDLAI) | [RAM0](https://esolangs.org/wiki/RAM0) | [circlefuck](https://esolangs.org/wiki/Circlefuck) | | [The Temporary Stack](https://esolangs.org/wiki/The_Temporary_Stack) | [Keys](https://esolangs.org/wiki/Keys) | 13 | | [huf](https://esolangs.org/wiki/Huf) | [Sophie](https://esolangs.org/wiki/Sophie) | [EXCON](https://esolangs.org/wiki/EXCON) | | | [Nevermind](https://esolangs.org/wiki/Nevermind) | 14 | | [Lightlang](https://esolangs.org/wiki/Lightlang) | [WII2D](https://esolangs.org/wiki/WII2D) | [Minifuck](https://esolangs.org/wiki/Minifuck) | | | [ZTOALC L](https://esolangs.org/wiki/ZTOALC_L) | 15 | | [Minsky Swap](https://esolangs.org/wiki/Minsky_Swap) | | | | | | 16 | 17 | ## Extra 18 | Implementations written in languages other than Python. 19 | 20 | | C++ | C++ (cont.) | x86 Assembly | Lean | Ruby | Rust | 21 | |------------------------------------------------------|--------------------------------------------------|------------------------------------------------------------|----------------------------------------------------------------------|----------------------------------------------------------------------|--------------------------------------------------| 22 | | [Basicfuck](https://esolangs.org/wiki/Basicfuck) | [Trash](https://esolangs.org/wiki/Trash) | [Brainpocalypse](https://esolangs.org/wiki/Brainpocalypse) | [Albabet](https://esolangs.org/wiki/Albabet) | [bit~](https://esolangs.org/wiki/Bit~) | [LaserFuck](https://esolangs.org/wiki/LaserFuck) | 23 | | [Dimensional](https://esolangs.org/wiki/Dimensional) | [2dFish](https://esolangs.org/wiki/2dFish) | [NoComment](https://esolangs.org/wiki/NoComment) | [BF-PDA](https://esolangs.org/wiki/BF-PDA) | [Number Seventy-Four](https://esolangs.org/wiki/Number_Seventy-Four) | [Unsquare](https://esolangs.org/wiki/Unsquare) | 24 | | [Forþ](https://esolangs.org/wiki/For%C3%BE) | [%^2^-1](https://esolangs.org/wiki/%25%5E2%5E-1) | [Stun Step](https://esolangs.org/wiki/Stun_Step) | [EXCON](https://esolangs.org/wiki/EXCON) | [Unsquare](https://esolangs.org/wiki/Unsquare) | | 25 | | [Kak](https://esolangs.org/wiki/Kak) | | [123](https://esolangs.org/wiki/123) | [Number Seventy-Four](https://esolangs.org/wiki/Number_Seventy-Four) | [3x](https://esolangs.org/wiki/3x) | | 26 | | [Painfuck](https://esolangs.org/wiki/Painfuck) | | [2 Bits, 1 Byte](https://esolangs.org/wiki/2_Bits,_1_Byte) | | | | 27 | 28 | ## Compilers 29 | Sorted by target language. 30 | 31 | | x86 Assembly | C | 32 | |------------------------------------------------|----------------------------------------------| 33 | | [BFStack](https://esolangs.org/wiki/BFStack) | [BF-PDA](https://esolangs.org/wiki/BF-PDA) | 34 | | [Home Row](https://esolangs.org/wiki/Home_Row) | [BFStack](https://esolangs.org/wiki/BFStack) | 35 | | [Jaune](https://esolangs.org/wiki/Jaune) | [EXCON](https://esolangs.org/wiki/EXCON) | 36 | | [Suffolk](https://esolangs.org/wiki/Suffolk) | [RAM0](https://esolangs.org/wiki/RAM0) | 37 | | [Unsquare](https://esolangs.org/wiki/Unsquare) | | 38 | 39 | ## Notes 40 | - For **circlefuck**, the `narcissist` program is the only program for which the interpreter doesn't work. The extended `quine` program doesn't work because it modifies a `+` symbol, causing it to increment incorrectly. 41 | - For **dotlang**, the interpreter skips over strings and warp names after parsing them, so printing a string with spaces is possible. If this additional feature seems to be a negative rather than a positive, feel free to create an issue. 42 | - For **Jaune**, only one character can be input at a time. 43 | - For **Suffolk**, although the language is specified to run on an infinite loop, a second command line argument may be given to set the number of loops. The default is `10`. 44 | - For **123**, input is given at the end of the program. The two are separated by a single `|`. 45 | 46 | ## Miscellaneous 47 | - The `binary.py` program implements a given boolean function in Dig. 48 | 49 | - The `generate.py` program outputs programs which output a given string in different languages. The supported languages are as follows: 50 | - BFStack 51 | - BrainIf 52 | - Container 53 | - Forþ (Forth) 54 | - LaserFuck 55 | - Painfuck 56 | - Suffolk 57 | - 123 58 | - %^2^-1 (Magnitude) 59 | -------------------------------------------------------------------------------- /compilers/assembly/bfstack.py: -------------------------------------------------------------------------------- 1 | import itertools 2 | import sys 3 | import re 4 | 5 | 6 | def parse(code): 7 | def con(*s): 8 | if s[0] in '+-': 9 | x = s.count('+') 10 | y = s.count('-') 11 | return '+', x - y 12 | return s[0], len(s) 13 | 14 | def key(v): 15 | if v in '+-': 16 | return '+' 17 | return v 18 | 19 | code = re.sub(r'[^><+-.,\][]', '', code) 20 | 21 | while re.search(r'(>[+-]*<|\+-|-\+)', code): 22 | code = re.sub('>[+-]*<', '', code) 23 | code = (code.replace('+-', '') 24 | .replace('-+', '')) 25 | 26 | while m := re.search(r'[>\]]\[', code): 27 | ind = m.start() + 1 28 | mat = 1 29 | 30 | while mat: 31 | ind += 1 32 | if ind == len(code): 33 | return [] 34 | elif (c := code[ind]) == '[': 35 | mat += 1 36 | elif c == ']': 37 | mat -= 1 38 | 39 | code = (code[:m.start() + 1] 40 | + code[ind + 1:]) 41 | 42 | code = re.sub(r'[+-]*\[[+-]]', '0', code) 43 | code = re.sub('[+-]+<', '<', code) 44 | code = itertools.groupby(code, key=key) 45 | code = [con(*g) for _, g in code] 46 | 47 | return code 48 | 49 | 50 | def comp(code): 51 | code = parse(code) 52 | jump = 0 53 | arr = [] 54 | res = ('global _start\n' 55 | '_start:\n' 56 | '\tlea ecx, [esp - 6]\n' 57 | '\tmov edx, 1\n' 58 | '\tmov esi, 1\n\n') 59 | 60 | ins = { 61 | '>': ['right', False, False], 62 | '<': ['left', False, False], 63 | '.': ['output', False, False], 64 | ',': ['input', False, False] 65 | } 66 | 67 | for char, num in code: 68 | if char == '+': 69 | if num > 1: 70 | res += f'\tadd byte [ecx], {num}\n' 71 | elif num == 1: 72 | res += '\tinc byte [ecx]\n' 73 | elif num == -1: 74 | res += '\tdec byte [ecx]\n' 75 | elif num < -1: 76 | res += f'\tsub byte [ecx], {-num}\n' 77 | elif char == '0': 78 | res += '\tmov byte [ecx], 0\n' 79 | elif char in '><.,': 80 | if num > 1: 81 | res += f'\tmov esi, {num}\n' 82 | ins[char][2] = True 83 | res += f'\tcall {ins[char][0]}\n' 84 | 85 | ins[char][1] = True 86 | elif char in '[]': 87 | for _ in range(num): 88 | if char == '[': 89 | jump += 1 90 | arr.append(jump) 91 | res += (f'.T{jump}:\n' 92 | '\tcmp byte [ecx], 0\n' 93 | f'\tje .B{jump}\n') 94 | else: 95 | m = arr.pop() 96 | res += (f'\tjmp .T{m}\n' 97 | f'.B{m}:\n') 98 | 99 | res += ('\n\tmov eax, 1\n' 100 | '\txor ebx, ebx\n' 101 | '\tint 80h\n') 102 | 103 | def end(s, mul): 104 | return (mul * ('\tdec esi\n' 105 | '\tcmp esi, 0\n' 106 | f'\tjg {s}\n' 107 | '\tinc esi\n') 108 | + '\tret\n') 109 | 110 | if ins['>'][1]: 111 | res += ('\nright:\n' 112 | '\tdec ecx\n' 113 | '\tmov byte [ecx], 0\n' 114 | + end('right', ins['>'][2])) 115 | if ins['<'][1]: 116 | res += ('\nleft:\n' 117 | '\tlea edi, [esp - 1]\n' 118 | '\tcmp ecx, edi\n' 119 | '\tje .done\n' 120 | '\tinc ecx\n') 121 | if ins['<'][2]: 122 | res += ('\tdec esi\n' 123 | '\tcmp esi, 0\n' 124 | '\tjg left\n' 125 | '\tinc esi\n') 126 | res += ('.done:\n' 127 | '\tret\n') 128 | if ins['.'][1]: 129 | res += ('\noutput:\n' 130 | '\tmov eax, 4\n' 131 | '\tmov ebx, 1\n' 132 | '\tint 80h\n' 133 | + end('output', ins['.'][2])) 134 | if ins[','][1]: 135 | res += ('\ninput:\n' 136 | '\tmov eax, 3\n' 137 | '\txor ebx, ebx\n' 138 | '\tdec ecx\n' 139 | '\tint 80h\n' 140 | + end('input', ins[','][2])) 141 | 142 | return res 143 | 144 | 145 | if __name__ == '__main__': 146 | if len(sys.argv) > 1: 147 | with open(sys.argv[1]) as f: 148 | data = f.read() 149 | 150 | with open('output.asm', 'w') as f: 151 | f.write(comp(data)) 152 | -------------------------------------------------------------------------------- /compilers/assembly/home-row.py: -------------------------------------------------------------------------------- 1 | from re import sub 2 | import sys 3 | 4 | 5 | def count(code, ind): 6 | code += ' ' 7 | num = 0 8 | 9 | if (start := code[ind]) in 'as': 10 | while (ins := code[ind]) in 'as': 11 | if ins == 'a': 12 | num += 1 13 | else: 14 | num -= 1 15 | ind += 1 16 | else: 17 | while code[ind] == start: 18 | num += 1 19 | ind += 1 20 | 21 | return num, ind 22 | 23 | 24 | def comp(code): 25 | res = '' 26 | 27 | func = { 28 | 'd': ['down', False, False], 29 | 'f': ['right', False, False], 30 | 'k': ['print', False, False] 31 | } 32 | 33 | reg = [ 34 | ('[^asdfjkl;]', ''), 35 | ('([^j])k{2,}', r'\1k'), 36 | (';{2,}', ';'), 37 | ('^((dddd)+|(ffff)+|k*j[^l]|k*l[^l]*l|k+)', ''), 38 | ('([^j])((dddd)+|(ffff)+)', r'\1'), 39 | ('([^j]k)(j[^l]|l[^l]*l)', r'\1') 40 | ] 41 | 42 | for x, y in reg: 43 | code = sub(x, y, code) 44 | 45 | skip = ind = 0 46 | end = False 47 | loop = 1 48 | 49 | while ind < len(code): 50 | num, new = count(code, ind) 51 | c = code[ind] 52 | 53 | if c in 'as': 54 | if ind and code[ind - 1] == 'j': 55 | num = 1 if c == 'a' else -1 56 | new = ind + 1 57 | 58 | if num > 1: 59 | res += f'\tadd dword [ecx], {num}\n' 60 | elif num == 1: 61 | res += '\tinc dword [ecx]\n' 62 | elif num == -1: 63 | res += '\tdec dword [ecx]\n' 64 | elif num < -1: 65 | res += f'\tsub dword [ecx], {-num}\n' 66 | elif c in 'dfk': 67 | if ind and code[ind - 1] == 'j': 68 | num, new = 1, ind + 1 69 | 70 | if num != 1: 71 | res += f'\tmov eax, {num}\n' 72 | func[c][2] = True 73 | res += f'\tcall {func[c][0]}\n' 74 | func[c][1] = True 75 | elif c == 'j': 76 | skip += 1 77 | ind = new 78 | end = True 79 | 80 | n = skip if skip - 1 else "" 81 | res += ('\tcmp dword [ecx], 0\n' 82 | f'\tje .skip{n}\n') 83 | 84 | continue 85 | elif c == 'l': 86 | loop += 1 87 | n = m if (m := (loop // 2 - 1)) else "" 88 | res += '\tcmp dword [ecx], 0\n' 89 | 90 | if loop % 2: 91 | res += (f'\tjne .top{n}\n' 92 | f'.bot{n}:\n') 93 | else: 94 | res += (f'\tje .bot{n}\n' 95 | f'.top{n}:\n') 96 | elif c == ';': 97 | res += ('\n\tmov eax, 1\n' 98 | '\txor ebx, ebx\n' 99 | '\tint 80h\n') 100 | 101 | if end: 102 | n = skip if skip - 1 else "" 103 | res += f'.skip{n}:\n' 104 | end = False 105 | ind = new 106 | 107 | def cell(r): 108 | return ('\tmov eax, 20\n' 109 | f'\tmul {r}\n' 110 | '\tlea ecx, [esp + eax]\n' 111 | '\tlea ecx, [ecx + 4*esi]\n' 112 | '\tmov eax, 1\n') 113 | 114 | if func['d'][1]: 115 | if func['d'][2]: 116 | s = ('\tadd edi, eax\n' 117 | '\tand edi, 3\n') 118 | else: 119 | s = ('\tinc edi\n' 120 | '\tand edi, 3\n') 121 | 122 | if func['k'][1]: 123 | s += ('\tmov ebx, edi\n' 124 | '\tcall cell\n') 125 | else: 126 | s += cell('edi') 127 | 128 | res += ('\ndown:\n' 129 | + s 130 | + '\tret\n') 131 | if func['f'][1]: 132 | if func['f'][2]: 133 | s = ('\tadd esi, eax\n' 134 | '\tand esi, 3\n') 135 | else: 136 | s = ('\tinc esi\n' 137 | '\tand esi, 3\n') 138 | 139 | if func['d'][1]: 140 | s += ('\tmov ebx, esi\n' 141 | '\tcall cell\n') 142 | else: 143 | s += cell('esi') 144 | 145 | res += ('\nright:\n' 146 | + s 147 | + '\tret\n') 148 | if func['k'][1]: 149 | b = func['k'][2] 150 | res += ('\nprint:\n' 151 | +'\tpush eax\n' * b 152 | + '\tmov eax, 4\n' 153 | '\tmov ebx, 1\n' 154 | '\tmov edx, 1\n' 155 | '\tint 80h\n' 156 | '\tmov dword [ecx], 0\n' 157 | + ('\tpop eax\n' 158 | '\tdec eax\n' 159 | '\tcmp eax, 0\n' 160 | '\tjg print\n' 161 | '\tinc eax\n') * b 162 | + '\tmov eax, 1\n' * (1 - b) 163 | + '\tret\n') 164 | 165 | if func['d'][1] and func['f'][1]: 166 | res += ('\ncell:\n' 167 | + cell('ebx') 168 | + '\tret\n') 169 | 170 | s = ('global _start\n' 171 | '_start:\n' 172 | '\tlea esp, [esp - 100]\n' 173 | '\tmov ecx, esp\n' 174 | '\txor edi, edi\n' 175 | '\txor esi, esi\n') 176 | 177 | if any(func[c][2] for c in 'dfk'): 178 | s += '\tmov eax, 1\n' 179 | 180 | return (f'{s}\n' + res).replace('\n\n\n', '\n\n') 181 | 182 | 183 | if __name__ == '__main__': 184 | if len(sys.argv) > 1: 185 | with open(sys.argv[1]) as f: 186 | data = f.read() 187 | 188 | with open('output.asm', 'w') as f: 189 | f.write(comp(data)) 190 | -------------------------------------------------------------------------------- /compilers/assembly/jaune.py: -------------------------------------------------------------------------------- 1 | from re import sub, findall 2 | import sys 3 | 4 | 5 | def count(code, ind): 6 | def check(k, s): 7 | ch = code[k] 8 | return ch.isnumeric() or ch in s 9 | 10 | start = code[ind] 11 | code += ' ' 12 | num = 0 13 | 14 | if start in '+-': 15 | if (n := code[ind - 1]).isnumeric(): 16 | num = int(start + code[ind - 1]) 17 | while check(ind, '+-'): 18 | x, y = code[ind:ind + 2] 19 | if x.isnumeric() and y in '+-': 20 | num += int(y + x) 21 | ind += 1 22 | else: 23 | return n, ind + 1 24 | elif start in ':$@?!': 25 | if (c := code[ind - 1]) == 'v': 26 | num = -1 27 | else: 28 | num = int(c) 29 | ind += 1 30 | else: 31 | while code[ind] == start: 32 | num += 1 33 | ind += 1 34 | 35 | return num, ind 36 | 37 | 38 | def prep(code): 39 | def rep(sym): 40 | return sub(r'\d[?!]', '', sym) 41 | 42 | code = sub(r'[^^v><\d+\-#&:?!.$@;%]', '', code) 43 | code = sub('([#.;%])\1+', '\1', code) 44 | code = sub('v[:$]', '', code) 45 | 46 | jump = [] 47 | rout = [] 48 | 49 | for c in ':$': 50 | r = fr'(?:[\d]{c})+' 51 | for s in findall(r, code): 52 | lst = [k for k in s if k.isnumeric()] 53 | num = jump if c == ':' else rout 54 | opr = '?!' if c == ':' else '@' 55 | 56 | plus = num[-1] + 1 if num else 0 57 | num.append(plus) 58 | m = str(plus) 59 | 60 | for n in lst: 61 | for k in opr: 62 | code = (code 63 | .replace(n + k, m + k)) 64 | 65 | code = code.replace(s, m + c) 66 | 67 | for s in findall(r'(?:[v\d][?!]){2,}', code): 68 | if '?' in s and '!' in s: 69 | if s[1] == '?': 70 | n = s.find('!') 71 | else: 72 | n = s.find('?') 73 | 74 | r += (s[:2] 75 | + rep(s[2:n - 1]) 76 | + s[n - 1:n + 1] 77 | + rep(s[n + 1:])) 78 | else: 79 | r = s[:2] + rep(s[2:]) 80 | 81 | code = code.replace(s, r) 82 | 83 | return code, jump, rout 84 | 85 | 86 | def comp(code): 87 | def add(m): 88 | return m + 1 if m else '' 89 | 90 | code, jump, rout = prep(code) 91 | inp = [False, False] 92 | ind = 0 93 | 94 | res = ('global _start\n' 95 | '_start:\n' 96 | '\tlea ecx, [esp - 60]\n' 97 | '\txor edi, edi\n' 98 | '\tmov edx, 1\n' 99 | '\tmov esi, 1\n\n') 100 | subr = { 101 | '^': ['output', False, False], 102 | 'v': ['input', False, False], 103 | '<': ['left', False, False], 104 | '&': ['mult', False, False] 105 | } 106 | 107 | while ind < len(code): 108 | c = code[ind] 109 | num, new = count(code, ind) 110 | 111 | if c in '^v<': 112 | if num > 1: 113 | res += f'\tmov esi, {num}\n' 114 | subr[c][2] = True 115 | res += f'\tcall {subr[c][0]}\n' 116 | subr[c][1] = True 117 | elif c == '&': 118 | if num > 1: 119 | res += (f'\tmov esi, {num}\n' 120 | f'\tcall {subr[c][0]}\n') 121 | subr[c][1] = subr[c][2] = True 122 | else: 123 | res += '\tadd [ecx], edi\n' 124 | elif c == '>': 125 | res += f'\tsub ecx, {4 * num}\n' 126 | elif c in '+-': 127 | if num: 128 | if isinstance(num, int): 129 | if num > 1: 130 | res += f'\tadd dword [ecx], {num}\n' 131 | elif num == 1: 132 | res += '\tinc dword [ecx]\n' 133 | elif num == -1: 134 | res += '\tdec dword [ecx]\n' 135 | else: 136 | res += f'\tsub dword [ecx], {-num}\n' 137 | else: 138 | if c == '+': 139 | res += '\tadd [ecx], eax\n' 140 | else: 141 | res += '\tsub [ecx], eax\n' 142 | elif c == '#': 143 | res += '\tmov edi, [ecx]\n' 144 | elif c == ':': 145 | res += f'.label{add(num)}:\n' 146 | elif c in '?!': 147 | res += ('\tcmp dword [ecx], 0\n' 148 | f'\tj{"n" if c == "?" else ""}e ') 149 | if num >= 0: 150 | res += f'.label{add(num)}\n' 151 | else: 152 | res += '.switch\n' 153 | inp[0] = True 154 | elif c == '.': 155 | res += ('\n\tmov eax, 1\n' 156 | '\txor ebx, ebx\n' 157 | '\tint 80h\n') 158 | elif c == '$': 159 | res += f'sub{add(num)}:\n' 160 | elif c == '@': 161 | if num >= 0: 162 | res += f'\tcall sub{add(num)}\n' 163 | else: 164 | res += '\tcall switch\n' 165 | inp[1] = True 166 | elif c == ';': 167 | res += '\tret\n' 168 | elif c == '%': 169 | res += '\tmov dword [ecx], 0\n' 170 | 171 | ind = new 172 | 173 | if jump and inp[0]: 174 | res += '\n.switch:\n' 175 | for k in jump[:-1]: 176 | res += (f'\tcmp eax, {k}\n' 177 | f'\tje .lab{add(k)}\n') 178 | for k in jump[::-1]: 179 | n = add(k) 180 | if k != jump[-1]: 181 | res += f'.lab{n}:\n' 182 | res += f'\tjmp .label{n}\n' 183 | if rout and inp[1]: 184 | res += '\nswitch:\n' 185 | for k in rout[:-1]: 186 | res += (f'\tcmp eax, {k}\n' 187 | f'\tje .sub{add(k)}\n') 188 | res += '\tret\n' 189 | for k in rout[::-1]: 190 | n = add(k) 191 | if k != rout[-1]: 192 | res += f'.sub{n}:\n' 193 | res += (f'\tcall sub{n}\n' 194 | '\tret\n') 195 | 196 | def end(opr): 197 | if subr[opr][2]: 198 | mul = ('\tdec esi\n' 199 | '\tcmp esi, 0\n' 200 | f'\tjg {subr[opr][0]}\n' 201 | '\tinc esi\n' 202 | '\tret\n') 203 | else: 204 | mul = '\tret\n' 205 | return mul 206 | 207 | if subr['^'][1]: 208 | res += ('\noutput:\n' 209 | '\tmov edi, [ecx]\n' 210 | '\tpush edi\n' 211 | '\n\tmov eax, 10\n' 212 | '\tcmp edi, 0\n' 213 | '\tjge .max\n' 214 | '\n\tmov dword [ecx], \'-\'\n' 215 | '\tcall print\n' 216 | '\tneg edi\n' 217 | '.max:\n' 218 | '\tcmp eax, edi\n' 219 | '\tjg .main\n' 220 | '\tmov ebx, 10\n' 221 | '\tmul ebx\n' 222 | '\tjmp .max\n' 223 | '.main:\n' 224 | '\tmov ebx, 10\n' 225 | '\txor edx, edx\n' 226 | '\tdiv ebx\n' 227 | '\n\txchg eax, edi\n' 228 | '\txor edx, edx\n' 229 | '\tdiv edi\n' 230 | '\tmov [ecx], eax\n' 231 | '\tmov eax, edx\n' 232 | '\txchg eax, edi\n' 233 | '\n\tadd dword [ecx], \'0\'\n' 234 | '\tcall print\n' 235 | '\tsub dword [ecx], \'0\'\n' 236 | '\n\tcmp eax, 1\n' 237 | '\tje .done\n' 238 | '\tjmp .main\n' 239 | '.done:\n' 240 | '\tpop edi\n' 241 | '\tmov [ecx], edi\n' 242 | + end('^') + 243 | '\nprint:\n' 244 | '\tpush eax\n' 245 | '\tmov eax, 4\n' 246 | '\tmov ebx, 1\n' 247 | '\tmov edx, 1\n' 248 | '\tint 80h\n' 249 | '\tpop eax\n' 250 | '\tret\n') 251 | if subr['v'][1]: 252 | s = ('\ninput:\n' 253 | '\tpush ecx\n' 254 | '\tmov eax, 3\n' 255 | '\tmov ebx, 0\n' 256 | '\tlea ecx, [esp - 4]\n' 257 | '\tint 80h\n' 258 | '\tmov eax, [esp - 4]\n' 259 | '\tsub eax, \'0\'\n' 260 | + end('v')) 261 | res += s.replace('ret', 'pop ecx\n\tret') 262 | if subr['<'][1]: 263 | res += ('\nleft:\n' 264 | '\tlea ecx, [ecx + 4*esi]\n' 265 | '\tlea eax, [esp - 48]\n' 266 | '\tcmp eax, ecx\n' 267 | '\tjge .done\n' 268 | '\tmov ecx, eax\n' 269 | '.done:\n' 270 | '\tret\n') 271 | if subr['&'][1]: 272 | res += ('\nmult:\n' 273 | '\tmov eax, edi\n' 274 | '\tmul esi\n' 275 | '\tadd [ecx], eax\n' 276 | '\tret\n') 277 | 278 | return res.replace('\n\n\n', '\n\n') 279 | 280 | 281 | if __name__ == '__main__': 282 | if len(sys.argv) > 1: 283 | with open(sys.argv[1]) as f: 284 | data = f.read() 285 | 286 | with open('output.asm', 'w') as f: 287 | f.write(comp(data)) 288 | -------------------------------------------------------------------------------- /compilers/assembly/suffolk.py: -------------------------------------------------------------------------------- 1 | from re import sub 2 | import sys 3 | 4 | 5 | def count(code, ind): 6 | char = code[ind] 7 | code += ' ' 8 | num = 0 9 | 10 | while code[ind] == char: 11 | num += 1 12 | ind += 1 13 | 14 | return num 15 | 16 | 17 | def comp(code, num): 18 | code = sub('[^><.,!]', '', code) 19 | res = ('global _start\n' 20 | '_start:\n' 21 | f'\tmov ebx, {num}\n' 22 | '\txor esi, esi\n' 23 | '\tlea ecx, [esp - 12]\n' 24 | '\tlea edi, [ecx - 4]\n' 25 | '\tmov edx, 1\n' 26 | '.main:\n') 27 | length = len(code) 28 | 29 | ind = 0 30 | add = False 31 | inp = False 32 | out = False 33 | exc = False 34 | 35 | while ind < length: 36 | n = count(code, ind) 37 | c = code[ind] 38 | 39 | if c == '>': 40 | s = f'sub edi, {n * 4}' 41 | elif c == '<': 42 | s = ('add esi, [edi]\n' 43 | '\tlea edi, [ecx - 4]') 44 | 45 | if n > 2: 46 | s += (f'\n\tmov edx, {n - 1}\n' 47 | '\tcall left') 48 | elif n == 2: 49 | s += '\n\tadd esi, [edi]' 50 | 51 | add = True 52 | elif c == '.': 53 | s = 'output' 54 | out = True 55 | elif c == ',': 56 | s = 'input' 57 | inp = True 58 | else: 59 | s = ('call excl' 60 | + f'\n\tadd DWORD [edi], {n - 1}' 61 | * (n > 1)) 62 | exc = True 63 | 64 | if c in '.,': 65 | s = '\n\t'.join(f'call {s}' for _ in range(n)) 66 | res += f'\t{s}\n' 67 | 68 | ind += n 69 | 70 | res += ('\n\tdec ebx\n' 71 | '\tcmp ebx, 0\n' 72 | '\tjg .main\n' 73 | '\tmov eax, 1\n' 74 | '\txor ebx, ebx\n' 75 | '\tint 80h') 76 | 77 | if inp: 78 | res += ('\n\ninput:\n' 79 | '\tpush ebx\n' 80 | '\tmov eax, 3\n' 81 | '\txor ebx, ebx\n' 82 | '\tint 80h\n' 83 | '\tadd esi, [ecx]\n' 84 | '\tpop ebx\n' 85 | '\tret') 86 | if out: 87 | res += ('\n\noutput:\n' 88 | '\tcmp esi, 0\n' 89 | '\tje .done\n' 90 | '\tpush ebx\n' 91 | '\tdec esi\n' 92 | '\tpush esi\n' 93 | '\tmov eax, 4\n' 94 | '\tmov ebx, 1\n' 95 | '\tint 80h\n' 96 | '\tpop esi\n' 97 | '\tinc esi\n' 98 | '\tpop ebx\n' 99 | '.done:\n' 100 | '\tret') 101 | if exc: 102 | res += ('\n\nexcl:\n' 103 | '\tinc DWORD [edi]\n' 104 | '\tsub [edi], esi\n' 105 | '\tcmp DWORD [edi], 0\n' 106 | '\tjge .done\n' 107 | '\tmov DWORD [edi], 0\n' 108 | '.done:\n' 109 | '\txor esi, esi\n' 110 | '\tlea edi, [ecx - 4]\n' 111 | '\tret') 112 | if add: 113 | res += ('\n\nleft:\n' 114 | '\tpush ebx\n' 115 | '\tmov eax, edx\n' 116 | '\tmov ebx, [edi]\n' 117 | '\tmul ebx\n' 118 | '\tadd esi, eax\n' 119 | '\tmov edx, 1\n' 120 | '\tpop ebx\n' 121 | '\tret') 122 | 123 | return res 124 | 125 | 126 | if __name__ == '__main__': 127 | if len(sys.argv) > 2: 128 | loop = int(sys.argv[2]) 129 | else: 130 | loop = 1 131 | 132 | if len(sys.argv) > 1: 133 | with open(sys.argv[1]) as f: 134 | data = f.read() 135 | 136 | with open('output.asm', 'w') as f: 137 | f.write(comp(data, loop)) 138 | -------------------------------------------------------------------------------- /compilers/assembly/unsquare.py: -------------------------------------------------------------------------------- 1 | from re import sub 2 | import sys 3 | 4 | 5 | def count(code, ind): 6 | code += ' ' 7 | num = 0 8 | 9 | if (start := code[ind]) in 'OI': 10 | if code[ind + 1] == 'A': 11 | num = 0 if start == 'O' else 1 12 | ind += 2 13 | 14 | while (ins := code[ind]) in '+-x': 15 | if ins == '+': 16 | num += 2 17 | elif ins == '-': 18 | num -= 2 19 | else: 20 | num *= 2 21 | ind += 1 22 | 23 | return num, ind 24 | if start in '+-': 25 | while (ins := code[ind]) in '+-': 26 | if ins == '+': 27 | num += 2 28 | else: 29 | num -= 2 30 | ind += 1 31 | else: 32 | while code[ind] == start: 33 | num += 1 34 | ind += 1 35 | 36 | return num, ind 37 | 38 | 39 | def prep(code): 40 | code = sub(r'[^OIAS+\-xPoi><]', '', code) 41 | ind = 0 42 | 43 | while ind + 1 < len(code): 44 | if (code[ind] in 'OI' 45 | and code[ind + 1] == 'A'): 46 | num, new = count(code, ind) 47 | if (num in range(2) 48 | and new < len(code) 49 | and code[new] == '>'): 50 | alt, res = new, 1 51 | while res: 52 | alt += 1 53 | if (c := code[alt]) == '>': 54 | res += 1 55 | elif c == '<': 56 | res -= 1 57 | code = code.replace( 58 | code[ind:alt + 1], 59 | code[ind:new] 60 | ) 61 | ind += 1 62 | 63 | code = sub(r'([OI]A[+\-x]*)+', r'\1', code) 64 | code = sub('(OO|II|PP)S+', '\1', code) 65 | 66 | return code.replace('SS', '') 67 | 68 | 69 | def comp(code): 70 | res = ('global _start\n' 71 | '_start:\n' 72 | '\tlea ecx, [esp - 4]\n' 73 | '\txor edi, edi\n' 74 | '\tmov edx, 1\n' 75 | '\tmov esi, 1\n\n') 76 | func = { 77 | 'O': ['zero', False, False], 78 | 'I': ['one', False, False], 79 | 'A': ['down', False, False], 80 | 'S': ['swap', False, False], 81 | 'P': ['up', False, False], 82 | 'o': ['output', False, False], 83 | 'i': ['input', False, False] 84 | } 85 | 86 | code = prep(code) 87 | ind = jmp = 0 88 | 89 | while ind < len(code): 90 | num, new = count(code, ind) 91 | if (c := code[ind]) in 'OI': 92 | if (code + ' ')[ind + 1] == 'A': 93 | res += f'\tmov edi, {num}\n' 94 | ind = new 95 | continue 96 | if c in 'OIASPoi': 97 | if num > 1: 98 | res += f'\tmov esi, {num}\n' 99 | func[c][2] = True 100 | res += f'\tcall {func[c][0]}\n' 101 | func[c][1] = True 102 | elif c in '+-': 103 | if num: 104 | if num > 0: 105 | res += f'\tadd edi, {num}\n' 106 | else: 107 | res += f'\tsub edi, {-num}\n' 108 | elif c == 'x': 109 | res += f'\tshl edi, {num}\n' 110 | elif c == '>': 111 | jmp += 1 112 | res += (f'.T{jmp}:\n' 113 | '\tcmp edi, 2\n' 114 | f'\tjb .B{jmp}\n') 115 | elif c == '<': 116 | res += (f'\tjmp .T{jmp}\n' 117 | f'.B{jmp}:\n') 118 | jmp -= 1 119 | 120 | ind = new 121 | 122 | res += ('\n\tmov eax, 1\n' 123 | '\txor ebx, ebx\n' 124 | '\tint 80h\n\n') 125 | 126 | def end(opr): 127 | if func[opr][2]: 128 | mul = ('\tdec esi\n' 129 | '\tcmp esi, 0\n' 130 | f'\tjg {func[opr][0]}\n' 131 | '\tinc esi\n' 132 | '\tret\n') 133 | else: 134 | mul = '\tret\n' 135 | return mul 136 | 137 | if func['O'][1]: 138 | res += ('zero:\n' 139 | '\tsub ecx, 4\n' 140 | '\tmov dword [ecx], 0\n' 141 | + end('O')) 142 | if func['I'][1]: 143 | res += ('one:\n' 144 | '\tsub ecx, 4\n' 145 | '\tmov dword [ecx], 1\n' 146 | + end('I')) 147 | if func['P'][1]: 148 | res += ('up:\n' 149 | '\tsub ecx, 4\n' 150 | '\tmov dword [ecx], edi\n' 151 | + end('P')) 152 | if func['A'][1]: 153 | if func['A'][2]: 154 | res += ('down:\n' 155 | '\tdec esi\n' 156 | '\tlea ecx, [ecx + 4*esi]\n' 157 | '\tmov edi, [ecx]\n' 158 | '\tadd ecx, 4\n' 159 | '\tmov esi, 1\n' 160 | '\tret\n') 161 | else: 162 | res += ('down:\n' 163 | '\tmov edi, [ecx]\n' 164 | '\tadd ecx, 4\n' 165 | '\tret\n') 166 | if func['S'][1]: 167 | res += ('swap:\n' 168 | '\tmov eax, [ecx]\n' 169 | '\tmov ebx, [ecx + 4]\n' 170 | '\tmov [ecx], ebx\n' 171 | '\tmov [ecx + 4], eax\n' 172 | '\tret\n') 173 | if func['o'][1]: 174 | res += ('output:\n' 175 | '\tmov eax, 4\n' 176 | '\tmov ebx, 1\n' 177 | '\tint 80h\n' 178 | + end('o')) 179 | if func['i'][1]: 180 | res += ('input:\n' 181 | '\tsub ecx, 4\n' 182 | '\tmov eax, 3\n' 183 | '\txor ebx, ebx\n' 184 | '\tint 80h\n' 185 | + end('i')) 186 | 187 | return (res.replace(':\n\n', ':\n') 188 | .strip() + '\n') 189 | 190 | 191 | if __name__ == '__main__': 192 | if len(sys.argv) > 1: 193 | with open(sys.argv[1]) as f: 194 | data = f.read() 195 | 196 | with open('output.asm', 'w') as f: 197 | f.write(comp(data)) 198 | -------------------------------------------------------------------------------- /compilers/c/RAM0.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | void skip(FILE *file, char c) { 6 | fseek(file, -2, SEEK_CUR); 7 | char ch = fgetc(file); 8 | 9 | if (ch == 'C') { 10 | fseek(file, 2, SEEK_CUR); 11 | } else { 12 | while ((ch = fgetc(file)) == c) {} 13 | if (!feof(file)) 14 | fseek(file, -1, SEEK_CUR); 15 | } 16 | } 17 | 18 | int main(int argc, char *argv[]) { 19 | FILE *file = fopen(argv[1], "r"); 20 | FILE *output = fopen("output.c", "w"); 21 | 22 | int max = 0, ind = 0, num = 0; 23 | char *str, ch; 24 | 25 | while ((ch = getc(file)) != EOF) { 26 | num |= ch == 'C' || isdigit(ch); 27 | max += ch == 'A'; 28 | } 29 | 30 | rewind(file); 31 | fprintf( 32 | output, 33 | "#include \n\nint z, n%s;" 34 | "\nint ram[%d];\n\nint main()\n{\n", 35 | num ? ", ind" : "", max); 36 | if (num) 37 | fputs( 38 | "\twhile (++ind)\n\t{\n\t\t" 39 | "switch (ind)\n\t\t{\n", output); 40 | 41 | while ((ch = getc(file)) != EOF) { 42 | str = ""; ind++; 43 | switch (ch) { 44 | case 'A': str = "z++;"; break; 45 | case 'L': str = "z = ram[z];"; break; 46 | case 'C': str = "ind += z == 0;"; break; 47 | case 'Z': str = "z = 0;"; 48 | skip(file, ch); break; 49 | case 'N': str = "n = z;"; 50 | skip(file, ch); break; 51 | case 'S': str = "ram[n] = z;"; 52 | skip(file, ch); break; 53 | default: 54 | if (isdigit(ch)) { 55 | int val; 56 | 57 | fseek(file, -1, SEEK_CUR); 58 | fscanf(file, "%d", &val); 59 | 60 | int len = snprintf( 61 | NULL, 0, "%d", val); 62 | str = malloc(len + 12); 63 | snprintf( 64 | str, len + 12, 65 | "ind = %i - 1;", val); 66 | } 67 | break; 68 | } 69 | 70 | if (strcmp(str, "")) { 71 | if (num) 72 | fprintf( 73 | output, "\t\t\tcase %d: " 74 | "%s break;\n", ind, str); 75 | else 76 | fprintf(output, "\t%s\n", str); 77 | } else { 78 | ind--; 79 | } 80 | } 81 | 82 | str = "\t\t\tdefault: ind = -1;" 83 | " break;\n\t\t}\n\t}\n"; 84 | fprintf( 85 | output, "%s\n\tprintf(\"Z: %s\\nN:" 86 | " %s\\n\", z, n);\n\treturn 0;\n}", 87 | num ? str : "", "%d", "%d"); 88 | 89 | fclose(file); 90 | fclose(output); 91 | 92 | return 0; 93 | } 94 | -------------------------------------------------------------------------------- /compilers/c/bf-pda.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(int argc, char* argv[]) { 4 | FILE *file = fopen(argv[1], "r"); 5 | FILE *output = fopen("output.c", "w"); 6 | int ind, tabs = 0; 7 | char *str, ch; 8 | 9 | fputs( 10 | "#include \n\nint stk[50]" 11 | ", ptr;\n\nint main() {\n", output); 12 | 13 | while ((ch = getc(file)) != EOF) { 14 | char *str = ""; 15 | switch (ch) { 16 | case '@': str = "stk[ptr] ^= 1;"; break; 17 | case '.': str = "printf(\"%d\", " 18 | "stk[ptr]);"; break; 19 | case '<': str = "stk[++ptr] = 0;"; break; 20 | case '>': str = "ptr -= !!ptr;"; break; 21 | case '[': str = "while (stk[ptr]) {"; break; 22 | case ']': str = "}"; tabs--; break; 23 | } 24 | 25 | for (ind = 0; ind < tabs; ind++) 26 | fputs("\t", output); 27 | if (strcmp(str, "")) 28 | fprintf(output, "\t%s\n", str); 29 | 30 | tabs += ch == '['; 31 | } 32 | 33 | fputs("\treturn 0;\n}\n", output); 34 | fclose(output); 35 | fclose(file); 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /compilers/c/bfstack.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(int argc, char *argv[]) { 5 | FILE *file = fopen(argv[1], "r"); 6 | FILE *output = fopen("output.c", "w"); 7 | int loop, brackets = 0; 8 | char ch; 9 | 10 | fprintf( 11 | output, 12 | "#include \n\nchar stack[500];\n" 13 | "int n, line;\n\nvoid input() {\n\tprintf" 14 | "(\"%sInput: \", line ? \"\\n\" : \"\");" 15 | "\n\tscanf(\"%s\", &stack[++n]);\n\tline" 16 | " = 0;\n}\n\nint main() {\n", "%s", "%s"); 17 | 18 | while ((ch = getc(file)) != EOF) { 19 | char *str = ""; 20 | switch (ch) { 21 | case '.': str = "printf(\"%c\", " 22 | "stack[n]); line++;"; break; 23 | case ',': str = "input();"; break; 24 | case '>': str = "stack[++n] = 0;"; break; 25 | case '<': str = "n -= !!n;"; break; 26 | case '+': str = "stack[n]++;"; break; 27 | case '-': str = "stack[n] += 255;"; break; 28 | case '[': str = "while (stack[n]) {"; break; 29 | case ']': str = "}"; brackets--; break; 30 | } 31 | 32 | for (loop = 0; loop < brackets; loop++) 33 | fputs("\t", output); 34 | if (strcmp(str, "")) 35 | fprintf(output, "\t%s\n", str); 36 | 37 | brackets += ch == '['; 38 | } 39 | 40 | fputs("\treturn 0;\n}\n", output); 41 | fclose(file); fclose(output); 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /compilers/c/excon.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(int argc, char* argv[]) { 4 | FILE *file = fopen(argv[1], "r"); 5 | FILE *output = fopen("output.c", "w"); 6 | char ch; 7 | 8 | fputs( 9 | "#include \n#include \n\nint" 10 | " pool[8], cell = 7;\n\nint binary(int* arr)\n{" 11 | "\n\tint ind, val = 0;\n\n\tfor(ind = 0; ind < " 12 | "8; ind++)\n\t\tval += (1 << (7 - ind)) * arr[i" 13 | "nd];\n\n\treturn val;\n}\n\nint main()\n{\n", 14 | output); 15 | 16 | while ((ch = getc(file)) != EOF) { 17 | char *str = ""; 18 | switch (ch) { 19 | case ':': str = "memset(pool, 0, 32); cell = 7;"; break; 20 | case '^': str = "pool[cell] ^= 1;"; break; 21 | case '!': str = "printf(\"%c\", binary(pool));"; break; 22 | case '<': str = "cell--;"; break; 23 | } 24 | 25 | if (strcmp(str, "")) 26 | fprintf(output, "\t%s\n", str); 27 | } 28 | 29 | fputs("\treturn 0;\n}\n", output); 30 | fclose(output); 31 | fclose(file); 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /extra/assembly/123.asm: -------------------------------------------------------------------------------- 1 | global _start 2 | _start: 3 | lea ecx, [esp - 1] 4 | xor ebx, ebx 5 | xor edi, edi 6 | mov esi, 128 7 | mov edx, 1 8 | .input: 9 | mov eax, 3 10 | int 80h 11 | 12 | cmp eax, 1 13 | jl .done 14 | cmp byte [ecx], '|' 15 | je .done 16 | 17 | dec ecx 18 | jmp .input 19 | .done: 20 | mov byte [ecx], '|' 21 | sub ecx, 4 22 | mov edx, esp 23 | .parse: 24 | dec edx 25 | cmp byte [edx], '1' 26 | je .left 27 | cmp byte [edx], '2' 28 | je .right 29 | cmp byte [edx], '3' 30 | je .jump 31 | 32 | cmp byte [edx], '|' 33 | jne .parse 34 | cmp esi, 128 35 | jg .final 36 | 37 | mov edx, esp 38 | jmp .parse 39 | .final: 40 | mov eax, 1 41 | mov ebx, 0 42 | int 80h 43 | 44 | .left: 45 | xor edi, esi 46 | cmp esi, 1024 47 | jl .shift 48 | mov esi, 64 49 | .shift: 50 | shl esi, 1 51 | jmp .parse 52 | 53 | .right: 54 | cmp esi, 1024 55 | je .read 56 | cmp esi, 512 57 | je .write 58 | 59 | shr esi, 1 60 | jmp .parse 61 | .read: 62 | push edx 63 | mov eax, 3 64 | xor ebx, ebx 65 | mov edx, 1 66 | int 80h 67 | pop edx 68 | 69 | mov edi, [ecx] 70 | mov esi, 128 71 | jmp .parse 72 | .write: 73 | mov [ecx], edi 74 | push edx 75 | mov eax, 4 76 | mov ebx, 1 77 | mov edx, 1 78 | int 80h 79 | pop edx 80 | 81 | mov esi, 128 82 | jmp .parse 83 | 84 | .jump: 85 | cmp esi, 128 86 | jg .parse 87 | 88 | mov eax, edi 89 | and eax, esi 90 | 91 | cmp eax, 0 92 | je .false 93 | .true: 94 | inc edx 95 | cmp edx, esp 96 | je .parse 97 | cmp byte [edx], '3' 98 | je .parse 99 | jmp .true 100 | .false: 101 | dec edx 102 | cmp byte [edx], '|' 103 | je .parse 104 | cmp byte [edx], '3' 105 | je .parse 106 | jmp .false 107 | -------------------------------------------------------------------------------- /extra/assembly/2b1b.asm: -------------------------------------------------------------------------------- 1 | global _start 2 | _start: 3 | mov eax, 3 4 | xor ebx, ebx 5 | lea ecx, [esp - 1] 6 | mov edx, 1 7 | int 80h 8 | 9 | mov al, [ecx] 10 | mov bl, 3 11 | mov cl, 8 12 | .parse: 13 | call num 14 | 15 | cmp dl, 0 16 | je .parse 17 | cmp dl, 1 18 | je .one 19 | cmp dl, 2 20 | je .two 21 | 22 | lea ecx, [esp - 1] 23 | mov [ecx], al 24 | 25 | mov eax, 4 26 | mov ebx, 1 27 | mov edx, 1 28 | int 80h 29 | 30 | mov eax, 1 31 | xor ebx, ebx 32 | int 80h 33 | 34 | .one: 35 | call num 36 | push bx 37 | push cx 38 | call ind 39 | call num 40 | cmp dl, 1 41 | jg .above 42 | 43 | xor al, bl 44 | jmp .done 45 | .above: 46 | mov dl, bl 47 | shl dl, 1 48 | and dl, bl 49 | xor al, dl 50 | .done: 51 | pop cx 52 | pop bx 53 | jmp .parse 54 | 55 | .two: 56 | call num 57 | call ind 58 | jmp .parse 59 | 60 | num: 61 | sub cl, 2 62 | and cl, 7 63 | ror bl, 2 64 | 65 | mov dl, bl 66 | and dl, al 67 | shr dl, cl 68 | ret 69 | 70 | ind: 71 | mov cl, dl 72 | shl cl, 1 73 | sub cl, 8 74 | neg cl 75 | 76 | mov bl, 3 77 | shl bl, cl 78 | ret -------------------------------------------------------------------------------- /extra/assembly/brainpocalypse.asm: -------------------------------------------------------------------------------- 1 | global _start 2 | _start: 3 | lea ecx, [esp - 16] 4 | mov ebx, 0 5 | mov edx, 1 6 | mov edi, 1 7 | mov esi, 1 8 | .input: 9 | mov eax, 3 10 | int 80h 11 | dec ecx 12 | cmp eax, 0 13 | jg .input 14 | 15 | mov byte [ecx], 0 16 | sub ecx, 4 17 | mov eax, ecx 18 | lea edx, [esp - 15] 19 | .parse: 20 | dec edx 21 | cmp byte [edx], '+' 22 | je .plus 23 | cmp byte [edx], '-' 24 | je .minus 25 | cmp byte [edx], '>' 26 | je .right 27 | cmp byte [edx], '<' 28 | je .left 29 | 30 | cmp byte [edx], 0 31 | jne .parse 32 | 33 | mov ecx, eax 34 | .state: 35 | call output 36 | dec edi 37 | cmp edi, 0 38 | je .final 39 | 40 | mov dword [ecx], ' ' 41 | call print 42 | sub ecx, 4 43 | jmp .state 44 | .final: 45 | mov eax, 1 46 | mov ebx, 0 47 | int 80h 48 | 49 | .plus: 50 | inc dword [ecx] 51 | jmp .parse 52 | .minus: 53 | cmp dword [ecx], 0 54 | je .goto 55 | dec dword [ecx] 56 | jmp .parse 57 | .right: 58 | sub ecx, 4 59 | inc esi 60 | cmp edi, esi 61 | jge .parse 62 | 63 | inc edi 64 | jmp .parse 65 | .left: 66 | cmp ecx, edi 67 | je .parse 68 | 69 | dec esi 70 | add ecx, 4 71 | jmp .parse 72 | .goto: 73 | lea edx, [esp - 15] 74 | jmp .parse 75 | 76 | output: 77 | push edi 78 | mov edi, [ecx] 79 | mov eax, 10 80 | .max: 81 | cmp eax, edi 82 | jg .main 83 | mov ebx, 10 84 | xor edx, edx 85 | mul ebx 86 | jmp .max 87 | .main: 88 | mov ebx, 10 89 | xor edx, edx 90 | div ebx 91 | 92 | xchg eax, edi 93 | xor edx, edx 94 | div edi 95 | mov [ecx], eax 96 | mov eax, edx 97 | xchg eax, edi 98 | 99 | add dword [ecx], '0' 100 | call print 101 | sub dword [ecx], '0' 102 | 103 | cmp eax, 1 104 | jne .main 105 | pop edi 106 | ret 107 | 108 | print: 109 | push eax 110 | mov eax, 4 111 | mov ebx, 1 112 | mov edx, 1 113 | int 80h 114 | pop eax 115 | ret -------------------------------------------------------------------------------- /extra/assembly/nocomment.asm: -------------------------------------------------------------------------------- 1 | global _start 2 | _start: 3 | lea ecx, [esp - 5] 4 | xor ebx, ebx 5 | mov edx, 1 6 | .input: 7 | mov eax, 3 8 | int 80h 9 | dec ecx 10 | 11 | cmp eax, 1 12 | jl .done 13 | jmp .input 14 | .done: 15 | mov byte [ecx], 0 16 | mov edx, ecx 17 | mov esi, ecx 18 | dec ecx 19 | lea edi, [esp - 4] 20 | .parse: 21 | dec edi 22 | cmp byte [edi], 'i' 23 | je .up 24 | cmp byte [edi], 'd' 25 | je .down 26 | cmp byte [edi], 'c' 27 | je .zero 28 | cmp byte [edi], 'l' 29 | je .left 30 | cmp byte [edi], 'r' 31 | je .right 32 | cmp byte [edi], 'n' 33 | je .on 34 | cmp byte [edi], 'f' 35 | je .off 36 | cmp byte [edi], 's' 37 | je .fore 38 | cmp byte [edi], 'b' 39 | je .back 40 | cmp byte [edi], 'o' 41 | je .output 42 | 43 | cmp byte [edi], 0 44 | jne .parse 45 | .final: 46 | mov eax, 1 47 | xor ebx, ebx 48 | int 80h 49 | 50 | .up: 51 | inc byte [ecx] 52 | jmp .parse 53 | .down: 54 | dec byte [ecx] 55 | jmp .parse 56 | .zero: 57 | mov byte [ecx], 0 58 | jmp .parse 59 | .left: 60 | add ecx, 2 61 | cmp ecx, esi 62 | jl .parse 63 | .right: 64 | sub ecx, 2 65 | jmp .parse 66 | .on: 67 | mov al, [ecx] 68 | sub edx, 2 69 | mov [edx], al 70 | jmp .parse 71 | .off: 72 | cmp edx, esi 73 | je .parse 74 | mov al, [edx] 75 | add edx, 2 76 | mov [ecx], al 77 | jmp .parse 78 | .fore: 79 | cmp byte [ecx], 0 80 | je .parse 81 | movzx eax, byte [edx] 82 | sub edi, eax 83 | jmp .parse 84 | .back: 85 | cmp byte [ecx], 0 86 | je .parse 87 | movzx eax, byte [edx] 88 | add edi, eax 89 | jmp .parse 90 | .output: 91 | push edx 92 | mov eax, 4 93 | mov ebx, 1 94 | mov edx, 1 95 | int 80h 96 | pop edx 97 | jmp .parse 98 | -------------------------------------------------------------------------------- /extra/assembly/stun-step.asm: -------------------------------------------------------------------------------- 1 | global _start 2 | _start: 3 | lea ecx, [esp - 20] 4 | xor ebx, ebx 5 | mov edx, 1 6 | mov edi, 1 7 | mov esi, 1 8 | .input: 9 | mov eax, 3 10 | int 80h 11 | dec ecx 12 | cmp eax, 0 13 | jg .input 14 | 15 | mov byte [ecx], 0 16 | sub ecx, 4 17 | mov eax, ecx 18 | dec dword [ecx] 19 | lea edx, [esp - 19] 20 | .parse: 21 | dec edx 22 | cmp byte [edx], '+' 23 | je .plus 24 | cmp byte [edx], '-' 25 | je .minus 26 | cmp byte [edx], '>' 27 | je .right 28 | cmp byte [edx], '<' 29 | je .left 30 | 31 | cmp byte [edx], 0 32 | jne .parse 33 | 34 | lea edx, [esp - 19] 35 | cmp dword [ecx], 0 36 | jge .parse 37 | 38 | mov ecx, eax 39 | .state: 40 | inc dword [ecx] 41 | call output 42 | dec edi 43 | cmp edi, 0 44 | je .final 45 | 46 | mov dword [ecx], ' ' 47 | call print 48 | sub ecx, 4 49 | jmp .state 50 | .final: 51 | mov eax, 1 52 | xor ebx, ebx 53 | int 80h 54 | 55 | .plus: 56 | inc dword [ecx] 57 | jmp .parse 58 | .minus: 59 | cmp dword [ecx], -1 60 | je .parse 61 | dec dword [ecx] 62 | jmp .parse 63 | .right: 64 | cmp dword [ecx], -1 65 | je .parse 66 | inc esi 67 | sub ecx, 4 68 | cmp edi, esi 69 | jge .parse 70 | inc edi 71 | jmp .parse 72 | .left: 73 | cmp ecx, edi 74 | je .parse 75 | cmp dword [ecx], -1 76 | je .parse 77 | dec esi 78 | add ecx, 4 79 | jmp .parse 80 | 81 | output: 82 | push edi 83 | mov edi, [ecx] 84 | mov eax, 10 85 | .max: 86 | cmp eax, edi 87 | jg .main 88 | mov ebx, 10 89 | mul ebx 90 | jmp .max 91 | .main: 92 | mov ebx, 10 93 | xor edx, edx 94 | div ebx 95 | 96 | xchg eax, edi 97 | xor edx, edx 98 | div edi 99 | mov [ecx], eax 100 | mov eax, edx 101 | xchg eax, edi 102 | 103 | add dword [ecx], '0' 104 | call print 105 | sub dword [ecx], '0' 106 | 107 | cmp eax, 1 108 | jne .main 109 | pop edi 110 | ret 111 | 112 | print: 113 | push eax 114 | mov eax, 4 115 | mov ebx, 1 116 | mov edx, 1 117 | int 80h 118 | pop eax 119 | ret 120 | -------------------------------------------------------------------------------- /extra/c++/%^2^-1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(int argc, char* argv[]) { 5 | std::ifstream file; 6 | bool out = false; 7 | int acc = 0; 8 | char c; 9 | 10 | if (argc > 1) { 11 | file = std::ifstream(argv[1]); 12 | if (!file.is_open()) 13 | return EXIT_FAILURE; 14 | } else { 15 | return EXIT_FAILURE; 16 | } 17 | 18 | while (file.get(c)) { 19 | if (acc > 3003) 20 | acc = 0; 21 | 22 | switch (c) { 23 | case 's': 24 | acc -= 2; 25 | break; 26 | case 'i': 27 | acc -= 3; 28 | break; 29 | case 'm': 30 | acc *= 2; 31 | break; 32 | case 'p': 33 | acc *= -1; 34 | break; 35 | case 'l': 36 | std::cout << acc; 37 | out = true; 38 | break; 39 | case 'e': 40 | std::cout << (char) acc; 41 | out = true; 42 | break; 43 | case 'n': 44 | if (out) 45 | std::cout << std::endl; 46 | std::cout << "Input: "; 47 | out = false; 48 | 49 | acc = getchar(); 50 | while ((c = getchar()) != '\n' 51 | && c != EOF); 52 | break; 53 | case '\'': 54 | acc = 0; 55 | break; 56 | case 't': 57 | if (acc != 0) { 58 | file.clear(); 59 | file.seekg(0); 60 | } 61 | break; 62 | } 63 | } 64 | 65 | file.close(); 66 | return EXIT_SUCCESS; 67 | } 68 | -------------------------------------------------------------------------------- /extra/c++/2dFish.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | void prompt(bool& out) { 7 | if (out) 8 | std::cout << std::endl; 9 | 10 | std::cout << "Input: "; 11 | out = false; 12 | } 13 | 14 | char get( 15 | std::vector& prog, 16 | int x, int y) { 17 | if (y < 0 || x < 0 18 | || y == prog.size() 19 | || x == prog[y].size()) 20 | exit(EXIT_FAILURE); 21 | 22 | return prog[y][x]; 23 | } 24 | 25 | void direct( 26 | char& c, 27 | int & x, 28 | int & y, 29 | char& dir) { 30 | if (c == '/' 31 | || c == '\\' 32 | || c == 'v' 33 | || c == '^') 34 | dir = c; 35 | 36 | switch (dir) { 37 | case '/': 38 | x += 1; 39 | break; 40 | case '\\': 41 | x -= 1; 42 | break; 43 | case 'v': 44 | y += 1; 45 | break; 46 | case '^': 47 | y -= 1; 48 | break; 49 | } 50 | } 51 | 52 | int main(int argc, char* argv[]) { 53 | std::vector prog; 54 | std::ifstream file; 55 | std::string str; 56 | bool mode = false, 57 | out = false; 58 | char dir = 0, 59 | c = 0; 60 | int acc = 0, 61 | x = 0, 62 | y = 0; 63 | 64 | if (argc > 1) { 65 | file = std::ifstream(argv[1]); 66 | std::string line; 67 | 68 | if (!file.is_open()) 69 | return EXIT_FAILURE; 70 | 71 | while (!file.eof()) { 72 | getline(file, line); 73 | prog.push_back(line); 74 | } 75 | 76 | if (prog.size() == 0) 77 | return EXIT_FAILURE; 78 | } else { 79 | return EXIT_FAILURE; 80 | } 81 | 82 | direct(prog[0][0], x, y, dir); 83 | c = get(prog, x, y); 84 | 85 | if (!dir) 86 | return EXIT_FAILURE; 87 | 88 | while (c != '@') { 89 | switch (c) { 90 | case 'i': 91 | mode = false; 92 | acc++; 93 | break; 94 | case 'd': 95 | mode = false; 96 | acc--; 97 | break; 98 | case 's': 99 | mode = false; 100 | acc *= acc; 101 | break; 102 | case 'o': 103 | std::cout 104 | << acc; 105 | out = true; 106 | break; 107 | case 'a': 108 | if (mode) { 109 | std::cout << str[0]; 110 | str = str.substr(1); 111 | } else { 112 | std::cout 113 | << (char) acc; 114 | } 115 | 116 | out = true; 117 | break; 118 | case '$': 119 | prompt(out); 120 | getline(std::cin, str); 121 | break; 122 | case '%': 123 | prompt(out); 124 | mode = false; 125 | std::cin >> acc; 126 | break; 127 | case '(': 128 | str = ""; 129 | mode = true; 130 | 131 | if (prog[y].substr(x).find(')') 132 | != std::string::npos) { 133 | int temp = x; 134 | 135 | while (prog[y][++x] != ')') 136 | str += prog[y][x]; 137 | 138 | if (dir != '/') 139 | x = temp; 140 | } else { 141 | return EXIT_FAILURE; 142 | } 143 | 144 | break; 145 | case '*': 146 | std::cout << str; 147 | str = ""; 148 | out = true; 149 | break; 150 | } 151 | 152 | direct(c, x, y, dir); 153 | c = get(prog, x, y); 154 | } 155 | 156 | return EXIT_SUCCESS; 157 | } 158 | -------------------------------------------------------------------------------- /extra/c++/basicfuck.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #define name "([_a-zA-Z]\\w*)(?:->(\\d+))?" 7 | typedef std::vector 8 | > dict; 9 | 10 | void error(std::string msg) { 11 | std::cout << msg 12 | << std::endl; 13 | 14 | exit(EXIT_FAILURE); 15 | } 16 | 17 | int index( 18 | std::string& key, 19 | dict& var) { 20 | std::regex r("(.+)->(.+)"); 21 | std::smatch m; 22 | int ind = 0; 23 | 24 | if (std::regex_match(key, m, r)) { 25 | ind += stoi(m[2]); 26 | key = m[1]; 27 | } 28 | 29 | for (auto k = var.begin(); k != var.end(); k++) 30 | if (k->first != key) 31 | ind += k->second; 32 | else 33 | return ind; 34 | 35 | error("Identifier is undefined."); 36 | return 0; 37 | } 38 | 39 | int run( 40 | std::smatch pre, 41 | std::vector& prog, 42 | std::vector& tape, 43 | size_t ptr) { 44 | char ver = pre[5].str()[0]; 45 | bool out = false; 46 | 47 | int bot = pre[2] != "" 48 | ? stoi(pre[2]) : INT_MIN, 49 | top = pre[3] != "" 50 | ? stoi(pre[3]) : INT_MAX; 51 | 52 | auto val = [&](int n) { 53 | return &tape[prog[ptr + n]]; 54 | }; 55 | 56 | while (ptr < prog.size()) { 57 | int c = prog[ptr++]; 58 | bool neg = false; 59 | 60 | if (c > -3) { 61 | int num = prog[ptr + 1]; 62 | 63 | if (num < 0) { 64 | num += 10; 65 | 66 | if (num % 2) 67 | num = --num / -2; 68 | else 69 | num /= 2; 70 | } else { 71 | num = tape[num]; 72 | } 73 | 74 | if (c == -2) 75 | num *= -1; 76 | 77 | int& n = *val(0); 78 | n += num; 79 | ptr += 2; 80 | 81 | if (n < bot) { 82 | if (ver == 'h') 83 | error("Underflow error."); 84 | if (ver == 'w') 85 | n = top; 86 | else 87 | n = bot; 88 | } 89 | 90 | if (n > top) { 91 | if (ver == 'h') 92 | error("Overflow error."); 93 | if (ver == 'w') 94 | n = bot; 95 | else 96 | n = top; 97 | } 98 | } else if (c > -5) { 99 | if (prog[ptr++] == -7) { 100 | neg = true; 101 | ptr++; 102 | } 103 | 104 | size_t end = ptr++; 105 | int pair = 1; 106 | 107 | while (pair != 0) { 108 | int val = prog[++end]; 109 | 110 | if (val == -8) 111 | pair++; 112 | else if (val == -9) 113 | pair--; 114 | } 115 | 116 | std::vector sub( 117 | prog.begin() + ptr, 118 | prog.begin() + end 119 | ); 120 | 121 | if (c == -3) { 122 | if (!!*val(-2) ^ neg) 123 | run(pre, sub, tape, 0); 124 | } else { 125 | while (!!*val(-2) ^ neg) 126 | run(pre, sub, tape, 0); 127 | } 128 | 129 | ptr = ++end; 130 | } else if (c == -5) { 131 | std::cout << (char) *val(0); 132 | out = true; 133 | ptr++; 134 | } else { 135 | if (out) 136 | std::cout << std::endl; 137 | 138 | std::cout << "Input: "; 139 | *val(0) = getchar(); 140 | out = false; 141 | ptr++; 142 | 143 | while ((c = getchar()) != '\n' 144 | && c != EOF); 145 | } 146 | } 147 | 148 | return EXIT_SUCCESS; 149 | } 150 | 151 | std::vector lexer( 152 | std::string prog) { 153 | std::vector tokens; 154 | 155 | while (prog.size()) { 156 | std::smatch m; 157 | std::regex reg( 158 | "(\\s*(" name "|\\d+|[!(){" 159 | "};]|[+-]=|->|<-)\\s*)[^]*" 160 | ); 161 | 162 | if (std::regex_match(prog, m, reg)) 163 | tokens.push_back(m[2]); 164 | else 165 | error("Invalid token."); 166 | 167 | prog = prog.substr(m.length(1)); 168 | } 169 | 170 | return tokens; 171 | } 172 | 173 | /* Prefix notation: 174 | * += : -1 175 | * -= : -2 176 | * if : -3 177 | * while : -4 178 | * write : -5 179 | * read : -6 180 | * ! : -7 181 | * { : -8 182 | * } : -9 183 | * Nonnegative numbers are variables. 184 | * Negative numbers below -9 are constants. 185 | * Odd means positive, even means negative, 186 | * e.g. 187 | * 0 : -10 188 | * 1 : -11, 2 : -13 189 | * -1 : -12, -2 : -14 190 | * etc. 191 | */ 192 | std::vector parser( 193 | std::vector& prog, 194 | dict& var) { 195 | size_t size = prog.size(), ind = 0; 196 | std::vector tokens; 197 | int pair = 0; 198 | 199 | auto out = []() { 200 | error("Invalid syntax."); 201 | }; 202 | 203 | auto match = [](std::string str) { 204 | std::regex r(name); 205 | return std::regex_match(str, r); 206 | }; 207 | 208 | auto find = [&](int n) { 209 | std::string t = prog[ind + n]; 210 | tokens.push_back(index(t, var)); 211 | }; 212 | 213 | while (ind < size) { 214 | std::string s = prog[ind++]; 215 | 216 | if ((s == "if" || s == "while") 217 | && ind + 4 < size) { 218 | tokens.push_back(s == "if" ? -3 : -4); 219 | 220 | if ((s = prog[ind]) == "!") { 221 | tokens.push_back(-7); 222 | ind++; 223 | } 224 | 225 | if (prog[ind++] == "(" 226 | && match(prog[ind++]) 227 | && prog[ind++] == ")" 228 | && prog[ind++] == "{") { 229 | pair++; 230 | find(-3); 231 | tokens.push_back(-8); 232 | } else { 233 | out(); 234 | } 235 | } else if ((s == "write" || s == "read") 236 | && ind + 2 < size) { 237 | std::string t 238 | = s == "write" 239 | ? "<-" : "->"; 240 | 241 | if (prog[ind++] == t 242 | && match(prog[ind++]) 243 | && prog[ind++] == ";") { 244 | tokens.push_back(s == "write" ? -5 : -6); 245 | find(-2); 246 | } else { 247 | out(); 248 | } 249 | } else if (match(s) && ind + 2 < size) { 250 | if (prog[ind][1] != '=' 251 | || prog[ind + 2] != ";") 252 | out(); 253 | 254 | tokens.push_back( 255 | prog[ind++] == "+=" 256 | ? -1 : -2 257 | ); 258 | 259 | std::string sec = prog[ind]; 260 | find(-2); 261 | 262 | if (!match(sec)) { 263 | if (!isdigit(sec[0])) 264 | out(); 265 | 266 | int n = 2 * stoi(sec); 267 | if (n > 0) n--; 268 | 269 | tokens.push_back(-n - 10); 270 | } else { 271 | find(0); 272 | } 273 | 274 | ind += 2; 275 | } else if (s == "}") { 276 | tokens.push_back(-9); 277 | pair--; 278 | } else { 279 | out(); 280 | } 281 | } 282 | 283 | if (pair != 0) 284 | out(); 285 | 286 | return tokens; 287 | } 288 | 289 | int main(int argc, char* argv[]) { 290 | std::regex com("\\s*//[^\n]*"); 291 | std::vector ops, tape; 292 | dict var; 293 | 294 | std::string prog, temp; 295 | std::smatch pre, m; 296 | std::ifstream file; 297 | int lim = -1; 298 | 299 | if (argc > 1) { 300 | std::regex dir( 301 | "#basicfuck t=" 302 | "(\\d+|unbounded)" 303 | " r=(\\d*)~(\\d*)" 304 | "( o=(wrap|halt|n" 305 | "earest))?\\s*" 306 | ); 307 | 308 | std::regex all( 309 | "#allocate(?: " 310 | name ",?)*\\s*" 311 | ); 312 | 313 | std::ifstream file = 314 | std::ifstream(argv[1]); 315 | 316 | if (!file.is_open()) 317 | return EXIT_FAILURE; 318 | 319 | getline(file, prog); 320 | temp = prog; 321 | 322 | if (std::regex_match(temp, pre, dir)) { 323 | int mode = 0; 324 | 325 | if (pre[2] != "") mode++; 326 | if (pre[3] != "") mode++; 327 | 328 | if (mode != 0 && pre[4] == "") 329 | error("Missing overflow directive."); 330 | else if (mode != 2 && pre[5] == "wrap") 331 | error("Invalid overflow directive."); 332 | 333 | if (pre[1] != "unbounded") 334 | lim = stoi(pre[1]); 335 | } else { 336 | error("Missing/Invalid directives."); 337 | } 338 | 339 | getline(file, prog); 340 | if (std::regex_match(prog, m, all)) { 341 | std::regex id("( " name ",?)[^]*"); 342 | prog = prog.substr(9); 343 | 344 | while (std::regex_match( 345 | prog, m, id)) { 346 | int len = (int) m.length(1), 347 | off = m[3] != "" ? stoi(m[3]) : 1; 348 | 349 | for (int k = 0; k < off; k++) 350 | tape.push_back(0); 351 | 352 | if (m[2] == "if" 353 | || m[2] == "while" 354 | || m[2] == "write" 355 | || m[2] == "read") 356 | error("Invalid identifier."); 357 | 358 | var.push_back(std::make_pair(m[2], off)); 359 | prog = prog.substr(len); 360 | } 361 | } else { 362 | error("Missing/Invalid identifiers."); 363 | } 364 | 365 | std::stringstream buffer; 366 | buffer << file.rdbuf(); 367 | prog = buffer.str(); 368 | file.close(); 369 | } else { 370 | return EXIT_FAILURE; 371 | } 372 | 373 | prog = std::regex_replace(prog, com, ""); 374 | std::regex add(name "\\s*[+-]=\\s*" name); 375 | 376 | if (std::regex_search(prog, add) 377 | && lim != -1) 378 | lim--; 379 | 380 | if (lim != -1 && lim < tape.size()) 381 | error("Insufficient memory."); 382 | 383 | std::vector words = lexer(prog); 384 | ops = parser(words, var); 385 | 386 | return run(pre, ops, tape, 0); 387 | } 388 | -------------------------------------------------------------------------------- /extra/c++/dimensional.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | int parse(std::string prog, int ind) { 10 | std::string sub = prog.substr(ind); 11 | return stoi(sub); 12 | } 13 | 14 | int index( 15 | std::string prog, 16 | int ind, 17 | char start, 18 | char end) { 19 | int match = 1; 20 | char c; 21 | 22 | while (match != 0) { 23 | if (!(c = prog[ind++])) 24 | break; 25 | 26 | if (c == start) 27 | match++; 28 | else if (c == end) 29 | match--; 30 | } 31 | 32 | return ind; 33 | } 34 | 35 | int* move( 36 | std::vector& ptr, 37 | std::vector& prime, 38 | int dim) { 39 | while (dim + 1 > ptr.size()) { 40 | int n = prime.back() + 1; 41 | 42 | while (true) { 43 | bool p = std::all_of( 44 | prime.begin(), 45 | prime.end(), 46 | [n](int k) {return n % k;} 47 | ); 48 | 49 | if (p) break; 50 | n++; 51 | } 52 | 53 | prime.push_back(n); 54 | ptr.push_back(0); 55 | } 56 | 57 | return &ptr[dim]; 58 | } 59 | 60 | char* value( 61 | std::vector& ptr, 62 | std::vector& prime, 63 | std::unordered_map& tape) { 64 | int num = 1; 65 | 66 | for (int k = 0; k < ptr.size(); k++) 67 | num *= (int) pow(prime[k], ptr[k]); 68 | 69 | if (tape.find(num) == tape.end()) 70 | tape[num] = 0; 71 | 72 | return &tape[num]; 73 | } 74 | 75 | int main(int argc, char* argv[]) { 76 | std::unordered_map tape; 77 | std::vector ptr, prime = {2}; 78 | std::vector loop, dloop; 79 | std::string prog; 80 | 81 | bool out = false; 82 | int dim, ind = 0; 83 | char c; 84 | 85 | auto val = [&]() { 86 | return value(ptr, prime, tape); 87 | }; 88 | 89 | auto mov = [&]() { 90 | return move(ptr, prime, dim); 91 | }; 92 | 93 | if (argc > 1) { 94 | std::ifstream file = 95 | std::ifstream(argv[1]); 96 | 97 | if (!file.is_open()) 98 | return EXIT_FAILURE; 99 | 100 | std::stringstream buffer; 101 | buffer << file.rdbuf(); 102 | prog = buffer.str(); 103 | file.close(); 104 | } else { 105 | return EXIT_FAILURE; 106 | } 107 | 108 | while (ind < prog.size()) { 109 | c = prog[ind++]; 110 | 111 | switch (c) { 112 | case '>': 113 | case '<': 114 | if (isdigit(prog[ind])) 115 | dim = parse(prog, ind); 116 | else 117 | dim = *val(); 118 | 119 | *mov() += c == '>' ? 1 : -1; 120 | break; 121 | case '+': 122 | *val() += 1; 123 | break; 124 | case '-': 125 | *val() -= 1; 126 | break; 127 | case ':': 128 | *val() = prog[ind++]; 129 | break; 130 | case '=': 131 | *val() = stoi( 132 | prog.substr(ind), 133 | nullptr, 16 134 | ); 135 | 136 | break; 137 | case '.': 138 | std::cout << *val(); 139 | out = true; 140 | break; 141 | case ',': 142 | if (out) 143 | std::cout << std::endl; 144 | 145 | std::cout << "Input: "; 146 | out = false; 147 | 148 | *val() = getchar(); 149 | while ((c = getchar()) != '\n' 150 | && c != EOF); 151 | break; 152 | case '[': 153 | if (*val() != 0) 154 | loop.push_back(ind - 1); 155 | else 156 | ind = index(prog, ind, '[', ']'); 157 | 158 | break; 159 | case ']': 160 | ind = loop.back(); 161 | loop.pop_back(); 162 | break; 163 | case '{': 164 | dim = parse(prog, ind); 165 | 166 | if (*mov() != 0) 167 | dloop.push_back(ind - 1); 168 | else 169 | ind = index(prog, ind, '{', '}'); 170 | 171 | break; 172 | case '}': 173 | ind = dloop.back(); 174 | dloop.pop_back(); 175 | break; 176 | case '?': 177 | dim = parse(prog, ind); 178 | *val() = *mov() % 256; 179 | break; 180 | case '!': 181 | dim = parse(prog, ind); 182 | *mov() = 0; 183 | break; 184 | case '*': 185 | while (prog[ind++] != '*'); 186 | break; 187 | } 188 | } 189 | 190 | return EXIT_SUCCESS; 191 | } 192 | -------------------------------------------------------------------------------- /extra/c++/forþ.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | int run(std::string& s, 9 | std::vector& stack, 10 | std::unordered_map 11 | & table, 12 | bool& out) { 13 | auto top = [&stack]() { 14 | if (stack.size()) 15 | return stack.back(); 16 | 17 | exit(EXIT_FAILURE); 18 | }; 19 | 20 | auto push = [&stack](int n) { 21 | stack.push_back(n); 22 | }; 23 | 24 | auto pop = [&stack, &top]() { 25 | int n = top(); 26 | stack.pop_back(); 27 | 28 | return n; 29 | }; 30 | 31 | auto exec = [&](std::string str) { 32 | run(str, stack, table, out); 33 | }; 34 | 35 | for (int k = 0; k < s.size(); k++) { 36 | char c = s[k]; 37 | 38 | if (47 < c && c < 58) { 39 | stack.push_back(c - 48); 40 | } else if (64 < c && c < 71) { 41 | stack.push_back(c - 55); 42 | } else if (c == ':') { 43 | push(top()); 44 | } else if (c == '~') { 45 | push(~pop()); 46 | } else if (c == '.') { 47 | std::cout << (char) pop(); 48 | out = true; 49 | } else if (c == ',') { 50 | if (out) 51 | std::cout << std::endl; 52 | 53 | out = false; 54 | std::string input; 55 | std::cout << "Input: "; 56 | getline(std::cin, input); 57 | 58 | for (char ch : input) 59 | push(ch); 60 | } else if (c == ';') { 61 | std::string str = table[pop()]; 62 | exec(str); 63 | } else if (c == 'o') { 64 | for (int n = 0; n < stack.size(); n++) { 65 | int val = pop(); 66 | auto it = stack.begin(); 67 | stack.insert(it + n, val); 68 | } 69 | } else if (c == 'c') { 70 | if (stack.size() < 3) 71 | return EXIT_FAILURE; 72 | 73 | push(stack[stack.size() - 3]); 74 | stack.erase(stack.end() - 4); 75 | } else if (c == '(' 76 | || c == '[' 77 | || c == '{') { 78 | char add = c, 79 | sub = (c != '(') + c + 1; 80 | int start = k, 81 | match = 1; 82 | 83 | while (match != 0) { 84 | c = s[++k]; 85 | 86 | if (c == 0) 87 | return EXIT_FAILURE; 88 | else if (c == add) 89 | match++; 90 | else if (c == sub) 91 | match--; 92 | } 93 | 94 | std::string scope 95 | = s.substr(++start, k - start); 96 | 97 | if (add == '(' && top()) 98 | exec(scope); 99 | else if (add == '[') 100 | while (top()) 101 | exec(scope); 102 | else 103 | table[top()] = scope; 104 | } else { 105 | if (stack.size() < 2) 106 | return EXIT_FAILURE; 107 | 108 | int two = pop(); 109 | int one = pop(); 110 | 111 | switch (c) { 112 | case '+': 113 | push(one + two); 114 | break; 115 | case '-': 116 | push(one - two); 117 | break; 118 | case '*': 119 | push(one * two); 120 | break; 121 | case '/': 122 | if (two == 0) 123 | return EXIT_FAILURE; 124 | 125 | push(one / two); 126 | break; 127 | case '%': 128 | if (two == 0) 129 | return EXIT_FAILURE; 130 | 131 | push(one % two); 132 | break; 133 | case 'v': 134 | push(two); 135 | push(one); 136 | break; 137 | default: 138 | push(one); 139 | push(two); 140 | break; 141 | } 142 | } 143 | } 144 | 145 | return EXIT_SUCCESS; 146 | } 147 | 148 | int main(int argc, char* argv[]) { 149 | std::unordered_map 150 | table; 151 | std::vector stack; 152 | std::string prog; 153 | bool out = false; 154 | 155 | if (argc > 1) { 156 | std::ifstream file 157 | = std::ifstream(argv[1]); 158 | 159 | if (!file.is_open()) 160 | return EXIT_FAILURE; 161 | 162 | std::stringstream buffer; 163 | buffer << file.rdbuf(); 164 | prog = buffer.str(); 165 | file.close(); 166 | } else { 167 | return EXIT_FAILURE; 168 | } 169 | 170 | return run(prog, stack, table, out); 171 | } 172 | -------------------------------------------------------------------------------- /extra/c++/kak.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(int argc, char* argv[]) { 6 | if (argc == 1) 7 | return EXIT_FAILURE; 8 | 9 | std::ifstream file(argv[1]); 10 | std::vector tape = { 0 }; 11 | size_t ptr = 0; 12 | char c; 13 | 14 | if (!file.is_open()) 15 | return EXIT_FAILURE; 16 | 17 | do { 18 | while (file.get(c)) { 19 | if (c == '!') { 20 | if (++ptr == tape.size()) 21 | tape.push_back(0); 22 | tape[ptr] = !tape[ptr]; 23 | } else if (c == '?' && !tape[ptr]) { 24 | file.get(c); 25 | 26 | while (c != '!' && c != '?' && c != '<') { 27 | if (!file.get(c)) 28 | return EXIT_FAILURE; 29 | } 30 | } else if (c == '<' && ptr) { 31 | ptr--; 32 | } 33 | } 34 | 35 | for (bool b : tape) 36 | std::cout << b; 37 | std::cout << std::endl; 38 | 39 | file.clear(); 40 | file.seekg(0); 41 | } while (tape[ptr]); 42 | 43 | file.close(); 44 | return EXIT_SUCCESS; 45 | } 46 | -------------------------------------------------------------------------------- /extra/c++/painfuck.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | void prompt(bool& out) { 8 | if (out) { 9 | std::cout << std::endl; 10 | out = false; 11 | } 12 | 13 | std::cout << "Input: "; 14 | } 15 | 16 | char trans(char c, int& n) { 17 | std::vector arr = { "pevkjzwr", "yuctsobqihald" }; 18 | 19 | for (auto s : arr) { 20 | size_t p = s.find(c); 21 | 22 | if (p != std::string::npos) 23 | return s[(p + n++) % s.size()]; 24 | } 25 | 26 | return 0; 27 | } 28 | 29 | int main(int argc, char* argv[]) { 30 | srand((unsigned int) time(nullptr)); 31 | std::ifstream text; 32 | 33 | std::vector loop, tape = {0}; 34 | std::string inp, prog; 35 | int val, 36 | ptr = 0, 37 | ind = 0, 38 | rep = 1; 39 | bool line = false; 40 | char c; 41 | 42 | if (argc > 1) { 43 | text = std::ifstream(argv[1]); 44 | int n = 0; 45 | 46 | if (!text.is_open()) 47 | return EXIT_FAILURE; 48 | 49 | while (text.get(c)) 50 | if (c = trans(c, n)) 51 | prog += c; 52 | 53 | text.close(); 54 | } else { 55 | return EXIT_FAILURE; 56 | } 57 | 58 | while (c = prog[ind++]) { 59 | while (rep > 0) { 60 | rep--; 61 | 62 | switch (c) { 63 | case 'p': 64 | tape[ptr] += 2; 65 | break; 66 | case 's': 67 | tape[ptr] -= 1; 68 | break; 69 | case 'r': 70 | ptr += 2; 71 | 72 | while (ptr >= tape.size()) 73 | tape.push_back(0); 74 | 75 | break; 76 | case 'l': 77 | if (ptr) ptr--; 78 | break; 79 | case 'i': 80 | prompt(line); 81 | std::cin >> inp; 82 | tape[ptr] = stoi(inp); 83 | break; 84 | case 'j': 85 | prompt(line); 86 | tape[ptr] = getchar(); 87 | while ((c = getchar()) != '\n' 88 | && c != EOF); 89 | break; 90 | case 'o': 91 | std::cout << tape[ptr]; 92 | line = true; 93 | break; 94 | case 'u': 95 | std::cout << (char) tape[ptr]; 96 | line = true; 97 | break; 98 | case 'a': 99 | if (tape[ptr] != 0) { 100 | loop.push_back(ind - 1); 101 | } else { 102 | val = 1; 103 | while (val != 0) { 104 | if (!(c = prog[ind++])) 105 | break; 106 | 107 | if (c == 'a') 108 | val++; 109 | else if (c == 'b') 110 | val--; 111 | } 112 | } 113 | 114 | break; 115 | case 'b': 116 | ind = loop.back(); 117 | loop.pop_back(); 118 | break; 119 | case 'k': 120 | val = tape[ptr]; 121 | tape[ptr] = val * val; 122 | break; 123 | case 'z': 124 | tape[ptr] = 0; 125 | break; 126 | case 'h': 127 | tape[ptr] /= 2; 128 | break; 129 | case 'w': 130 | if (ptr + 1 != tape.size()) 131 | tape[ptr] = tape[ptr + 1]; 132 | else 133 | tape[ptr] = 0; 134 | break; 135 | case 'q': 136 | if (ptr) 137 | tape[ptr] = tape[ptr - 1]; 138 | break; 139 | case 'c': 140 | rep = 1; 141 | 142 | while (c == 'c') { 143 | c = prog[ind++]; 144 | rep *= 7; 145 | } 146 | 147 | break; 148 | case 'y': 149 | if (rand() % 2) 150 | c = prog[ind++]; 151 | break; 152 | case 'e': 153 | return EXIT_SUCCESS; 154 | case 'v': 155 | if (tape[ptr] != 0) 156 | c = prog[ind++]; 157 | break; 158 | case 'd': 159 | ptr = 0; 160 | break; 161 | case 't': 162 | val = ind; 163 | rep = 1; 164 | 165 | while (prog[--ind] == 't') 166 | rep *= 3; 167 | 168 | c = prog[ind]; 169 | ind = val; 170 | break; 171 | } 172 | } 173 | 174 | rep++; 175 | } 176 | 177 | return EXIT_SUCCESS; 178 | } 179 | -------------------------------------------------------------------------------- /extra/c++/trash.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | bool prime(int n) { 8 | if (n < 2) 9 | return false; 10 | 11 | for (int k = 2; k < sqrt(n) + 1; k++) 12 | if (n % k == 0) 13 | return false; 14 | 15 | return true; 16 | } 17 | 18 | int main(int argc, char* argv[]) { 19 | std::string prog; 20 | int val, num = 0; 21 | 22 | if (argc > 1) { 23 | std::ifstream file = 24 | std::ifstream(argv[1]); 25 | char c; 26 | 27 | if (!file.is_open()) 28 | return EXIT_FAILURE; 29 | 30 | while (file.get(c)) 31 | if (c == 't') 32 | num++; 33 | else if (isdigit(c)) 34 | break; 35 | 36 | if (file.eof()) 37 | return EXIT_FAILURE; 38 | 39 | file.seekg(-1, file.cur); 40 | std::stringstream buffer; 41 | buffer << file.rdbuf(); 42 | 43 | prog = buffer.str(); 44 | val = stoi(prog); 45 | file.close(); 46 | } else { 47 | return EXIT_FAILURE; 48 | } 49 | 50 | if (num) { 51 | if (prime(val)) { 52 | for (int k = 0; k < num; k++) 53 | while (!prime(++val)); 54 | 55 | std::cout << val 56 | << std::endl; 57 | } else { 58 | std::cout << 0 59 | << std::endl; 60 | } 61 | } 62 | 63 | return EXIT_SUCCESS; 64 | } 65 | -------------------------------------------------------------------------------- /extra/lean/74.lean: -------------------------------------------------------------------------------- 1 | import system.io 2 | open io 3 | 4 | def file_name : string := "test.txt" 5 | -- name of the file containing the BF-PDA program 6 | def limit : ℕ := 100 7 | -- max number of commands 8 | 9 | def read_file : io string := do 10 | f ← fs.read_file file_name, 11 | return f^.to_string 12 | 13 | 14 | def main : 15 | string.iterator → char → bool 16 | → ℕ → string → io unit 17 | | i c b 0 s := put_str s 18 | | i c ff (n + 1) s := 19 | if s.front = 'H' then put_str s 20 | else do 21 | let x := i.to_string.mk_iterator, 22 | main x x.curr tt n s 23 | | i '0' b (n + 1) s := 24 | main i.next i.next.curr i.has_next n ("0" ++ s) 25 | | i '1' b (n + 1) s := 26 | main i.next i.next.curr i.has_next n ("1" ++ s) 27 | | i 'H' b (n + 1) s := do 28 | let x := if s.front = '0' then "H" else "", 29 | main i.next i.next.curr i.has_next n (x ++ s) 30 | | i c b (n + 1) s := 31 | main i.next i.next.curr i.has_next n s 32 | 33 | 34 | #eval do 35 | c ← read_file, 36 | let i := string.mk_iterator c, 37 | main i i.curr tt limit "" 38 | -------------------------------------------------------------------------------- /extra/lean/albabet.lean: -------------------------------------------------------------------------------- 1 | import system.io 2 | open io 3 | 4 | def file_name : string := "test.txt" 5 | 6 | 7 | def read_file : io string := do 8 | f ← fs.read_file file_name, 9 | return f^.to_string 10 | 11 | 12 | def main : 13 | ℕ → string.iterator → char 14 | → ℕ → ℕ → string 15 | → io unit 16 | | 0 i c x y s := put_str s 17 | | (n + 1) i 'a' x y s := 18 | main n i.next i.next.curr (x + 1) y s 19 | | (n + 1) i 'b' x y s := 20 | main n i.next i.next.curr (x - 1) y s 21 | | (n + 1) i 'c' x y s := 22 | main n i.next i.next.curr 0 y s 23 | | (n + 1) i 'd' x y s := 24 | main n i.next i.next.curr 0 x s 25 | | (n + 1) i 'e' x y s := 26 | main n i.next i.next.curr x x s 27 | | (n + 1) i 'f' x y s := 28 | main n i.next i.next.curr x 0 s 29 | | (n + 1) i 'g' x y s := 30 | main n i.next i.next.curr (x * y) y s 31 | | (n + 1) i 'h' x y s := 32 | main n i.next i.next.curr (x * x) y s 33 | | (n + 1) i 'i' x y s := 34 | main n i.next i.next.curr x y 35 | (s ++ to_string (char.of_nat x)) 36 | | (n + 1) i c x y s := 37 | main n i.next i.next.curr x y s 38 | 39 | 40 | #eval do 41 | c ← read_file, 42 | let i := string.mk_iterator c, 43 | main (string.length c) i i.curr 0 0 "" 44 | -------------------------------------------------------------------------------- /extra/lean/bf-pda.lean: -------------------------------------------------------------------------------- 1 | import system.io 2 | open io 3 | 4 | def file_name : string := "test.txt" 5 | -- name of the file containing the BF-PDA program 6 | def limit : ℕ := 100 7 | -- max number of commands 8 | 9 | def read_file : io string := do 10 | f ← fs.read_file file_name, 11 | return f^.to_string 12 | 13 | 14 | def list.pop (l : list ℕ) := l.reverse.tail.reverse 15 | def list.top (l : list ℕ) := l.reverse.head 16 | 17 | 18 | def list.flip (l : list ℕ) := 19 | l.pop ++ [(l.top + 1) % 2] 20 | 21 | 22 | def find : 23 | string.iterator → string.iterator → char → bool 24 | → ℤ → ℕ → string.iterator 25 | | i j c b 0 n := 26 | if b then j else j.next.next 27 | | i j c b z 0 := i.next 28 | | i j c b z (n + 1) := do 29 | let x := if b then j.next else j.prev, 30 | let k := if c = '[' then z + 1 31 | else if c = ']' then z - 1 32 | else z, 33 | find i x x.curr b k n 34 | 35 | 36 | def main : 37 | string.iterator → char → ℕ → list ℕ 38 | → string → io unit 39 | | i c 0 l s := put_str s 40 | | i '@' (m + 1) l s := 41 | main i.next i.next.curr m l.flip s 42 | | i '.' (m + 1) l s := 43 | main i.next i.next.curr m l 44 | (s ++ (char.of_nat (48 + l.top)).to_string) 45 | | i '<' (m + 1) l s := 46 | main i.next i.next.curr m (l ++ [0]) s 47 | | i '>' (m + 1) l s := 48 | main i.next i.next.curr m l.pop s 49 | | i '[' (m + 1) l s := do 50 | let j := i.next, 51 | let x := if l.top = 0 then 52 | find i j j.curr tt 1 i.to_string.length 53 | else j, 54 | main x x.curr m l s 55 | | i ']' (m + 1) l s := do 56 | let j := i.prev, 57 | let x := if l.top = 1 then 58 | find i j j.curr ff (0 - 1) i.to_string.length 59 | else i.next, 60 | main x x.curr m l s 61 | | i c (m + 1) l s := do 62 | main i.next i.next.curr m l s 63 | 64 | 65 | #eval do 66 | c ← read_file, 67 | let i := string.mk_iterator c, 68 | main i i.curr limit [] "" 69 | -------------------------------------------------------------------------------- /extra/lean/excon.lean: -------------------------------------------------------------------------------- 1 | import system.io 2 | open io 3 | 4 | def file_name : string := "test.txt" 5 | -- name of the file containing the EXCON program 6 | 7 | 8 | def read_file : io string := do 9 | f ← fs.read_file file_name, 10 | return f^.to_string 11 | 12 | 13 | def empty_list := list.repeat 0 8 14 | 15 | 16 | def gets : list ℕ → ℕ → ℕ := 17 | λ l n, option.get_or_else (list.nth l n) 0 18 | 19 | 20 | def flips : list ℕ → ℕ → list ℕ := 21 | λ l n, list.update_nth l n (((gets l n) + 1) % 2) 22 | 23 | 24 | def to_s : list ℕ → ℕ → ℕ → string 25 | | l 0 m := to_string (char.of_nat m) 26 | | l (n + 1) m := 27 | to_s l n (m + (pow 2 (6 - n)) * (gets l (n + 1))) 28 | 29 | 30 | def main : 31 | ℕ → string.iterator → char 32 | → list ℕ → ℕ → string 33 | → io unit 34 | | 0 i c l n s := put_str s 35 | | (m + 1) i ':' l n s := 36 | main m i.next i.next.curr empty_list 7 s 37 | | (m + 1) i '^' l n s := 38 | main m i.next i.next.curr (flips l n) n s 39 | | (m + 1) i '!' l n s := 40 | main m i.next i.next.curr l n (s ++ to_s l 7 0) 41 | | (m + 1) i '<' l n s := 42 | main m i.next i.next.curr l ((n - 1) % 8) s 43 | | (m + 1) i c l n s := 44 | main m i.next i.next.curr l n s 45 | 46 | 47 | #eval do 48 | c ← read_file, 49 | let i := string.mk_iterator c, 50 | main (string.length c) i i.curr empty_list 7 "" 51 | -------------------------------------------------------------------------------- /extra/r/excon.r: -------------------------------------------------------------------------------- 1 | file <- commandArgs(trailingOnly=TRUE)[1] 2 | size <- file.info(file)$size 3 | strg <- readChar(file, size) 4 | syms <- strsplit(strg, "")[[1]] 5 | 6 | 7 | tape <- rep(c(0), times = 8) 8 | cell <- 8 9 | 10 | 11 | bin <- function(arr) { 12 | sum <- 0 13 | for (k in c(1:8)) 14 | if (tape[k]) 15 | sum <- sum + 2 ^ (8 - k) 16 | return(sum) 17 | } 18 | 19 | 20 | for (char in syms) { 21 | if (char == ":") { 22 | cell <- 8 23 | tape <- rep(c(0), times = 8) 24 | } else if (char == "^") { 25 | tape[cell] <- 1 - tape[cell] 26 | } else if (char == "!") { 27 | num <- bin(tape) 28 | cat(intToUtf8(num)) 29 | } else if (char == "<") { 30 | cell <- cell - 1 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /extra/ruby/3x.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | code = File.read(ARGV[0]) 4 | ARGV.clear 5 | line = '' 6 | ptr = [] 7 | stk = [] 8 | var = {} 9 | ind = 0 10 | var.default = 3 11 | 12 | def find(str, sym) 13 | num = 1 14 | while num.nonzero? 15 | case str[sym += 1] 16 | when '(' 17 | num += 1 18 | when ')' 19 | num -= 1 20 | end 21 | end 22 | sym 23 | end 24 | 25 | while (c = code[ind]) 26 | case c 27 | when '3' 28 | stk.push(3) 29 | when 'x' 30 | x = stk.pop 31 | y = stk.pop 32 | z = stk.pop 33 | n = (x - y) / z 34 | stk.push(n) 35 | when '?' 36 | print "#{line}Input: " 37 | stk.push(gets.to_i) 38 | line = '' 39 | when '!' 40 | print stk.pop 41 | line = 10.chr 42 | when 'v' 43 | n = stk.pop 44 | var[stk.pop] = n 45 | when '^' 46 | stk.push(var[stk.pop]) 47 | when '#' 48 | x = stk.pop 49 | y = stk.pop 50 | stk.push(x).push(y) 51 | when '(' 52 | if stk[-1].nonzero? 53 | ptr.push(ind) 54 | else 55 | ind = find(code, ind) 56 | end 57 | when ')' 58 | if stk[-1].nonzero? 59 | ind = ptr[-1] 60 | else 61 | ptr.pop 62 | end 63 | when '[' 64 | s = code[ind..-1] 65 | print s[/\[([^\]]*)\]/, 1] 66 | line = 10.chr 67 | end 68 | ind += 1 69 | end 70 | -------------------------------------------------------------------------------- /extra/ruby/74.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | code = File.read(ARGV[0]).chars 4 | data = '' 5 | 6 | loop do 7 | code.each do |c| 8 | case c 9 | when '0' 10 | data = "0#{data}" 11 | when '1' 12 | data = "1#{data}" 13 | when 'H' 14 | data.gsub!(/^0/, 'H0') 15 | end 16 | end 17 | break if data[0] == 'H' 18 | end 19 | 20 | print data 21 | -------------------------------------------------------------------------------- /extra/ruby/bit.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | code = File.read(ARGV[0]) 4 | tape = [cell = i = 0] * 8 5 | line = '' 6 | ARGV.clear 7 | 8 | def find(str, sym, dir) 9 | num = dir 10 | while num.nonzero? 11 | case str[sym += dir] 12 | when '{' 13 | num += 1 14 | when '}' 15 | num -= 1 16 | end 17 | end 18 | sym 19 | end 20 | 21 | while (c = code[i]) 22 | case c 23 | when '~' 24 | tape[cell] ^= 1 25 | when '>' 26 | cond = cell + 8 > tape.size 27 | tape.push(0) if cond 28 | cell += 1 29 | when '<' 30 | cell -= 1 if cell.nonzero? 31 | when ')' 32 | print "#{line}Input: " 33 | val = '0' * 8 + gets[0].ord.to_s(2) 34 | tape[cell..cell + 7] = 35 | val[-8..-1].chars.map(&:to_i) 36 | line = '' 37 | when '(' 38 | val = tape[cell..cell + 7] 39 | print val.join.to_i(2).chr 40 | line = 10.chr 41 | when '{' 42 | i = find(code, i, 1) \ 43 | if tape[cell].zero? 44 | when '}' 45 | i = find(code, i, -1) \ 46 | unless tape[cell].zero? 47 | end 48 | i += 1 49 | end 50 | -------------------------------------------------------------------------------- /extra/ruby/unsquare.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | code = File.read(ARGV[0]) 4 | ARGV.clear 5 | 6 | ind = acc = 0 7 | line = '' 8 | ptr = [] 9 | stk = [] 10 | 11 | def find(str, sym) 12 | num = 1 13 | while num.nonzero? 14 | case str[sym += 1] 15 | when '>' 16 | num += 1 17 | when '<' 18 | num -= 1 19 | end 20 | end 21 | sym 22 | end 23 | 24 | while (c = code[ind]) 25 | case c 26 | when 'O' 27 | stk.push(0) 28 | when 'I' 29 | stk.push(1) 30 | when 'A' 31 | acc = stk.pop 32 | when 'S' 33 | x = stk.pop 34 | y = stk.pop 35 | stk.push(x).push(y) 36 | when '+' 37 | acc += 2 38 | when '-' 39 | acc -= 2 40 | when 'x' 41 | acc *= 2 42 | when 'P' 43 | stk.push(acc) 44 | when 'o' 45 | print stk[-1].chr 46 | line = 10.chr 47 | when 'i' 48 | print "#{line}Input: " 49 | stk.push(gets[0].ord) 50 | line = '' 51 | when '>' 52 | if acc.nonzero? && (acc != 1) 53 | ptr.push(ind) 54 | else 55 | ind = find(code, ind) 56 | end 57 | when '<' 58 | if acc.nonzero? && (acc != 1) 59 | ind = ptr[-1] 60 | else 61 | ptr.pop 62 | end 63 | end 64 | ind += 1 65 | end 66 | -------------------------------------------------------------------------------- /extra/rust/laserfuck.rs: -------------------------------------------------------------------------------- 1 | use rand::Rng; 2 | use std::env; 3 | use std::fs; 4 | use std::io::{ 5 | self, Write, BufRead 6 | }; 7 | 8 | struct Laser( 9 | usize, usize, usize); 10 | 11 | fn wrap(lsr: &mut Laser, 12 | len: usize) { 13 | let Laser(a, b, d) = lsr; 14 | 15 | if let (0, _, 0) 16 | | (_, 0, 2) 17 | = (*a, *b, *d) { 18 | *a = len; 19 | return; 20 | } 21 | 22 | match d { 23 | 0 => *a -= 1, 24 | 1 => *a += 1, 25 | 2 => *b -= 1, 26 | 3 => *b += 1, 27 | _ => () 28 | } 29 | } 30 | 31 | fn run(text: Vec>) { 32 | let mut rng 33 | = rand::thread_rng(); 34 | let mut lsrs = Vec::new(); 35 | let mut jmp = false; 36 | let mut ind = 0; 37 | let mut ptr = 0; 38 | 39 | let len = text.len(); 40 | let new = (0, false); 41 | let mut tape = vec![new]; 42 | 43 | for (k, v) in text.iter() 44 | .enumerate() { 45 | if let Some(n) = v.iter() 46 | .position(|&c| c == 'o') { 47 | if lsrs.len() > 0 { 48 | return; 49 | } else { 50 | let num 51 | = rng.gen_range(0..4); 52 | let lsr = Laser(k, n, num); 53 | lsrs.push(lsr); 54 | } 55 | } 56 | } 57 | 58 | while lsrs.len() > 0 { 59 | wrap(&mut lsrs[ind], len); 60 | let Laser(x, y, mut m) 61 | = lsrs[ind]; 62 | 63 | if jmp { 64 | jmp = false; 65 | continue; 66 | } 67 | 68 | let get = text.get(x) 69 | .and_then(|k| k.get(y)); 70 | let op = if let Some(c) = get 71 | {*c} else {'x'}; 72 | 73 | match op { 74 | '>' => { 75 | ptr += 1; 76 | 77 | if ptr == tape.len() { 78 | tape.push(new); 79 | } 80 | } 81 | '<' => { 82 | if ptr > 0 { 83 | ptr -= 1; 84 | } else { 85 | tape.insert(0, new); 86 | } 87 | } 88 | ',' => { 89 | let mut val 90 | = String::new(); 91 | print!("Input: "); 92 | 93 | io::stdout() 94 | .flush() 95 | .unwrap(); 96 | 97 | io::stdin() 98 | .read_line(&mut val) 99 | .unwrap(); 100 | 101 | tape[ptr].0 102 | = if let "\r\n" | "\n" 103 | = &*val { 104 | 0 105 | } else { 106 | val.chars() 107 | .next() 108 | .unwrap() 109 | as i32 110 | }; 111 | } 112 | 'x' => { 113 | lsrs.remove(ind); 114 | continue; 115 | } 116 | '*' => { 117 | let n = rng.gen_range(0..2); 118 | let d = 2 * (1 - m / 2) + n; 119 | lsrs.push(Laser(x, y, d)); 120 | } 121 | '_' | '(' => { 122 | if m < 2 && 123 | (tape[ptr].0 != 0 124 | || op == '_') { 125 | m = 1 - m; 126 | } 127 | } 128 | '|' | ')' => { 129 | if m > 1 && 130 | (tape[ptr].0 != 0 131 | || op == '|') { 132 | m = 5 - m; 133 | } 134 | } 135 | '/' => m = 3 - m, 136 | '^' | 'v' | '{' | '}' 137 | => m = "^v{}" 138 | .find(op) 139 | .unwrap(), 140 | '\\' => m = (m + 2) % 4, 141 | '+' => tape[ptr].0 += 1, 142 | '-' => tape[ptr].0 -= 1, 143 | '#' => jmp = true, 144 | _ => () 145 | } 146 | 147 | if let ',' | '+' | '-' = op { 148 | tape[ptr].1 = true; 149 | } 150 | 151 | lsrs[ind].2 = m; 152 | ind = (ind + 1) 153 | % lsrs.len(); 154 | } 155 | 156 | let out = text.len() > 0 157 | && text[0].len() > 0 158 | && text[0][0] == '\u{FF}'; 159 | let mut post = false; 160 | 161 | for c in tape.iter() { 162 | if c.1 && c.0 >= 0 { 163 | if out { 164 | if let Some(val) 165 | = char::from_u32( 166 | c.0 as u32) { 167 | print!("{}", val); 168 | continue; 169 | } 170 | } else { 171 | if post { 172 | print!("\n"); 173 | } else { 174 | post = true; 175 | } 176 | 177 | print!("{}", c.0); 178 | } 179 | } 180 | } 181 | } 182 | 183 | fn main() { 184 | let args: Vec 185 | = env::args().collect(); 186 | let file = fs::File::open(&args[1]) 187 | .expect("invalid file"); 188 | 189 | let buff = io::BufReader::new(file); 190 | let clct = |s: Result| s 191 | .unwrap() 192 | .chars() 193 | .collect(); 194 | let mut text: Vec> 195 | = buff.lines() 196 | .map(clct) 197 | .collect(); 198 | 199 | let max = text 200 | .iter() 201 | .map(|x| x.len()) 202 | .fold(0, |x, y| x.max(y)); 203 | 204 | for v in text.iter_mut() { 205 | while v.len() < max { 206 | v.push(' '); 207 | } 208 | } 209 | 210 | run(text); 211 | } 212 | -------------------------------------------------------------------------------- /extra/rust/unsquare.rs: -------------------------------------------------------------------------------- 1 | use std::io::{self, Write}; 2 | use ::core::fmt::Display; 3 | use std::char; 4 | use std::env; 5 | use std::fs; 6 | 7 | fn print(value: T) { 8 | print!("{}", value); 9 | io::stdout() 10 | .flush() 11 | .unwrap(); 12 | } 13 | 14 | fn run(text: Vec) { 15 | let mut stk = Vec::new(); 16 | let mut jmp = Vec::new(); 17 | let mut out = false; 18 | let mut ind = 0; 19 | let mut acc = 0; 20 | 21 | while ind < text.len() { 22 | match text[ind] { 23 | 'O' => stk.push(0), 24 | 'I' => stk.push(1), 25 | 'A' => acc = stk.pop() 26 | .expect("empty stack"), 27 | 'S' => { 28 | let n = stk.len(); 29 | stk.swap(n - 1, n - 2); 30 | } 31 | '+' => acc += 2, 32 | '-' => acc -= 2, 33 | 'x' => acc *= 2, 34 | 'P' => stk.push(acc), 35 | 'o' => { 36 | out = true; 37 | let val = stk.last() 38 | .expect("empty stack"); 39 | 40 | if let Some(c) 41 | = char::from_u32( 42 | *val as u32) { 43 | print(c); 44 | } else { 45 | print(val); 46 | } 47 | } 48 | 'i' => { 49 | let mut val = String::new(); 50 | 51 | if out { 52 | print('\n'); 53 | out = false; 54 | } 55 | 56 | while val.trim() == "" { 57 | print("Input: "); 58 | io::stdin() 59 | .read_line(&mut val) 60 | .unwrap(); 61 | } 62 | 63 | stk.push( 64 | val.chars() 65 | .next() 66 | .unwrap() 67 | as i128 68 | ); 69 | } 70 | '>' => { 71 | if acc == 0 || acc == 1 { 72 | let mut num = 1; 73 | 74 | while num > 0 { 75 | ind += 1; 76 | 77 | match text[ind] { 78 | '>' => num += 1, 79 | '<' => num -= 1, 80 | _ => () 81 | } 82 | } 83 | } else { 84 | jmp.push(ind - 1); 85 | } 86 | } 87 | '<' => ind = jmp.pop() 88 | .expect("missing bracket"), 89 | _ => () 90 | } 91 | 92 | ind += 1; 93 | } 94 | } 95 | 96 | fn main() { 97 | let args: Vec 98 | = env::args().collect(); 99 | let text = fs::read_to_string(&args[1]) 100 | .expect("invalid file") 101 | .chars() 102 | .collect(); 103 | 104 | run(text); 105 | } 106 | -------------------------------------------------------------------------------- /miscellaneous/ascii-art.txt: -------------------------------------------------------------------------------- 1 | #########################################Invalid characters are ignored.######################################### 2 | bf = '++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.' 3 | print(bf.replace('>', '\\\n \\\n /\n/\n\n') 4 | .replace('<', ' /\n/\n\\\n \\\n\n') 5 | .replace('-', '-----\n\n') 6 | .replace('+', ' |\n |\n-----\n |\n |\n\n') 7 | .replace('[', ' ___\n|\n|\n|\n|\n|___\n\n') 8 | .replace(']', '___\n |\n |\n |\n |\n___|\n\n') 9 | .replace('.', '##\n##\n\n') 10 | .replace(',', '##\n##\n_|\n\n')) # Python code for converting brainfuck to ASCII art. 11 | #########################################Invalid characters are ignored.######################################### 12 | 13 | | 14 | | 15 | ----- 16 | | 17 | | 18 | 19 | | 20 | | 21 | ----- 22 | | 23 | | 24 | 25 | | 26 | | 27 | ----- 28 | | 29 | | 30 | 31 | | 32 | | 33 | ----- 34 | | 35 | | 36 | 37 | | 38 | | 39 | ----- 40 | | 41 | | 42 | 43 | | 44 | | 45 | ----- 46 | | 47 | | 48 | 49 | | 50 | | 51 | ----- 52 | | 53 | | 54 | 55 | | 56 | | 57 | ----- 58 | | 59 | | 60 | 61 | ___ 62 | | 63 | | 64 | | 65 | | 66 | |___ 67 | 68 | \ 69 | \ 70 | / 71 | / 72 | 73 | | 74 | | 75 | ----- 76 | | 77 | | 78 | 79 | | 80 | | 81 | ----- 82 | | 83 | | 84 | 85 | | 86 | | 87 | ----- 88 | | 89 | | 90 | 91 | | 92 | | 93 | ----- 94 | | 95 | | 96 | 97 | ___ 98 | | 99 | | 100 | | 101 | | 102 | |___ 103 | 104 | \ 105 | \ 106 | / 107 | / 108 | 109 | | 110 | | 111 | ----- 112 | | 113 | | 114 | 115 | | 116 | | 117 | ----- 118 | | 119 | | 120 | 121 | \ 122 | \ 123 | / 124 | / 125 | 126 | | 127 | | 128 | ----- 129 | | 130 | | 131 | 132 | | 133 | | 134 | ----- 135 | | 136 | | 137 | 138 | | 139 | | 140 | ----- 141 | | 142 | | 143 | 144 | \ 145 | \ 146 | / 147 | / 148 | 149 | | 150 | | 151 | ----- 152 | | 153 | | 154 | 155 | | 156 | | 157 | ----- 158 | | 159 | | 160 | 161 | | 162 | | 163 | ----- 164 | | 165 | | 166 | 167 | \ 168 | \ 169 | / 170 | / 171 | 172 | | 173 | | 174 | ----- 175 | | 176 | | 177 | 178 | / 179 | / 180 | \ 181 | \ 182 | 183 | / 184 | / 185 | \ 186 | \ 187 | 188 | / 189 | / 190 | \ 191 | \ 192 | 193 | / 194 | / 195 | \ 196 | \ 197 | 198 | ----- 199 | 200 | ___ 201 | | 202 | | 203 | | 204 | | 205 | ___| 206 | 207 | \ 208 | \ 209 | / 210 | / 211 | 212 | | 213 | | 214 | ----- 215 | | 216 | | 217 | 218 | \ 219 | \ 220 | / 221 | / 222 | 223 | | 224 | | 225 | ----- 226 | | 227 | | 228 | 229 | \ 230 | \ 231 | / 232 | / 233 | 234 | ----- 235 | 236 | \ 237 | \ 238 | / 239 | / 240 | 241 | \ 242 | \ 243 | / 244 | / 245 | 246 | | 247 | | 248 | ----- 249 | | 250 | | 251 | 252 | ___ 253 | | 254 | | 255 | | 256 | | 257 | |___ 258 | 259 | / 260 | / 261 | \ 262 | \ 263 | 264 | ___ 265 | | 266 | | 267 | | 268 | | 269 | ___| 270 | 271 | / 272 | / 273 | \ 274 | \ 275 | 276 | ----- 277 | 278 | ___ 279 | | 280 | | 281 | | 282 | | 283 | ___| 284 | 285 | \ 286 | \ 287 | / 288 | / 289 | 290 | \ 291 | \ 292 | / 293 | / 294 | 295 | ## 296 | ## 297 | 298 | \ 299 | \ 300 | / 301 | / 302 | 303 | ----- 304 | 305 | ----- 306 | 307 | ----- 308 | 309 | ## 310 | ## 311 | 312 | | 313 | | 314 | ----- 315 | | 316 | | 317 | 318 | | 319 | | 320 | ----- 321 | | 322 | | 323 | 324 | | 325 | | 326 | ----- 327 | | 328 | | 329 | 330 | | 331 | | 332 | ----- 333 | | 334 | | 335 | 336 | | 337 | | 338 | ----- 339 | | 340 | | 341 | 342 | | 343 | | 344 | ----- 345 | | 346 | | 347 | 348 | | 349 | | 350 | ----- 351 | | 352 | | 353 | 354 | ## 355 | ## 356 | 357 | ## 358 | ## 359 | 360 | | 361 | | 362 | ----- 363 | | 364 | | 365 | 366 | | 367 | | 368 | ----- 369 | | 370 | | 371 | 372 | | 373 | | 374 | ----- 375 | | 376 | | 377 | 378 | ## 379 | ## 380 | 381 | \ 382 | \ 383 | / 384 | / 385 | 386 | \ 387 | \ 388 | / 389 | / 390 | 391 | ## 392 | ## 393 | 394 | / 395 | / 396 | \ 397 | \ 398 | 399 | ----- 400 | 401 | ## 402 | ## 403 | 404 | / 405 | / 406 | \ 407 | \ 408 | 409 | ## 410 | ## 411 | 412 | | 413 | | 414 | ----- 415 | | 416 | | 417 | 418 | | 419 | | 420 | ----- 421 | | 422 | | 423 | 424 | | 425 | | 426 | ----- 427 | | 428 | | 429 | 430 | ## 431 | ## 432 | 433 | ----- 434 | 435 | ----- 436 | 437 | ----- 438 | 439 | ----- 440 | 441 | ----- 442 | 443 | ----- 444 | 445 | ## 446 | ## 447 | 448 | ----- 449 | 450 | ----- 451 | 452 | ----- 453 | 454 | ----- 455 | 456 | ----- 457 | 458 | ----- 459 | 460 | ----- 461 | 462 | ----- 463 | 464 | ## 465 | ## 466 | 467 | \ 468 | \ 469 | / 470 | / 471 | 472 | \ 473 | \ 474 | / 475 | / 476 | 477 | | 478 | | 479 | ----- 480 | | 481 | | 482 | 483 | ## 484 | ## 485 | 486 | \ 487 | \ 488 | / 489 | / 490 | 491 | | 492 | | 493 | ----- 494 | | 495 | | 496 | 497 | | 498 | | 499 | ----- 500 | | 501 | | 502 | 503 | ## 504 | ## -------------------------------------------------------------------------------- /miscellaneous/binary.py: -------------------------------------------------------------------------------- 1 | from inspect import signature 2 | 3 | 4 | def convert(func): 5 | num = len(signature(func).parameters) 6 | total = 2 ** (num + 1) - 1 7 | lines = ['' for _ in range(total)] 8 | pos = [total // 2] 9 | 10 | for j in range(num + 1): 11 | if j < num: 12 | for k in range(total): 13 | if k in pos: 14 | lines[k] += '>2$~;#@' 15 | else: 16 | lines[k] += ' ' * 7 17 | pos = [i + 2 ** (num - j - 1) for i in pos] + \ 18 | [i - 2 ** (num - j - 1) for i in pos] 19 | for k in pos: 20 | lines[k] = lines[k][:-2] + '> ' 21 | else: 22 | for k in range(2 ** num): 23 | arg_list = [0] * num + [int(i) for i in bin(k)[2:]] 24 | lines[k * 2] += f'>$3{func(*arg_list[-num:])}:@' 25 | 26 | lines[0] = '\'' + lines[0][1:] 27 | return '\n'.join(k for k in lines).replace('> >', '> ') 28 | -------------------------------------------------------------------------------- /miscellaneous/generate.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | 4 | def bfstack(text): 5 | res = '>\n' 6 | acc = 0 7 | 8 | for c in text: 9 | n = ord(c) - acc 10 | if abs(n) < ord(c) + 3: 11 | o = '+' if n > 0 else '-' 12 | res += o * abs(n) + '.\n' 13 | else: 14 | o = "+" * ord(c) 15 | res += f'[-]{o}.\n' 16 | acc = ord(c) 17 | 18 | return res 19 | 20 | 21 | def brainif(text): 22 | res = '' 23 | acc = 0 24 | 25 | for c in text: 26 | if (n := ord(c)) < acc: 27 | res += f'\nif {acc} move right\n' 28 | for k in range(n): 29 | res += f'if {k} increment\n' 30 | res += f'if {n} output\n' 31 | else: 32 | res += '\n' 33 | for k in range(acc, n): 34 | res += f'if {k} increment\n' 35 | res += f'if {n} output\n' 36 | acc = ord(c) 37 | 38 | return res.strip() 39 | 40 | 41 | def container(text): 42 | ind = last = 0 43 | if text: 44 | res = ('A:\n' 45 | '+1 EXIT>=1\n\n' 46 | 'PRINT:\n' 47 | '+1 PRINT<=0\n' 48 | '-1 PRINT>=1\n\n' 49 | 'OUT:\n') 50 | else: 51 | return ('EXIT=1:\n' 52 | '-1 EXIT>=0') 53 | 54 | for c in text: 55 | if (o := ord(c) - last) >= 0: 56 | res += (f'+{o} A>={ind}\n' 57 | f'-{o} A>={ind + 1}\n') 58 | else: 59 | res += (f'-{-o} A>={ind}\n' 60 | f'+{-o} A>={ind + 1}\n') 61 | last = ord(c) 62 | ind += 2 63 | 64 | res += ('EXIT=1:\n' 65 | f'-1 A>={ind - 2}') 66 | 67 | return res 68 | 69 | 70 | def forth(text): 71 | s = "0123456789ABCDEF" 72 | text = text[::-1] 73 | res = '' 74 | 75 | for c in text: 76 | o = ord(c) 77 | n = int(math.log(o, 15)) 78 | m = o // (15 ** n) 79 | p = o - m * 15 ** n 80 | 81 | res += (n * 'F' 82 | + (n - 1) * '*' 83 | + s[m] + '*' 84 | + s[p] + '+') 85 | return f'0{res}[.]' 86 | 87 | 88 | def laserfuck(text): 89 | data = [*map(ord, text)] 90 | code = '' 91 | 92 | def get(m): 93 | s = '' 94 | 95 | for n in data: 96 | q = n // m 97 | s += '>' + '+' * q 98 | 99 | return s.rstrip('>') 100 | 101 | while True: 102 | top = max(data) 103 | sqr = int(math.sqrt(top)) 104 | end = get(1) 105 | 106 | if not top: 107 | break 108 | 109 | if all(data): 110 | sqr = min([sqr, *data]) 111 | 112 | ops = get(sqr) 113 | bck = ops.count('>') 114 | 115 | if bck < 11: 116 | ops += bck * '<' 117 | else: 118 | ops += '[<]>' 119 | 120 | ops = '+' * sqr \ 121 | + f'[{ops}-]' 122 | 123 | diff = ( 124 | len(end) - len(ops) 125 | - ops.count('[') * 7 126 | ) 127 | 128 | if diff < 0: 129 | break 130 | 131 | code += ops 132 | data = [ 133 | k % sqr for 134 | k in data 135 | ] 136 | 137 | if '[' not in code: 138 | return f'\xFF}}}}{end}\n|o^\n _ ' 139 | 140 | loop = re.search(r'\[([^[\]]*)', code)[1] 141 | code = code.replace(loop, '', 1) 142 | code = code.replace('[]', '[}]') 143 | frst = code.find('[') + 8 144 | 145 | num = 0 146 | res = [ 147 | ' }}', 148 | '|o^', 149 | ' _ ' 150 | ] 151 | 152 | for c in code: 153 | if c == '[': 154 | top = 'v } }' 155 | bot = '}#^)#^' 156 | num += 1 157 | elif c == ']': 158 | top = '#/)' 159 | bot = ' / ' 160 | else: 161 | top = c 162 | bot = ' ' 163 | 164 | k = 2 - (num == 2) 165 | res[0] += top 166 | res[3 - k] += bot 167 | res[k] += len(top) * ' ' 168 | 169 | if c == ']': 170 | num -= 1 171 | 172 | search = re.search( 173 | '} }v?', res[0])[0] 174 | spaces = ' ' * len(search) 175 | res[0] = res[0].replace( 176 | search, spaces, 1) 177 | 178 | rest = len(loop) + frst 179 | over = len(end) + frst 180 | over -= len(res[0]) - 2 181 | half = (max(over, 0) // 2) + 1 182 | 183 | if end: 184 | res[0] += end[:half] + '^' 185 | end = end[half:] 186 | end = f'x{end[::-1]}{{' 187 | 188 | end = end.rjust(len(res[0])) 189 | res.insert(0, end) 190 | else: 191 | res[0] += 'x' 192 | 193 | size = len(res[0]) 194 | cntr = 2 195 | 196 | while (rest // cntr) > size: 197 | cntr += 1 198 | 199 | cntr += cntr % 2 200 | botm = (rest // cntr) + 1 201 | lnth = botm + 1 202 | 203 | res.insert(0, 204 | f'\xFF}}{loop[:lnth]}v') 205 | 206 | for k in range(cntr - 1): 207 | part = loop[lnth:lnth + botm] 208 | lnth += botm 209 | 210 | if not k % 2: 211 | part = part[::-1] 212 | move = 'v{}{{' 213 | else: 214 | move = '}}{}v' 215 | 216 | part = part.rjust(botm) 217 | part = move.format(part) 218 | res.insert(k + 1, ' ' + part) 219 | 220 | num = ' ' * (frst - 5) 221 | beg = f' ^{num}{{ {{' 222 | 223 | cntr -= 1 224 | res[cntr] = res[cntr].replace( 225 | ' v' + ' ' * frst, 226 | beg + 'v ' 227 | ) 228 | 229 | return '\n'.join(res) 230 | 231 | 232 | def magnitude(text): 233 | def close(val, start): 234 | if start > val: 235 | return 0 236 | 237 | while start <= val: 238 | start *= 2 239 | 240 | return start // 2 241 | 242 | mode = True 243 | prog = '' 244 | last = 0 245 | 246 | for c in text: 247 | n = ord(c) - last 248 | 249 | if abs(n) > ord(c): 250 | prog += '\'' 251 | mode = True 252 | last = 0 253 | n = ord(c) 254 | 255 | if n and mode == (n < 0) and last: 256 | prog += 'p' 257 | mode ^= 1 258 | 259 | n = abs(n) 260 | 261 | if not last: 262 | x = close(n, 2) 263 | y = close(n, 3) 264 | 265 | if n - x < n - y: 266 | num = int(math.log(x // 2, 2)) 267 | prog += 's' + num * 'm' 268 | n -= x 269 | elif y: 270 | num = int(math.log(y // 3, 2)) 271 | prog += 'i' + num * 'm' 272 | n -= y 273 | 274 | if n == 1: 275 | prog += 'ips' 276 | mode ^= 1 277 | elif n > 2: 278 | prog += (n // 3) * 'i' 279 | n = n % 3 280 | 281 | if n % 3 == 1: 282 | prog = prog[:-1] 283 | n += 3 284 | 285 | prog += (n // 2) * 's' \ 286 | + mode * 'p' + 'e' 287 | last = ord(c) 288 | mode = False 289 | 290 | return prog 291 | 292 | 293 | def painfuck(text): 294 | def add(val): 295 | return (val // 2) * 'p' \ 296 | + (val % 2) * 'ps' 297 | 298 | def close(val, s, op): 299 | if val > 7: 300 | pwr = int(math.log(val, 7)) 301 | s += pwr * 'c' + op 302 | val -= 7 ** pwr 303 | if val: 304 | pwr = 1 305 | while 2 * val >= (3 ** pwr - 1): 306 | pwr += 1 307 | 308 | s += op + (pwr - 2) * 't' 309 | val -= (3 ** (pwr - 1) - 1) // 2 310 | 311 | return val, s 312 | 313 | def loop(val, s, op): 314 | sqr = int(math.sqrt(val)) 315 | 316 | if sqr > 3: 317 | move = ('rl', 'lr')['r' in res] 318 | s += move + add(sqr) + 'al' 319 | 320 | if op == 'p': 321 | s += add(sqr) 322 | else: 323 | s += sqr * 's' 324 | 325 | s += move + 'sbl' 326 | val -= sqr ** 2 327 | 328 | return val, s 329 | 330 | res = '' 331 | last = 0 332 | 333 | for c in text: 334 | n = ord(c) - last 335 | 336 | if abs(n) > ord(c): 337 | res += ('rl', 'lr')['r' in res] 338 | n = ord(c) 339 | 340 | if n > 0: 341 | if n % 2: 342 | res += 'ps' 343 | 344 | n, res = close(n // 2, res, 'p') 345 | n, res = loop(n * 2, res, 'p') 346 | res += add(n) 347 | elif n < 0: 348 | n = abs(n) 349 | n, res = close(n, res, 's') 350 | n, res = loop(n, res, 's') 351 | res += n * 's' 352 | 353 | last = ord(c) 354 | res += 'u' 355 | 356 | cyc = ['rwzjkvep', 'dlahiqbostcuy'] 357 | text = res 358 | res = '' 359 | 360 | for k in range(len(text)): 361 | for c in cyc: 362 | if text[k] in c: 363 | n = c.find(text[k]) 364 | res += c[(n + k) % len(c)] 365 | 366 | return res 367 | 368 | 369 | def suffolk(text): 370 | res = '' 371 | num = 0 372 | 373 | for c in text: 374 | n = ord(c) + 1 375 | a = int(n ** 0.5) 376 | b = (n // a) * '<' 377 | c = (n % a) * '>!' 378 | if a > num: 379 | num = a 380 | res += (f'{a * "!"}{c}' 381 | f'><{b}.!>><>!\n') 382 | 383 | return ('>>!' * num 384 | + '\n' + res[-1:]) 385 | 386 | 387 | def _123(text): 388 | res = '' 389 | last = 0 390 | 391 | for c in text: 392 | b = (bin(ord(c) ^ last)[2:] 393 | .zfill(8) 394 | .rstrip('0')) 395 | s = (b.replace('0', '2') 396 | .replace('1', '122')) 397 | 398 | if n := len(b): 399 | res += (f'{s[:-2]}' 400 | f'{"121" * n}'[:-1] 401 | + '\n') 402 | else: 403 | res += '12112\n' 404 | last = ord(c) 405 | 406 | return res + '1' 407 | -------------------------------------------------------------------------------- /miscellaneous/laserfuck.txt: -------------------------------------------------------------------------------- 1 | ÿ}>+++++++>++++++++++>++++++++++>++++++++++>+++++++++++>++++>+++>++++++++v 2 | ^ { {v+++>++++++++++>++++++++++>+++++++++++>+++++++++++>+++{ 3 | x++>+>+>>++>+>+>++>++>+>++>{ 4 | }}++++++++++v } }<#/)>-#/)+++v } }>>>++>++>>+>>+++>>+>++>>+v } }<#/)>-#/)^ 5 | |o^ }#^)#^ / }#^)#^ / 6 | _ }#^)#^ / }#^)#^ / 7 | -------------------------------------------------------------------------------- /miscellaneous/lightlang.txt: -------------------------------------------------------------------------------- 1 | This code prints 'Hello, world' (with an exclamation mark) 2 | as the concatenation of 7 bit strings corresponding to each 3 | character's ASCII value. It ignores invalid characters. 4 | 5 | code | letter | ASCII 6 | --------------------------------- 7 | ^!^!!^!^!!! | H | 100 1000 8 | ^!!^!!^!^!^! | e | 110 0101 9 | !!^!^!!^!! | l | 110 1100 10 | ^!!^!^!!^!! | l | 110 1100 11 | ^!!^!^!!!! | o | 110 1111 12 | ^!^!^!^!!^!! | , | 010 1100 13 | !^!^!!!!! | | 010 0000 14 | ^!!!^!^!!! | w | 111 0111 15 | !!^!^!!!! | o | 110 1111 16 | !!!^!!^!^! | r | 111 0010 17 | ^!!^!^!!^!! | l | 110 1100 18 | ^!!^!!^!^!! | d | 110 0100 19 | !^!^!!!!^! # ! | 010 0001 -------------------------------------------------------------------------------- /miscellaneous/mammalian.txt: -------------------------------------------------------------------------------- 1 | ######### H ######### 2 | SEED 3 | SPRINT 4 | SPRINT 5 | SPRINT 6 | SEED 7 | SEED 8 | SEED 9 | SEED 10 | SEED 11 | SEED 12 | SEED 13 | SEED 14 | DIGEST 15 | PRONOUNCE 16 | 17 | ######### e ######### 18 | EXCRETE 19 | CONFLAGRATE 20 | SPRINT 21 | SPRINT 22 | SPRINT 23 | SPRINT 24 | SEED 25 | SEED 26 | SEED 27 | SEED 28 | DIGEST 29 | PRONOUNCE 30 | 31 | ######### ll ######### 32 | EXCRETE 33 | SPRINT 34 | SPRINT 35 | SPRINT 36 | SPRINT 37 | SPRINT 38 | SPRINT 39 | SEED 40 | SEED 41 | SEED 42 | SEED 43 | SEED 44 | DIGEST 45 | PRONOUNCE 46 | PRONOUNCE 47 | 48 | ######### o ######### 49 | EXCRETE 50 | CONFLAGRATE 51 | CONFLAGRATE 52 | CONSUME 53 | EXCRETE 54 | CONSUME 55 | PRONOUNCE 56 | 57 | ######### , ######### 58 | EXCRETE 59 | SPRINT 60 | FISSION 61 | FISSION 62 | CONSUME 63 | FISSION 64 | CONSUME 65 | FISSION 66 | CONSUME 67 | FISSION 68 | CONSUME 69 | FISSION 70 | EXCRETE 71 | SPRINT 72 | EXCRETE 73 | CONSUME 74 | SEED 75 | SEED 76 | SEED 77 | SEED 78 | SEED 79 | SEED 80 | SEED 81 | SEED 82 | SEED 83 | SEED 84 | SEED 85 | CONSUME 86 | PRONOUNCE 87 | 88 | ######### [ ] ######### 89 | EXCRETE 90 | CONFLAGRATE 91 | SPRINT 92 | FISSION 93 | FISSION 94 | CONSUME 95 | CONSUME 96 | EXCRETE 97 | DIGEST 98 | PRONOUNCE 99 | 100 | ######### w ######### 101 | CONFLAGRATE 102 | CONFLAGRATE 103 | CONSUME 104 | EXCRETE 105 | SPRINT 106 | FISSION 107 | SPRINT 108 | CONSUME 109 | CONSUME 110 | EXCRETE 111 | CONSUME 112 | CONSUME 113 | EXCRETE 114 | SPRINT 115 | DIGEST 116 | EXCRETE 117 | SEED 118 | SEED 119 | CONSUME 120 | PRONOUNCE 121 | 122 | ######### o ######### 123 | CONSUME 124 | PRONOUNCE 125 | 126 | ######### r ######### 127 | EXCRETE 128 | FISSION 129 | DIGEST 130 | EXCRETE 131 | CONSUME 132 | CONSUME 133 | SEED 134 | CONSUME 135 | PRONOUNCE 136 | 137 | ######### l ######### 138 | EXCRETE 139 | CONFLAGRATE 140 | SPRINT 141 | FISSION 142 | SEED 143 | SEED 144 | SEED 145 | SEED 146 | SEED 147 | SEED 148 | SEED 149 | CONSUME 150 | PRONOUNCE 151 | 152 | ######### d ######### 153 | SEED 154 | SEED 155 | SEED 156 | CONSUME 157 | PRONOUNCE 158 | 159 | ######### ! ######### 160 | EXCRETE 161 | FISSION 162 | FISSION 163 | SEED 164 | SEED 165 | SEED 166 | SEED 167 | CONSUME 168 | CONSUME 169 | PRONOUNCE 170 | 171 | ######### \n ######### 172 | FISSION 173 | FISSION 174 | SEED 175 | SEED 176 | CONSUME 177 | CONSUME 178 | PRONOUNCE -------------------------------------------------------------------------------- /miscellaneous/suffolk.txt: -------------------------------------------------------------------------------- 1 | Counter | Addition | Multiplication (Add counter n times) | Math | Corresponding character 2 | ------------------------------------------------------------------------------------------------------- 3 | !!!!!!!! >!>< <<<<<<<<<.! | 8 * 9 + 1 | H 4 | !!!!! >>!>><>< <<<<<<<<<<<<<<<<<<<<.! | 5 * 20 + 2 | e 5 | !!!!!!!!! >< <<<<<<<<<<<<.! | 9 * 12 + 1 | l 6 | !!!!!!!!! >< <<<<<<<<<<<<.! | 9 * 12 + 1 | l 7 | !!!!!!!!!! >><>< <<<<<<<<<<<.! | 10 * 11 + 2 | o 8 | !!!!! <<<<<<<<<.! | 5 * 9 | [comma] 9 | !!! <<<<<<<<<<<.! | 3 * 11 | [space] 10 | !!!!!!!!!! <<<<<<<<<<<<.! | 10 * 12 | w 11 | !!!!!!!! <<<<<<<<<<<<<<.! | 8 * 14 | o 12 | !!!!! <<<<<<<<<<<<<<<<<<<<<<<.! | 5 * 23 | r 13 | !!!!!!!!! >< <<<<<<<<<<<<.! | 9 * 12 + 1 | l 14 | !!!!!!!!!! >< <<<<<<<<<<.! | 10 * 10 + 1 | d 15 | !!! >< <<<<<<<<<<<.! | 2 * 17 | [exclamation] 16 | !!! <<<<<<<<<<<.! >>>!>>>!>>><>>!>>><>! | 3 * 11 | [space] - clears memory for next cycle -------------------------------------------------------------------------------- /other/bitdeque.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import re 3 | 4 | 5 | def run(code): 6 | lst = ('INJECT', 'PUSH', 7 | 'EJECT', 'POP', 8 | 'INVERT', r'GOTO *(\d+)') 9 | 10 | join = f'({"|".join(lst)})' 11 | code = re.findall(join, code) 12 | ind = reg = 0 13 | deq = [] 14 | 15 | while ind < len(code): 16 | sym = code[ind][0] 17 | if sym == 'PUSH': 18 | deq.append(reg) 19 | elif sym == 'INJECT': 20 | deq.insert(0, reg) 21 | elif sym == 'POP': 22 | reg = (deq.pop() if 23 | deq else 0) 24 | elif sym == 'EJECT': 25 | reg = (deq.pop(0) if 26 | deq else 0) 27 | elif sym == 'INVERT': 28 | reg ^= 1 29 | elif reg: 30 | num = int(sym[4:]) 31 | ind = num - 1 32 | 33 | ind += 1 34 | print(*deq) 35 | 36 | 37 | if __name__ == '__main__': 38 | if len(sys.argv) > 1: 39 | with open(sys.argv[1]) as file: 40 | data = file.read() 41 | run(data) 42 | -------------------------------------------------------------------------------- /other/clockwise.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | 4 | def init(code): 5 | col = [1, 0, -1, 0] 6 | row = [0, 1, 0, -1] 7 | x = y = r = 0 8 | 9 | def move(acc): 10 | nonlocal x, y, r 11 | o = code[y][x] 12 | c = ((o == 'R') 13 | or (o == '?' and acc) 14 | or (o == '!' and not acc)) 15 | 16 | r = (r + c) % 4 17 | x += col[r] 18 | y += row[r] 19 | b = x or y or not r 20 | 21 | return o, b 22 | return move 23 | 24 | 25 | def run(code): 26 | size = max(len(lne) for lne in code) 27 | code = [c.ljust(size) for c in code] 28 | move = init(code) 29 | cont = True 30 | 31 | inp = [] 32 | out = [] 33 | acc = 0 34 | 35 | if '.' in ''.join(code): 36 | for k in input('Input: '): 37 | val = bin(ord(k))[2:] 38 | inp += list(val.zfill(7)) 39 | 40 | while cont: 41 | ins, cont = move(acc) 42 | if ins in 'R?!': 43 | continue 44 | elif ins == '+': 45 | acc += 1 46 | elif ins == '-': 47 | acc -= 1 48 | elif ins == '.': 49 | n = int(inp[0]) 50 | acc = (acc | 1) - 1 + n 51 | inp = inp[1:] + [inp[0]] 52 | elif ins == ';': 53 | out.append(str(acc % 2)) 54 | elif ins == 'S': 55 | acc = 0 56 | 57 | if len(out) == 7: 58 | val = int(''.join(out), 2) 59 | print(chr(val), end='') 60 | out = [] 61 | 62 | 63 | if __name__ == '__main__': 64 | if len(sys.argv) > 1: 65 | with open(sys.argv[1]) as file: 66 | data = file.readlines() 67 | run(data) 68 | -------------------------------------------------------------------------------- /other/container.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | 4 | class Con: 5 | def __init__(self, name): 6 | self.name = name 7 | self.rules = [] 8 | 9 | def add(self, cond): 10 | n, c = cond.split() 11 | self.rules.append((int(n), c)) 12 | 13 | def update(self, var): 14 | def val(s): 15 | if s in var: 16 | return var[s] 17 | return int(s) 18 | 19 | res = var[self.name] 20 | for n, c in self.rules: 21 | if '<' in c: 22 | x, y = c.split('<=') 23 | b = val(x) <= val(y) 24 | else: 25 | x, y = c.split('>=') 26 | b = val(x) >= val(y) 27 | 28 | if b: 29 | res += n 30 | 31 | if res < 0: 32 | res = 0 33 | 34 | return res 35 | 36 | 37 | def run(code): 38 | queue = [] 39 | inp = False 40 | obj = [] 41 | var = {} 42 | new = {} 43 | 44 | for line in code: 45 | line = line.strip() 46 | if ':' in line: 47 | line = line[:-1] 48 | if '=' in line: 49 | x, y = line.split('=') 50 | var[x] = int(y) 51 | obj.append(Con(x)) 52 | else: 53 | var[line] = 0 54 | obj.append(Con(line)) 55 | elif line: 56 | obj[-1].add(line) 57 | 58 | while True: 59 | for o in obj: 60 | new[o.name] = o.update(var) 61 | 62 | if 'PRINT' in var: 63 | if var['PRINT'] == 0 and bool(new['PRINT']): 64 | if 'OUT' in var: 65 | print(chr(new['OUT'] % (1 << 7)), end='') 66 | inp = True 67 | if '' in var: 68 | if var[''] == 0 and bool(new['']): 69 | 70 | while not queue: 71 | s = input('\n' * inp + 'Input: ') 72 | queue += list(s) 73 | 74 | new['INPUT'] = ord(queue[0]) 75 | queue = queue[1:] 76 | inp = True 77 | if 'EXIT' in var: 78 | if var['EXIT'] != new['EXIT']: 79 | exit(new['EXIT']) 80 | 81 | var = new.copy() 82 | 83 | 84 | if __name__ == '__main__': 85 | if len(sys.argv) > 1: 86 | with open(sys.argv[1]) as file: 87 | data = file.readlines() 88 | run(data) 89 | -------------------------------------------------------------------------------- /other/keys.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import re 3 | 4 | 5 | def run(code): 6 | x = code[0].strip() 7 | y = code[1].strip() 8 | r = re.compile( 9 | r'(-_|_-|\\-|/' 10 | r'_|[^\\/\-_])') 11 | 12 | if x == y and not r.search(x): 13 | print('Accept.') 14 | else: 15 | print('Reject.') 16 | 17 | 18 | if __name__ == '__main__': 19 | if len(sys.argv) > 1: 20 | with open(sys.argv[1]) as file: 21 | data = file.readlines() 22 | run(data) 23 | -------------------------------------------------------------------------------- /other/nevermind.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | 4 | def find(code, ind): 5 | if 'end' in (op := code[ind]): 6 | match = op[3:] 7 | move = -1 8 | else: 9 | match = 'end' + op 10 | move = 1 11 | 12 | num = move 13 | ind += move 14 | 15 | while num: 16 | if ind == len(code): 17 | return ind 18 | if code[ind][0] == op: 19 | num += move 20 | elif code[ind][0] == match: 21 | num -= move 22 | ind += move 23 | return ind - 1 24 | 25 | 26 | def run(code): 27 | ind = 0 28 | var = {} 29 | skip = False 30 | 31 | for num, val in enumerate(code): 32 | val = (val.lstrip() 33 | .rstrip('\n') 34 | .split(',')) 35 | 36 | code[num] = [ 37 | v.replace('*44', ',') 38 | for v in val if v 39 | ] 40 | 41 | while ind < len(code): 42 | if (c := code[ind]) and not skip: 43 | for x, y in enumerate(c[1:]): 44 | if isinstance(y, str): 45 | if y[0] == '$': 46 | c[x] = var[y[1:].strip()] 47 | if (isinstance(c[x], str) 48 | and c[x].isdigit()): 49 | c[x] = int(c[x]) 50 | 51 | if (op := c[0]) == 'print': 52 | print(*c[1:], sep='') 53 | elif op == 'input': 54 | var['answer'] = input(c[1]) 55 | elif op == 'make': 56 | if len(c) == 5: 57 | if (o := c[3]) == '+': 58 | v = c[2] + c[4] 59 | elif o == '-': 60 | v = c[2] - c[4] 61 | elif o == '*': 62 | v = c[2] * c[4] 63 | else: 64 | v = c[2] / c[4] 65 | var[c[1]] = v 66 | else: 67 | var[c[1]] = c[2] 68 | elif op == 'if': 69 | x, o, y = c[1:4] 70 | if o == '>': 71 | b = x > y 72 | elif o == '<': 73 | b = x < y 74 | else: 75 | b = x == y 76 | if not b: 77 | ind = find(code, ind) 78 | elif op == 'loop': 79 | if c[1]: 80 | c[1] -= 1 81 | else: 82 | ind = find(code, ind) 83 | skip = False 84 | elif op == 'endloop': 85 | ind = find(code, ind) + 1 86 | skip = True 87 | ind += 1 88 | 89 | 90 | if __name__ == '__main__': 91 | if len(sys.argv) > 1: 92 | with open(sys.argv[1]) as file: 93 | data = file.readlines() 94 | run(data) 95 | -------------------------------------------------------------------------------- /other/ztoalc.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | 4 | def run(code): 5 | ptr = int(code[0]) 6 | inp = False 7 | var = {} 8 | 9 | def val(exp): 10 | nonlocal inp 11 | 12 | if exp == 'input': 13 | s = ord(input('\n' * inp + 'Input: ')[0]) 14 | inp = False 15 | return s 16 | elif exp in var: 17 | return var.get(exp) 18 | elif exp.isnumeric(): 19 | return int(exp) 20 | elif exp[0] == '-' and exp[1:].isnumeric(): 21 | return int(exp) 22 | elif exp[0] == '[': 23 | return [0] * val(exp[1:-1]) 24 | else: 25 | arg = exp[:-1].split('[') 26 | return var[arg[0]][val(arg[1])] 27 | 28 | while p := ptr - 1: 29 | ins = code[p] if p < len(code) else '' 30 | lst = ins.split() 31 | 32 | if 'print' in ins: 33 | print(chr(val(lst[1])), end='') 34 | inp = True 35 | elif 'jump' in ins: 36 | if val(lst[2]): 37 | ptr += 1 38 | continue 39 | elif ' =' in ins: 40 | var[lst[0]] = val(lst[2]) 41 | elif '+' in ins: 42 | var[lst[0]] += val(lst[2]) 43 | elif '-' in ins: 44 | var[lst[0]] -= val(lst[2]) 45 | 46 | if ptr % 2: 47 | ptr = 3 * ptr + 1 48 | else: 49 | ptr //= 2 50 | 51 | 52 | if __name__ == '__main__': 53 | if len(sys.argv) > 1: 54 | with open(sys.argv[1]) as file: 55 | data = file.readlines() 56 | run(data) 57 | -------------------------------------------------------------------------------- /register-based/RAM0.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import re 3 | 4 | 5 | def init(): 6 | z = n = 0 7 | ram = {} 8 | 9 | def output(): 10 | res = (f'z: {z}\n' 11 | f'n: {n}\n' 12 | 'ram: {') 13 | 14 | for x, y in ram.items(): 15 | res += f'\n {x}: {y},' 16 | if ram: 17 | res = res[:-1] + '\n' 18 | print(res + '}') 19 | 20 | def change(op): 21 | nonlocal z, n 22 | if op == 'Z': 23 | z = 0 24 | elif op == 'A': 25 | z += 1 26 | elif op == 'N': 27 | n = z 28 | elif op == 'L': 29 | z = ram.get(z, 0) 30 | elif op == 'S': 31 | ram[n] = z 32 | elif op == 0: 33 | output() 34 | return not z 35 | return change 36 | 37 | 38 | def run(code): 39 | expr = r'([ZANCLS]|[1-9]\d*)' 40 | code = re.findall(expr, code) 41 | func = init() 42 | ind = 0 43 | 44 | while ind < len(code): 45 | skip = func(c := code[ind]) 46 | if c == 'C' and skip: 47 | ind += 1 48 | elif c.isdigit(): 49 | ind = int(c) - 2 50 | ind += 1 51 | 52 | func(0) 53 | 54 | 55 | if __name__ == '__main__': 56 | if len(sys.argv) > 1: 57 | with open(sys.argv[1]) as file: 58 | data = file.read() 59 | run(data) 60 | -------------------------------------------------------------------------------- /register-based/WII2D.py: -------------------------------------------------------------------------------- 1 | import secrets 2 | import copy 3 | import sys 4 | 5 | 6 | def init(lst): 7 | n = len(lst) 8 | m = len(lst[0]) 9 | lst = [ 10 | (-1, 0), (1, 0), 11 | (0, -1), (0, 1) 12 | ] 13 | 14 | def move(x, y, vel): 15 | a, b = lst[vel] 16 | x = (x + a) % n 17 | y = (y + b) % m 18 | return x, y 19 | return move 20 | 21 | 22 | def close(lst): 23 | def start(x, y): 24 | def dist(c): 25 | a = abs(c[0] - x) 26 | b = abs(c[1] - y) 27 | return a + b 28 | return dist 29 | 30 | pos = [] 31 | 32 | for num, val in enumerate(lst): 33 | for n, v in enumerate(val): 34 | if num and v == '@': 35 | pos.append((num, n)) 36 | 37 | def find(x, y): 38 | arr = copy.deepcopy(pos) 39 | arr.sort(key=start(x, y)) 40 | if (c := (x, y)) in arr: 41 | arr.remove(c) 42 | return arr[0] if arr else None 43 | return find 44 | 45 | 46 | def update(op, acc): 47 | if op.isdigit(): 48 | return int(op) 49 | elif op == '+': 50 | return acc + 1 51 | elif op == '-': 52 | return acc - 1 53 | elif op == '*': 54 | return acc * 2 55 | elif op == '/': 56 | return acc // 2 57 | elif op == 's': 58 | return acc ** 2 59 | elif op == '~': 60 | print(chr(acc), end='') 61 | return acc 62 | 63 | 64 | def run(code): 65 | for num, val in enumerate(code): 66 | if '!' in val: 67 | x, y = num, val.find('!') 68 | break 69 | else: 70 | return 71 | 72 | size = max(len(lst) for lst in code) 73 | code = [c.ljust(size) for c in code] 74 | 75 | find = close(code) 76 | move = init(code) 77 | vel = acc = 0 78 | 79 | while True: 80 | if (op := code[x][y]) in '^v<>': 81 | vel = '^v<>'.index(op) 82 | elif op == '?': 83 | vel = secrets.randbelow(4) 84 | elif op == '|': 85 | if vel % 2: 86 | vel -= 1 87 | else: 88 | vel += 1 89 | elif op == '@': 90 | if w := find(x, y): 91 | x, y = w 92 | x -= 1 93 | continue 94 | elif op == '.': 95 | return 96 | 97 | acc = update(op, acc) 98 | x, y = move(x, y, vel) 99 | 100 | 101 | if __name__ == '__main__': 102 | if len(sys.argv) > 1: 103 | with open(sys.argv[1]) as file: 104 | data = file.readlines() 105 | run(data) 106 | -------------------------------------------------------------------------------- /register-based/bio.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import re 3 | 4 | 5 | def run(code): 6 | lang = '([01][oOiI][xyz]|})' 7 | code = re.findall(lang, code) 8 | code = [k.lower() for k in code] 9 | 10 | reg = [0] * 3 11 | stk = [] 12 | ind = 0 13 | 14 | while ind < len(code): 15 | r = 'xyz'.find(code[ind][-1]) 16 | c = code[ind][:2] 17 | 18 | if c == '0o': 19 | reg[r] += 1 20 | elif c == '1o': 21 | reg[r] -= 1 22 | elif c == '1i': 23 | print(chr(reg[r]), end='') 24 | elif c == '}': 25 | ind = stk.pop() - 1 26 | else: 27 | if reg[r]: 28 | stk.append(ind) 29 | else: 30 | mat = 1 31 | while mat: 32 | ind += 1 33 | if ind == len(code): 34 | break 35 | else: 36 | c = code[ind][:2] 37 | if c == '0i': 38 | mat += 1 39 | elif c == '}': 40 | mat -= 1 41 | ind += 1 42 | 43 | 44 | if __name__ == '__main__': 45 | if len(sys.argv) > 1: 46 | with open(sys.argv[1]) as file: 47 | data = file.read() 48 | run(data) 49 | -------------------------------------------------------------------------------- /register-based/dig.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | 4 | def run(code, func=lambda: 0): 5 | size = max(len(lne) for lne in code) 6 | code = [c.ljust(size) for c in code] 7 | 8 | direct = [(-1, 0), (0, 1), (1, 0), (0, -1)] 9 | line = mole = num = x = y = 0 10 | move = 1 11 | 12 | def value(): 13 | lst = [] 14 | for i, j in direct: 15 | if (0 <= x + i < len(code) 16 | and 0 <= y + j < size): 17 | val = code[x + i][y + j] 18 | if val.isdigit(): 19 | lst.append(int(val)) 20 | return lst[0] 21 | 22 | while True: 23 | char = code[x][y] 24 | if num: 25 | if char == '%': 26 | if (n := value()) == 1: 27 | mole = 10 28 | elif n == 0: 29 | mole = 32 30 | elif char in '=~': 31 | temp = input('\n' * line + 'Input: ') 32 | line = False 33 | 34 | if char == '=': 35 | mole = ord(temp[0]) 36 | else: 37 | mole = int(temp[0]) 38 | elif char == ':': 39 | if mole < 10: 40 | print(mole, end='') 41 | else: 42 | print(chr(mole), end='') 43 | 44 | line = True 45 | mole = 0 46 | elif char == '+': 47 | mole += value() 48 | elif char == '-': 49 | mole -= value() 50 | elif char == '*': 51 | mole += value() 52 | elif char == '/': 53 | mole //= value() 54 | elif char == ';': 55 | code[x] = (code[x][:y] 56 | + str(mole) 57 | + code[x][y + 1:]) 58 | elif char.isdigit(): 59 | mole = int(char) 60 | elif char.isalpha() or char in '.,!?': 61 | mole = ord(char) 62 | num -= 1 63 | else: 64 | if char in '^>\'<': 65 | move = '^>\'<'.find(char) 66 | elif char == '#': 67 | if (n := value()) == 1: 68 | move += 1 69 | elif n == 0: 70 | move -= 1 71 | move %= 4 72 | elif char == '$': 73 | if func(): 74 | break 75 | num = value() 76 | elif char == '@': 77 | break 78 | 79 | x += direct[move][0] 80 | y += direct[move][1] 81 | 82 | 83 | if __name__ == '__main__': 84 | if len(sys.argv) > 1: 85 | with open(sys.argv[1]) as file: 86 | data = file.readlines() 87 | run(data) 88 | -------------------------------------------------------------------------------- /register-based/dotlang.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import re 3 | 4 | 5 | class Dot: 6 | list = [(-1, 0), (0, 1), (1, 0), (0, -1)] 7 | mx = my = 0 8 | code = '' 9 | 10 | def __init__(self, x, y, d): 11 | self.val = None 12 | self.dir = d 13 | self.x = x 14 | self.y = y 15 | 16 | @staticmethod 17 | def set(code): 18 | Dot.code = code 19 | Dot.mx = len(code) 20 | Dot.my = len(code[0]) 21 | 22 | def new(self, val): 23 | if re.match(r'\d+\.\d+', val): 24 | self.val = float(val) 25 | elif re.match(r'\d+', val): 26 | self.val = int(val) 27 | else: 28 | self.val = val 29 | 30 | def move(self): 31 | x, y = Dot.list[self.dir] 32 | self.x += x 33 | self.y += y 34 | if x: 35 | return 0 <= self.x < Dot.mx 36 | return 0 <= self.y < Dot.my 37 | 38 | def match(self, regex): 39 | line = Dot.code[self.x][self.y:] 40 | return re.match(regex, line) 41 | 42 | def find(self, warp, ret=False): 43 | for num, val in enumerate(Dot.code): 44 | if warp in val: 45 | x, y = num, val.find(warp) 46 | if self.dir == 1: 47 | y += len(warp) - 1 48 | if ret: 49 | return Dot(x, y, self.dir) 50 | else: 51 | self.x, self.y = x, y 52 | return True 53 | return False 54 | 55 | 56 | def run(code): 57 | if code == [' ']: 58 | print(' ', end='') 59 | return 60 | 61 | size = max(len(lne) for lne in code) 62 | code = [c.ljust(size) for c in code] 63 | line = False 64 | dots = [] 65 | curr = 0 66 | 67 | for num, val in enumerate(code): 68 | if '•' in val: 69 | k = val.find('•') 70 | if k and (v := val[k - 1]) in '^>v<': 71 | d = '^>v<'.find(v) 72 | else: 73 | d = 1 74 | dots.append(Dot(num, k, d)) 75 | Dot.set(code) 76 | break 77 | else: 78 | return 79 | 80 | while dots: 81 | dot = dots[curr] 82 | val = code[dot.x][dot.y] 83 | 84 | if val in '^>v<': 85 | dot.dir = '^>v<'.find(val) 86 | elif val == '#': 87 | if g := dot.match(r'#(\d+(\.\d+)?|`.*`)'): 88 | dot.new(g[0][1:].replace('`', '')) 89 | if dot.dir == 1: 90 | dot.y += len(g[0]) - 1 91 | elif dot.val is not None: 92 | print(dot.val, end='') 93 | line = True 94 | else: 95 | return 96 | elif val == '~': 97 | dot.new(input('\n' * line + 'Input: ')) 98 | line = False 99 | elif val == '(': 100 | if g := dot.match(r'\(`\w+'): 101 | name = ')' + g[0][1:] 102 | if not (d := dot.find(name, True)): 103 | return 104 | dots.append(d) 105 | else: 106 | match = 1 107 | x, y = dot.x, dot.y 108 | while match: 109 | if dot.dir % 2: 110 | y += 1 111 | if y == size: 112 | return 113 | else: 114 | x += 1 115 | if x == Dot.mx: 116 | return 117 | if (c := code[x][y]) == '(': 118 | match += 1 119 | elif c == ')': 120 | match -= 1 121 | dots.append(Dot(x, y, dot.dir)) 122 | elif val == 'W': 123 | if dot.match('W~'): 124 | warp = input('\n' * line + 'Warp: ') 125 | if not dot.find('W%s`s' % warp): 126 | return 127 | line = False 128 | elif g := dot.match(r'W\w+`s'): 129 | warp = g[0][:-1] + 'e' 130 | if not dot.find(warp): 131 | return 132 | elif val in '!?:': 133 | t = (str, float, int)['!?:'.find(val)] 134 | if isinstance(dot.val, t): 135 | if dot.dir % 2: 136 | dot.dir -= 1 137 | else: 138 | dot.dir += 1 139 | 140 | if val not in ' \n' and dot.move(): 141 | curr = (curr + 1) % len(dots) 142 | else: 143 | dots.pop(curr) 144 | 145 | 146 | if __name__ == '__main__': 147 | f = open(sys.argv[1], encoding='utf-8') 148 | data = f.readlines() 149 | f.close() 150 | 151 | run(data) 152 | -------------------------------------------------------------------------------- /register-based/dsdlai.py: -------------------------------------------------------------------------------- 1 | import secrets as s 2 | import sys 3 | import dig 4 | 5 | 6 | def rand(): 7 | num = s.randbelow(71) + 20 8 | 9 | def chance(): 10 | n = s.randbelow(100) + 1 11 | if n <= num: 12 | print('\nYou died.') 13 | return n <= num 14 | return chance 15 | 16 | 17 | if __name__ == '__main__': 18 | if len(sys.argv) > 1: 19 | with open(sys.argv[1]) as file: 20 | data = file.readlines() 21 | dig.run(data, rand()) 22 | -------------------------------------------------------------------------------- /register-based/huf.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import re 3 | 4 | 5 | def run(code): 6 | code = re.findall( 7 | '#[^#@]+@', code) 8 | code = ''.join(code) 9 | num = mul = 0 10 | 11 | for sym in code: 12 | if sym == '#': 13 | num = mul = 0 14 | elif sym == '>': 15 | val = chr(num) 16 | print(val, end='') 17 | num = 0 18 | elif sym == '|': 19 | mul = 1 20 | elif sym == '!': 21 | num *= (mul - 1) 22 | mul = 0 23 | elif sym == '+': 24 | if mul: 25 | mul += 1 26 | else: 27 | num += 1 28 | 29 | 30 | if __name__ == '__main__': 31 | if len(sys.argv) > 1: 32 | with open(sys.argv[1]) as file: 33 | data = file.read() 34 | run(data) 35 | -------------------------------------------------------------------------------- /register-based/lightlang.py: -------------------------------------------------------------------------------- 1 | import secrets 2 | import time 3 | import sys 4 | 5 | 6 | def run(code): 7 | bit = ind = 0 8 | vel = new = 1 9 | 10 | while ind < len(code): 11 | sym = code[ind] 12 | if sym == '^': 13 | bit ^= 1 14 | elif sym == '!': 15 | print(bit, end='') 16 | new = 0 17 | elif sym == '?': 18 | val = input('\nInput: '[new:]) 19 | bit = (not val) + 0 20 | new = 1 21 | elif sym == '@': 22 | bit = secrets.randbelow(2) 23 | elif sym == '&' and bit: 24 | ind += vel 25 | elif sym == '#': 26 | return 27 | elif sym == '<': 28 | ind = -vel 29 | elif sym == '/': 30 | vel *= -1 31 | elif sym == '_': 32 | time.sleep(1) 33 | 34 | ind += vel 35 | 36 | 37 | if __name__ == '__main__': 38 | if len(sys.argv) > 1: 39 | with open(sys.argv[1]) as file: 40 | data = file.read() 41 | run(data) 42 | -------------------------------------------------------------------------------- /register-based/minsky-swap.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import re 3 | 4 | 5 | def run(text): 6 | ind = ptr = val = 0 7 | reg = [0, 0] 8 | nums = [] 9 | code = '' 10 | 11 | if re.search('[+~*]', text): 12 | code = (s := text.split('\n'))[0] 13 | code = re.sub('[^+~*]', '', code) 14 | if len(s) > 1: 15 | nums = re.findall(r'\d+', s[1]) 16 | nums = [int(k) for k in nums] 17 | else: 18 | cmp = r'(inc|swap|decnz)\((\d*)\);' 19 | cmp = re.compile(fr'(?:^|\W){cmp}') 20 | for m in cmp.findall(text): 21 | if (s := m[0][0]) == 'i': 22 | code += '+' 23 | elif s == 's': 24 | code += '*' 25 | else: 26 | code += '~' 27 | skip = int(m[1]) 28 | nums.append(skip) 29 | 30 | while ind < len(code): 31 | if (op := code[ind]) == '+': 32 | reg[ptr] += 1 33 | elif op == '~': 34 | if reg[ptr]: 35 | reg[ptr] -= 1 36 | val += 1 37 | else: 38 | ind = nums[val] - 2 39 | val = code[:ind - 1].count('~') 40 | elif op == '*': 41 | ptr ^= 1 42 | 43 | ind += 1 44 | print(*reg) 45 | 46 | 47 | if __name__ == '__main__': 48 | if len(sys.argv[1]) > 1: 49 | with open(sys.argv[1]) as file: 50 | data = file.read() 51 | run(data) 52 | -------------------------------------------------------------------------------- /register-based/movesum.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import re 3 | 4 | 5 | def run(code): 6 | reg = re.compile(r'(\d+) *= *(\d+)') 7 | arr = {k: 0 for k in range(5)} 8 | num = ind = 0 9 | new = 1 10 | 11 | for m in reg.finditer(code[0]): 12 | x, y = m[1], m[2] 13 | if x == '42': 14 | x = input('Key: ') 15 | elif y == '42': 16 | y = input('Value: ') 17 | y = y if y else 0 18 | arr[int(x)] = int(y) 19 | 20 | code = code[1:] 21 | while num < 2: 22 | copy = arr.copy() 23 | expr = (r'(move *(-?\d+)' 24 | r' *(-?\d+)|sum)') 25 | 26 | if m := re.search(expr, code[ind]): 27 | if m[1] == 'sum': 28 | arr[0] = arr[1] 29 | for k in range(2, 5): 30 | arr[0] += arr[k] 31 | else: 32 | if m[2].isdigit(): 33 | x = int(m[2]) 34 | n = arr.get(x, 0) 35 | 36 | if m[3].isdigit(): 37 | y = int(m[3]) 38 | arr[y] = n 39 | else: 40 | print(n, end=' ') 41 | new = 0 42 | elif m[3].isdigit(): 43 | y = int(m[3]) 44 | n = input('\nInput: '[new:]) 45 | arr[y] = int(n) if n else 0 46 | new = 1 47 | 48 | num = (num + 1) * (arr == copy) 49 | ind = (ind + 1) % len(code) 50 | 51 | 52 | if __name__ == '__main__': 53 | if len(sys.argv) > 1: 54 | with open(sys.argv[1]) as file: 55 | data = file.readlines() 56 | run(data) 57 | -------------------------------------------------------------------------------- /register-based/polynomial.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import sys 3 | import re 4 | 5 | 6 | def prime(number): 7 | for val in range(2, int(np.sqrt(number)) + 1): 8 | if not number % val: 9 | return False 10 | return True 11 | 12 | 13 | def brackets(string, pointer): 14 | length = len(string[pointer]) == 1 15 | end = string[pointer][0] in [2, 6] 16 | direct = (1, -1)[length and end] 17 | count = direct 18 | while count: 19 | pointer += direct 20 | if len(string[pointer]) == 1: 21 | if string[pointer][0] in [2, 6]: 22 | count -= 1 23 | else: 24 | count += 1 25 | return pointer 26 | 27 | 28 | def convert(pre): 29 | pre = [np.round(k) for k in pre] 30 | pre = sorted(pre, key=lambda x: np.imag(x) or x) 31 | post = [] 32 | num = 2 33 | 34 | while pre: 35 | if not prime(num): 36 | num += 1 37 | continue 38 | for root in pre: 39 | if im := np.imag(root): 40 | for val in range(1, 7): 41 | # to the power of, not multiply 42 | if im == num ** val: 43 | pre.remove(root) 44 | post.append([int(np.real(root)), val]) 45 | break 46 | else: 47 | for val in range(1, 9): 48 | if root == num ** val: 49 | pre.remove(root) 50 | post.append([val]) 51 | break 52 | num += 1 53 | return post 54 | 55 | 56 | def sanitize(code): 57 | code = code[5:].replace('x^0', '') 58 | reg_dict = { 59 | r'^x': '1x', r'(\D)x': r'\g<1>1x', 60 | r'x([+-])': r'x^1\1', 61 | } 62 | for regex in reg_dict: 63 | code = re.sub(regex, reg_dict[regex], code) 64 | 65 | code = code + 'x^0' 66 | mono = re.findall(r'-?\d+x\^\d+', code) 67 | post = [] 68 | 69 | for k in range(int(mono[0].split('x^')[1]) + 1): 70 | for m in mono: 71 | if k == int((nums := m.split('x^'))[1]): 72 | post.insert(0, int(nums[0])) 73 | mono.remove(m) 74 | break 75 | else: 76 | post.insert(0, 0) 77 | return post 78 | 79 | 80 | def run(code): 81 | code = re.sub(r'[^\df(x)=+-^]', '', code) 82 | if code[:5] != 'f(x)=': 83 | return 84 | 85 | code = sanitize(code) 86 | code = [k for k in np.roots(code) if np.imag(k) >= 0] 87 | code = convert(code) 88 | 89 | ind = reg = 0 90 | new = 1 91 | sym = [ 92 | lambda r, a: r + a, lambda r, a: r - a, 93 | lambda r, a: r * a, lambda r, a: r / a, 94 | lambda r, a: r % a, lambda r, a: r ** a, 95 | lambda: reg > 0, 0, 96 | lambda: reg < 0, lambda: not reg 97 | ] 98 | 99 | while ind < len(code): 100 | one, *rest = code[ind] 101 | if two := (rest + [0])[0]: 102 | if one: 103 | reg = sym[two - 1](reg, one) 104 | elif two - 1: 105 | val = input('\nInput: '[new:]) + chr(0) 106 | reg = ord(val[0]) or -1 107 | new = 1 108 | else: 109 | print(chr(max(0, reg)), end='') 110 | new = 0 111 | elif one in [2, 6]: 112 | beg = code[brackets(code, ind)][0] 113 | if beg > 4 and sym[(beg - 1) % 4 + 6](): 114 | ind = brackets(code, ind) 115 | elif not sym[(one % 4) + 5](): 116 | ind = brackets(code, ind) 117 | ind += 1 118 | 119 | 120 | if __name__ == '__main__': 121 | if len(sys.argv) > 1: 122 | with open(sys.argv[1]) as file: 123 | data = file.read() 124 | run(data) 125 | -------------------------------------------------------------------------------- /register-based/qoibl.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import re 3 | 4 | 5 | def run(code): 6 | line = False 7 | var = {} 8 | 9 | def parse(expr): 10 | nonlocal line 11 | 12 | if (op := expr[0]) == 'tt': 13 | print(chr(parse(expr[1:-1])), end='') 14 | line = True 15 | elif op == 'we': 16 | ind = expr.index('we', 1) 17 | var[parse(expr[1:ind])] \ 18 | = parse(expr[ind + 1:-1]) 19 | elif op == 'rr': 20 | ind = expr.index('rr', 1) 21 | while parse(expr[1:ind]): 22 | parse(expr[ind + 1:-1]) 23 | elif 'yr' in expr: 24 | beg = expr.index('yr') 25 | num = expr[beg + 1] 26 | x = parse(expr[:beg]) 27 | y = parse(expr[beg + 3:]) 28 | 29 | if num == 'ee': 30 | return x == y 31 | elif num == 'ey': 32 | return x > y 33 | elif num == 'ye': 34 | return x < y 35 | elif num == 'yy': 36 | return x != y 37 | elif 'ry' in expr: 38 | beg = expr.index('ry') 39 | num = expr[beg + 1] 40 | x = parse(expr[:beg]) 41 | y = parse(expr[beg + 3:]) 42 | 43 | if num == 'ee': 44 | return x + y 45 | elif num == 'ey': 46 | return x - y 47 | elif num == 'ye': 48 | return x * y 49 | elif num == 'yy': 50 | return x // y 51 | elif op == 'qe': 52 | return var.get(parse(expr[1:-1]), 0) 53 | elif op == 'et': 54 | n = input('\n' * line + 'Input: ') 55 | line = False 56 | return ord(n[0]) 57 | elif re.fullmatch('[ey]+', op): 58 | op = (op.replace('e', '0') 59 | .replace('y', '1')) 60 | return int(op, 2) 61 | 62 | for exp in code: 63 | parse(exp.split()) 64 | 65 | 66 | if __name__ == '__main__': 67 | if len(sys.argv) > 1: 68 | with open(sys.argv[1]) as file: 69 | data = file.readlines() 70 | run(data) 71 | -------------------------------------------------------------------------------- /register-based/sophie.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import re 3 | 4 | 5 | def find(code, ind): 6 | opr = code[ind] 7 | end = chr(ord(opr) + 2) 8 | match = 1 9 | 10 | while match: 11 | ind += 1 12 | if ind == len(code): 13 | break 14 | elif (c := code[ind]) == opr: 15 | match += 1 16 | elif c == end: 17 | match -= 1 18 | return ind 19 | 20 | 21 | def run(code): 22 | acc = ind = 0 23 | skp = False 24 | stk = [] 25 | new = 1 26 | 27 | while ind < len(code): 28 | if (c := code[ind]) == '[': 29 | if skp: 30 | ind = find(code, ind) 31 | else: 32 | stk.append(ind) 33 | elif c in ']*': 34 | ind = stk.pop() - 1 35 | if c == '*': 36 | skp = True 37 | elif c == '.': 38 | print(acc, end='') 39 | new = 0 40 | elif c == ':': 41 | num = input('\nInput: '[new:]) 42 | new = 1 43 | if num.isdigit(): 44 | acc = int(num) 45 | elif c == ',': 46 | print(chr(acc), end='') 47 | new = 0 48 | elif c == ';': 49 | val = input('\nInput: '[new:]) 50 | new = 1 51 | if val: 52 | acc = ord(val[0]) 53 | elif c == '{': 54 | ind = find(code, ind) 55 | elif c == '&': 56 | return 57 | else: 58 | val = code[ind:] 59 | if m := re.match(r'@\$(\d+){', val): 60 | n = m.end() - 1 61 | if acc == int(m[1]): 62 | ind += n 63 | else: 64 | ind = find(code, ind + n) + 1 65 | elif m := re.match(r'@\$?(.){', val): 66 | n = m.end() - 1 67 | if acc == ord(m[1]): 68 | ind += n 69 | else: 70 | ind = find(code, ind + n) + 1 71 | elif m := re.match(r'#\$(\d+)', val): 72 | acc = int(m[1]) 73 | ind += m.end() - 1 74 | elif m := re.match(r'#\$?(.)', val): 75 | acc = ord(m[1]) 76 | ind += m.end() - 1 77 | 78 | ind += 1 79 | 80 | 81 | if __name__ == '__main__': 82 | if len(sys.argv) > 1: 83 | with open(sys.argv[1]) as file: 84 | data = file.read() 85 | run(data) 86 | -------------------------------------------------------------------------------- /stack-based/bfstack.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | 4 | def run(code): 5 | stk = [] 6 | lst = [] 7 | new = 1 8 | ind = 0 9 | 10 | while ind < len(code): 11 | if (char := code[ind]) == '>': 12 | stk.append(0) 13 | elif char == '<': 14 | stk.pop() 15 | elif char == '+': 16 | stk[-1] = (stk[-1] + 1) % 256 17 | elif char == '-': 18 | stk[-1] = (stk[-1] - 1) % 256 19 | elif char == '.': 20 | print(chr(stk[-1]), end='') 21 | new = 0 22 | elif char == ',': 23 | val = input('\nInput: '[new:]) 24 | stk.append(ord(val[0])) 25 | new = 1 26 | elif char == '[': 27 | if stk[-1]: 28 | lst.append(ind) 29 | else: 30 | match = 1 31 | while match: 32 | ind += 1 33 | if ind == len(code): 34 | break 35 | elif (o := code[ind]) == '[': 36 | match += 1 37 | elif o == ']': 38 | match -= 1 39 | elif char == ']': 40 | ind = lst.pop() - 1 41 | 42 | ind += 1 43 | 44 | 45 | if __name__ == '__main__': 46 | if len(sys.argv) > 1: 47 | with open(sys.argv[1]) as file: 48 | data = file.read() 49 | run(data) 50 | -------------------------------------------------------------------------------- /stack-based/eval.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import re 3 | 4 | 5 | def run(code): 6 | ptr = 0 7 | stk = [[], []] 8 | app = stk[ptr].append 9 | pop = stk[ptr].pop 10 | 11 | dct = { 12 | '`': lambda: app(1 - ptr), 13 | '^': lambda: app(stk[ptr][-1]), 14 | '0': lambda: app(0), 15 | '+': lambda: app(pop() + 1), 16 | '-': lambda: app(pop() - 1), 17 | '.': lambda: print(pop(), end=''), 18 | '=': lambda: stk[1 - ptr].append(pop()), 19 | ';': lambda: pop() 20 | } 21 | 22 | def ins(sym): 23 | nonlocal ptr 24 | ind = 0 25 | 26 | while ind < len(sym): 27 | if (char := sym[ind]) in dct: 28 | dct[char]() 29 | elif char == '~': 30 | ptr ^= 1 31 | elif char == '*': 32 | stk[ptr] = stk[ptr][::-1] 33 | elif char == '?': 34 | if not pop(): 35 | ind += 1 36 | elif char == '!': 37 | ins(pop()) 38 | elif char in '"\'': 39 | s = (re.match('[^"]*', sym[ind + 1:]) 40 | [0].replace('`', '"')) 41 | ind += len(s) + 1 42 | if char == '\'': 43 | s = f'"{s}"' 44 | 45 | app(s) 46 | 47 | ind += 1 48 | 49 | ins(code) 50 | 51 | 52 | if __name__ == '__main__': 53 | if len(sys.argv) > 1: 54 | with open(sys.argv[1]) as file: 55 | data = file.read() 56 | run(data) 57 | -------------------------------------------------------------------------------- /stack-based/modulous.py: -------------------------------------------------------------------------------- 1 | import secrets 2 | import sys 3 | import re 4 | 5 | 6 | def run(code): 7 | reg = re.compile(r'\[([^\[\]\"]*("[^"]*")?)]') 8 | code = [k[0] for k in reg.findall(code)] 9 | var = {f'VAR{k}': 0 for k in range(1, 5)} 10 | 11 | stk = [] 12 | new = 1 13 | ind = 0 14 | 15 | while ind < len(code): 16 | mod = code[ind] 17 | arg = mod.split() 18 | ind += 1 19 | 20 | if 'JMP' in mod: 21 | cond = True 22 | val = stk[-1] if stk else 0 23 | 24 | if 'NIF' in mod: 25 | cond = val != int(arg[-1]) 26 | elif 'IF' in mod: 27 | cond = val == int(arg[-1]) 28 | 29 | if cond: 30 | if arg[1] == 'F': 31 | ind += int(arg[2]) - 1 32 | else: 33 | ind -= int(arg[2]) + 1 34 | elif 'ADD' in mod: 35 | stk[-1] += int(arg[1]) 36 | elif 'SUB' in mod: 37 | stk[-1] -= int(arg[1]) 38 | elif 'RST' == mod: 39 | ind = -1 40 | elif 'PSH' in mod: 41 | if 'INT' in mod: 42 | stk.append(int(arg[2])) 43 | elif 'STR' in mod: 44 | m = mod.split('"')[1] 45 | stk += [ord(c) for c in m][::-1] 46 | elif 'VAR' in mod: 47 | var[arg[1]] = stk[-1] 48 | elif 'POP' == mod: 49 | stk.pop() 50 | elif 'SWP' == mod: 51 | stk.append(stk.pop(-2)) 52 | elif 'PRT' in mod: 53 | if 'VAR' in mod: 54 | n = var[arg[1]] 55 | else: 56 | n = stk.pop() 57 | 58 | if 'INT' in mod: 59 | print(n, end='') 60 | else: 61 | print(chr(n), end='') 62 | new = 0 63 | elif 'INP' in mod: 64 | n = input('\nInput: '[new:]) 65 | new = 1 66 | 67 | if 'INT' in mod and n: 68 | stk.append(int(n)) 69 | else: 70 | stk += [ord(c) for c in n][::-1] 71 | elif 'END' == mod: 72 | return 73 | elif 'DUP' == mod: 74 | stk.append(stk[-1]) 75 | elif 'RND' in mod: 76 | n = secrets.randbelow(int(arg[1])) 77 | stk.append(n) 78 | elif '+' in mod: 79 | arg = mod.split('+') 80 | var[arg[0]] += int(arg[1]) 81 | elif '-' in mod: 82 | arg = mod.split('-') 83 | var[arg[0]] += int(arg[1]) 84 | 85 | 86 | if __name__ == '__main__': 87 | if len(sys.argv) > 1: 88 | with open(sys.argv[1]) as file: 89 | data = file.read() 90 | run(data) 91 | -------------------------------------------------------------------------------- /stack-based/temporary.py: -------------------------------------------------------------------------------- 1 | import secrets 2 | import sys 3 | 4 | 5 | def run(code): 6 | code = code.split() 7 | 8 | stk = [] 9 | num = True 10 | new = False 11 | ptr = comm = 0 12 | 13 | def parse(char): 14 | nonlocal new, ptr, comm, num 15 | rest = code[ptr][1:] 16 | 17 | if char == '@': 18 | s = input('\n' * new + 'Input: ') 19 | stk.extend(ord(c) for c in s) 20 | new = False 21 | elif char == 'v': 22 | stk.append(int(rest)) 23 | elif char == '*': 24 | stk.extend(ord(c) for c in rest) 25 | elif char in 'oO': 26 | num = char == 'O' 27 | elif char == '+': 28 | stk.append(stk[-1]) 29 | elif char == ':': 30 | ptr += 1 31 | n = len(stk) 32 | while len(stk) == n: 33 | parse(code[ptr][0]) 34 | comm += 1 35 | elif char == '\\': 36 | ptr += 1 37 | while len(stk): 38 | parse(code[ptr][0]) 39 | comm += 1 40 | elif char == '€': 41 | parse(secrets.choice('@v*oO+:\\')) 42 | 43 | while stk and sum(stk[1:]) / 2 > stk[0]: 44 | new = True 45 | n = stk.pop(0) - 1 46 | print(n if num else chr(n), end='') 47 | 48 | while ptr < len(code): 49 | parse(code[ptr][0]) 50 | ptr += 1 51 | comm += 1 52 | if comm % 15 == 0: 53 | stk = [] 54 | 55 | 56 | if __name__ == '__main__': 57 | f = open(sys.argv[1], encoding='utf-8') 58 | data = f.read() 59 | f.close() 60 | 61 | run(data) 62 | -------------------------------------------------------------------------------- /tape-based/6-5.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import re 3 | 4 | 5 | def num(char): 6 | if char.isdigit(): 7 | return int(char) 8 | return ord(char.upper()) - 55 9 | 10 | 11 | def run(code): 12 | code = f' {code}' 13 | code = re.sub('([^78])C[^\n]*', '\1', code) 14 | 15 | cell = ind = 0 16 | tape = [0] 17 | line = 1 18 | 19 | while ind < len(code): 20 | if (c := code[ind]) == '1': 21 | cell += 2 22 | while len(tape) < cell + 1: 23 | tape.append(0) 24 | elif c == '3' and cell: 25 | cell -= 1 26 | elif c in '56': 27 | tape[cell] += int(c) 28 | elif c in '29': 29 | tape[cell] -= int(c) % 6 + 3 30 | elif c == '8': 31 | val = num(code[ind + 1]) 32 | reg = f'([^4]*4){val}' 33 | if m := re.match(reg, code): 34 | ind = m.end() - 1 35 | elif c == '7': 36 | val = num(code[ind + 1]) 37 | if tape[cell] == val: 38 | ind += 1 39 | elif c == '0': 40 | return 41 | elif c == 'A': 42 | print(chr(tape[cell]), end='') 43 | line = 0 44 | elif c == 'B': 45 | val = input('\nInput: '[line:]) 46 | tape[cell] = ord(val[0]) 47 | line = 1 48 | 49 | ind += 1 50 | 51 | 52 | if __name__ == '__main__': 53 | if len(sys.argv) > 1: 54 | with open(sys.argv[1]) as file: 55 | data = file.read() 56 | run(data) 57 | -------------------------------------------------------------------------------- /tape-based/ascii-art.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import re 3 | 4 | 5 | def parse(code): 6 | code = re.sub(' +\n', '\n', code) 7 | code = code.split('\n\n') 8 | res = '' 9 | sym = { 10 | (0, '-'): '-', (1, '#'): '.', 11 | (2, '|'): ',', (3, '\\'): '<', 12 | (3, '/'): '>', (4, '|'): '+', 13 | (5, '_'): '[', (5, '|'): ']' 14 | } 15 | 16 | for c in code: 17 | t = (c.count('\n'), c[-1]) 18 | if t in sym: 19 | res += sym[t] 20 | return res 21 | 22 | 23 | def init(code, tape): 24 | stk = [] 25 | 26 | def find(ind, ptr): 27 | if code[ind] == '[': 28 | if tape[ptr]: 29 | stk.append(ind) 30 | else: 31 | match = 1 32 | while match: 33 | ind += 1 34 | if ind == len(code): 35 | return 36 | elif code[ind] == '[': 37 | match += 1 38 | elif code[ind] == ']': 39 | match -= 1 40 | else: 41 | if tape[ptr]: 42 | return stk[-1] 43 | stk.pop() 44 | return ind 45 | 46 | return find 47 | 48 | 49 | def run(code): 50 | tape = [0] 51 | code = parse(code) 52 | find = init(code, tape) 53 | 54 | ind = ptr = 0 55 | new = 1 56 | 57 | while ind < len(code): 58 | if (char := code[ind]) == '>': 59 | ptr += 1 60 | if ptr == len(tape): 61 | tape.append(0) 62 | elif char == '<' and ptr: 63 | ptr -= 1 64 | elif char in '+': 65 | tape[ptr] = (tape[ptr] + 1) % 256 66 | elif char == '-': 67 | tape[ptr] = (tape[ptr] - 1) % 256 68 | elif char == '.': 69 | print(chr(tape[ptr]), end='') 70 | new = 0 71 | elif char == ',': 72 | val = input('\nInput: '[:new]) 73 | tape[ptr] = ord(val[0]) 74 | new = 1 75 | elif char in '[]': 76 | ind = find(ind, ptr) 77 | if ind is None: 78 | return 79 | 80 | ind += 1 81 | 82 | 83 | if __name__ == '__main__': 84 | if len(sys.argv) > 1: 85 | with open(sys.argv[1]) as file: 86 | data = file.read() 87 | run(data) 88 | -------------------------------------------------------------------------------- /tape-based/back.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | 4 | def run(code): 5 | size = max(len(lne) for lne in code) 6 | code = [c.ljust(size) for c in code] 7 | 8 | x, y = 0, 0 9 | a, b = 0, 1 10 | tape = [0] 11 | cell = 0 12 | 13 | while True: 14 | if (c := code[x][y]) == '\\': 15 | a, b = b, a 16 | elif c == '/': 17 | a, b = -b, -a 18 | elif c == '<': 19 | if cell: 20 | cell -= 1 21 | elif c == '>': 22 | cell += 1 23 | if cell == len(tape): 24 | tape.append(0) 25 | elif c == '-': 26 | tape[cell] ^= 1 27 | elif c == '+' and not tape[cell]: 28 | x, y = x + a, y + b 29 | elif c == '*': 30 | break 31 | 32 | x = (x + a) % len(code) 33 | y = (y + b) % size 34 | 35 | print(*tape) 36 | 37 | 38 | if __name__ == '__main__': 39 | if len(sys.argv) > 1: 40 | with open(sys.argv[1]) as file: 41 | data = file.readlines() 42 | run(data) 43 | -------------------------------------------------------------------------------- /tape-based/brainif.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | 4 | def run(code): 5 | cells = [0] 6 | ind = ptr = 0 7 | inp = False 8 | 9 | while ind < len(code): 10 | line = code[ind].strip() 11 | arr = line.split() 12 | 13 | if line and cells[ptr] == int(arr[1]): 14 | if 'inc' in line: 15 | cells[ptr] += 1 16 | elif 'right' in line: 17 | ptr += 1 18 | if ptr == len(cells): 19 | cells.append(0) 20 | elif 'left' in line: 21 | ptr = max(0, ptr - 1) 22 | elif 'goto' in line: 23 | ind = int(arr[3]) - 2 24 | elif 'input' in line: 25 | s = '' 26 | 27 | while not s: 28 | s = input('\n' * inp + 'Input: ') 29 | 30 | cells[ptr] = ord(s[0]) 31 | inp = False 32 | elif 'output' in line: 33 | print(chr(cells[ptr]), end='') 34 | inp = True 35 | 36 | ind += 1 37 | 38 | 39 | if __name__ == '__main__': 40 | if len(sys.argv) > 1: 41 | with open(sys.argv[1]) as file: 42 | data = file.readlines() 43 | run(data) 44 | -------------------------------------------------------------------------------- /tape-based/circlefuck.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import re 3 | 4 | 5 | def parse(code): 6 | reg = (r'\\(?:\d\d\d|' 7 | r'[\dA-F](?:$|[^\d]))') 8 | exp = r'((^|[^\\]) |\\( )|(\\)o)' 9 | 10 | for s in re.findall(reg, code): 11 | if len(s) == 4: 12 | val = oct(int(s[1:])) 13 | new = val[2:].zfill(3) 14 | else: 15 | new = f'x0{s[1:]}' 16 | code = code.replace(s, f'\\{new}') 17 | 18 | code = re.sub(exp, r'\2\3\4', code) 19 | code = ''.join(c for c in code if 20 | 31 < ord(c) < 127) 21 | code = bytes(code, 'utf-8') 22 | code = code.decode('unicode_escape') 23 | 24 | return [ord(c) for c in code] 25 | 26 | 27 | def find(code, ind, ptr): 28 | char = chr(code[ind]) 29 | if char == '[': 30 | if code[ptr]: 31 | return ind 32 | mode = 1 33 | else: 34 | if not code[ptr]: 35 | return ind 36 | mode = -1 37 | 38 | match = mode 39 | start = ind 40 | num = len(code) 41 | 42 | while match: 43 | ind = (ind + mode) % num 44 | sym = chr(code[ind]) 45 | if ind == start: 46 | return -1 47 | elif sym == '[': 48 | match += 1 49 | elif sym == ']': 50 | match -= 1 51 | return ind 52 | 53 | 54 | def run(code): 55 | code = parse(code) 56 | ind = ptr = 0 57 | new = 1 58 | 59 | while True: 60 | if (char := chr(code[ind])) == '>': 61 | ptr = (ptr + 1) % len(code) 62 | elif char == '<': 63 | ptr = (ptr - 1) % len(code) 64 | elif char == '+': 65 | code[ptr] = (code[ptr] + 1) % 256 66 | elif char == '-': 67 | code[ptr] = (code[ptr] - 1) % 256 68 | elif char == ',': 69 | val = input('\nInput: '[new:]) 70 | code[ptr] = ord(val[0]) 71 | new = 1 72 | elif char in '[]': 73 | ind = find(code, ind, ptr) 74 | if ind == -1: 75 | return 76 | elif char == '.': 77 | val = chr(code[ptr]) 78 | print(val, end='') 79 | new = 0 80 | elif char == '@': 81 | return 82 | elif char == '#': 83 | ind += 1 84 | elif char == '{': 85 | code.insert(ptr, 0) 86 | ind += 1 87 | elif char == '}': 88 | code.pop(ptr) 89 | 90 | ind = (ind + 1) % len(code) 91 | 92 | 93 | if __name__ == '__main__': 94 | if len(sys.argv) > 1: 95 | with open(sys.argv[1]) as file: 96 | data = file.read() 97 | run(data) 98 | -------------------------------------------------------------------------------- /tape-based/excon.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | 4 | def run(code): 5 | pool, cell = ([0] * 8, 7) 6 | 7 | for sym in code: 8 | if sym == ':': 9 | pool, cell = ([0] * 8, 7) 10 | elif sym == '^': 11 | pool[cell] ^= 1 12 | elif sym == '!': 13 | num = ''.join(map(str, pool)) 14 | print(chr(int(num, 2)), end='') 15 | elif sym == '<': 16 | cell -= 1 17 | 18 | 19 | if __name__ == '__main__': 20 | if len(sys.argv) > 1: 21 | with open(sys.argv[1]) as file: 22 | data = file.read() 23 | run(data) 24 | -------------------------------------------------------------------------------- /tape-based/mammalian.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import re 3 | 4 | 5 | def total(op, lst): 6 | if op: 7 | size = list(map(len, lst)) 8 | flat = sum(lst, []) 9 | m = len(flat) 10 | 11 | for k in range(m // 2): 12 | x, y = flat[k], flat[m - k - 1] 13 | n = m - k - 1 14 | 15 | if x > y: 16 | num = x // y 17 | flat[k] -= num 18 | flat[n] = (x + num) % 256 19 | elif x < y: 20 | num = y % x 21 | flat[k] += num 22 | flat[n] -= num 23 | 24 | for k in range(23): 25 | lst[k] = flat[:size[k]] 26 | flat = flat[size[k]:] 27 | else: 28 | for num in range(23): 29 | lst[num][0] += num + 1 30 | lst[num][0] %= 256 31 | 32 | 33 | def partial(op, curr, acc): 34 | if op == 2: 35 | curr.append(acc % 256) 36 | acc = 0 37 | elif op == 3: 38 | m = (len(curr) - 1) // 2 39 | acc = curr.pop(m) 40 | elif op == 4: 41 | m = (len(curr) - 1) // 2 42 | num = curr.pop(m) // 2 43 | curr.insert(0, num) 44 | curr.append(num) 45 | else: 46 | acc ^= sum(curr) 47 | 48 | return acc 49 | 50 | 51 | def run(code): 52 | ins = ( 53 | 'SEED', 'CONFLAGRATE', 54 | 'EXCRETE', 'CONSUME', 55 | 'FISSION', 'DIGEST', 56 | 'SPRINT', 'LEAPFROG', 57 | 'ACCEPT', 'PRONOUNCE' 58 | ) 59 | 60 | code = re.findall(f'({"|".join(ins)})', code) 61 | lst = [[0] for _ in range(23)] 62 | ind = ptr = acc = 0 63 | new = 1 64 | 65 | while ind < len(code): 66 | n = ins.index(code[ind]) 67 | curr = lst[ptr] 68 | if n < 2: 69 | total(n, lst) 70 | elif n < 6: 71 | acc = partial(n, curr, acc) 72 | elif n == 6 and acc < len(curr): 73 | ptr = (ptr + curr[acc]) % 23 74 | elif n == 7 and curr[-1]: 75 | ind = acc - curr[0] - 1 76 | elif n == 8: 77 | val = input('\nInput: '[new:]) 78 | new = 1 79 | if val: 80 | m = ord(val[0]) ^ acc 81 | lst[0].append(m % 256) 82 | elif n == 9: 83 | print(chr(acc % 256), end='') 84 | new = 0 85 | 86 | ind += 1 87 | 88 | 89 | if __name__ == '__main__': 90 | if len(sys.argv) > 1: 91 | with open(sys.argv[1]) as file: 92 | data = file.read() 93 | run(data) 94 | -------------------------------------------------------------------------------- /tape-based/minifuck.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | 4 | def run(code): 5 | tape = [0] * 8 6 | ptr = ind = 0 7 | inp = 1 8 | 9 | while ind < len(code): 10 | ins = code[ind] 11 | if ins == '<' and ptr: 12 | ptr -= 1 13 | elif ins in '.[': 14 | ptr += 1 15 | if ptr + 1 >= len(tape): 16 | tape.append(0) 17 | tape[ptr] ^= 1 18 | 19 | if ins == '.': 20 | lst = map(str, tape[:8]) 21 | if n := int(''.join(lst), 2): 22 | print(chr(n), end='') 23 | inp = 0 24 | else: 25 | val = input('\nInput: '[inp:]) 26 | val = bin(ord(val[0]))[2:].zfill(8) 27 | tape = [*map(int, val)] + tape[8:] 28 | inp = 1 29 | else: 30 | if not tape[ptr]: 31 | tape[ptr + 1] ^= 1 32 | ind += 1 33 | 34 | ind += 1 35 | 36 | 37 | if __name__ == '__main__': 38 | if len(sys.argv) > 1: 39 | with open(sys.argv[1]) as file: 40 | data = file.read() 41 | run(data) 42 | -------------------------------------------------------------------------------- /tape-based/suffolk.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | 4 | def run(code, limit=10): 5 | tape = [0] 6 | num = ind = 0 7 | ptr = acc = 0 8 | new = 1 9 | 10 | while num < limit: 11 | if (sym := code[ind]) == '>': 12 | ptr += 1 13 | if ptr == len(tape): 14 | tape.append(0) 15 | elif sym == '<': 16 | acc += tape[ptr] 17 | ptr = 0 18 | elif sym == '!': 19 | val = tape[ptr] + 1 - acc 20 | tape[ptr] = max(0, val) 21 | ptr = acc = 0 22 | elif sym == ',': 23 | inp = input('\nInput: '[new:]) 24 | acc = acc + ord(inp[0]) if inp else 0 25 | new = 1 26 | elif sym == '.' and acc: 27 | val = chr(acc - 1) 28 | print(val, end='') 29 | new = 0 30 | 31 | ind += 1 32 | if ind == len(code): 33 | ind = 0 34 | num += 1 35 | 36 | 37 | if __name__ == '__main__': 38 | if len(sys.argv) > 1: 39 | with open(sys.argv[1]) as file: 40 | data = file.read() 41 | if len(sys.argv) > 2: 42 | run(data, int(sys.argv[2])) 43 | else: 44 | run(data) 45 | --------------------------------------------------------------------------------