aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/tool/compiler/reference/variable.lux
blob: a8ce4c0490d6b5c6bfe0c88fbb6278f20832b695 (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
(.module:
  [library
   [lux #*
    [abstract
     [equivalence (#+ Equivalence)]
     [hash (#+ Hash)]]
    [control
     [pipe (#+ case>)]]
    [data
     [text
      ["%" format (#+ Format)]]]
    [math
     [number
      ["n" nat]
      ["i" int]]]]])

(type: #export Register
  Nat)

(type: #export Variable
  (#Local Register)
  (#Foreign Register))

(implementation: #export equivalence
  (Equivalence Variable)
  
  (def: (= reference sample)
    (case [reference sample]
      (^template [<tag>]
        [[(<tag> reference') (<tag> sample')]
         (n.= reference' sample')])
      ([#Local] [#Foreign])

      _
      #0)))

(implementation: #export hash
  (Hash Variable)
  
  (def: &equivalence
    ..equivalence)
  
  (def: hash
    (|>> (case> (^template [<factor> <tag>]
                  [(<tag> register)
                   ($_ n.* <factor>
                       (\ n.hash hash register))])
                ([2 #Local]
                 [3 #Foreign])))))

(template: #export (self)
  (#..Local 0))

(def: #export self?
  (-> Variable Bit)
  (|>> (case> (^ (..self))
              true

              _
              false)))

(def: #export format
  (Format Variable)
  (|>> (case> (#Local local)
              (%.format "+" (%.nat local))
              
              (#Foreign foreign)
              (%.format "-" (%.nat foreign)))))