aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/meta/compiler/reference.lux
blob: b7eb370def7904dfdbe5e754fc08000ff232d87e (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
(.require
 [library
  [lux (.except local)
   [abstract
    [equivalence (.only Equivalence)]
    [hash (.only Hash)]]
   [control
    ["[0]" pipe]]
   [data
    [text
     ["%" \\format (.only Format)]]]
   [math
    [number
     ["n" nat]]]
   [meta
    ["[0]" symbol]
    [macro
     ["^" pattern]]]]]
 ["[0]" /
  ["[1][0]" variable (.only Variable)]])

(type .public Constant
  Symbol)

(type .public Reference
  (Variant
   {#Variable Variable}
   {#Constant Constant}))

(def .public equivalence
  (Equivalence Reference)
  (implementation
   (def (= reference sample)
     (when [reference sample]
       (^.with_template [<tag> <equivalence>]
         [[{<tag> reference} {<tag> sample}]
          (at <equivalence> = reference sample)])
       ([#Variable /variable.equivalence]
        [#Constant symbol.equivalence])

       _
       false))))

(def .public hash
  (Hash Reference)
  (implementation
   (def equivalence
     ..equivalence)

   (def (hash value)
     (when value
       (^.with_template [<factor> <tag> <hash>]
         [{<tag> value}
          (|> value
              (at <hash> hash)
              (n.* <factor>))])
       ([2 #Variable /variable.hash]
        [3 #Constant symbol.hash])
       ))))

(with_template [<name> <family> <tag>]
  [(def .public <name>
     (template (<name> content)
       [(<| {<family>}
            {<tag>}
            content)]))]

  [local   ..#Variable /variable.#Local]
  [foreign ..#Variable /variable.#Foreign]
  )

(with_template [<name> <tag>]
  [(def .public <name>
     (template (<name> content)
       [(<| {<tag>}
            content)]))]

  [variable ..#Variable]
  [constant ..#Constant]
  )

(`` (def .public self
      (template (self)
        [(..variable (,, (/variable.self)))])))

(def .public format
  (Format Reference)
  (|>> (pipe.when
         {#Variable variable}
         (/variable.format variable)
         
         {#Constant constant}
         (%.symbol constant))))