Live data from Hacker News

Reader Macros in Common Lisp (2014)

lisper.in

11–20 of 37 posts

Re: Reader Macros in Common Lisp (2014)

#11
post #7
post #2

Apropos. Is there reader macro definitions for "(" and ")" ? Or is Gödel rolling in his grave for such insolent heresy?

Sure, what's so special about it? (get-macro-character #\() => SB-IMPL::READ-LIST (get-macro-character #\)) => SB-IMPL::READ-RIGHT-PAREN (that one just signals an error, the read-list one picks up the closing #\))

In other words : You cannot redefine ")" because it cannot be undefined while reading the new definition.

Re: Reader Macros in Common Lisp (2014)

#12
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.

Re: Reader Macros in Common Lisp (2014)

#13
post #7

Earlier quoted context omitted.

Sure, what's so special about it? (get-macro-character #\() => SB-IMPL::READ-LIST (get-macro-character #\)) => SB-IMPL::READ-RIGHT-PAREN (that one just signals an error, the read-list one picks up the closing #\))

In other words : You cannot redefine ")" because it cannot be undefined while reading the new definition.

No, it can totally be redefined. Reading happens before evaluation

Re: Reader Macros in Common Lisp (2014)

#14
post #7

Earlier quoted context omitted.

Sure, what's so special about it? (get-macro-character #\() => SB-IMPL::READ-LIST (get-macro-character #\)) => SB-IMPL::READ-RIGHT-PAREN (that one just signals an error, the read-list one picks up the closing #\))

In other words : You cannot redefine ")" because it cannot be undefined while reading the new definition.

Sure you can.

    CL-USER> (set-macro-character #\) (get-macro-character #\())
    T
    CL-USER> )format t "Hello")
    Hello
    NIL

Re: Reader Macros in Common Lisp (2014)

#15
post #7

Earlier quoted context omitted.

Sure, what's so special about it? (get-macro-character #\() => SB-IMPL::READ-LIST (get-macro-character #\)) => SB-IMPL::READ-RIGHT-PAREN (that one just signals an error, the read-list one picks up the closing #\))

In other words : You cannot redefine ")" because it cannot be undefined while reading the new definition.

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 meant when they said “it signals an error”. Specifically, it signals that there are unbalanced parentheses.

If you redefine the #\) reader macro, you could use it in a top-level context. That’s probably not a good idea since it’s easy to accidentally have too many #\) when closing a deeply nested expression and thus accidentally invoke the macro.

Aside from signaling an error, the #\) reader macro is also useful because it changes the rules when reading symbols. Basically, if you write (+ foo bar), the existence of the #\) macro helps the reader know that you’re referencing the “bar” symbol rather than the “bar)” symbol.

Generally, when people define new balanced-pair syntax for Common Lisp, (such as a #\{ macro for hash tables) they will follow the same pattern and define a corresponding reader macro for the closing side that always signals an error for all the same reasons.

Edit: also, as others have pointed out, you seem to be mistakenly assuming that the redefinition takes affect mid-way through reading the expression. That’s not how CL works. CL cleanly separates the process of executing code into a few distinct phases. First, the reader reads an entire expression (“form” in lisp terms). Then, that form is macroexpanded (traditional macros, not reader macros!) as needed before (optionally) being compiled and then executed.

The change to the read table would happen during the execution phase — well ordered after the original characters for that form are out of the picture.

You COULD force a change to the readtable mid-way through reading a form using the #. reader macro, but that definitely gets into chainsaw-juggling territory.

Re: Reader Macros in Common Lisp (2014)

#16
post #15

Earlier quoted context omitted.

In other words : You cannot redefine ")" because it cannot be undefined while reading the new definition.

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 macros, for example, you make your own Read. Better Read could even be in standard package "Additional-macros-for-common-lisp".

Re: Reader Macros in Common Lisp (2014)

#17
post #15

Earlier quoted context omitted.

In other words : You cannot redefine ")" because it cannot be undefined while reading the new definition.

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

Re: Reader Macros in Common Lisp (2014)

#18
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…

I’m guessing #ASM does the assembly at read or load time?

Does it desugar to something like this?

  (assemble ‘((mov rax %x) (add rax 1)))
Anyhow I think this is a great project. I hope you share it! I’ve often thought bootstrapping an assembler with lisp macros would be an interesting way to build a compiler from the very ground up. Assembler macros are nothing new, but I’ve never heard of an homoiconic implementation.

Re: Reader Macros in Common Lisp (2014)

#19
post #18
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…

I’m guessing #ASM does the assembly at read or load time? Does it desugar to something like this? (assemble ‘((mov rax %x) (add rax 1))) Anyhow I think this is a great project. I hope you share it! I’ve often thought bootstrapping an assembler with lisp macros would be an interesting way to build a compiler from the very ground up. Assembler macros are nothing new, but I’ve never heard of an homoiconic implementation…

The original LISP's "Lisp Assembly Program" was rather like this -- see Appendix C in the Lisp 1.5 manual, from 1962: https://www.softwarepreservation.org/projects/LISP/book/LISP...

Re: Reader Macros in Common Lisp (2014)

#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
Post reply on HN