aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/source/luxc/lang/analysis/reference.lux
blob: 56aba35de3c16f5558d414bf23e21e24aad666c9 (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
(.module:
  lux
  (lux (control monad)
       [macro]
       (macro [code])
       (lang (type ["tc" check])))
  (luxc ["&" lang]
        (lang ["&." scope]
              ["la" analysis #+ Analysis]
              [".L" variable #+ Variable])))

## [Analysers]
(def: (analyse-definition def-name)
  (-> Ident (Meta Analysis))
  (do macro.Monad<Meta>
    [[actualT def-anns _] (&.with-error-tracking
                            (macro.find-def def-name))]
    (case (macro.get-symbol-ann (ident-for #.alias) def-anns)
      (#.Some real-def-name)
      (analyse-definition real-def-name)

      _
      (do @
        [_ (&.infer actualT)
         def-name (macro.normalize def-name)]
        (wrap (code.symbol def-name))))))

(def: (analyse-variable var-name)
  (-> Text (Meta (Maybe Analysis)))
  (do macro.Monad<Meta>
    [?var (&scope.find var-name)]
    (case ?var
      (#.Some [actualT ref])
      (do @
        [_ (&.infer actualT)]
        (wrap (#.Some (` ((~ (code.int (variableL.from-ref ref))))))))

      #.None
      (wrap #.None))))

(def: #export (analyse-reference reference)
  (-> Ident (Meta Analysis))
  (case reference
    ["" simple-name]
    (do macro.Monad<Meta>
      [?var (analyse-variable simple-name)]
      (case ?var
        (#.Some varA)
        (wrap varA)

        #.None
        (do @
          [this-module macro.current-module-name]
          (analyse-definition [this-module simple-name]))))

    _
    (analyse-definition reference)))