/lib/template.arc

http://github.com/alimoeeny/arc · Unknown · 39 lines · 31 code · 8 blank · 0 comment · 0 complexity · dd6656f086026fe2be9560ebf1ad9c57 MD5 · raw file

  1. ; originally from http://awwx.ws/template5.arc
  2. (load "lib/util.arc")
  3. (implicit guillemet pr)
  4. ; turn adjacent characters into a string
  5. (def coalesce-chars (lst)
  6. (xloop (lst lst a nil)
  7. (if (no lst)
  8. (rev a)
  9. (let (chars rest) (span [is (type _) 'char] lst)
  10. (if chars
  11. (next rest (cons (coerce chars 'string) a))
  12. (next cdr.lst (cons car.lst a)))))))
  13. (def slurp-template-field (closing port)
  14. (read:string:accum a
  15. (whiler c (readc port) [or (no _) (is _ closing)]
  16. (a c))))
  17. (def slurp-template (port (o accum))
  18. (cons 'template
  19. (coalesce-chars:accum a
  20. (whiler c (readc port) [or (no _) (is _ #\)]
  21. (a (if (is c #\«)
  22. (list 'guillemet (slurp-template-field #\» port))
  23. (is c #\)
  24. (list 'pr (slurp-template-field #\⌋ port))
  25. c))))))
  26. (extend-readtable #\ slurp-template)
  27. (mac template xs
  28. `(do ,@(map [if (isa _ 'string)
  29. `(pr ,_)
  30. _]
  31. xs)))