ExDiceRoller v0.4.0-alpha ExDiceRoller.Compiler

Provides functionality for compiling expressions into ready-to-execute functions.

> parsed =
  {{:operator, '+'},
  {{:operator, '-'}, {:roll, {:digit, '1'}, {:digit, '4'}},
    {{:operator, '/'}, {:roll, {:digit, '3'}, {:digit, '6'}}, {:digit, '2'}}},
  {:roll, {:roll, {:digit, '1'}, {:digit, '4'}},
    {:roll, {:digit, '1'}, {:digit, '6'}}}}

> fun = ExDiceRoller.Compiler.compile(parsed)
#Function<0.47893785/2 in ExDiceRoller.Compiler.compile_add/4>

> fun.([], [])
4

> ExDiceRoller.Compiler.fun_info(fun)
{#Function<0.47893785/2 in ExDiceRoller.Compiler.compile_add/4>,
  :"-compile_add/4-fun-0-",
  [
    {#Function<13.47893785/2 in ExDiceRoller.Compiler.compile_sub/4>,
      :"-compile_sub/4-fun-0-",
      [
        {#Function<12.47893785/2 in ExDiceRoller.Compiler.compile_roll/4>,
        :"-compile_roll/4-fun-3-", [1, 4]},
        {#Function<4.47893785/2 in ExDiceRoller.Compiler.compile_div/4>,
        :"-compile_div/4-fun-1-",
        [
          {#Function<12.47893785/2 in ExDiceRoller.Compiler.compile_roll/4>,
            :"-compile_roll/4-fun-3-", [3, 6]},
          2
        ]}
      ]},
    {#Function<9.47893785/2 in ExDiceRoller.Compiler.compile_roll/4>,
      :"-compile_roll/4-fun-0-",
      [
        {#Function<12.47893785/2 in ExDiceRoller.Compiler.compile_roll/4>,
        :"-compile_roll/4-fun-3-", [1, 4]},
        {#Function<12.47893785/2 in ExDiceRoller.Compiler.compile_roll/4>,
        :"-compile_roll/4-fun-3-", [1, 6]}
      ]}
  ]}

Link to this section Summary

Functions

Compiles a provided t:Parser.expression/0 into an anonymous function

Shows the nested functions and relationships of a compiled function. The structure of the fun_info result is {<function>, <atom with name, arity, and ordered function #>, [<recursive info about child functions>]}

Link to this section Types

Link to this type args()
args() :: Keyword.t()
Link to this type compiled_fun()
compiled_fun() :: (args(), opts() -> integer())
Link to this type compiled_val()
compiled_val() :: compiled_fun() | number()
Link to this type fun_info_tuple()
fun_info_tuple() :: {function(), atom(), [any()]}
Link to this type opts()
opts() :: [atom() | {atom(), any()}]

Link to this section Functions

Compiles a provided t:Parser.expression/0 into an anonymous function.

iex> expr = "1d2+x"
"1d2+x"
iex> {:ok, tokens} = ExDiceRoller.Tokenizer.tokenize(expr)
{:ok,
[
  {:digit, 1, '1'},
  {:roll, 1, 'd'},
  {:digit, 1, '2'},
  {:basic_operator, 1, '+'},
  {:var, 1, 'x'}
]}
iex> {:ok, parsed} = ExDiceRoller.Parser.parse(tokens)
{:ok, {{:operator, '+'}, {:roll, {:digit, '1'}, {:digit, '2'}}, {:var, 'x'}}}
iex> fun = ExDiceRoller.Compiler.compile(parsed)
iex> fun.([x: 1], [:explode])
2

During calculation, float values are left as float for as long as possible. If a compiled roll is invoked with a float as the number of dice or sides, that value will be rounded to an integer. Finally, the return value is an integer. Rounding rules can be found at Kernel.round/1.

Link to this function fun_info(fun)
fun_info(compiled_fun()) :: fun_info_tuple()

Shows the nested functions and relationships of a compiled function. The structure of the fun_info result is {<function>, <atom with name, arity, and ordered function #>, [<recursive info about child functions>]}.

> {:ok, fun} = ExDiceRoller.compile("1d8+(1-x)d(2*y)")
#=> {:ok, #Function<0.84780260/1 in ExDiceRoller.Compiler.compile_add/4>}

> ExDiceRoller.Compiler.fun_info fun
#=> {#Function<0.16543174/1 in ExDiceRoller.Compiler.compile_add/4>,
:"-compile_add/4-fun-0-",
[
  {#Function<12.16543174/1 in ExDiceRoller.Compiler.compile_roll/4>,
    :"-compile_roll/4-fun-3-", [1, 8]},
  {#Function<9.16543174/1 in ExDiceRoller.Compiler.compile_roll/4>,
    :"-compile_roll/4-fun-0-",
    [
      {#Function<15.16543174/1 in ExDiceRoller.Compiler.compile_sub/4>,
      :"-compile_sub/4-fun-2-",
      [
        1,
        {#Function<16.16543174/1 in ExDiceRoller.Compiler.compile_var/1>,
          :"-compile_var/1-fun-0-", ['x']}
      ]},
      {#Function<8.16543174/1 in ExDiceRoller.Compiler.compile_mul/4>,
      :"-compile_mul/4-fun-2-",
      [
        2,
        {#Function<16.16543174/1 in ExDiceRoller.Compiler.compile_var/1>,
          :"-compile_var/1-fun-0-", ['y']}
      ]}
    ]}
]}