aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/meta/compiler/reference/variable.lux
blob: 0e038ed065799b756ccf4cf35284d066a40c52d4 (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
76
77
(.require
 [library
  [lux (.except)
   [abstract
    [equivalence (.only Equivalence)]
    [hash (.only Hash)]]
   [control
    ["[0]" pipe]]
   [data
    [text
     ["%" \\format (.only Format)]]]
   [math
    [number
     ["n" nat]
     ["i" int]]]
   [meta
    [macro
     ["^" pattern]]]]])

(type .public Register
  Nat)

(type .public Variable
  (Variant
   {#Local Register}
   {#Foreign Register}))

(def .public equivalence
  (Equivalence Variable)
  (implementation
   (def (= reference sample)
     (when [reference sample]
       (^.with_template [<tag>]
         [[{<tag> reference'} {<tag> sample'}]
          (n.= reference' sample')])
       ([#Local] [#Foreign])

       _
       false))))

(def .public hash
  (Hash Variable)
  (implementation
   (def equivalence
     ..equivalence)
   
   (def hash
     (|>> (pipe.when
            (^.with_template [<factor> <tag>]
              [{<tag> register}
               (|> register
                   (at n.hash hash)
                   (n.* <factor>))])
            ([2 #Local]
             [3 #Foreign]))))))

(def .public self
  (template (self)
    [{..#Local 0}]))

(def .public self?
  (-> Variable Bit)
  (|>> (pipe.when
         (..self)
         true

         _
         false)))

(def .public format
  (Format Variable)
  (|>> (pipe.when
         {#Local local}
         (%.format "+" (%.nat local))
         
         {#Foreign foreign}
         (%.format "-" (%.nat foreign)))))