-
Notifications
You must be signed in to change notification settings - Fork 11
/
configuration.sc
89 lines (84 loc) · 4.78 KB
/
configuration.sc
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
77
78
79
80
81
82
83
84
85
86
87
88
89
(library (core configuration)
(export define-config define-config-table)
(import (chezscheme))
(define-syntax define-config
(lambda (stx)
(syntax-case stx ()
[(k var fname default-value)
(with-syntax
([var-set! (datum->syntax
#'k
(string->symbol
(format "set-~a!" (syntax->datum #'var))))]
)
#'(begin
(define tmp (let
([exists (file-exists? fname)])
(if exists
(call-with-input-file
fname
(lambda (p)
(box (read p))))
(box default-value))))
(define-syntax var
(lambda (stx)
(syntax-case stx (set!)
[id (identifier? #'id) #'(unbox tmp)])))
(define-syntax var-set!
(syntax-rules ()
[(_ v) (begin (set-box! tmp v)
(call-with-output-file
fname
(lambda (p)
(write v p))))]))
))])))
(define-syntax define-config-table
(lambda (stx)
(syntax-case stx ()
[(k table-name fname [var default-value] ...)
(with-syntax
([(tmps ...) (generate-temporaries #'(var ...))]
[(table-var ...) (map (lambda (id)
(datum->syntax #'k
(string->symbol
(format "~a-~a"
(syntax->datum #'table-name)
(syntax->datum id)))))
(syntax->list #'(var ...)))]
[(var-set! ...) (map (lambda (id)
(datum->syntax #'k
(string->symbol
(format "set-~a-~a!"
(syntax->datum #'table-name)
(syntax->datum id)))))
(syntax->list #'(var ...)))]
)
#'(begin
(define readed-table
(if (file-exists? fname)
(call-with-input-file fname
(lambda (p)
(read p)))
'()))
(define tmps
(let ([quer (assoc 'var readed-table)])
(if quer
(box (cdr quer))
(box default-value)))) ...
(define-syntax table-var (identifier-syntax (unbox tmps)))
...
(define-syntax var-set!
(syntax-rules ()
[(_ v) (begin (set-box! tmps v)
(call-with-output-file fname
(lambda (p)
(write (cons
(cons 'var (unbox tmps))
(filter
(lambda (x)
(not (equal? (car x) 'var)))
(list (cons 'var (unbox tmps))
...))) p))))]))
...))])))
)
(import (core configuration))