-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipc.scm
164 lines (134 loc) · 4.59 KB
/
ipc.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
;; (define-ipc-spec NAME RECEIVER ANNOTATED-DSSSL-LAMBDA-LIST)
;; Annonated lambda list element: (name type)
;; Before including this file:
;; (begin-for-syntax
;; (define *ipc-receiver* (quote frontend/backend))
;; (define *ipc-send-procedure* (quote whatever)))
(begin-for-syntax
(import srfi-1 srfi-69)
(define (type->predicate type)
(case type
((integer) 'exact-integer?)
((*) 'any?)
(else
(symbol-append type '?))))
;; name -> (lambda-list type-declaration predicates)
(define *ipc-specs* (make-hash-table)))
(define (any? o) #t)
(define-syntax define-ipc-spec
(ir-macro-transformer
(lambda (exp inject compare)
(let ((receiver (cadr exp))
(spec (caddr exp)))
(if (compare receiver *ipc-receiver*)
(cons 'define-ipc-receiver spec)
(cons 'define-ipc-sender spec))))))
(define-syntax define-ipc-receiver
(ir-macro-transformer
(lambda (exp inject compare)
(let* ((name (strip-syntax (cadr exp)))
(typed-args (cddr exp))
(arg-names (map (lambda (p) (strip-syntax (car p))) typed-args))
(arg-types (map (lambda (p) (strip-syntax (cadr p))) typed-args))
(arg-predicates (map type->predicate arg-types)))
(hash-table-set! *ipc-specs*
name
(list arg-names
arg-types
arg-predicates))
'(void)))))
(define-syntax define-ipc-implementation
(ir-macro-transformer
(lambda (exp inject compare)
(let* ((signature (cadr exp))
(name (strip-syntax (car signature)))
(args (cdr signature))
(body (cddr exp))
(proc-name (inject (symbol-append '|ipc-impl:| name)))
(spec (hash-table-ref *ipc-specs* name))
(types (cadr spec))
(predicates (caddr spec)))
(assert (equal? (car spec) (map strip-syntax args)))
`(begin
(: ,proc-name (,@types -> *))
(define ,proc-name
(lambda ,args
,@(map
(lambda (arg pred)
`(assert (,pred ,arg)))
args
predicates)
,@body))
(hash-table-set! ,*ipc-hash-table* ',name ,proc-name))))))
(define-syntax define-ipc-sender
(ir-macro-transformer
(lambda (exp inject compare)
(let* ((name (cadr exp))
(typed-args (cddr exp))
(args (map car typed-args))
(types (map cadr typed-args))
(assertions (map
(lambda (arg type)
;; (assert (type-pred? arg))
`(assert (,(type->predicate (strip-syntax type)) ,arg)))
args
types))
(proc-name (inject (symbol-append '|ipc:| (strip-syntax name)))))
`(begin
(: ,proc-name (,@types -> *))
(define ,proc-name
(lambda ,args
,@assertions
(,*ipc-send-procedure*
(list ',name ,@args)))))))))
;; Backend -> Frontend
;; ===================
(define-ipc-spec frontend
(notifications (room-id symbol)
(new-hls integer)
(new-notifs integer)))
(define-ipc-spec frontend
(read-marker (room-id symbol)
(event-id symbol)))
(define-ipc-spec frontend
(room-name (room-id symbol)
(room-name string)))
(define-ipc-spec frontend
(room-members (room-id symbol)
(members list)))
(define-ipc-spec frontend
(message (room-id symbol)
(message list)))
(define-ipc-spec frontend
(message-before (room-id symbol)
(event-id symbol)
(message list)))
(define-ipc-spec frontend
(remove (room-id symbol)
(event-id symbol)))
(define-ipc-spec frontend
(info (message string)))
;; Frontend -> backend
;; ===================
(define-ipc-spec backend
(message:text (room-id symbol)
(str string)))
(define-ipc-spec backend
(message:emote (room-id symbol)
(str string)))
(define-ipc-spec backend
(mark-last-message-as-read (room-id symbol)))
(define-ipc-spec backend
(fill-hole (room-id symbol)
(event-id symbol)
(count integer)))
(define-ipc-spec backend
(login (server string)
(username string)
(password string)))
(define-ipc-spec backend
(join-room (room string)))
(define-ipc-spec backend
(leave-room (room-id string)))
(define-ipc-spec backend
(stop))