Login
7 branches 0 tags
Ben (RPi4) Some improvements to the stdlib 5c55298 4 years ago 224 Commits
nujel / stdlib / 0_quasiquote.nuj
[defun quasiquote-real [l depth]
       [if [nil? l]
           #nil
           [if [pair? l]
               [if [eq? [caar l] 'unquote-splicing]
                   [if [zero? depth]
                       [list 'append
                             [cadr [car l]]
                             [quasiquote-real [cdr l] depth]]
                       [list 'unquote-splicing
                             [quasiquote-real [cadr l] [+ -1 depth]]]]
                   [if [eq? [car l] 'unquote]
                       [if [zero? depth]
                           [cadr l]
                           [list 'unquote
                                 [quasiquote-real [cadr l] [+ -1 depth]]]]
                       [if [eq? [car l] 'quasiquote]
                           [quasiquote-real [quasiquote-real [cadr l] [+ 1 depth]] depth]
                           [if [zero? depth]
                               [list 'cons
                                     [quasiquote-real [car l] depth]
                                     [quasiquote-real [cdr l] depth]]
                               [cons [quasiquote-real [car l] depth]
                                     [quasiquote-real [cdr l] depth]]]]]]
               [if [and [zero? depth] [symbol? l]]
                   [cons 'quote [cons l]]
                   l]]]
]

[defmacro quasiquote [l]
          [quasiquote-real l 0]
]

[defun unquote [expr]
        [throw [list :unquote-without-quasiquote "unquote should only occur inside a quasiquote, never evaluated directly"]]
]

[defun unquote-splicing [expr]
        [throw [list :unquote-splicing-without-quasiquote "unquote-splicing should only occur inside a quasiquote, never evaluated directly"]]
]