-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.scm
76 lines (62 loc) · 1.82 KB
/
util.scm
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
70
71
72
73
74
75
76
;;; Logging
(define (type-name x)
(cond ((eq? #t x) "#t")
((eq? #f x) "#f")
((symbol? x) "symbol")
((char? x) "char")
((string? x) "string")
((number? x) "number")
((null? x) "empty-list")
((special? x) "special-form")
((pair? x) "pair")))
(define (fatal-error mesg . args)
(print-call-chain (current-error-port))
(print-stderr (format "EE Error: ~a ~a~%" mesg
(string-join args " ")))
(exit 1))
(define (debug-log mesg . args)
(print-stderr (sprintf "II ~a ~a~%" mesg
(string-join args " "))))
(define (todo)
(print-call-chain (current-error-port))
(print-stderr (sprintf "EE TODO Error")))
(define (qq a)
(sprintf "\"~a\"" a))
;;; IO Functions
(define (read-scm-file filename)
(let loop ((port (open-input-file filename))
(src (list)))
(define x (read port))
(if (eof-object? x)
(reverse src)
(loop port (cons x src)))))
(define (get-extension filename)
(define ind (string-index-right filename #\.))
(if (eq? ind #f) filename
(substring filename (+ ind 1))))
(define (replace-ext filename ext)
(define ind (string-index-right filename #\.))
(if (eq? ind #f)
(string-append filename ext)
(string-append (substring filename 0 ind) ext)))
(define (print-stderr s)
(format (current-error-port) "~a" s))
;;; List Functions
(define (member? lst x)
(not (eq? (member lst x) #f)))
(define (last lst)
(car (reverse lst)))
(define (single? lst)
(eq? 1 (length lst)))
(define (car-or-null lst)
(if (null? lst) lst
(car lst)))
;;; Some missing scheme functions
(define (repeat what times)
(let loop ((res (list))
(n times))
(if (<= n 0)
res
(loop (cons what res) (- n 1)))))
(define (inc x)
(+ x 1))