News for hackers. Stuff that matters.
Re:Ask HN: Guide for Implementing Common Lisp by bitwize on Thursday February 01, 2024 @11:55PM (#39225210)
Mark: How do I compile Lisp source code?

Nolan: That's the neat thing -- you don't.

Lisp compilers operate on Lisp data structures -- generally speaking, mostly lists and atoms. The 'read' function is what turns text into these data structures; reader macros alter how this function operates and control what data structures will be emitted by the reader. This really is the neat part about Lisp -- it's much simpler to write a compiler that consumes an AST directly than one that consumes text, and the AST is expressed directly in terms of Lisp's primitive data structures which you can easily write your own functions over. And it's a relative doddle to write a reader for s-expressions compared to other expression formats. Join these two relatively simple pieces together and you have a complete Lisp compiler system.

As for regular macros, they transform the AST before it's evaluated or compiled. So they come into play after the reader has consumed the source and produced an AST, but before the evaluator or compiler can work.

Re:Ask HN: Guide for Implementing Common Lisp by HexDecOctBin on Friday February 02, 2024 @12:28AM (#39225401)
> Join these two relatively simple pieces together and you have a complete Lisp compiler system.

I see, this clarifies things a lot. Thanks.

Re:Ask HN: Guide for Implementing Common Lisp by lispm on Friday February 02, 2024 @01:37AM (#39225768)
Lisp compilers and interpreters are using s-expressions (-> nested lists of atoms) as source input. But these are not an AST. The compiler/interpreter has to find out what these lists & atoms actually mean and may build an AST.

Macros also see only lists/atoms. Macros then find out on their own what the embedded code should mean. That's different from some other languages, where macros work over an AST. Which means that the code is parsed and needs to follow a syntax. That's not the case in Common Lisp, macro forms can be in any syntax. (infix a + b) is legal syntax in Common Lisp, the macro could for example convert infix syntax to prefix. In a language where the operator + is defined to be a prefix operator (-> syntax), an expression prefix(+ a b) could not parse the thing into an addition call AST node, since it would need to know what syntax rules to use for the code. Somehow it would need to build an AST for a different syntax.

Re:Ask HN: Guide for Implementing Common Lisp by jfoutz on Friday February 02, 2024 @06:50AM (#39227603)
I think bitwize's reply is one of the best comments I've read here. If you don't have the context it's really pretty condescending and snarky. but, hexdecoctbin thanked them for the reply.

based on your username, I'll absolutely stipulate that you're correct. bitwize didn't really answer the question correctly.

I'm not saying you're wrong. I'm saying bitwize said what OP needed to hear to grok the answer, and that seems pretty fucking lispy to me.