;; lib/guest/prefix.sx — prefix-rename macro.
;;
;; A guest runtime often re-exports a stretch of host primitives under a
;; language-specific prefix. The prefix-rename macro replaces the repeated
;; (define lang-foo foo) boilerplate with a single declarative call.
;;
;; Two entry shapes are supported:
;;
;;   (prefix-rename "cl-" '(gcd lcm expt floor truncate))
;;     ;; expands to (begin (define cl-gcd gcd)
;;     ;;                   (define cl-lcm lcm) ...)
;;
;;   (prefix-rename "cl-"
;;     '((mod modulo)
;;       (arrayp? vector?)
;;       (ceiling ceil)))
;;     ;; expands to (begin (define cl-mod modulo)
;;     ;;                   (define cl-arrayp? vector?)
;;     ;;                   (define cl-ceiling ceil))
;;
;; Mixed lists are supported — bare symbols are same-name aliases, two-element
;; lists are (alias target) pairs.

(defmacro
  prefix-rename
  (prefix entries-q)
  (let
    ((entries (nth entries-q 1)))
    (cons
      (quote begin)
      (map
        (fn
          (entry)
          (cond
            ((= (type-of entry) "symbol")
              (list
                (quote define)
                (make-symbol (str prefix (symbol-name entry)))
                entry))
            ((and (list? entry) (= (len entry) 2))
              (list
                (quote define)
                (make-symbol (str prefix (symbol-name (first entry))))
                (nth entry 1)))
            (:else (error (str "prefix-rename: invalid entry " entry)))))
        entries))))
