Brainfux

hex.pm version travis-ci status Coverage Status

Brainfux enables you to define brainfuck function in elixir.

At the compile time,

  • unmatched brackets are detected and an error is raised
  • characters other than +-><,.[] are stripped
  • code after the last . or [] including . are trimmed
  • + or - preceding , are removed
  • +-, -+, <>, >< are removed recursively (for example, ++>>-+<<- is turned into +)
  • leading +, -, >, <, . are computed beforehand

Installation

Add :brainfux to your list of dependencies in mix.exs:

def deps do
  [{:brainfux, "~> 0.2.5"}]
end

Usage

defmodule Sample do
  use Brainfux

  # Define bf function
  defbf hello_world """
    +++++++++
    [
      >++++++++
      >+++++++++++
      >+++++
      <<<-
    ]
    >.
    >++.+++++++..+++.
    >-.------------.
    <++++++++.--------.+++.------.--------.
    >+.
  """

  # bf function that reads input
  defbf echo """
    ,[.,]
  """

  @spec shift_string(String.t) :: String.t
  def shift_string(str) do
    # The bfn macro makes an anonymous bf function
    (bfn ",[+.,]").(str)
  end
end

Sample.hello_world
# => "Hello, world!"

# You can pass string as an input
Sample.echo("foo")
# => "foo"

Sample.shift_string("HAL")
# => "IBM"

Infinite loops

Infinite loops are not detected at compile time, so the termination of functions are not ensured.

use Brainfux

(bfn ",[+]").("a")
# => never ends

(bfn "+[>-]").("")
# => never ends