/lib/template.arc
http://github.com/alimoeeny/arc · Unknown · 39 lines · 31 code · 8 blank · 0 comment · 0 complexity · dd6656f086026fe2be9560ebf1ad9c57 MD5 · raw file
- ; originally from http://awwx.ws/template5.arc
- (load "lib/util.arc")
- (implicit guillemet pr)
- ; turn adjacent characters into a string
- (def coalesce-chars (lst)
- (xloop (lst lst a nil)
- (if (no lst)
- (rev a)
- (let (chars rest) (span [is (type _) 'char] lst)
- (if chars
- (next rest (cons (coerce chars 'string) a))
- (next cdr.lst (cons car.lst a)))))))
- (def slurp-template-field (closing port)
- (read:string:accum a
- (whiler c (readc port) [or (no _) (is _ closing)]
- (a c))))
- (def slurp-template (port (o accum))
- (cons 'template
- (coalesce-chars:accum a
- (whiler c (readc port) [or (no _) (is _ #\”)]
- (a (if (is c #\«)
- (list 'guillemet (slurp-template-field #\» port))
- (is c #\⌊)
- (list 'pr (slurp-template-field #\⌋ port))
- c))))))
- (extend-readtable #\“ slurp-template)
- (mac template xs
- `(do ,@(map [if (isa _ 'string)
- `(pr ,_)
- _]
- xs)))