7 ms·
Making a Python interpreter in 1024 bytes
- TZubiri 10d agoA lot of criticism of python often mentions the whitespace as lexical scope tokens, and that criticism is usually posited by users of the language. As implementer of an interpreter, did you feel that whitespace for lexical scoping made the job of writing the lexer significantly more complex?
- nomel 10d agoAnd, there are multiple white space symbols! <space><space><tab><space> is different than <space><tab><space><space> So you also have to track the actual sequence of counts of white space used for each level, rather than just a simple count.
- rmunn 10d agoOr you just forbid mixing spaces and tabs in the same indentation sequence, the way most whitespace-sensitive languages seem to end up doing. Or you make a slightly more reasonable rule: spaces may follow tabs, but no tabs may follow a space. That's at least unambiguous.
- fc417fc802 10d agoBut it also feels arbitrary and annoyingly restrictive. On top of that there are at least 25 whitespace codepoints in UTF. Should your language really be opinionated about when, where, and in what order (for example) the "mongolian vowel separator" appears?
- Mogzol 10d ago> and annoyingly restrictive How so? In what scenario would you ever need to use a sequence like <tab><space><tab> in indentation in your source code? Let alone using esoteric Unicode whitespace characters for indentation. I think it is perfectly reasonable for the language to make the restriction that indentation must be either all tabs, tabs followed by spaces, or all spaces.
- fc417fc802 10d ago> In what scenario would you ever need to use a sequence like <tab><space><tab> in indentation in your source code? Writing a lisp in an editor that doesn't do boneheaded things with tabs? That's the only one I've personally run into but lack of imagination is hardly a good excuse to implement arbitrary restrictions. > Let alone using esoteric Unicode whitespace characters for indentation. How do you know what's esoteric in other countries? I certainly don't. I'm not an expert in linguistics but I'm sure that people everywhere in the world write computer programs at this point. > I think it is perfectly reasonable ... Without any concrete justification? Why would entirely artificial restrictions ever be seen as reasonable?
- rmunn 10d agoWhat "boneheaded things with tabs" are you referring to? The picture I'm piecing together from your comments suggests that you may use tab characters in a different way than most people seem to, so I'd quite like a further explanation of how you use tab characters and how you expect an editor to handle them.
- fc417fc802 10d ago> you may use tab characters in a different way than most people seem to As far as I'm aware there are three primary and entirely independent uses for tabs. Indentation, field separation, and typesetting. When it comes to typesetting and also to display of fields with a narrow maximum width the concept of a tabstop is useful. Boneheaded things with tabs was in reference to code editors (where tabs are more or less exclusively used for indentation) most often failing to provide the ability to disable tabstop. In practice this generally hasn't been a point of friction because until fairly recently the most popular languages (ie C & co) didn't support constructs that would lead to adding any indentation outside of the prefix of a line.
- rmunn 10d agoAgreed... but see my Lisp example in https://news.ycombinator.com/item?id=49593406 https://news.ycombinator.com/item?id=49593406 for a place where tabs end up unavoidably ambiguous. Because in that `cond` example the tabs look like they're used for indentation, but they're actually being used for alignment, something that falls more under typesetting than under indentation. A smart editor that has read the Lisp code (note that I'm using "read" in its Lisp meaning, i.e. "parsed into a syntax tree") can make those tab characters correctly align the forms the way you'd want them to be aligned for maximum understanding... but really, you would want space characters, not tab characters, in that context. So even though in most cases those three uses for tabs are independent, in some languages they get muddled. F# is another language where you may want to align text on line two with the middle of line one, e.g. if you write let person = { FirstName = "Bill" LastName = "Gates" } Now, that's considered bad style according to https://learn.microsoft.com/en-us/dotnet/fsharp/style-guide/formatting#avoid-formatting-that-is-sensitive-to-name-length https://learn.microsoft.com/en-us/dotnet/fsharp/style-guide/... — and they're entirely write to recommend against that style — but it's legal syntax. And it's probably why https://learn.microsoft.com/en-us/dotnet/fsharp/style-guide/formatting#use-spaces-not-tabs https://learn.microsoft.com/en-us/dotnet/fsharp/style-guide/... forbids tab characters entirely in F# code. (Those two links point to different anchors in the same document, BTW: the HN link shortening is going to make them look identical until you hover over them).
- rmunn 10d agoI mean, obviously that one should only appear within Mongolian text and not within indentation. To state explicitly what should be implicitly obvious, there is no valid reason (that I'm aware of, I welcome any non-facetious correction) to use any character except U+0009 and U+0020 within indentation. Horizontal Record Separator? Zero-width joiner? Language-specific whitespace characters like your example? All make sense within human text (well, maybe not HRS), but in programming, they should be eschewed in favor of the characters that can be typed in every single keyboard layout in the world. Even languages that don't put spaces between words, such as Thai, still put spaces between sentences (or comma phrases) and therefore keep the space bar in their keyboard layout. And since mixing tabs and spaces (even between lines, where some lines are tab-indented and some are space-indented) creates problems for whitespace-sensitive language, there's a reason why every whitespace-sensitive language I'm aware of has tended to either outright forbid, or at least discourage, U+0009 and its ambiguous meaning (since its meaning isn't clear until you know people's editor configurations, which are usually not available to the validation code running in CI or on other people's machines).
- fc417fc802 10d ago> To state explicitly what should be implicitly obvious, there is no valid reason ... There doesn't need to be an articulable reason. Or rather there's generally no expectation that a central authority will be able to reliably enumerate such. Everything should default to being permitted and only ever be restricted for good reason. But since you asked. U+2003 for example carries formatting information. Maybe an editor could be written (or even already exists) that would find that useful. Who is any third party to dictate that? U+00A0 similarly communicates information about the desired formatting and I can see no reason it would be unreasonable for someone to use it nor why its use should pose a technical challenge to a compiler. > creates problems for whitespace-sensitive langauge Does it? That seems like an invented problem to me. You have a running prefix composed of arbitrary whitespace characters. Any change in that prefix is a change in the level of indentation. You can add or remove arbitrary amounts from the end of the prefix. In the event you remove from it the result must exactly match the previous stack level. What's so complicated about this? > outright forbid, or at least discourage, U+0009 and its ambiguous meaning (since its meaning isn't clear until you know people's editor configurations ... Did you mix up your code points there? It's space that's ambiguous, not tab. Regardless I think that a compiler worrying about the specifics of text editors or other tooling would be backward information flow and a massive abstraction violation. Semantic meaning is entirely dictated by the compiler, not the other way around. There's no convincing reason (IMO) to impose restrictions that aren't technically necessary or to otherwise needlessly employ solutions that would reduce generalization.
- hmry 10d agoOh, that's really elegant! I've got a whitespace sensitive language of my own, and I think I'll change it to use that rule! Thanks! (Until now, I went with the standard approach: Remember the leading whitespace of the previous line. Then compare with the new line's leading whitespace: If they are the same, then no change in indentation. If the old one is a prefix of the new one, it's an indent. If the new one is a prefix of the old one, it's a dedent. If neither, it's an error)
- rmunn 10d agoThat seems like a decent way to handle the mixed-spaces-and-tabs scenario, even between lines: one line starts with `<tab><tab>`, the next line `<tab><tab><sp><sp><sp><sp>`, that's an indent. (Probably someone who likes 4-space indents and 8-space tab characters). Follow that up with `<sp>*12` and that looks like the same indent to someone who uses 4-space tabs, but not the same indent to someone who uses 8-space tabs. So your proposed prefix-matching rule would correctly flag that scenario, forcing people stop and figure it out. EDIT to add this P.S.: Actually, my "spaces may follow a tab but tabs may not follow a space" rule, while elegant, is incomplete. Your prefix-matching rule is actually necessary in order to deal with the "two tabs on one line, twelve spaces on the next line" situation. That would be legal under the "spaces may follow a tab but tabs may not follow a space" rule, but it's ambiguous whether that's an indent or a dedent. If tabs mean eight spaces then it's going from 16 to 12, a dedent; if tabs mean four spaces then it's going from 8 to 12, an indent.
- jubilanti 10d ago> <space><space><tab><space> is different than <space><tab><space><space> in my view, both are the same, both `is` (or ===) an IndentationError raise
- fc417fc802 10d agoIt's just a stack containing strings at the end of the day. Really not a big deal.
- nomel 7d agoCare to give it a go? I'm curious to see how many bytes of the 1024 bytes your implementation would eat up.
- TZubiri 10d agoRight, pointers to strings but yeah. Essentially the whitespace count specifies the stack depth at which a line is to be executed. A decrease in stack depth means all superior levels are terminated. Doesn't affect function call stacks though.
- TZubiri 10d agoFor a 1024 byte implementation (and even way more complex impl.) You would just force one whitespace char, and definitely no mixing.
- zephen 10d ago> that criticism is usually posited by users of the language. Uhhh, no. Sure, it's posited by people who feel they are are forced to use it, but it's basically unlearning other syntax. Here's a study about people with no experience. They do better with python: https://www.researchgate.net/publication/262256894_An_Empirical_Investigation_into_Programming_Language_Syntax https://www.researchgate.net/publication/262256894_An_Empiri... When the scala language made whitespace optional, it was very divisive, but now it's extremely well accepted.
- TZubiri 10d agoI meant users of languages ( application programmers) as opposed to compiler programmers, not python programmers specifically, so I'm including devs that use other languages and see in python a tool that they would consume.
- mbirth 10d agoAt a former workplace where most stuff was done in PHP, some colleagues used whitespace very liberally. Like, indentation was just a random amount of whitespace, every line slightly different. Sometimes 2 or more spaces between keywords, etc. After that experience Python code is like eye-bleach to me.
- pansa2 10d ago> did you feel that whitespace for lexical scoping made the job of writing the lexer significantly more complex? Significant indentation requires a more complex lexer because it means the lexical grammar is no longer regular. The lexer can't just be a finite state machine, instead it has to maintain a stack of previous indentation levels. But I don't think many modern languages have a regular lexical grammar anyway. Without significant indentation, some other features still require the lexer to maintain a stack - e.g. string interpolation (Python's f-strings).
- ni5arga 10d agothe blog post is pretty well-written! loved how he wrote about the the code-golfing part.
- teddyh 10d agoFor those who actually need something like this in production, there is Snek: <https://sneklang.org/ https://sneklang.org/> “Snek is a tiny embeddable language targeting processors with only a few kB of flash and ram.”
- hankbond 10d agoGood use of free will and well-written. Very nice walkthrough austin!
- Scubabear68 10d agoI 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.
- SPBS 10d agoIt’s true, the title should have said “Python-like”
- happycube 10d agoTBF the fizzbuzz code works just fine in CPython.
- benatkin 10d agoIndeed, for some code, CPython and this interpreter produce identical output. I gave it an upboat.
- benatkin 10d agoI like python subset. However, many don't see it that way.
- hmry 10d agoYou're right, mathematically it's undeniably true. However... One could imagine an even smaller subset interpreter. It's an interpreter for a subset of Python, consisting only of the programs that print "Hello World". Since it doesn't do any error checking, for all other programs the output is undefined. Implementing it is very simple: Just ignore the input file, and print "Hello World". As a bonus, it's an interpreter for the subset of "Hello World" programs in all other programming languages too! </tongue-in-cheek>
- Sophira 9d agoThis has actually been done: https://esolangs.org/wiki/Hello https://esolangs.org/wiki/Hello While quines (programs that print their own source code) aren't possible in Hello, there's a version that makes it possible: https://esolangs.org/wiki/Hello_Plus_Plus https://esolangs.org/wiki/Hello_Plus_Plus And if you say the original isn't good enough because it can't do anything else... well, esolangs have you covered there too: https://esolangs.org/wiki/HQ9_Plus https://esolangs.org/wiki/HQ9_Plus (It should be noted that these are all joke esolangs. There are actual languages on the wiki that are very interesting though - I would definitely recommend you take them out!)
- tempodox 10d agoThis seems to be in the same spirit as Justine Tunney's SectorLISP. Very cool. https://justine.lol/sectorlisp/ https://justine.lol/sectorlisp/
- gabrielsroka 10d agoOr sectorC https://github.com/xorvoid/sectorc https://github.com/xorvoid/sectorc Edit: I wonder if sectorC could compile python1024
- forgotpwd16 10d agoSectorLISP 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.
- anitil 10d agoThis 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.
- userbinator 10d agobut 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.
- jrdres 10d agoThe 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!
- eru 10d agoIf you are willing to sacrifice performance, you can implement dicts via linear lookup in much less code than a proper hash table.
- FridgeSeal 10d agoIt’s Python, you’ve already sacrificed performance, what a little bit more?
- eru 10d agoThat's the spirit! (Slightly less silly: the folks at https://github.com/faster-cpython https://github.com/faster-cpython are doing great work, too.)
- marcelo-earth 10d agoReading 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 :)
- kylecazar 10d agoThe quintessential example is donut.c. I was amazed when I first came across it. https://www.a1k0n.net/2006/09/15/obfuscated-c-donut.html https://www.a1k0n.net/2006/09/15/obfuscated-c-donut.html
- bolangi 10d agoThanks, this makes me happy.
- eclipticplane 10d ago> This was my first attempt at obfuscated C and I feel it's pretty amateurish I love feelings of inadequacy at 11:07pm on a Sunday. Here's the judges' remarks and author commentary on the second edition of this from the 2006 results: https://www.ioccc.org/2006/sloane/index.html https://www.ioccc.org/2006/sloane/index.html
- deleted 10d ago[deleted]
- Forgeties79 10d agoAbsolute legend > it’s worked on every system I’ve tried so far though Still my favorite part of the whole thing. Classic moment of “who among us hasn’t doesn’t this?” lol
- agumonkey 10d agoAlways fun to see this pop up years later. (Damn, 20 years now)
- HappMacDonald 10d ago
- luciana1u 10d ago[dead]
- ssfdg 10d agoI don't understand the point of this. If they wanted to make a Python interpreter, why didn't they just ask an AI to do it?
- Donald 10d ago> To feel human, I write code by hand on the weekends. "Things won are done; joy’s soul lies in the doing." - Troilus and Cressida
- bblb 10d agoWhy do anything. Why even do the AI version of this. Probably curiosity. If there's an AI version of code golfing, I'd be curious to see it. Maybe they golf worse or much better than us meat bags.
- userbinator 10d agoJust ask your preferred AI tool if it can shrink the OP's code while keeping the same functionality; I suspect it could. At this point, LLMs are probably no worse than an average human at sizecoding or targeting resource-constrained platforms in general; https://news.ycombinator.com/item?id=49226923 https://news.ycombinator.com/item?id=49226923 is a recent example of how powerful they've become.
- CornerPlot 9d ago[dead]
- timbit42 9d agoThe first line of the article answers this.
- userbinator 10d agoTo 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 https://www.jsoftware.com/ioj/iojATW.htm More generally, the array programming culture seems to consider this level of density the norm: https://news.ycombinator.com/item?id=45800777 https://news.ycombinator.com/item?id=45800777
- peter_d_sherman 10d agoThe condensed version is impressive to be sure, but I'm an even bigger fan of the readable version: https://github.com/AZHenley/python1024/blob/main/python1024_readable.c https://github.com/AZHenley/python1024/blob/main/python1024_... Well done!
- andai 10d agoAlso by the author: Let's make a teeny tiny compiler https://news.ycombinator.com/item?id=36102460 https://news.ycombinator.com/item?id=36102460
- yuxinking 10d ago[dead]
- sriniwasx 10d ago[dead]
- galkk 10d agoI hate when they measure the size of source code instead of the size of a binary. I appreciate .kkrieger much more than this monstrosity
- einpoklum 10d agotl;dr: 1. Choose a small fragment of the language 2. Adapt an existing interpreter 3. Use a tool to shorten the code, 'minify'
- krttherealest 10d agowell written, looks cool ngl
- stevefan1999 10d agoBut 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/ https://justine.lol/sectorlisp/
- rbtms 9d agoI 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.
- teddyh 9d agoA single machine code instruction is sufficient: <https://en.wikipedia.org/w/index.php?title=One-instruction_set_computer&oldid=1363456096 https://en.wikipedia.org/w/index.php?title=One-instruction_s...>
- MiroslavPokorny 9d agoLove the challenge, but im sorry eliminating error checking is cheating and means its not a proper self contained interpreter, because it requires a human to pre-verify and enter only correct programs.
- shmoil 9d agoThe 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)?
- Liquid_Fire 9d agoIt'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.
- yenepho 9d ago> To feel human, I write code by hand on the weekends. Like a psychopath. /s
- larodi 9d agothis must be sorta the python demoscene or pythoscene.
- timonoko 9d agoMore interesting idea was to make compiler for some subset of Python bytecode. But Antigravity said this idea suxs, for some complex reasons. If the target is ATtiny, makes more sense to ask the AI translate Python to C++, which approach works amazingly well indeed.
- jeanmichelselli 9d agoInteresting, especially when you know they used to ship a complete BASIC interpreter in less than 32 KB in the past..
- timbit42 9d agoTiny BASIC is under 4K. Bare bones Forths are about 4K.
- GregBuchholz 9d agoThings That Turbo Pascal is Smaller Than: https://prog21.dadgum.com/116.html https://prog21.dadgum.com/116.html
- gabrielsroka 9d agoMicrosoft BASICs from the 70s/80s were 4-8 KB
- kristianp 9d agoSector C might have some insights into how to make this even smaller or more featurefull. It has some interesting hacks. https://xorvoid.com/sectorc.html https://xorvoid.com/sectorc.html