diff options
| author | Andrey Orst <andreyorst@gmail.com> | 2020-11-02 21:43:46 +0300 |
|---|---|---|
| committer | Andrey Orst <andreyorst@gmail.com> | 2020-11-02 21:43:46 +0300 |
| commit | d7ebb3e607da6444d8215b1ee273c4247489f862 (patch) | |
| tree | 343392e6e5a081bd980fd865f2d54d9fbac8bb9e /macros/core.fnl | |
| parent | 9878bb3f75db689e44f0b11cef926c7a39bc6dd5 (diff) | |
feature(macros): add def and defonce macros
Diffstat (limited to 'macros/core.fnl')
| -rw-r--r-- | macros/core.fnl | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/macros/core.fnl b/macros/core.fnl index de6a1b0..f300dbd 100644 --- a/macros/core.fnl +++ b/macros/core.fnl @@ -3,6 +3,11 @@ (local unpack (or table.unpack _G.unpack)) (local insert table.insert) +(fn multisym->sym [s] + (if (multi-sym? s) + (values (sym (string.gsub (tostring s) ".*[.]" "")) true) + (values s false))) + (fn check-bindings [bindings] (and (assert-compile (sequence? bindings) "expected binding table" []) (assert-compile (= (length bindings) 2) "expected exactly two forms in binding vector." bindings))) @@ -178,4 +183,17 @@ (fn ,(unpack fn-tail))) multifn#)) +(fn* core.def + [name expr] + (let [(s m) (multisym->sym name)] + (if m + `(local ,s (do (local ,s ,expr) (set ,name ,s) ,s)) + `(local ,name ,expr)))) + +(fn* core.defonce + [name expr] + (if (in-scope? name) + nil + (def name expr))) + core |