PageRenderTime 3ms CodeModel.GetById 1ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/dynvar.arc

http://github.com/alimoeeny/arc
Unknown | 37 lines | 32 code | 5 blank | 0 comment | 0 complexity | 3e22ad06feed16431b4cacb5eb8b6a03 MD5 | raw file
 1(require "lib/util.arc")
 2(require "lib/thread-cell.arc")
 3
 4(mac with* (parms . body)
 5  (let uparms (mapeach (var val) (pair parms)
 6                (list var val (uniq (+ "old-" var)) (uniq (+ "new-" var))))
 7    `(with ,(mappendeach (var val old new) uparms
 8              `(,old ,var ,new ,val))
 9       ; we should arguably use dynamic-wind here, but pg's method seems to be
10       ; to just use protect instead and never use continuations as anything but
11       ; escape procedures.
12       (protect
13         (fn ()
14           ; update the variables
15           (= ,@(mappendeach (var val old new) uparms
16                  `(,var ,new)))
17           ,@body)
18         (fn ()
19           ; restore the variables
20           (= ,@(mappendeach (var val old new) uparms
21                  `(,var ,old))))))))
22
23(mac let* (var val . body)
24  `(with* (,var ,val) ,@body))
25
26(mac withs* (parms . body)
27  (if no.parms `(do ,@body)
28    (let (var val . rest) parms
29      `(let* ,var ,val (withs* ,rest ,@body)))))
30
31; Thread-local vars to use with the above
32(mac dynvar (name (o init-val))
33  `(= ,name (thread-cell ,init-val)))
34
35(mac dynvars vars
36  `(do ,@(mapeach var vars
37           (if atom.var `(dynvar ,var) `(dynvar ,@var)))))