Skip to content

Commit

Permalink
refactor: legacy dependency-map code into lang/fennel/dependency-tracker
Browse files Browse the repository at this point in the history
  • Loading branch information
rktjmp committed Feb 28, 2024
1 parent cc78919 commit 409f88f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 52 deletions.
43 changes: 0 additions & 43 deletions fnl/hotpot/dependency-map.fnl

This file was deleted.

10 changes: 5 additions & 5 deletions fnl/hotpot/lang/fennel/compiler.fnl
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@
;; to avoid circular dependencies.
;; By putting it here we can be sure that the dep map module is already
;; in memory before hotpot took over macro module searching.
(let [dep-map (require :hotpot.dependency-map)]
(let [{: set-macro-modname-path} (require :hotpot.lang.fennel.dependency-tracker)]
;; later, when a module needs a macro, we will know what file the
;; macro came from and can then track the macro file for changes
;; when refreshing the cache.
(dep-map.set-macro-modname-path modname fnl-path)
(set-macro-modname-path modname fnl-path)
;; eval macro as per fennel's implementation.
(fennel.eval fnl-code options modname)))))

Expand Down Expand Up @@ -145,9 +145,9 @@

(λ compile-record [record modules-options macros-options preprocessor]
"Compile fnl-path to lua-path, returns true or false compilation-errors"
(let [{: deps-for-fnl-path} (require :hotpot.dependency-map)
{: lua-path : src-path : modname} record
{:new new-macro-dep-tracking-plugin} (require :hotpot.lang.fennel.dependency-tracker)
(let [{: lua-path : src-path : modname} record
{:new new-macro-dep-tracking-plugin
: deps-for-fnl-path} (require :hotpot.lang.fennel.dependency-tracker)
modules-options (doto modules-options
(tset :module-name modname)
(tset :filename src-path)
Expand Down
49 changes: 45 additions & 4 deletions fnl/hotpot/lang/fennel/dependency-tracker.fnl
Original file line number Diff line number Diff line change
@@ -1,3 +1,43 @@
;; NOTE: This is used in the macro loader, so you may not use any
;; macros in here, or probably any requires either to avoid
;; circular compile chains.

;; to track macro-file dependencies, we have two steps:
;;
;; 1. when a macro is found by the macro searcher, record the [modname, path]
;; pair. (set-macro-modname-path)
;; 2. when a macro is required by a regular module, record that module and the
;; macro modname. (fnl-path-depends-on-macro-module)

(var macro-mods-paths {})
(var fnl-file-macro-mods {})

(fn set-macro-modname-path [mod path]
(let [existing-path (. macro-mods-paths mod)
fmt string.format]
(assert (or (= existing-path path)
(= existing-path nil))
(fmt "already have mod-path for %s, current: %s, new: %s" mod existing-path path))
(tset macro-mods-paths mod path)))

(fn fnl-path-depends-on-macro-module [fnl-path macro-module]
(let [list (or (. fnl-file-macro-mods fnl-path) [])]
;; guard nil inserts because they will effectively truncate the list also
;; we will raise a hard error because it's likely a pretty ununsual case
;; but one that we want to reproduce and fix if possible.
(if macro-module
(do
(table.insert list macro-module)
(tset fnl-file-macro-mods fnl-path list))
(error (.. "tried to insert nil macro dependencies for "
fnl-path ", please report this issue")))))

(fn deps-for-fnl-path [fnl-path]
(match (. fnl-file-macro-mods fnl-path)
; list may contain duplicates, so we can dedup via keys
deps (icollect [_ mod (ipairs deps)]
(. macro-mods-paths mod))))

(fn new [fnl-path required-from-modname]
{:versions [:1.1.0 :1.1.1 :1.2.0 :1.2.1 :1.3.0 :1.3.1 :1.4.0 :1.4.1]
:name (.. :hotpot-macro-dep-tracking-for- required-from-modname)
Expand Down Expand Up @@ -44,17 +84,18 @@
macro-modname (fennel.eval (fennel.view second)
{:module-name required-from-modname}
required-from-modname
fnl-path)
dep-map (require :hotpot.dependency-map)]
fnl-path)]
(assert macro-modname (.. "congratulations, you're doing something weird, "
"probably with recursive relative macro requires, "
"please open a bug with an example of your setup"))
; (print (fennel.view second))
; (vim.pretty_print {:fnl-path fnl-path
; :required-from-modname required-from-modname
; :macro-modname macro-modname})
(dep-map.fnl-path-depends-on-macro-module fnl-path macro-modname))
(fnl-path-depends-on-macro-module fnl-path macro-modname))
;; dont halt other plugins
(values nil))})

{: new}
{: deps-for-fnl-path
: set-macro-modname-path
: new}

0 comments on commit 409f88f

Please sign in to comment.