aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/meta/target/jvm/bytecode/environment/limit/stack.lux
blob: 7517c2b60ffb683528125fb74bd568ad78c61d6f (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
(.require
 [library
  [lux (.except)
   [abstract
    ["[0]" equivalence (.only Equivalence)]]
   [control
    ["[0]" maybe]
    ["[0]" try (.only Try)]]
   [data
    [text
     ["%" \\format]]
    [binary
     [\\format (.only Format)]]]
   [meta
    [type
     ["[0]" nominal (.except def)]]]]]
 ["[0]" /////
  [encoding
   ["[1][0]" unsigned (.only U2)]]])

(nominal.def .public Stack
  U2

  (with_template [<frames> <name>]
    [(def .public <name>
       Stack
       (|> <frames> /////unsigned.u2 maybe.trusted abstraction))]

    [0 empty]
    [1 catch]
    )

  (def .public equivalence
    (Equivalence Stack)
    (of equivalence.functor each
        (|>> representation)
        /////unsigned.equivalence))

  (def .public format
    (Format Stack)
    (|>> representation /////unsigned.format/2))

  (def stack
    (-> U2 Stack)
    (|>> abstraction))

  (with_template [<op> <name>]
    [(def .public (<name> amount)
       (-> U2 (-> Stack (Try Stack)))
       (|>> representation
            (<op> amount)
            (of try.functor each ..stack)))]

    [/////unsigned.+/2 push]
    [/////unsigned.-/2 pop]
    )

  (def .public (max left right)
    (-> Stack Stack Stack)
    (abstraction
     (/////unsigned.max/2 (representation left)
                          (representation right))))

  (def .public text
    (%.Format Stack)
    (|>> representation /////unsigned.value %.nat))
  )

(def .public length
  /////unsigned.bytes/2)