commit bb84abeea261ff5ee1941b7d3ae8c5a879ac28a8
parent 9386a295cce0c58c4844626ec4ad4eca9f0dd7c0
Author: Ed van Bruggen <edvb@uw.edu>
Date: Thu, 26 Mar 2020 00:34:01 -0700
Add doc string registry to document primitives
Diffstat:
3 files changed, 77 insertions(+), 9 deletions(-)
diff --git a/Makefile b/Makefile
@@ -13,7 +13,7 @@ SRC = tisp.c main.c
TIB = tibs/math.c tibs/io.c tibs/time.c tibs/string.c
OBJ = $(SRC:.c=.o) $(TIB:.c=.o)
LIB = tibs/libtibmath.so tibs/libtibio.so
-TSP = tibs/lib.tsp tibs/repl.tsp
+TSP = tibs/lib.tsp tibs/doc.tsp tibs/repl.tsp
all: options $(EXE)
diff --git a/tibs/doc.tsp b/tibs/doc.tsp
@@ -0,0 +1,73 @@
+(define docstr-reg
+ '((car
+ "(car lst)"
+ "return first element of list")
+ (cdr
+ "(cdr lst)"
+ "return rest of list after first element")
+ (cons
+ "(cons a d)"
+ "create new pair with a car of a and cdr of d")
+ (quote
+ "(quote expr)"
+ "return expression unevaluated")
+ (void
+ "(void)"
+ "return void type")
+ (eval
+ "(eval expr)"
+ "evaluate expression, can be dangerous to use in practice")
+ (=
+ "(= . vals)"
+ "return boolean depending on if multiple values are all equal")
+ (cond
+ "(cond . (expr . body))"
+ "conditional statement")
+ (type
+ "(type val)"
+ "return a string stating the argument's type")
+ (get
+ "(get val prop)"
+ "get the property of the given value depending on its type")
+ (lambda
+ "(lambda args . body)"
+ "create anonymous function")
+ (macro
+ "(macro args . body)"
+ "create anonymous macro")
+ (define
+ "(define var val)"
+ "(define (func . args) . body)"
+ "creates new variable with value, or create new function if argument list given")
+ (set!
+ "(set! var val)"
+ "change the variable var to val")
+ (load
+ "(load lib)"
+ "loads the library given as a string")
+ (error
+ "(error func msg)"
+ "throw error, print message with function name given as symbol")
+ (version
+ "(version)"
+ "return string of tisp's version number")))
+
+(define (doc proc)
+ "get documentation of function supplied by its doc string"
+ (unless (procedure? proc)
+ (error 'doc "documentation only exists for procedures"))
+ (define (lookup proc)
+ (recur f ((docstr docstr-reg))
+ (cond ((nil? docstr)
+ (error 'doc (get proc 'name) ": no documentation found"))
+ ((= (caar docstr) (get proc 'name))
+ (map disp (cdar docstr)))
+ (else (f (cdr docstr))))))
+ (if (or (function? proc) (macro? proc))
+ (let ((docstr (car (get proc 'body))))
+ (if (string? docstr)
+ (disp (cons (get proc 'name) (get proc 'args)) "\n"
+ docstr)
+ (lookup proc)))
+ (lookup proc))
+ (void))
diff --git a/tibs/lib.tsp b/tibs/lib.tsp
@@ -55,6 +55,7 @@
(define (boolean? x) (or (= x t) (nil? x)))
(define (true? x) (= x t))
(define false? nil?)
+(define (procedure? x) (or (primitive? x) (or (function? x) (macro? x))))
(define (rational? x) (or (integer? x) (ratio? x)))
(define (number? x) (or (rational? x) (decimal? x)))
@@ -74,14 +75,8 @@
(defmacro (var name eq . val)
"define new variable with given value"
`(if (= ',eq '=)
- ,(list* 'define name val)
- (error 'var "incorrect form")))
-
-(define (doc proc)
- "get documentation of function"
- (if (string? (car (get proc 'body)))
- (disp (car (get proc 'body)))
- (error 'doc "procedure not documented")))
+ ,(list* 'define name val)
+ (error 'var "incorrect form")))
(defmacro (assert expr)
`(unless ,expr