;;; -*-scheme-*-

;;; GNU Mes --- Maxwell Equations of Software
;;; Copyright © 2016,2017 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
;;;
;;; This file is part of GNU Mes.
;;;
;;; GNU Mes is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Mes is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Mes.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;;; quasiquote.mes is loaded after base.  It provides quasiquote
;;; written in Scheme.

;;; Code:

(mes-use-module (mes base))

(define (quasiquote-expand x)
  (cond ((vector? x) (list 'list->vector (quasiquote-expand (vector->list x))))
        ((not (pair? x)) (cons 'quote (cons x '())))
        ((eq? (car x) 'quasiquote) (quasiquote-expand (quasiquote-expand
                                             (if (null? (cddr x)) (cadr x)
                                                 (cons 'list (cdr x))))))
        ((eq? (car x) 'unquote) (if (null? (cddr x)) (cadr x)
                                    (cons 'list (cdr x))))
        ((and (pair? (car x)) (eq? (caar x) 'unquote-splicing))
         ((lambda (d)
            (if (null? (cddar x)) (list 'append (cadar x) d)
                (list 'quote (append (cdar x) d))))
          (quasiquote-expand (cdr x))))
        (else ((lambda (a d)
                 (if (pair? d)
                     (if (eq? (car d) 'quote)
                         (if (and (pair? a) (eq? (car a) 'quote))
                             (list 'quote (cons (cadr a) (cadr d)))
                             (if (null? (cadr d))
                                 (list 'list a)
                                 (list 'cons* a d)))
                         (if (memq (car d) '(list cons*))
                             (cons (car d) (cons a (cdr d)))
                             (list 'cons* a d)))
                     (list 'cons* a d)))
               (quasiquote-expand (car x))
               (quasiquote-expand (cdr x))))))

(define-macro (quasiquote x)
  (quasiquote-expand x))
