-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparen-node.lisp
More file actions
151 lines (117 loc) · 3.48 KB
/
paren-node.lisp
File metadata and controls
151 lines (117 loc) · 3.48 KB
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
(defpackage :paren-node
(:use :cl :parenscript)
(:export
;; Utils
#:create-dict
#:concat
#:parse-json
#:stringify-json
;; Node.js
#:log-msg
;; Socket.io
#:call
#:on-event
#:on-connection
#:emit
;;Redis
#:with-redis
#:let-redis-1
#:let-redis**
#:print-redis
#:do-redis
#:list-redis*))
(in-package :paren-node)
;; Utils
(defmacro+ps create-dict (keys values)
(let ((dict-sym (gensym)))
`(let ((,dict-sym (create)))
(loop for k in ,keys
for v in ,values do
(setf (getprop ,dict-sym k) v))
,dict-sym)))
(defmacro+ps concat (&rest rest)
`(concatenate 'string ,@rest))
(defmacro+ps parse-json (str)
`(chain -J-S-O-N (parse ,str)))
(defmacro+ps stringify-json (obj)
`(chain -J-S-O-N (stringify ,obj)))
;; Node.js
(defmacro+ps log-msg (&rest msg)
`(chain console (log (concatenate 'string ,@msg))))
;; Socket.io
(defmacro+ps call (obj func &rest rest)
`(chain ,obj (,func ,@rest)))
(defmacro+ps on-event (obj event &rest rest)
`(call ,obj on ,event ,@rest))
(defmacro+ps on-connection (func)
`(on-event (@ io sockets) "connection" ,func))
(defmacro+ps emit (socket name obj)
`(call ,socket emit ,name ,obj))
;; Redis
(defmacro+ps with-redis (&rest body)
`(progn
,@(loop for s in body collect
(cons 'call (cons '*client* s)))))
(defun exp-p (name)
(let ((l (length name)))
(if (< l 4)
nil
(equal "-EXP" (string-upcase (subseq name (- l 4)))))))
(defmacro+ps let-redis-1 (binding &body body)
(cons 'with-redis
(list (append
;; Expand macro first if the command is ending with -exp
(let ((s (cadr binding)))
(if (exp-p (symbol-name (car s)))
(macroexpand s)
s))
`(#'(lambda (err ,(car binding))
#+nil
(if err (log-msg "Error:" err) (log-msg "OK:" ,(car binding)))
,@body))))))
(defmacro+ps let-redis** (bindings &body body)
;; Send all of command first and execute body when all of results are received.
(let ((function-syms
(loop for i from 0 to (length bindings) collect (gensym)))
(sym-final (gensym))
(sym-count (gensym)))
`(let ((,sym-count 0)
,@(loop for b in bindings collect `(,(car b) nil)))
(labels ((,sym-final () ,@body)
,@(loop for b in bindings
for f in function-syms collect
`(,f (reply)
(setf ,sym-count (1+ ,sym-count))
(setf ,(car b) reply)
(when (eql ,sym-count ,(length bindings)) (,sym-final)))))
,@(loop for b in bindings
for f in function-syms collect
`(let-redis-1 ,b (,f ,(car b))))))))
(defmacro+ps print-redis (s)
`(let-redis-1 (output ,s) (log-msg output)))
(defmacro+ps do-redis ((var list) &body body)
`(dolist (,var ,list) (with-redis ,@body)))
(defmacro+ps list-redis* ((var list) (v exp) &body body)
(let ((sym-var (gensym))
(sym-count (gensym))
(sym-length (gensym))
(sym-results (gensym))
(sym-final (gensym))
(sym-handler (gensym))
(sym-idx (gensym)))
`(let ((,sym-count 0)
(,sym-length (length ,list))
(,sym-results (make-array)))
(labels ((,sym-final () (let ((,v ,sym-results)) ,@body))
(,sym-handler (idx result)
;; Debug code
#+nil
(log-msg "Handler: " idx " => " result)
(setf ,sym-count (1+ ,sym-count))
(setf (aref ,sym-results idx) result)
(when (eql ,sym-count ,sym-length) (,sym-final))))
(loop for i in ,list
for idx from 0 do
(let ((,var i)
(,sym-idx idx)) ; Index should be saved
(let-redis-1 (,sym-var ,exp) (,sym-handler ,sym-idx ,sym-var))))))))