
;;; Implementation of S48's DEFINE-ENUMERATION macro suitable for
;;; bootstrap from R5RS.
;;;
;;; This performs too much computation at run time -- avoid this
;;; implementation if you can use a better one.



(define (vector-index/1 pred v0)
  (let ((v0-len (vector-length v0)))
    (let ((len v0-len))                       ;(MIN V0-LEN V1-LEN ...)
      (let loop ((i 0))
	(cond
	 ((= i len)
	  #f)
	 ((pred (vector-ref v0 i))
	  i)
	 (else
	  (loop (+ i 1))))))))
;; alternate definition if SRFI 43 is available and efficient:
;; (define vector-index/1 vector-index)



(define-syntax define-enumeration
  (syntax-rules ()
    ((define-enumeration enum-name (element ...))
     (define-syntax enum-name
       (syntax-rules (components enum)
	 ((enum-name enum lookup-name)
	  (vector-index/1 (lambda (x) (eq? x 'lookup-name))
			  (enum-name components)))
	 ((enum-name components)
	  '#(element ...)))))))



(define-syntax components
  (syntax-rules ()
    ((components enum-name)
     (enum-name components))))

(define-syntax enum
  (syntax-rules ()
    ((enum enum-name lookup-name)
     (enum-name enum lookup-name))))
