aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/data/text/format.lux
blob: 575448be257da007e42077323c5da1dd939bd2ed (plain)
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
(;module:
  lux
  (lux (control monad)
       (data [bool]
             [char]
             [number]
             [text]
             [ident]
             (coll [list "" Monad<List>]))
       [type]
       [compiler]
       (macro [ast]
              ["s" syntax #+ syntax: Syntax])))

## [Syntax]
(def: #hidden _append_
  (-> Text Text Text)
  (:: text;Monoid<Text> append))

(syntax: #export (format [fragments (s;many s;any)])
  {#;doc (doc "Text interpolation as a macro."
              (format "Static part " (%t static) " doesn't match URI: " uri))}
  (wrap (list (` ($_ _append_ (~@ fragments))))))

## [Formatters]
(type: #export (Formatter a)
  {#;doc "A way to produce readable text from values."}
  (-> a Text))

(do-template [<name> <type> <formatter>]
  [(def: #export <name>
     (Formatter <type>)
     <formatter>)]

  [%b     Bool  (:: bool;Codec<Text,Bool> encode)]
  [%n     Nat   (:: number;Codec<Text,Nat> encode)]
  [%i     Int   (:: number;Codec<Text,Int> encode)]
  [%d     Deg  (:: number;Codec<Text,Deg> encode)]
  [%r     Real  (:: number;Codec<Text,Real> encode)]
  [%c     Char  (:: char;Codec<Text,Char> encode)]
  [%t     Text  (:: text;Codec<Text,Text> encode)]
  [%ident Ident (:: ident;Codec<Text,Ident> encode)]
  [%ast   AST   ast;to-text]
  [%type  Type  type;to-text]
  [%bin     Nat   (:: number;Binary@Codec<Text,Nat> encode)]
  [%oct     Nat   (:: number;Octal@Codec<Text,Nat> encode)]
  [%hex     Nat   (:: number;Hex@Codec<Text,Nat> encode)]
  )

(def: #export (%list formatter)
  (All [a] (-> (Formatter a) (Formatter (List a))))
  (function [values]
    (case values
      #;Nil
      "(list)"

      _
      (format "(list " (text;join-with " " (map formatter values)) ")"))))