refactor compiler.fnl to share destructure recursion
This commit is contained in:
parent
8b6d905450
commit
1c40c93b4d
@ -102,24 +102,10 @@ later by fennel-ls.language to answer requests from the client."
|
|||||||
:write
|
:write
|
||||||
:mutate)))
|
:mutate)))
|
||||||
|
|
||||||
(λ define [?definition binding scope ?opts]
|
(λ for-each-binding-in [binding ?definition action]
|
||||||
;; Add a definition to the definitions
|
|
||||||
;; recursively explore the binding (which, in the general case, is a destructuring assignment)
|
|
||||||
;; right now I'm not keeping track of *how* the symbol was destructured: just finding all the symbols for now.
|
|
||||||
;; also, there's no logic for (values)
|
|
||||||
(λ recurse [binding keys depth]
|
(λ recurse [binding keys depth]
|
||||||
(if (sym? binding)
|
(if (sym? binding)
|
||||||
(let [definition
|
(action binding ?definition keys keys.multival)
|
||||||
{: binding
|
|
||||||
:definition ?definition
|
|
||||||
:referenced-by (or (?. definitions binding :referenced-by) [])
|
|
||||||
:keys (if (< 0 (length keys))
|
|
||||||
(fcollect [i 1 (length keys)]
|
|
||||||
(. keys i)))
|
|
||||||
:multival keys.multival
|
|
||||||
:var? (?. ?opts :isvar)}]
|
|
||||||
(tset (. definitions-by-scope scope) (tostring binding) definition)
|
|
||||||
(tset definitions binding definition))
|
|
||||||
(list? binding)
|
(list? binding)
|
||||||
(let [nested? (not= depth 0)]
|
(let [nested? (not= depth 0)]
|
||||||
(if nested? (error (.. "I didn't expect to find a multival destructure in " (view binding) " at " (view keys))))
|
(if nested? (error (.. "I didn't expect to find a multival destructure in " (view binding) " at " (view keys))))
|
||||||
@ -135,7 +121,7 @@ later by fennel-ls.language to answer requests from the client."
|
|||||||
(recurse child keys (+ depth 1))
|
(recurse child keys (+ depth 1))
|
||||||
(or (sym? key :&) (sym? prev :&))
|
(or (sym? key :&) (sym? prev :&))
|
||||||
;; currently the "rest" param is defined to []
|
;; currently the "rest" param is defined to []
|
||||||
(define [] child scope ?opts)
|
(for-each-binding-in child [] action)
|
||||||
(or (sym? child :&as) (sym? child :&))
|
(or (sym? child :&as) (sym? child :&))
|
||||||
child
|
child
|
||||||
(do
|
(do
|
||||||
@ -144,26 +130,28 @@ later by fennel-ls.language to answer requests from the client."
|
|||||||
(table.remove keys))))))
|
(table.remove keys))))))
|
||||||
(recurse binding [] 0))
|
(recurse binding [] 0))
|
||||||
|
|
||||||
|
(λ define [?definition binding scope ?opts]
|
||||||
|
(for-each-binding-in binding ?definition
|
||||||
|
(fn [symbol ?definition keys ?multival]
|
||||||
|
(let [definition
|
||||||
|
{:binding symbol
|
||||||
|
:definition ?definition
|
||||||
|
:referenced-by (or (?. definitions symbol :referenced-by) [])
|
||||||
|
:keys (if (< 0 (length keys))
|
||||||
|
(fcollect [i 1 (length keys)]
|
||||||
|
(. keys i)))
|
||||||
|
:multival ?multival
|
||||||
|
:var? (?. ?opts :isvar)}]
|
||||||
|
(tset (. definitions-by-scope scope) (tostring symbol) definition)
|
||||||
|
(tset definitions symbol definition)))))
|
||||||
|
|
||||||
(λ mutate [_?definition binding scope]
|
(λ mutate [_?definition binding scope]
|
||||||
(λ recurse [binding keys]
|
(for-each-binding-in binding _?definition
|
||||||
(if (sym? binding)
|
(fn [symbol _?definition _keys]
|
||||||
;; (let [;; future work may need to care about mutations
|
(when (not (multisym? symbol))
|
||||||
;; _mutation
|
(reference symbol scope :write)
|
||||||
;; {: binding
|
(if (. references symbol)
|
||||||
;; :new-definition ?definition
|
(tset (. references symbol :target) :var-set true))))))
|
||||||
;; :keys (if (< 0 (length keys))
|
|
||||||
;; (fcollect [i 1 (length keys)]
|
|
||||||
;; (. keys i)))}]
|
|
||||||
(when (not (multisym? binding))
|
|
||||||
(reference binding scope :write)
|
|
||||||
(if (. references binding)
|
|
||||||
(tset (. references binding :target) :var-set true)))
|
|
||||||
(= :table (type binding))
|
|
||||||
(each [k v (iter binding)]
|
|
||||||
(table.insert keys k)
|
|
||||||
(recurse v keys)
|
|
||||||
(table.remove keys))))
|
|
||||||
(recurse binding []))
|
|
||||||
|
|
||||||
(λ destructure [to from scope {:declaration ?declaration? : symtype &as opts}]
|
(λ destructure [to from scope {:declaration ?declaration? : symtype &as opts}]
|
||||||
;; I really don't understand symtype
|
;; I really don't understand symtype
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user