Live data from Hacker News

Compiling a Lisp: Reader

bernsteinbear.com

11–20 of 30 posts

Re: Compiling a Lisp: Reader

#11
post #8

I've been working my way through Beautiful Racket [1] over the last week or so. It's been awesome. If this kind of thing is of interest to you, I highly recommend it. It's a book focused on a way of thinking that involves building small domain-specific languages to solve your problems. It's so easy. Racket (if you're not aware of what it is) is just a lisp, but the standard library includes an incredible amount of su…

Thank you for sharing the link to that book. I'm very much not a pro coder, just a sysadmin-turned-devops person who writes Terraform, Ansible, and CloudFormation YAML. (And Emacs-Lisp) This book seems really fascinating to me and the idea that I could put together my own devops-oriented DSL is a very intriguing idea!

Re: Compiling a Lisp: Reader

#12
post #8

I've been working my way through Beautiful Racket [1] over the last week or so. It's been awesome. If this kind of thing is of interest to you, I highly recommend it. It's a book focused on a way of thinking that involves building small domain-specific languages to solve your problems. It's so easy. Racket (if you're not aware of what it is) is just a lisp, but the standard library includes an incredible amount of su…

Thank you for sharing the link to that book. I'm very much not a pro coder, just a sysadmin-turned-devops person who writes Terraform, Ansible, and CloudFormation YAML. (And Emacs-Lisp) This book seems really fascinating to me and the idea that I could put together my own devops-oriented DSL is a very intriguing idea!

If you are new into lisp I highly recommmend you htdp2. It also uses subsets of racket to teach. I love how it teaches the fundamentals of programming. Simple and clean.

https://htdp.org/2018-01-06/Book/

Re: Compiling a Lisp: Reader

#13
post #12

Earlier quoted context omitted.

Thank you for sharing the link to that book. I'm very much not a pro coder, just a sysadmin-turned-devops person who writes Terraform, Ansible, and CloudFormation YAML. (And Emacs-Lisp) This book seems really fascinating to me and the idea that I could put together my own devops-oriented DSL is a very intriguing idea!

If you are new into lisp I highly recommmend you htdp2. It also uses subsets of racket to teach. I love how it teaches the fundamentals of programming. Simple and clean. https://htdp.org/2018-01-06/Book/

Thank you! Greatly Appreciated :)

Re: Compiling a Lisp: Reader

#14

Oh cool, someone else besides me posted my blog :D I'm the author and here to answer questions and take constructive criticism. If you want to comment but don't have an HN account, please check out the mailing list: https://lists.sr.ht/~max/compiling-lisp

I love seeing long form blog posts like this! Whilst its not something I’d personally do for a long time, its great to have resources on the web that go into detail in advanced topics like building a lisp interpreter. From another lisper, congrats and well done!

I agree that I really enjoy reading in-depth blog post series, but after releasing a 3-part series myself, I'm not sure if I would do it again. Each subsequent post got about 1/3 the views of the previous one, and wow does it take sooo much more effort to write than a one-off post!

This one is great though. I just every few days for the next post!

Re: Compiling a Lisp: Reader

#15

Earlier quoted context omitted.

I love seeing long form blog posts like this! Whilst its not something I’d personally do for a long time, its great to have resources on the web that go into detail in advanced topics like building a lisp interpreter. From another lisper, congrats and well done!

I agree that I really enjoy reading in-depth blog post series, but after releasing a 3-part series myself, I'm not sure if I would do it again. Each subsequent post got about 1/3 the views of the previous one, and wow does it take sooo much more effort to write than a one-off post! This one is great though. I just every few days for the next post!

You're right, it is a lot of effort! I made myself a promise that I wouldn't sprint through the first couple and then fall off a cliff on the rest. That's been a failing of mine in the past.

Progress is currently regular-ish because I've already implemented this stuff before I started writing. Once we get to labels and label calls, though, there very well could be a significant slowdown. I haven't figured out how to do that properly!

I'm glad you like the series and I'm very flattered that you posted it.

Re: Compiling a Lisp: Reader

#16

For a very simple, very readable Lisp reader, see Lumen's reader.l file: 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.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…

Femtolisp 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

CMUCL has a fairly complex, but still comprehensible implementation of a Common Lisp reader:

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

Re: Compiling a Lisp: Reader

#17

Earlier quoted context omitted.

I agree that I really enjoy reading in-depth blog post series, but after releasing a 3-part series myself, I'm not sure if I would do it again. Each subsequent post got about 1/3 the views of the previous one, and wow does it take sooo much more effort to write than a one-off post! This one is great though. I just every few days for the next post!

You're right, it is a lot of effort! I made myself a promise that I wouldn't sprint through the first couple and then fall off a cliff on the rest. That's been a failing of mine in the past. Progress is currently regular-ish because I've already implemented this stuff before I started writing. Once we get to labels and label calls, though, there very well could be a significant slowdown. I haven't figured out how to…

I am following the "Compiling a Lisp" series, its great, please keep posting, and thanks for your effort.

Re: Compiling a Lisp: Reader

#18
post #3

For a very simple, very readable Lisp reader, see Lumen's reader.l file: 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.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…

Why are atoms that start with numbers accepted as symbols? Is there any usecase for that?

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

Re: Compiling a Lisp: Reader

#19
post #3

For a very simple, very readable Lisp reader, see Lumen's reader.l file: 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.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…

Why are atoms that start with numbers accepted as symbols? Is there any usecase for that?

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

Re: Compiling a Lisp: Reader

#20
The Common Lisp specification has the entire reader algorithm laid out[1]. It fits on a single page, and it's really easy to understand.

Note, however, that there is heavy lifting done by the default read-table (the CL reader is table-based and is dynamically modifiable[2]), so you will only be able to read tokens if you implement what is on that page.

However, the most common characters are nearly trivial to implement: #\( #\" #\' as well as the most common dispatch characters for #\#

1: http://www.lispworks.com/documentation/HyperSpec/Body/02_b.h...

2: Someone modified the lisp reader to be able to read in valid C89 code, and implemented a backend to compile that to common-lisp: https://github.com/vsedach/Vacietis

Post reply on HN