aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/source/luxc/synthesizer/function.lux
blob: e8b2a7ec4d8b06314f0e709243d7bdc7949f7c28 (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
(;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))]

  [self?     i.=]
  [local?    i.>]
  [captured? i.<]
  )

(do-template [<name> <comp> <ref>]
  [(def: #export (<name> arity)
     (-> ls;Arity Bool)
     (<comp> <ref> arity))]

  [nested? n.> +1]
  [top?    n.= +0]
  )

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

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

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

(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])))