Reimplement %allocate-instance in Scheme
[bpt/guile.git] / module / ice-9 / match.scm
1 ;;; -*- mode: scheme; coding: utf-8; -*-
2 ;;;
3 ;;; Copyright (C) 2010, 2011, 2012 Free Software Foundation, Inc.
4 ;;;
5 ;;; This library is free software; you can redistribute it and/or
6 ;;; modify it under the terms of the GNU Lesser General Public
7 ;;; License as published by the Free Software Foundation; either
8 ;;; version 3 of the License, or (at your option) any later version.
9 ;;;
10 ;;; This library is distributed in the hope that it will be useful,
11 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;;; Lesser General Public License for more details.
14 ;;;
15 ;;; You should have received a copy of the GNU Lesser General Public
16 ;;; License along with this library; if not, write to the Free Software
17 ;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
19 (define-module (ice-9 match)
20 #:export (match
21 match-lambda
22 match-lambda*
23 match-let
24 match-let*
25 match-letrec))
26
27 (define (error _ . args)
28 ;; Error procedure for run-time "no matching pattern" errors.
29 (apply throw 'match-error "match" args))
30
31 ;; Support for record matching.
32
33 (define-syntax slot-ref
34 (syntax-rules ()
35 ((_ rtd rec n)
36 (struct-ref rec n))))
37
38 (define-syntax slot-set!
39 (syntax-rules ()
40 ((_ rtd rec n value)
41 (struct-set! rec n value))))
42
43 (define-syntax is-a?
44 (syntax-rules ()
45 ((_ rec rtd)
46 (and (struct? rec)
47 (eq? (struct-vtable rec) rtd)))))
48
49 ;; Compared to Andrew K. Wright's `match', this one lacks `match-define',
50 ;; `match:error-control', `match:set-error-control', `match:error',
51 ;; `match:set-error', and all structure-related procedures. Also,
52 ;; `match' doesn't support clauses of the form `(pat => exp)'.
53
54 ;; Unmodified public domain code by Alex Shinn retrieved from
55 ;; the Chibi-Scheme repository, commit 1206:acd808700e91.
56 ;;
57 ;; Note: Make sure to update `match.test.upstream' when updating this
58 ;; file.
59 (include-from-path "ice-9/match.upstream.scm")