Repository navigation
Macros for working with code as data - #14
Merged
Merged
Conversation
- %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.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 4 potential issues.
❌ 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.
…butes in %macroexpand.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Seam had
%quote,%evaland%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,attributeornothing; predicates%list?,%symbol?,%string?,%number?,%raw?,%attribute?.(%attribute key value)→:key valuewith 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)%mapand%filternow take a list and give a list.%filternow 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.%fold,%find,%any,%all,%zip,%enumerate(:from),%flatten(:depth),%group-by, and:byas another name for%sort's:key.Trees
(%walk f tree)rebuilds a tree bottom-up, replacing each node with whatfgives. 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,valueorfalse), 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.rsmodule and wiring a large set of built-ins throughexpander.rs.Quoting and construction:
(%quasi ...),(%unquote ...),(%splice ...), and(%read "...")let macros build ASTs;%unquote/%spliceoutside%quasiare errors. Introspection:%type, type predicates (%list?, etc.),%attribute, and%dictcomplement existing%quote/%eval.Higher-order lists:
%mapand%filterare 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%sortgains:byas an alias for:key.Tree transforms:
%walkand%selectsupport structural rewrites and extraction (e.g. TOC from headings). Macro authoring:%gensym,%defined, and one-step%macroexpandfor 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.