From d7ebb3e607da6444d8215b1ee273c4247489f862 Mon Sep 17 00:00:00 2001 From: Andrey Orst Date: Mon, 2 Nov 2020 21:43:46 +0300 Subject: feature(macros): add def and defonce macros --- macros/core.fnl | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'macros') 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 -- cgit v1.2.3