4 ms·
> Parsing S-expressions is super easy. No, it's !@#$%^& not. This pisses me off every single time I see it. Parsing s-expressions requires on-the-fly allocat
by bsder 9y ago
> Parsing S-expressions is super easy.
No, it's !@#$%^& not.
This pisses me off every single time I see it.
Parsing s-expressions requires on-the-fly allocation of memory and something resembling recursion.
If you can't allocate memory--good luck.
If you don't have recursion in your language, you're going to have to build it yourself--good luck.
And god help you if you have dot notation--(a b) is a tree of (a . (b . null)) while (a . b) is an actual pair.
Clojure shooting the idea of building things out of cons pairs and using lists composed of arrays is one of its most important design choices.
- taeric 9y agoStrictly, parsing s-expressions shouldn't require the allocation of memory "on-the-fly". At least, not moreso than anything else you will be doing. Now, defun/define/etc will require more memory management than people would care to admit to. However, just parsing the s-expression is dead simple. Recursion will make it easier, but I'm actually not sure it is required here. For the most part, just keeping a manual stack of what you're dealing with would be easy enough and won't take much effort. Again I'll stress, though; that anyone that thinks implementing a lisp is dead easy, is likely overstating how easy it can be done. Easier than you could think. Not guaranteed easier than you think.
- phoe-krk 9y agoParsing s-expressions requires on-the-fly allocation of memory and something resembling recursion. Parsing any code requires on-the-fly allocation of memory because your function can contain 10 or 100 or 1000 lines of code and, when you are at the beginning, you don't know how long the whole function is. Parsing any code requires something resembling recursion because your function can contain 3 or 10 or 20 nested scopes and, when you are at the beginning, you don't know how deep the whole function is. Of course that you can say that you can pre-read the code and compute how much memory you need beforehand, but that applies to Lisp as much as it applies to any other language.
- bsder 9y ago> Parsing any code requires on-the-fly allocation of memory because your function can contain 10 or 100 or 1000 lines of code and, when you are at the beginning, you don't know how long the whole function is. This is not necessarily true. Parsing a language can be bounded by the grammar. If you don't have indefinitely nested structures (like arithmetic open parentheses), your parsing is linear and you can bound the memory (ie. your parsing memory cannot be longer than your input string, for example). I believe that Tcl and Microsoft Basic fell into this category. (I believe that Ousterhout used to actually generate code in the input buffer behind the current parsing point precisely because Tcl was designed to do this). > Parsing any code requires something resembling recursion because your function can contain 3 or 10 or 20 nested scopes and, when you are at the beginning, you don't know how deep the whole function is. Indefinitely nested lexical scoping is not a requirement for a language. Famously Javascript screws up all manner of programmers because they expect that to be true, but it is not. And many languages start out with a single global scope and grow lexical scoping only after they gain some complexity.
- pubby 9y agoI'm a little confused at where you're coming from. Do you program microcontrollers or something? Since when has recursion or the ability to allocate memory been an issue? 30 years ago, maybe? I understand lisp isn't trivial to implement if all you have is assembly with no OS or libraries, but beyond that it's pretty damn easy. Even in C it only takes a few dozen lines. > Javascript I'm not a JS programmer, but I think JS has had infinite lexical scope at function-level since the beginning. It does not have scope at block-level, which is the confusing part.
- tripzilch 9y agoHow is this relevant? Nobody said that "Parsing any code is super easy".