aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/static.lux
blob: 3f9d9caed0ec783f191fe7740d5dc03eab883967 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
(.module:
  [library
   [lux {"-" [nat int rev]}
    ["." meta]
    [abstract
     [monad {"+" [do]}]]
    [control
     ["<>" parser
      ["<.>" code]]]
    [macro
     [syntax {"+" [syntax:]}]
     ["." code]]
    [math
     [number {"+" [hex]}]
     ["." random {"+" [Random]}]]]])

(template [<name> <type> <format>]
  [(syntax: .public (<name> [expression <code>.any])
     (\ meta.monad each
        (|>> (:as <type>) <format> list)
        (meta.eval <type> expression)))]

  [nat .Nat code.nat]
  [int .Int code.int]
  [rev .Rev code.rev]
  [frac .Frac code.frac]
  [text .Text code.text]
  )

(def: pcg_32_magic_inc
  Nat
  (hex "FEDCBA9876543210"))

(with_expansions [<type> (Ex (_ a)
                           [(-> a Code)
                            a])]
  (syntax: .public (literal [format <code>.any
                             expression <code>.any])
    (do meta.monad
      [pair (meta.eval (type <type>)
                       (` [(~ format) (~ expression)]))
       .let [[format expression] (:as <type> pair)]]
      (in (list (format expression))))))

(template [<name> <random> <format>]
  [(syntax: .public (<name> [])
     (do meta.monad
       [seed meta.seed
        .let [[_ result] (random.result (random.pcg_32 [..pcg_32_magic_inc seed])
                                        <random>)]]
       (in (list (<format> result)))))]

  [random_nat random.nat code.nat]
  [random_int random.int code.int]
  [random_rev random.rev code.rev]
  [random_frac random.frac code.frac]
  )

(with_expansions [<type> (Ex (_ a)
                           [(-> a Code)
                            (Random a)])]
  (syntax: .public (random [format <code>.any
                            random <code>.any])
    (do meta.monad
      [pair (meta.eval (type <type>)
                       (` [(~ format) (~ random)]))
       .let [[format random] (:as <type> pair)]
       seed meta.seed
       .let [[_ result] (random.result (random.pcg_32 [..pcg_32_magic_inc seed])
                                       random)]]
      (in (list (format result))))))