From 34ae8addc83267960d0b2daede2bcc91ea24d809 Mon Sep 17 00:00:00 2001 From: Andrey Orst Date: Mon, 2 Nov 2020 22:33:25 +0300 Subject: feature(macros): support attribute table in def and defonce --- macros/core.fnl | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) (limited to 'macros') diff --git a/macros/core.fnl b/macros/core.fnl index f300dbd..31ac1ae 100644 --- a/macros/core.fnl +++ b/macros/core.fnl @@ -184,16 +184,28 @@ 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)))) + ([name expr] (def {} name expr)) + ([attr-map name expr] + (if (not (or (table? attr-map) + (string? attr-map))) + (error "def: expected keyword or literal table as first argument" 2)) + (let [(s multi) (multisym->sym name) + f (if (if (table? attr-map) + (. attr-map :dynamic) + (if (= attr-map :dynamic) + true + (error (.. "unsupported attribute keyword: :" attr-map) 2))) + 'var 'local)] + (if multi + `(,f ,s (do (,f ,s ,expr) (set ,name ,s) ,s)) + `(,f ,name ,expr))))) (fn* core.defonce - [name expr] - (if (in-scope? name) - nil - (def name expr))) + ([name expr] + (defonce {} name expr)) + ([attr-map name expr] + (if (in-scope? (if (table? attr-map) name attr-map)) + nil + (def attr-map name expr)))) core -- cgit v1.2.3