summaryrefslogtreecommitdiff
path: root/scheme/geiser/introspection.scm
blob: ffa2c738e55f1d8bde746abbc1028c6db457c4a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
;; introspection.scm -- name says it all

;; Copyright (C) 2009 Jose Antonio Ortega Ruiz

;; Author: Jose Antonio Ortega Ruiz <jao@gnu.org>
;; Start date: Sun Feb 08, 2009 18:44

;; This file 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.

;; This file 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 this program.  If not, see <http://www.gnu.org/licenses/>.

;;; Comentary:

;; Procedures introspecting on scheme objects and their properties.

;;; Code:

(define-module (geiser introspection)
  #:export (proc-args completions)
  #:use-module (system vm program)
  #:use-module (ice-9 session)
  #:use-module (srfi srfi-1))

(define (proc-args proc)
  (let ((proc (and (symbol? proc)
                   (module-bound? (current-module) proc)
                   (eval proc (current-module)))))
    (cond ((not proc) #f)
          ((program? proc) (program-args proc))
          ((procedure? proc) (procedure-args proc))
          ((macro? proc) (macro-args proc))
          (else #f))))

(define (program-args program)
  (let* ((arity (program-arity program))
         (arg-no (first arity))
         (opt (> (second arity) 0))
         (args (map first (take (program-bindings program) arg-no))))
    (format-args (if opt (drop-right args 1) args) (and opt (last args)))))

(define (procedure-args proc)
  (let* ((arity (procedure-property proc 'arity))
         (req (first arity))
         (opt (third arity)))
    (format-args (map (lambda (n)
                        (string->symbol (format "arg~A" (+ 1 n))))
                      (iota req))
                 (and opt 'rest))))

(define (macro-args macro)
  (format-args '(...) #f))

(define (format-args args opt)
  (list (cons 'required args)
        (cons 'optional (or opt '()))))

(define (completions prefix)
  (map symbol->string (apropos-internal (string-append "^" prefix))))

;;; introspection.scm ends here