CSquare

github.com/mohawkjohn/csquare

Description

C, then D, C++, C# – now C^2, simple C templates using Ruby.

Consider this to be a sort of carpenter’s square. We call it C^2, or csquare. It’s a simple tool for simple jobs.

This gem was developed for use in NMatrix (part of the SciRuby Project). We wanted to be able to write a single function and have it be modified to produce C sources for each datatype (rational, complex, integer, float, Ruby object, etc).

It also produces some rudimentary function pointer arrays if you so desire, so that these functions can be accessed using array notation.

Experimental! Use at your own risk. Actually, don’t use this at all! It’s extremely buggy and probably won’t be useful for your purposes. It’s really custom-designed to handle a specific use case: NMatrix dtype templates.

Example

A full example is available in NMatrix starting at line 696: github.com/mohawkjohn/nmatrix/blob/csquare/ext/nmatrix/generator.rb

Full examples of templates are available here: github.com/mohawkjohn/nmatrix/tree/csquare/ext/nmatrix/templates

(Note that those templates and example are licensed as part of SciRuby.)

Here’s a simple CSquare source for complex numbers:

# looks in templates/ directory, outputs "test.c" and "test.h"
g = CSquare::Generator.new("templates", "test") do |c|
  # Do this to register abbreviations for basic types
  c.template_type(:float, 'TYPE') do |t|
    t.type :f32, 'float'
    t.type :f64, 'double'
  end

  c.template_type(:complex, 'TYPE', :r => 'FLOAT', :i => 'FLOAT') do |t|

    t.type :c64, 'complex64', :long => :c128, 'FLOAT' => :f32
    t.type :c128, 'complex128', 'FLOAT' => :f64

    t.sources %w{div2 div4 mul2 mul4}

    t.op :'==', 'TYPE' => '$0.r == $1.r && $0.i == $1.i', [:integer, :float] => '$0.r == $1 && $0.i == 0'

    assign_str =
    t.op :'=', [:integer, :boolean, :float] => '$0 = (struct $t) { $1, 0 }'

    t.op :'*', 'TYPE' => 'mul2($0, $1)', :integer => 'mul4($0.r, $0.i, $1, 0)', :float => 'mul4($0.r, $0.i, $1, 0)'
    t.op :'/', 'TYPE' => 'div2($0, $1)', :integer => 'div4($0.r, $0.i, $1, 0)', :float => 'div4($0.r, $0.i, $1, 0)'
    t.op :'+', 'TYPE' => 'add2($0, $1)'
    t.op :'-', 'TYPE' => 'sub2($0, $1)'
  end
end

Installation

gem install cast csquare

License

BSD 2-clause. Copyright John Woods (mohawkjohn on github), 2012.