4 ms·
> One of the interesting insights I got while building my own compiler was that despite what every compiler book says, there are languages that can be compiled
by justinpombrio 9y ago
> One of the interesting insights I got while building my own compiler was that despite what every compiler book says, there are languages that can be compiled on the fly without building an AST.
PL (but not compiler) researcher here. What you're saying sounds to me like "One of the interesting insights I got while programming my own game was that despite what every game programming book says, there are games that can be programmed without defining any functions." I mean, yes, you can do that, and it may in fact be more efficient, but it's going to cause trouble down the line if you ever want to do something more complex.
- skybrian 9y agoYes, there's a reason compiler books start with parsing and building an AST. But I think studying Lua [1] or the Wren codebase [2] is still worthwhile, provided you keep in mind that they're relatively simple languages that are designed to be both fast and embedded. [1] https://www.lua.org/doc/jucs05.pdf https://www.lua.org/doc/jucs05.pdf [2] https://github.com/munificent/wren https://github.com/munificent/wren
- mojuba 9y agoNo, it's not the same thing. A compiler without the AST phase itself is more compact and arguably more beautiful, but again not every language is compile-able this way (C++ being a notable example).
- compiler-guy 9y agoYeah, the grandparent is only true if you don't want to do interesting things with the language, like nearly any but the most basic peephole optimization. But a fun fact, if you know about single-pass compiling, you can figure out a lot of why the original C syntax is what it is. A declaration is a "reserve some memory" statement. Local variables had to be declared at the beginning of scope so that the compiler could deallocate them at scope close. Without an AST, the compiler had to produce instructions immediately. With a true AST, you don't need such a restrictive syntax. There are many other possible examples.
- munificent 9y ago> What you're saying sounds to me like "One of the interesting insights I got while programming my own game was that despite what every game programming book says, there are games that can be programmed without defining any functions." Ex-game programmer and current PL programmer here. I think a better analogy is that you can make a game without having a scene graph. And, indeed, you can. And it works fine for many simple games, though as some scales it starts to be a hindrance.
- HumanDrivenDev 9y agowell there's no need for an AST in a concatenative language - that might be what he's talking about.