aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/meta/target/jvm/bytecode/address.lux
blob: 3e6d4f4c2f846a8b3e4b4683f0b229e1815fa27f (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
72
73
74
75
(.require
 [library
  [lux (.except)
   [abstract
    [equivalence (.only Equivalence)]
    [monad (.only do)]]
   [control
    ["[0]" try (.only Try)]]
   [data
    [binary
     [\\format (.only Format)]]
    [text
     ["%" \\format]]]
   [math
    [number
     ["n" nat]]]
   [meta
    [type
     ["[0]" nominal (.except def)]]]]]
 ["[0]" //
  [jump (.only Big_Jump)]
  ["/[1]" //
   [encoding
    ["[1][0]" unsigned (.only U2)]
    ["[1][0]" signed (.only S4)]]]])

(nominal.def .public Address
  U2

  (def .public value
    (-> Address U2)
    (|>> representation))

  (def .public start
    Address
    (|> 0 ///unsigned.u2 try.trusted abstraction))

  (def .public (move distance)
    (-> U2 (-> Address (Try Address)))
    (|>> representation
         (///unsigned.+/2 distance)
         (at try.functor each (|>> abstraction))))

  (def with_sign
    (-> Address (Try S4))
    (|>> representation ///unsigned.value .int ///signed.s4))

  (def .public (jump from to)
    (-> Address Address (Try Big_Jump))
    (do try.monad
      [from (with_sign from)
       to (with_sign to)]
      (///signed.-/4 from to)))

  (def .public (after? reference subject)
    (-> Address Address Bit)
    (n.> (|> reference representation ///unsigned.value)
         (|> subject representation ///unsigned.value)))

  (def .public equivalence
    (Equivalence Address)
    (implementation
     (def (= reference subject)
       (at ///unsigned.equivalence =
           (representation reference)
           (representation subject)))))

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

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