Using expanders to read a file without evaluating it

Edit
Created: 2021/8/3
Updated: 2021/8/3

One of the fiddlier corners of Janet has been loading project.janet files without actually executing all of the code and having access to the core of jpm

Below is a small snippet that loads the top-level “declare-“ forms into a map, given a janet file.

(defn capture-declares [path] 
    (def *capture* @{})
    (defn append-capture [name & arg]
      (update *capture* name (fn [val] (array/push (or val @[]) ;arg))))
    (defn only-captures [form] 
      (when (string/has-prefix? "declare-" (string (form 0)))
        (append-capture (form 0) (slice form 1))
        form)
      nil)
    (dofile path :expander only-captures)
    *capture*)