Decoder

^ Up

2026 January

I've mulled over this quite a bit now, and I believe I've figured out what kind of design I want for the "decoder" component.

To recap: Xisp has a "parser" that implements an extremely bare-bones s-expression format (though with some interesting syntax sugar baked in), with a lot of the features you would expect of a typical "reader" being offloaded into a second pass over the data.

That second pass is done by the decoder and will handle, among other things:

(To be clear, foo.bar actually becomes (#DOT foo & bar) at the parse stage, and foo:bar becomes (#COLON foo & bar) and so on. The decoder then substitutes #DOT and #COLON and the like for references to macros that actually implement the feature.)

The decoder is also going to be extensible, to allow for something similar to reader macros in Common Lisp, but closer to regular macros because this extensibility will be based on runes: A list beginning with a rune can invoke a decoder procedure for that rune, and these can be user-defined.

I've previously agonized over whether this means that the decoder is essentially the same thing as a macro expander, or rather, whether it would make sense to merge the functionality of the two. But I've come to the conclusion that this would be wrong.

Key differences between the decoder and a macro expander include:

These differences not only mean that the implementation will be quite different, but also that the decoder is conceptually a very different thing. No doubt there will be some similarity in their algorithms, but the conceptual simplicity of the decoder (no notion of scope or identifier bindings) means that you can reason about what it will do to source files much more easily.

Macros in Scheme have a completely different "feel" to them. They're really part of the program logic. The whole point of hygienic macros is that they fit in seamlessly with the rest of your program, rather than being a disjoint pre-processor operating outside program logic. That's valuable in a different way. (Xisp will also support hygienic macros like Scheme.)

Although the decoder is not as smart as a macro expander, I still intend to make it fairly powerful, supporting:

These will be turned off by default, so a decoded file cannot run arbitrary code, or maliciously embed /dev/random! The standard "Xisp code decoder" configuration used to read program and library files will then enable these features.

Splicing could be used for the same effect as an import, but import makes it explicit that no expressions are being inserted. Files with decoder rules could also be compiled into a binary, which the import mechanism could locate and use, instead of parsing the source file again every time.

Here's some imaginary Xisp source files demonstrating decoder use:

;; a.xisp

(#IMPORT "ht.xisp")  ;may load compiled code of ht.xisp from a cache

(define my-hash-table #ht((a 1) (b 2)))  ;#ht imported from ht.xisp

(#DEFINE (#foo x y)
  (import (de tkammer my-helper-module))
  (let ((blah (frobnicate x))
        (blub (quiblify y)))
    `(foo bar ,(generate blah blub))))

(#foo x y z)   ;decoder error

(#foo x y)     ;proper use

(a b #foo x y) ;also works, but don't do it please

(#DEFINE #bar '(+ 1 2))

(import (xisp io))  ;imports the print function

(print #bar)   ;will print 3

(#SPLICE "b.xisp")
;; ht.xisp

(#DEFINE (#ht & entries)
  (define ht (make-hash-table))
  (loop ((key value) entries)
    (ht.set key value))
  ht)
;; b.xisp

(define (foobar)
  (let ((data (#STRING "example.data")))
    (data.do-something)))

(define cycle #%0=(1 2 3 & #%0%))

If you find the use of uppercase to be ugly, consider that a feature, because messing with the decoder this much would be discouraged. The only example above that actually makes some sense is the one defining hash table syntax.

Actually, since I want to make it possible to serialize absolutely anything in Xisp, a regular macro could also be used to construct hash-table literals. See the serialization note on that.

However, this causes such custom object literals to not stand out:

(define (foobar)
  (let ((my-ht (ht (a 1) (b 2))))  ;doesn't look like a literal
    (use my-ht here)))

(define (foobar)
  (let ((my-ht #ht((a 1) (b 2))))  ;more obvious that it's a literal
    (use my-ht here)))

For this reason, it would be a convention that decoder rules are used to implement new object literal syntax, and macros used for when you want to output code, with hygienic bindings.

;; Can't do this with decoder rules

(import (xisp base))
(import (de tkammer my-module))

(define-syntax (my-macro x y <body>)
  (let ((x (call something from my-module))
        (y (also bind this one to something))
        (foo (this is a new local identifier)))
    (do-something-with foo)
    ;; this could also contain a `foo` without clashing:
    <body>))

Decoder rules would probably be equivalent in power to Common Lisp macros, but it will only be possible to bind them to runes, not to regular identifiers, so they will be demarcated very clearly. They aren't intended for the same purposes as Common Lisp macros, so the equal power is merely incidental. Use hygienic macros if you want "real" Lisp macros; decoder rules are only for superficial syntax enrichment, not meant to be intertwined with program logic.