#lang scheme/base
(require
"rep.ss"
"../tools.ss"
"ns-tx.ss" scheme/shared
(for-syntax
scheme/base
syntax/stx
"ns-tx.ss"
))
(provide
(all-defined-out))
(define-for-syntax (tx _tx id stx)
(_tx #`(#,id #,@(stx-cdr stx))))
(define-syntax (define-ns stx) (tx define-ns-tx #'define stx))
(define-syntax (define-syntax-ns stx) (tx define-ns-tx #'define-syntax stx))
(define-syntax (redefine!-ns stx) (tx redefine!-ns-tx #'define stx))
(define-syntax (definitions-ns stx) (tx definitions-ns-tx #'define-ns stx))
(define-syntax (redefinitions!-ns stx) (tx definitions-ns-tx #'redefine!-ns stx))
(define-syntax (let-ns stx) (let-ns-tx #`(let #,@(stx-cdr stx))))
(define-syntax (letrec-ns stx) (let-ns-tx #`(letrec #,@(stx-cdr stx))))
(define-syntax (shared-ns stx) (let-ns-tx #`(shared #,@(stx-cdr stx))))
(define-sr (tx->syntax-ns ns (name tx) ...)
(begin (define-syntax-ns ns name tx) ...))
(define-syntax (ns stx)
(syntax-case stx ()
((_ ns name)
(ns-prefixed #'ns #'name))))
(define-syntax (parameterize-words-ns! stx)
(parameterize-words-ns-tx
#`(word-parameter! #,@(stx-cdr stx))))
(define-syntax (parameterize-words-ns stx)
(parameterize-words-ns-tx
#`(word-parameter #,@(stx-cdr stx))))
(define-syntax (parameterize/super-words stx)
(parameterize/super-words-ns-tx
#`(word-parameter #,@(stx-cdr stx))))
(define (ns-name ns [name '||])
(syntax->datum
(ns-prefixed (datum->syntax #f ns)
(datum->syntax #f name))))
(define (ns-name? ns)
(let* ((prefix (symbol->string (ns-name ns)))
(lp (string-length prefix)))
(lambda (sym)
(let* ((str (symbol->string sym))
(l (string-length str)))
(and (> l lp)
(string=? prefix (substring str 0 lp))
(string->symbol (substring str lp)))))))
(define (ns-mapped-symbols ns)
(let ((basename (ns-name? ns)))
(foldl
(lambda (sym collect)
(let ((it (basename sym)))
(if it (cons it collect) collect)))
'()
(namespace-mapped-symbols))))