Project

smacro

0.0
The project is in a healthy, maintained state
Expand Ruby source macros before compiling the result with Spinel.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Runtime

>= 1.0, < 2.0
 Project Readme

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.