Skip to content

Macros for working with code as data - #14

Merged
Demonstrandum merged 2 commits into
masterfrom
code-macros
Nov 7, 2026
Merged

Demonstrandum merged 2 commits into
masterfrom
code-macros

Conversation

@Demonstrandum

@Demonstrandum Demonstrandum commented Nov 6, 2026 •

Copy link
Copy Markdown
Owner

Seam had %quote, %eval and %match, but little for building, inspecting or rewriting expressions. This adds:

Building code

  • (%quasi ...) with (%unquote x) and (%splice xs): quote with values filled in, e.g. (%quasi (%+ (%unquote %x) 2)) → (%+ 1 2). Nested %quasis work as in Lisp.
  • (%read "...") parses text as seam code, unexpanded.

Inspecting and building nodes

  • (%type x) → symbol, number, string, raw, list, attribute or nothing; predicates %list?, %symbol?, %string?, %number?, %raw?, %attribute?.
  • (%attribute key value) → :key value with a computed key; (%dict ((a 1) (b 2))) → (:a 1 :b 2), the reverse of %items.

List functions (taking a macro name or a %lambda)

  • %map and %filter now take a list and give a list. %filter now keeps the items its predicate is true for; before, it kept the predicate's non-empty results. Given items directly, (%map f a b c), they still give them unwrapped.
  • New: %fold, %find, %any, %all, %zip, %enumerate (:from), %flatten (:depth), %group-by, and :by as another name for %sort's :key.

Trees

  • (%walk f tree) rebuilds a tree bottom-up, replacing each node with what f gives. Giving nothing removes the node, and giving several splices them in. Attributes have their values walked.
  • (%select h2 tree) / (%select (h2 h3) tree) gives every matching list, in order, e.g. for a table of contents.

Writing macros

  • %gensym, %defined (macro, value or false), and %macroexpand, which shows one step of a user macro's expansion with its parameters substituted.

The new macros live in parse/expander/code.rs. S-expression output now separates every item that lacks whitespace, so (%quote (%f (a))) prints (%f (a)) rather than (%f(a)).

Documented in USAGE.md, with tests for each macro.


Note

Medium Risk
Large new surface in the macro expander with breaking semantic changes to %filter (and stricter list-shaped %map); output formatting also changes for s-expressions.

Overview
This PR turns Seam’s macro expander into a code-as-data toolkit by adding a new parse/expander/code.rs module and wiring a large set of built-ins through expander.rs.

Quoting and construction: (%quasi ...), (%unquote ...), (%splice ...), and (%read "...") let macros build ASTs; %unquote/%splice outside %quasi are errors. Introspection: %type, type predicates (%list?, etc.), %attribute, and %dict complement existing %quote/%eval.

Higher-order lists: %map and %filter are reimplemented to take a function plus a list (predicate truthiness for %filter, not “non-empty results”); new macros include %fold, %find, %any, %all, %zip, %enumerate, %flatten, %group-by, and %sort gains :by as an alias for :key.

Tree transforms: %walk and %select support structural rewrites and extraction (e.g. TOC from headings). Macro authoring: %gensym, %defined, and one-step %macroexpand for user macros.

S-expression rendering now inserts a space between adjacent items that lack leading whitespace (assemble/sexp.rs). README/USAGE.md and dedicated tests cover the new surface.

Reviewed by Cursor Bugbot for commit af05046. Bugbot is set up for automated code reviews on this repo. Configure here.

- %quasi, with %unquote and %splice, builds code with values filled in,
  and %read reads text as code.
- %type and %list? (etc.) tell what kind of node something is, and
  %attribute and %dict build attributes and dictionaries.
- %map and %filter take a list (and %filter keeps the items its predicate
  is true for), and %fold, %find, %any, %all, %zip, %enumerate, %flatten,
  %group-by and %sort :by are new.
- %walk rewrites every node of a tree, and %select finds lists in one.
- %gensym, %defined and %macroexpand help with writing macros.

S-expression output separates every item without whitespace, so a macro
call's first argument is no longer joined to its name.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 4 potential issues.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 76d6661. Configure here.

Comment thread crates/seam/src/parse/expander/code.rs Resolved
Comment thread crates/seam/src/parse/expander/code.rs Resolved
Comment thread crates/seam/src/parse/expander/code.rs Resolved
Comment thread crates/seam/src/parse/expander/code.rs Resolved
@Demonstrandum
Demonstrandum merged commit 923ec73 into master Nov 7, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant