tisp

tiny lisp
git clone git://edryd.org/tisp
Log | Files | Refs | LICENSE

doc.tsp (2238B)


      1 (define docstr-reg
      2   '((car
      3       "(car lst)"
      4       "return first element of list")
      5     (cdr
      6       "(cdr lst)"
      7       "return rest of list after first element")
      8     (cons
      9       "(cons a d)"
     10       "create new pair with a car of a and cdr of d")
     11     (quote
     12       "(quote expr)"
     13       "return expression unevaluated")
     14     (void
     15       "(void)"
     16       "return void type")
     17     (eval
     18       "(eval expr)"
     19       "evaluate expression, can be dangerous to use in practice")
     20     (=
     21       "(= . vals)"
     22       "return boolean depending on if multiple values are all equal")
     23     (cond
     24       "(cond . (expr . body))"
     25       "conditional statement")
     26     (typeof
     27       "(typeof val)"
     28       "return a string stating the argument's type")
     29     (get
     30       "(get val prop)"
     31       "get the property of the given value depending on its type")
     32     (lambda
     33       "(lambda args . body)"
     34       "create anonymous function")
     35     (macro
     36       "(macro args . body)"
     37       "create anonymous macro")
     38     (define
     39       "(define var . val)"
     40       "(define (func . args) . body)"
     41       "creates new variable with value, or create new function if argument list given"
     42       "if value for variable is not given, make it a self-evaluating symbol")
     43     (set!
     44       "(set! var val)"
     45       "change the variable var to val")
     46     (load
     47       "(load lib)"
     48       "loads the library given as a string")
     49     (error
     50       "(error func msg)"
     51       "throw error, print message with function name given as symbol")
     52     (version
     53       "(version)"
     54       "return string of tisp's version number")))
     55 
     56 (define (doc proc)
     57   "get documentation of function supplied by its doc string"
     58   (unless (procedure? proc)
     59     (error 'doc "documentation only exists for procedures"))
     60   (define (lookup proc)
     61     (recur f ((docstr docstr-reg))
     62       (cond ((nil? docstr)
     63              (error 'doc (get proc 'name) ": no documentation found"))
     64             ((= (caar docstr) (get proc 'name))
     65              (map disp (cdar docstr)))
     66             (else (f (cdr docstr))))))
     67   (if (or (function? proc) (macro? proc))
     68     (let ((docstr (car (get proc 'body))))
     69       (if (string? docstr)
     70         (disp (cons (get proc 'name) (get proc 'args)) "\n"
     71               docstr)
     72         (lookup proc)))
     73     (lookup proc))
     74   (void))