aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/source/luxc/synthesizer/function.lux
blob: be6a74da046c525d07a8201653be7f8de1a32ee2 (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
(;module:
  lux
  (lux (data (coll [list "L/" Functor<List> Fold<List>])))
  (luxc (lang ["la" analysis]
              ["ls" synthesis])))

(def: #export (environment scope)
  (-> Scope (List ls;Variable))
  (|> scope
      (get@ [#;captured #;mappings])
      (L/map (function [[_ _ ref]]
               (case ref
                 (#;Local idx)
                 (nat-to-int idx)
                 
                 (#;Captured idx)
                 (|> idx n.inc nat-to-int (i.* -1))
                 )))))

(do-template [<name> <comp>]
  [(def: #export (<name> var)
     (-> ls;Variable Bool)
     (<comp> 0 var))]

  [function-var? i.=]
  [local-var?    i.>]
  [captured-var? i.<]
  )

(def: #export (nested-function? scope-args)
  (-> Nat Bool)
  (n.> +1 scope-args))

(def: #export (adjust-var scope-args var)
  (-> Nat ls;Variable ls;Variable)
  (|> scope-args n.dec nat-to-int (i.+ var)))

(def: #export (to-captured idx)
  (-> Nat Int)
  (|> idx n.inc nat-to-int (i.* -1)))

(def: #export (to-local idx)
  (-> Nat Int)
  (nat-to-int idx))

(def: #export (unfold-apply apply)
  (-> la;Analysis [la;Analysis (List la;Analysis)])
  (loop [apply apply
         args (list)]
    (case apply
      (#la;Apply arg func)
      (recur func (#;Cons arg args))

      _
      [apply args])))