

Join the discussion
Write your take first — we'll ask for email only when you're ready to publish.
- Hacker News
- The author lost me right away.
>> I started with the most basic code I could think of: 1 + 2
OK, how did you fit that into 512/1024 bytes??? Did you try print(3 * * 1000)?
by shmoil - It's not real Python. It's essentially a made-up language that resembles a subset of Python.
That said, if I was building a small ugly toy Python implementation, I would probably also not implement long integers. Or the power operator. At least not in the first iteration.
by Liquid_Fire - this must be sorta the python demoscene or pythoscene.by larodi
- I was very disappointed that this is “interpreting” some tiny made up language.
This is not Python, or even within three orders of magnitude of Python.
by Scubabear68 - Yeah the amount of Python code that would work here is probably not a lot more than this specific FizzBuzz example. Lots of shortcuts taken, which I guess is understandable.
- It’s true, the title should have said “Python-like”by SPBS
- This is really cool! It's so fun to see what you can achieve and what's optional. I have seen the 'single character variable' limitation in some other minilangs before, but using the source itself as the target of function calls and loops is new to me. It does make a lot of sense but I wouldn't have thought of that.by anitil
- but using the source itself as the target of function calls and loops is new to me
This was standard practice on interpreters for 8-bit microcomputers; with only a 64K total address space, creating an AST first seems immensely wasteful, so you interpret from the source directly.
I believe shells still do this when you run shell scripts; I know the DOS COMMAND.COM definitely does.
by userbinator - This seems to be in the same spirit as Justine Tunney's SectorLISP. Very cool.by tempodox
- SectorLISP makes an important question in its implementation: how much can strip down Lisp before stops being Lisp. Same is not done for submitted interpreter. So, although SectorLISP goal is to be a Lisp-reduced-to-its-essentials implementation, the Python-1024 goal seems to be imitating Python in most minimal code possible.by forgotpwd16
- Or sectorC
https://github.com/xorvoid/sectorc
Edit: I wonder if sectorC could compile python1024
by gabrielsroka - But to be honest, I wonder what is the smallest interpretable and practical Turing Complete VM? I would argue that implementing a brainfuck that we lower Python interpreter to, or even say like an interpreter untyped lambda calculus or SKI combinator would be very useful, especially for the hardware bootstrapping.
I'm talking about things like SectorLisp https://justine.lol/sectorlisp/
by stevefan1999 - A single machine code instruction is sufficient: <https://en.wikipedia.org/w/index.php?title=One-instruction_s...>by teddyh
- I think we would need to balance practicability and code size since they tend to be mutually exclusive. Generally speaking, code size is not an important metric to make useful code, and usefulness is usually not the main point of code golf exercises such as this one.
The exception to this is obviously embedded systems with very low amounts of resources where C and assembly are practically unrivaled.
by rbtms - Sector C might have some insights into how to make this even smaller or more featurefull. It has some interesting hacks. https://xorvoid.com/sectorc.htmlby kristianp
- Also by the author:
Let's make a teeny tiny compiler
by andai - To be precise this is 1024 bytes of C, which compiles to a binary many times larger, and implements a very tiny subset of Python.
loops work by jumping backwards and reparsing the source each iteration
This is how the DOS .bat processing works; not sure if Unix-style shells are the same, as I've never had the need to exploit that "feature".
Another comment here has mentioned C4, but another extremely dense (and slightly larger, since it wasn't actually deliberately(!) "code-golfed") interpreter you may want to look at is the J Incunabulum:
https://www.jsoftware.com/ioj/iojATW.htm
More generally, the array programming culture seems to consider this level of density the norm:
by userbinator - Bash lines are buffered, so modifying behind the program position doesn't really work, but you can self-append to the file to keep a script going infinitely.by shakna
- Reading the article, I can't believe I just found out Code Golf is a thing. I've been a programmer for more than a decade.
But yes, amazing project! I like that it's human-made :)
- by NooneAtAll3
- This is my favorite : https://www.cise.ufl.edu/~manuel/obfuscate/pi.cby rottc0dd
- Get thee to
Also https://github.com/nanochess
And then if you really want to go large
https://phoboslab.org/log/2021/09/q1k3-making-of
I still have a soft spot for https://www.pouet.net/prod.php?which=1221
by Lerc - The quintessential example is donut.c. I was amazed when I first came across it.by kylecazar
- For those who actually need something like this in production, there is Snek: <https://sneklang.org/> “Snek is a tiny embeddable language targeting processors with only a few kB of flash and ram.”by teddyh
- Yes, but compiling or modifying Snek from source is very challenging. I wish it was one single C file for an example base like Posix, instead of many files for many platforms plus a custom parser in Python (Lola).by jrdres
- Or Forth.by eru
- The code makes me smile, because it's nasty. This isn't like C4, a tiny but complete C compiler which does error checking on its subset. Instead, this is worse than Sector C, which takes every shortcut and just plain assumes everything in the source is right.
This "Python" just plain assumes for keywords: Any "f" is a "for [x] in range[y]" (exactly that, no other for's). Any "w" is a "while". Any "i" is an "if". Any "d" is a "def". Any "p" is a "print("
Nasty, nasty.
(Also nasty is that the code snippets in the article has more comments than the github copy of the "readable" version. You need the article to understand what's going on.)
This is a just a bit too simple for a "Tiny Python". If somebody is willing to allow a few more K's of bytes, I'd love to see at least lists & dicts here--Lisp can do them!
by jrdres - Reminds me of the good 'ol Apple II BASIC. You can name your variables whatever you want, but only the first two letters matter.by adamddev1
- It looks very much like some techniques used when minifying JavaScript.by XYen0n
- If you are willing to sacrifice performance, you can implement dicts via linear lookup in much less code than a proper hash table.by eru