Next: , Previous: , Up: expander utils   [Index]


15.4.7 Special processors

Function: parse-logic-predicate-syntax stx
Function: parse-logic-predicate-syntax stx tail-proc

Given a syntax object stx parse it as logic predicate expression with expected format:

stx = (and ?expr0 ?expr ...)
    | (or  ?expr0 ?expr ...)
    | (xor ?expr0 ?expr ...)
    | (not ?expr)
    | ?expr

where and, or, xor, not are the identifiers exported by (vicare). If a standalone ?expr is found: apply the procedure tail-proc to it gather its single return value; tail-proc defaults to the identity function.

Return a syntax object representing the logic predicate with the standalone expressions replaced by the return values of tail-proc.

(define (tail-proc stx)
  (syntax-case stx ()
    ((?expr0 ?expr ...)
     #`(or #,@(map (lambda (expr)
                     (parse-logic-predicate-syntax
                        expr tail-proc))
                (syntax->list #'(?expr0 ?expr ...)))))
    (_
     (list #'here stx))))

(define-syntax-rule (doit ?input)
  (parse-logic-predicate-syntax (syntax ?input) tail-proc))

(doit (and 1 2))
⇒ #'(and (here 1) (here 2))

(doit (or 1 2))
⇒ #'(or (here 1) (here 2))

(doit (xor 1 2)
⇒ #'(xor (here 1) (here 2))

(doit (not 1))
⇒ (not (here 1))

(doit (and (xor (not 1) 2)
           (or  (not 3) 4)))
⇒ #'(and (xor (not (here 1)) (here 2))
          (or  (not (here 3)) (here 4)))

(doit (1 2))
⇒ #'(or (here 1) (here 2))