mes/module/mes/quasiquote.mes

95 lines
3.6 KiB
Scheme

;;; -*-scheme-*-
;;; Mes --- Maxwell Equations of Software
;;; Copyright © 2016 Jan Nieuwenhuizen <janneke@gnu.org>
;;;
;;; This file is part of Mes.
;;;
;;; 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.
;;;
;;; 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 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-macro (quasiquote x)
(define (check x)
(cond ((pair? (cdr x)) (cond ((null? (cddr x)))
(#t (error (car x) "invalid form ~s" x))))))
(define (loop x)
;;(display "LOOP") (newline)
(cond
((not (pair? x)) (cons 'quote (cons x '())))
((eq? (car x) 'quasiquote) (check x) (loop (loop (cadr x))))
((eq? (car x) 'unquote) (check x) (cadr x))
((eq? (car x) 'unquote-splicing)
(error 'unquote-splicing "invalid context for ~s" x))
(;;(and (pair? (car x)) (eq? (caar x) 'unquote-splicing))
(cond ((pair? (car x)) (eq? (caar x) 'unquote-splicing))
(#t #f))
(check (car x))
;; (let ((d (loop (cdr x))))
;; (cond ((equal? d '(quote ())) (cadar x))
;; ;;(#t `(append ,(cadar x) ,d))
;; (#t (list 'append (cadar x) d))
;; ))
((lambda (d)
(list 'append (cadar x) d))
(loop (cdr x))))
(#t
;; (let ((a (loop (car x)))
;; (d (loop (cdr x))))
;; (cond ((pair? d)
;; (cond ((eq? (car d) 'quote)
;; (cond ((and (pair? a) (eq? (car a) 'quote))
;; `'(,(cadr a) . ,(cadr d)))
;; (#t (cond ((null? (cadr d))
;; `(list ,a))
;; (#t `(cons* ,a ,d))))))
;; (#t (cond ((memq (car d) '(list cons*))
;; `(,(car d) ,a ,@(cdr d)))
;; (#t `(cons* ,a ,d))))))
;; (#t `(cons* ,a ,d))))
((lambda (a d)
;;(display "LAMBDA AD") (newline)
(cond ((pair? d)
(cond ((eq? (car d) 'quote)
(cond (;;(and (pair? a) (eq? (car a) 'quote))
(cond ((pair? a) (eq? (car a) 'quote))
(#t #f))
(list 'quote (cons (cadr a) (cadr d))))
(#t (cond ((null? (cadr d))
(list 'list a))
(#t (list 'cons* a d))))))
(#t (cond ((memq (car d) '(list cons*))
;;`(,(car d) ,a ,@(cdr d))
(cons (car d) (cons a (cdr d)))
)
;;(#t `(cons* ,a ,d))
(#t (list 'cons* a d))
))))
;;(#t `(cons* ,a ,d))
(#t (list 'cons* a d))
))
(loop (car x))
(loop (cdr x)))
)))
(loop x))