mes/tests/psyntax.test

181 lines
4.8 KiB
Plaintext
Raw Normal View History

#! /bin/sh
# -*-scheme-*-
echo ' ()' | cat $($(dirname $0)/../scripts/include.mes $0) $0 /dev/stdin | $(dirname $0)/../scripts/mes $MES_FLAGS "$@"
#paredit:||
exit $?
!#
;;; -*-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/>.
(mes-use-module (mes base-0))
(mes-use-module (mes base))
(mes-use-module (mes quasiquote))
(mes-use-module (mes let))
(mes-use-module (srfi srfi-0))
(mes-use-module (mes scm))
(mes-use-module (mes psyntax-0))
(mes-use-module (mes psyntax-pp))
(mes-use-module (mes psyntax-1))
(mes-use-module (mes test))
(pass-if "first dummy" #t)
(pass-if-not "second dummy" #f)
(cond-expand
(guile
;;(use-modules (ice-9 syncase))
(define sc-expand identity)
(define syntax-object->datum syntax->datum)
(define datum->syntax-object datum->syntax)
)
(mes))
(when (not guile?)
(pass-if "andmap"
(seq? (andmap (lambda (x) (> x 0)) '(3 2 1)) #t))
(pass-if "andmap 2"
(seq? (andmap (lambda (x) (> x 0)) '(3 2 1 0)) #f))
(pass-if "putprop" (putprop 'foo '*sc-expander 'bar))
(pass-if "getprop"
(seq? (getprop 'foo '*sc-expander) 'bar))
)
(pass-if "syntax-case"
(sequal? (let* ((sexp '(syntax-case '((1 2) (3 4)) ()
(((x ...) ...) (syntax (x ... ...)))))
(expanded (sc-expand sexp)))
(primitive-eval expanded))
'(1 2 3 4)))
(pass-if "sc-expand"
(sequal? (let ()
(syntax-case '((1 2) (3 4)) ()
(((x ...) ...) (syntax (x ... ...)))))
'(1 2 3 4)))
(pass-if "syntax-object->datum"
(sequal? (syntax-object->datum (syntax (set! a b)))
'(set! a b)))
(pass-if "syntax-case swap!"
(sequal? (syntax-object->datum
(let ((exp '(set! a b)))
(syntax-case exp ()
((swap! a b)
(syntax
(let ((temp a))
(set! a b)
(set! b temp)))))))
'(let ((temp a)) (set! a b) (set! b temp))))
(when (not guile?)
(pass-if "syntax-case manual swap!"
(sequal?
(let* ((sc (sc-expand '(syntax-case exp () ((swap! a b) (syntax (let ((temp a)) (set! a b) (set! b temp)))))))
(exp '(swap foo bar))
(foo "foo")
(bar "bar")
(s (eval sc (current-module)))
(d (syntax-object->datum s)))
(eval d (current-module))
(list foo bar))
'("bar" "foo"))))
(pass-if "define-syntax swap! [syntax-case]"
(sequal?
(let ()
(define-syntax swap!
(lambda (exp)
(syntax-case exp ()
((swap! a b)
(syntax
((lambda (temp)
(set! a b)
(set! b temp)) a))))))
(let ((foo "foo")
(bar "bar"))
(swap! foo bar)
(list foo bar)))
(list "bar" "foo")))
(pass-if "define-syntax swap! [syntax-case+let]"
(sequal?
(let ()
(define-syntax swap!
(lambda (exp)
(syntax-case exp ()
((swap! a b)
(syntax
(let ((temp a))
(set! a b)
(set! b temp)))))))
(let ((foo "foo")
(bar "bar"))
(swap! foo bar)
(list foo bar)))
(list "bar" "foo")))
(pass-if "define-syntax sr:when [syntax-rules]"
(sequal?
(let ()
(define-syntax sr:when
(syntax-rules ()
((sc:when condition exp ...)
(if condition
(begin exp ...)))))
(let ()
(sr:when #t "if not now, then?")))
"if not now, then?"))
(pass-if "define-syntax-rule"
(sequal?
(let ()
(define-syntax-rule (sre:when c e ...)
(if c (begin e ...)))
(let ()
(sre:when #t "if not now, then?")))
"if not now, then?"))
(pass-if-equal "syntax-rules plus"
(+ 1 2 3)
(let ()
(define-syntax plus
(syntax-rules ()
((plus x ...) (+ x ...))))
(plus 1 2 3)))
(when guile?
(pass-if-equal "macro with quasisyntax"
'("foo" "foo")
(let ()
(define-syntax string-let
(lambda (stx)
(syntax-case stx ()
((_ id body ...)
#`(let ((id #,(symbol->string (syntax->datum #'id))))
body ...)))))
(string-let foo (list foo foo)))))
(result 'report)