forked from okuoku/xitomatl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.ypsilon.sls
66 lines (56 loc) · 1.91 KB
/
common.ypsilon.sls
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
;; Copyright 2009 Derick Eddington. My MIT-style license is in the file named
;; LICENSE from the original collection this file is distributed with.
(library (xitomatl common)
(export
add1 sub1
format printf fprintf pretty-print
gensym
time
with-input-from-string with-output-to-string
system
;; TODO: add to as needed/appropriate
)
(import
(rnrs)
(only (core)
format system
set-current-input-port! set-current-output-port!)
(prefix (only (core) pretty-print gensym) ypsilon:)
(only (time) time))
(define (add1 x) (+ x 1))
(define (sub1 x) (- x 1))
(define (fprintf port fmt-str . fmt-args)
(put-string port (apply format fmt-str fmt-args)))
(define (printf fmt-str . fmt-args)
(apply fprintf (current-output-port) fmt-str fmt-args))
(define pretty-print
(case-lambda
((x)
(pretty-print x (current-output-port)))
((x p)
(ypsilon:pretty-print x p)
(newline p))))
(define gensym
(case-lambda
(()
(ypsilon:gensym))
((name)
(ypsilon:gensym (cond ((string? name) name)
((symbol? name) (symbol->string name))
(else (assertion-violation 'gensym
"not a string or symbol" name)))))))
(define (parameterize-current-port port set-port! val thunk)
(define (swap)
(let ((t (port)))
(set-port! val)
(set! val t)))
(dynamic-wind swap thunk swap))
(define (with-input-from-string str thunk)
(parameterize-current-port current-input-port set-current-input-port!
(open-string-input-port str) thunk))
(define (with-output-to-string thunk)
(let-values (((sop get) (open-string-output-port)))
(parameterize-current-port current-output-port set-current-output-port!
sop thunk)
(get)))
)