https://github.com/izo0x90/adding-goto-statement-to-python
Just what it sounds like, decorator that patches function bytecode to allow GOTO's *ROFL*
https://github.com/izo0x90/adding-goto-statement-to-python
basic breaking-bad bytecode bytecode-manipulation cpython djikstra donotuse evil-mode fun goto interpreter obscure python python-internals python-interpreter regression ridiculous stackmachine toy-project
Last synced: 6 months ago
JSON representation
Just what it sounds like, decorator that patches function bytecode to allow GOTO's *ROFL*
- Host: GitHub
- URL: https://github.com/izo0x90/adding-goto-statement-to-python
- Owner: izo0x90
- License: gpl-3.0
- Created: 2021-10-24T13:40:38.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2023-04-03T01:50:28.000Z (over 2 years ago)
- Last Synced: 2025-03-06T04:17:32.521Z (10 months ago)
- Topics: basic, breaking-bad, bytecode, bytecode-manipulation, cpython, djikstra, donotuse, evil-mode, fun, goto, interpreter, obscure, python, python-internals, python-interpreter, regression, ridiculous, stackmachine, toy-project
- Language: Python
- Homepage:
- Size: 30.3 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Adding GOTO statement into Python
[GOTO Considered harmful](https://homepages.cwi.nl/~storm/teaching/reader/Dijkstra68.pdf) by Edgar Dijkstra
"Your scientists were so preoccupied with whether or not they could, they didn’t stop to think if they should." - That JURASSIC PARK guy
You guys remember basic.
Just what it sounds like, a decorator that patches the bytecode of a decorated function to allow variable assignments to specific string constants to be treated as GOTO/JUMP statements and LABELS to jump to.
Just in case it needs to be said, yes this is a joke and has no actual uses, nor should anyone use it for anything real other than having fun.

All relevant code in main.py.
Use:
```Python
@goto_mutator # << Decorate func you want to patch GOTOs into with mutator
some_func():
pass # << Do regular stuff
pass
pass
x = "#GOTO_mylabel" # << This is the goto statement, var name does not matter,
# statement has to be a string const,
# starting with keyword #GOTO_ after that label name
pass
pass
pass # << Do more regular stuff, of course after patching
# the goto statemet will skip right over this secion
x = "#GOTOLABEL_mylabel" # << This is the LABEL we will that will be jumped to
pass
pass # << Keep doing stuff
```
You should be able to define as many jumps and labels as desired, mutator will build a map and patch in jumps.
Live REPL demo here:
[https://replit.com/@HristoGueorguie/Adding-GOTO-statement-to-Python-cue-eveil-laughter](https://replit.com/@HristoGueorguie/Adding-GOTO-statement-to-Python-cue-eveil-laughter).
---
Links to refs:
[https://rushter.com/blog/python-bytecode-patch/](https://rushter.com/blog/python-bytecode-patch/) - Patching bytecode article by Artem Golubin
[https://www.aosabook.org/en/500L/a-python-interpreter-written-in-python.html](https://www.aosabook.org/en/500L/a-python-interpreter-written-in-python.html) - Python Interpreter in Python article by Allison Kaptur