Live data from Hacker News

Making a Python interpreter in 1024 bytes

austinhenley.com

31–40 of 120 posts

Re: Making a Python interpreter in 1024 bytes

#33
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?

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

Re: Making a Python interpreter in 1024 bytes

#34

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 :)

The 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

Re: Making a Python interpreter in 1024 bytes

#35
post #22

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?

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 se…

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

Re: Making a Python interpreter in 1024 bytes

#36
post #11

Earlier quoted context omitted.

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

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

You'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!

Re: Making a Python interpreter in 1024 bytes

#37
post #6
post #3

Earlier quoted context omitted.

And, there are multiple white space symbols! is different than So you also have to track the actual sequence of counts of white space used for each level, rather than just a simple count.

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.

Oh, 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)

Re: Making a Python interpreter in 1024 bytes

#39
post #22

Earlier quoted context omitted.

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 se…

> 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…

> Did you mix up your code points there? It's space that's ambiguous, not tab.

Space is always the same width, but tab means a variable number of spaces (usually either 4 or 8, but I've seen 3 before) depending on people's editor configuration.

What makes you say that the space character, U+0020, is ambiguous?

Post reply on HN