5 ms·
For a very simple, very readable Lisp reader, see Lumen's reader.l file: https://github.com/sctb/lumen/blob/master/reader.l https://github.com/sctb/lumen/blob/m
by sillysaurusx 6y ago
For a very simple, very readable Lisp reader, see Lumen's reader.l file: https://github.com/sctb/lumen/blob/master/reader.l https://github.com/sctb/lumen/blob/master/reader.l
It compiles to Javacript and Lua, so if you prefer reading those, you can:
https://github.com/sctb/lumen/blob/master/bin/reader.js https://github.com/sctb/lumen/blob/master/bin/reader.js
https://github.com/sctb/lumen/blob/master/bin/reader.lua https://github.com/sctb/lumen/blob/master/bin/reader.lua
It turns out that you can greatly simplify the code by e.g. "does an atom start with 0x, 0-9, or dash? if so, try converting it to a number."
That turns out to handle cases like 1e9 too. Whereas "1e" becomes a valid lisp symbol. So you can set it to your own value.
The emacs source code is also worth reading. It's not simple, but it's simple enough to be understandable: https://github.com/emacs-mirror/emacs/blob/7b3e94b6648ed00c6948c09267894b548b2868e7/src/lread.c#L2396-L2660 https://github.com/emacs-mirror/emacs/blob/7b3e94b6648ed00c6...
I like emacs source code because it handles "every possible real-world case that you can think of."
- grugagag 6y agoWhy are atoms that start with numbers accepted as symbols? Is there any usecase for that?
- gumby 6y agoThe use case is someone wanted to name a symbol that way. What’s wrong with that?
- grugagag 6y agoNothing wrong, was just wondering in case I missed something, in most languages by convention variables and functions cannot start with a number. I'm learning lisp and ran into this at some point just had the chance to ask someone experienced why.
- deleted 6y ago[deleted]
- timonoko 6y agoI had floating point implemented like that in Nokolisp. If the item contains anything except number characters, it is a symbol. Stupid, but very transparent eventually, because modified arithmetic operators test if a symbol can be interpreted as a floating point number.
- aidenn0 6y agoCommon Lisp has a concept of a "potential number" number which is a token that looks numberish, and it permits implementations to have custom numeric literals that are potential numbers. Even then though, if you have e.g. an asterisk or a percent or something it will not be a potential number. http://www.lispworks.com/documentation/HyperSpec/Body/02_caa.htm http://www.lispworks.com/documentation/HyperSpec/Body/02_caa...
- brendyn 6y agoI don't know about lumen, but i think in Scheme a symbol is "just" an immutable string, except that is used in a different ways for different purposes than strings which are for text. Here is Guile Scheme (not sure if this is standard or Guile specific): scheme@(guile-user)> 1e9 $2 = 1.0e9 scheme@(guile-user)> (string->symbol "1e9") $3 = #{1e9}# ;; This the double quote equivalent for symbols. scheme@(guile-user)> (define #{1e9}# "foo") scheme@(guile-user)> #{1e9}# $4 = "foo" As you can see, 1e9 is read as a number, but that does not mean it cannot also be a symbol, it just requires us sneaking around the reader or using the quoting construct.
- jhgb 6y agoLots of things you might want to name have natural names starting with numbers. If you're writing, say, a TSP solver, some of your procedures may be called 2-opt or 3-opt, for example.
- grugagag 6y agoI like that about lisp and surprisingly I came to like the kebab case, i find it quite easy to read at a glance. Before toying with scheme that and the parentheses seemed a bit dated but I came to like them quite a bit
- mcculley 6y agoI attribute a big chunk of my real-world education (back in the 90s) about how to ship a complicated software product on multiple architectures to reading the Mosaic and XEmacs source code. It helped me immensely when trying to ship my own software.
- ct-1111 6y agoFemtolisp has a nice, comprehensible Lisp reader written in C, with syntax very similar to Common Lisp: https://github.com/JeffBezanson/femtolisp/blob/master/read.c https://github.com/JeffBezanson/femtolisp/blob/master/read.c CMUCL has a fairly complex, but still comprehensible implementation of a Common Lisp reader: https://gitlab.common-lisp.net/cmucl/cmucl/-/blob/master/src/code/reader.lisp https://gitlab.common-lisp.net/cmucl/cmucl/-/blob/master/src... It uses a FSM to recognize numbers and symbols when tokenizing, and it demonstrates an implementation of Common Lisp readtables. Here's how the Common Lisp Hyperspec specifies the Common Lisp reader (as an algorithm): http://www.lispworks.com/documentation/HyperSpec/Body/02_b.htm http://www.lispworks.com/documentation/HyperSpec/Body/02_b.h...