aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/source/luxc/analyser/reference.lux
blob: 5f09ee77462d5cd0f38983e286dd778c572cbd08 (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
(;module:
  lux
  (lux (control monad)
       [macro #+ Monad<Lux>]
       (type ["TC" check]))
  (luxc ["&" base]
        (lang ["la" analysis #+ Analysis])
        ["&;" env]))

## [Analysers]
(def: (analyse-definition def-name)
  (-> Ident (Lux Analysis))
  (do Monad<Lux>
    [actual (macro;find-def-type def-name)
     expected macro;expected-type
     _ (&;within-type-env
        (TC;check expected actual))]
    (wrap (#la;Definition def-name))))

(def: (analyse-variable var-name)
  (-> Text (Lux (Maybe Analysis)))
  (do Monad<Lux>
    [?var (&env;find var-name)]
    (case ?var
      (#;Some [actual ref])
      (do @
        [expected macro;expected-type
         _ (&;within-type-env
            (TC;check expected actual))]
        (wrap (#;Some (#la;Variable ref))))

      #;None
      (wrap #;None))))

(def: #export (analyse-reference reference)
  (-> Ident (Lux Analysis))
  (case reference
    ["" simple-name]
    (do Monad<Lux>
      [?var (analyse-variable simple-name)]
      (case ?var
        (#;Some analysis)
        (wrap analysis)

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

    _
    (analyse-definition reference)))