summaryrefslogtreecommitdiff
path: root/macros/core.fnl
diff options
context:
space:
mode:
authorAndrey Orst <andreyorst@gmail.com>2020-11-05 20:48:38 +0300
committerAndrey Orst <andreyorst@gmail.com>2020-11-05 21:39:16 +0300
commitfed527410fa6b8a28999045f93d31a0427962d9e (patch)
tree517ecf3e20e094475fa8c6b6601833d4b0409788 /macros/core.fnl
parent4bb3a70a881a928f8ac258154df681e63dabda83 (diff)
feature(macros): inject metadata into definitions
Diffstat (limited to 'macros/core.fnl')
-rw-r--r--macros/core.fnl40
1 files changed, 26 insertions, 14 deletions
diff --git a/macros/core.fnl b/macros/core.fnl
index 31ac1ae..b073d27 100644
--- a/macros/core.fnl
+++ b/macros/core.fnl
@@ -152,6 +152,19 @@
(fn string? [x]
(= (type x) :string))
+(fn* core.with-meta [val meta]
+ `(let [val# ,val
+ (res# fennel#) (pcall require :fennel)]
+ (if res#
+ (each [k# v# (pairs ,meta)]
+ (fennel#.metadata:set val# k# v#)))
+ val#))
+
+(fn* core.meta [v]
+ `(let [(res# fennel#) (pcall require :fennel)]
+ (if res#
+ (. fennel#.metadata ,v))))
+
(fn* core.defmulti
[name & opts]
(let [docstring (if (string? (first opts)) (first opts))
@@ -162,7 +175,7 @@
`(local ,name
(let [multimethods# {}]
(setmetatable
- {}
+ ,(with-meta {} {:fnl/docstring docstring})
{:__call
(fn [_# ...]
,docstring
@@ -186,25 +199,24 @@
(fn* core.def
([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)]
+ (let [attr-map (if (table? attr-map) attr-map
+ (string? attr-map) {attr-map true}
+ (error "def: expected keyword or literal table as first argument" 2))
+ (s multi) (multisym->sym name)
+ docstring (or (. attr-map :doc)
+ (. attr-map :fnl/docstring))
+ f (if (. attr-map :dynamic) 'var 'local)]
(if multi
- `(,f ,s (do (,f ,s ,expr) (set ,name ,s) ,s))
- `(,f ,name ,expr)))))
+ `(,f ,s (do (,f ,s ,expr)
+ (set ,name ,s)
+ ,(with-meta s {:fnl/docstring docstring})))
+ `(,f ,name ,(with-meta expr {:fnl/docstring docstring}))))))
(fn* core.defonce
([name expr]
(defonce {} name expr))
([attr-map name expr]
- (if (in-scope? (if (table? attr-map) name attr-map))
+ (if (in-scope? name)
nil
(def attr-map name expr))))