Live data from Hacker News

Reader Macros in Common Lisp (2014)

lisper.in

21–30 of 37 posts

Re: Reader Macros in Common Lisp (2014)

#21
post #15

Earlier quoted context omitted.

That’s completely incorrect. The parent comment has it right. In normal usage, the meaning of #\) is entirely determined by the implementation for the #\( reader macro. This happens because the reader macro for #\( keeps consuming characters until it has located AND consumed the matching #\). The reader would only run the #\) reader macro if it encountered a #\) that didn’t have a corresponding #\(. That’s what they…

Ok. I understood my error in about 5.3 second but let it stay, because it was funny, and would trigger somebody to generate a wall-chart of explanations. Anyways already in 1982 I was diabolically opposed to this kind of shit. Read and Print should be as simple as possible and always one-to-one. When you print something to disk, that is what you get when reading, no additional adjustment needed. If you want reading m…

> If you want reading macros, for example, you make your own Read. Better Read could even be in standard package "Additional-macros-for-common-lisp".

It makes little sense to reimplement (and maintain over time) the whole Read if you only care about changing some small aspect of it. Instead, you can consider standard Read mechanism from CL to be extensible - reader macros are plugins/hooks/customization points/whatever you want to call it. You can maintain 1:1 Read/Print compatibility easily by defining/overriding a matching printer method, which is the Print side plugin/hook/customization point/whatever.

The only thing that could be simpler than this would be some magic that lets you automatically derive a read macro and a printing method from a simple declaration, but for that you'd have to sacrifice Turing completeness.

In my experience, reader macros are just freaking people out for unconscious and irrational reasons. I can tell because they still freak me out a little, even though I'm conceptually fine with them.

Re: Reader Macros in Common Lisp (2014)

#22
post #3

Great timing. It's my turn to write a Lisp, and I was just thinking about implementing reader macros this morning :-) My goal is to create the barest-metal Lisp OS (basically Lisp REPL with full ring-0 access, the OS) with an asm reader macro to write low level assembly opcodes you can jmp into. (defn add1 (x) (declare x int64) #asm( mov rax, %x add rax, 1 )) (Just thought of this syntax on the spot, I'm a few dozen…

Why a reader macro? Just use s-expressions, like sbcl and ccl.

Indeed. That would ease making other tools, e.g. a super-optimizer, which generate or modify assembly code.

Re: Reader Macros in Common Lisp (2014)

#23
post #3

Great timing. It's my turn to write a Lisp, and I was just thinking about implementing reader macros this morning :-) My goal is to create the barest-metal Lisp OS (basically Lisp REPL with full ring-0 access, the OS) with an asm reader macro to write low level assembly opcodes you can jmp into. (defn add1 (x) (declare x int64) #asm( mov rax, %x add rax, 1 )) (Just thought of this syntax on the spot, I'm a few dozen…

Out of curiosity, why not write a Scheme or Common Lisp compiler? Or just a subset?

Re: Reader Macros in Common Lisp (2014)

#24
post #20

so I started learning common lisp lately. It's insane how one can combine very high level language, with down-to-metal compilation. I think that's the only language allowing it? Absolutely impressive. Feels like writing python but compiling to native

Julia has similar capabilities, probably because it was heavily inspired by lisps. You can even modify Julia's compiler from within Julia. I often write code at a python level of abstraction and then use julia introspection to check the machine code that was generated.

Re: Reader Macros in Common Lisp (2014)

#25
post #20

so I started learning common lisp lately. It's insane how one can combine very high level language, with down-to-metal compilation. I think that's the only language allowing it? Absolutely impressive. Feels like writing python but compiling to native

Forth is even more "down to the metal" than lisp, but yes I agree it is quite impressive.

Re: Reader Macros in Common Lisp (2014)

#26
Okay let me get this straight...

Are you saying that I don't need to use parenthesis as much in lisp? Instead I could define a few "reader macros" and then immediately have a different syntax?

Say if I wanted to change

    (if (test-clause) (action1)
    (action2))
To

    if (test-clause) do (action1)
    else (action2)

Is this actually possible inline in lisp, meaning I just need to import a library that implements the if-reader-macro?

Re: Reader Macros in Common Lisp (2014)

#27
post #15

Earlier quoted context omitted.

That’s completely incorrect. The parent comment has it right. In normal usage, the meaning of #\) is entirely determined by the implementation for the #\( reader macro. This happens because the reader macro for #\( keeps consuming characters until it has located AND consumed the matching #\). The reader would only run the #\) reader macro if it encountered a #\) that didn’t have a corresponding #\(. That’s what they…

> Aside from signaling an error, the #\) reader macro is also useful because it changes the rules when reading symbols. This is wrong. See http://www.lispworks.com/documentation/lw50/CLHS/Body/02_ad....

> A macro character is either terminating or non-terminating. The difference between terminating and non-terminating macro characters lies in what happens when such characters occur in the middle of a token. If a non-terminating macro character occurs in the middle of a token, the function associated with the non-terminating macro character is not called, and the non-terminating macro character does not terminate the token's name; it becomes part of the name as if the macro character were really a constituent character. A terminating macro character terminates any token, and its associated reader macro function is called no matter where the character appears. The only non-terminating macro character in standard syntax is sharpsign.

http://www.lispworks.com/documentation/lw50/CLHS/Body/02_add...

That is the system I was alluding to. It’s been a few years since I’ve done anything with CL and so it seems my memory was slightly off.

Re: Reader Macros in Common Lisp (2014)

#28
post #3

Great timing. It's my turn to write a Lisp, and I was just thinking about implementing reader macros this morning :-) My goal is to create the barest-metal Lisp OS (basically Lisp REPL with full ring-0 access, the OS) with an asm reader macro to write low level assembly opcodes you can jmp into. (defn add1 (x) (declare x int64) #asm( mov rax, %x add rax, 1 )) (Just thought of this syntax on the spot, I'm a few dozen…

Out of curiosity, why not write a Scheme or Common Lisp compiler? Or just a subset?

I want an operating system from first principles. To write the smallest OS with the smallest language and the least amount of restriction.

The goal is being able to replace every single part of the runtime/interpreter if you wish, but the OS itself starts with the fewest functions/features possible. cons, lambda, and the other usual suspects, a way of running actual native machine code, the homoiconicity of LISP, a runtime-editable image, packed into a UEFI binary.

You could build yourself a Scheme or a Common Lisp on top of it if you wish.

Re: Reader Macros in Common Lisp (2014)

#29
post #26

Okay let me get this straight... Are you saying that I don't need to use parenthesis as much in lisp? Instead I could define a few "reader macros" and then immediately have a different syntax? Say if I wanted to change (if (test-clause) (action1) (action2)) To if (test-clause) do (action1) else (action2) Is this actually possible inline in lisp, meaning I just need to import a library that implements the if-reader-ma…

For this concept of user-defined languages/DSLs, look no further than Racket.

Re: Reader Macros in Common Lisp (2014)

#30
post #26

Okay let me get this straight... Are you saying that I don't need to use parenthesis as much in lisp? Instead I could define a few "reader macros" and then immediately have a different syntax? Say if I wanted to change (if (test-clause) (action1) (action2)) To if (test-clause) do (action1) else (action2) Is this actually possible inline in lisp, meaning I just need to import a library that implements the if-reader-ma…

This works in many Common Lisp implementations: https://readable.sourceforge.io
Post reply on HN