smacro

smacro expands Ruby macros before handing the resulting source to the Spinel AOT compiler. Macro definitions run under CRuby at build time; the generated program does not need the gem at runtime.

Install locally

gem build smacro.gemspec
gem install ./smacro-0.1.0.gem

The gem requires Ruby 3.0 or later and Prism. Spinel is needed to compile the expanded result.

Use

Put macro definitions in app.smacro.rb next to app.rb:

Smacro.define(:fields) do |*names|
  "attr_accessor " + names.map { |name| ":#{name}" }.join(", ")
end

Call the macro in app.rb:

class User
  fields :name, :email
end

Expand and compile:

smacro app.rb -o app.expanded.rb
spinel app.expanded.rb

Pass a macro file as the second argument when its name differs from app.smacro.rb. The existing ruby smacro.rb app.rb command still works. For Ruby code, use require "smacro" to access Smacro.define and Smacro.expand.

The current prototype accepts literal macro arguments (symbols, strings, integers, booleans, nil, and arrays). It does not yet support macro definitions inside the source file, nested macro calls, or Crystal's AST template syntax. sh run.sh compares the sample's CRuby and Spinel results.