cast v0.1.0 Cast

This module provides macros to generate elixir code from C AST.

Usage

First you need to generate an XML representation of C AST thanks to castxml (version >= 0.2), in GCC mode (other modes may be supported in the future).

castxml has many options. Its usage is out of the scope of this document. However, generating AST from a simple C project may be as simple as:

castxml -c -x c -std=gnu11 --castxml-cc-gnu gcc --castxml-gccxml -o ast.xml hello.c

The module where you want to generate elixir code from C AST must begin with:

use Cast, ast: "/path/to/ast.xml"

Then, you can use the following macros to generate constants accessors or enumeration modules:

WARNING: XML parsing is done through DOM parser, memory usage can be really high

Example:

Given following C code:

  const int version = 1;

  enum pets {
    elephant = 0,
    mouse
  }

Following elixir module:

  defmodule Hello do
    use Cast, ast: "/path/to/ast.xml

    const :version, cast: &integer/1
    enum Pets, name: "pets"
  end

Will result in:

  %{elephant: 0, mouse: 1} = Hello.Pets.mapping()

  0 == Hello.Pets.value :elephant
  1 == Hello.Pets.value :mouse

  :elephant == Hello.Pets.key 0
  :mouse == Hello.Pets.key 1

  1 == Hello.version()

Link to this section Summary

Link to this section Types

Link to this type

ast()

ast() :: term()
Link to this type

enum_filter()

enum_filter() :: {:contains, String.t()}
Link to this type

enum_filters()

enum_filters() :: [enum_filter()]