Live data from Hacker News

Making a Python interpreter in 1024 bytes

austinhenley.com

21–30 of 120 posts

Re: Making a Python interpreter in 1024 bytes

#21
post #6

Earlier quoted context omitted.

Or 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.

But 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?

> and annoyingly restrictive

How so? In what scenario would you ever need to use a sequence like 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.

Re: Making a Python interpreter in 1024 bytes

#22
post #6

Earlier quoted context omitted.

Or 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.

But 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?

I 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).

Re: Making a Python interpreter in 1024 bytes

#24
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!

Re: Making a Python interpreter in 1024 bytes

#25
post #14
post #2

A 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?

> 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_Empiri... When the scala language made whitespace optional, it was very divisive, but now it's extremely…

At 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.

Re: Making a Python interpreter in 1024 bytes

#26
post #21

Earlier quoted context omitted.

But 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?

> and annoyingly restrictive How so? In what scenario would you ever need to use a sequence like 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.

> In what scenario would you ever need to use a sequence like 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?

Re: Making a Python interpreter in 1024 bytes

#27
post #11

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.

It’s true, the title should have said “Python-like”

I like python subset. However, many don't see it that way.

Re: Making a Python interpreter in 1024 bytes

#28
post #11

Earlier quoted context omitted.

It’s true, the title should have said “Python-like”

TBF the fizzbuzz code works just fine in CPython.

Indeed, for some code, CPython and this interpreter produce identical output. I gave it an upboat.

Re: Making a Python interpreter in 1024 bytes

#29

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.

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.

Re: Making a Python interpreter in 1024 bytes

#30
post #24

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…

If you are willing to sacrifice performance, you can implement dicts via linear lookup in much less code than a proper hash table.
Post reply on HN