Operate v0.1.0-beta.15 Operate.Tape View Source

Module for working with Operate tapes.

An Operate program is a tape made up of one or more cells, where each cell contains a single atomic procedure call (known as an "Op").

When a tape is run, each cell is executed in turn, with the result from each cell is passed to the next cell. This is known as the "state". Each cell returns a new state, until the final cell in the tape returns the result of the tape.

Examples

iex> {:ok, tape} = %Operate.Tape{cells: [
...>   %Operate.Cell{op: "return function(state, a) return (state or 0) + a end", params: [2]},
...>   %Operate.Cell{op: "return function(state, a) return (state or 0) + a end", params: [3]},
...>   %Operate.Cell{op: "return function(state, a) return (state or 0) + a end", params: [4]}
...> ]}
...> |> Operate.Tape.run(Operate.VM.init)
...> tape.result
9

Link to this section Summary

Types

t()

Operate Tape

Functions

Converts the given Operate.BPU.Transaction.t/0 into a Operate.Tape.t/0. Returns the result in an :ok / :error tuple pair.

As from_bpu/1, but returns the result or raises an exception.

Returns a list of Op references from the tape's cells. If a map of aliases is specifed, this is used to alias references to alternative values.

Runs the tape in the given VM state.

As run/3, but returns the tape or raises an exception.

Sets the given Ops into the cells of the given tape. If a map of aliases is specifed, this is used to reverse map any procedure scripts onto aliased cells.

Validates the given tape. Returns true if all the tape's cells are valid.

Link to this section Types

Specs

t() :: %Operate.Tape{
  cells: [Operate.Cell.t(), ...],
  error: binary(),
  index: integer(),
  result: Operate.VM.lua_output(),
  tx: Operate.BPU.Transaction
}

Operate Tape

Link to this section Functions

Link to this function

from_bpu(tx, index \\ nil)

View Source

Specs

from_bpu(Operate.BPU.Transaction.t(), integer() | nil) ::
  {:ok, t()} | {:error, String.t()}

Converts the given Operate.BPU.Transaction.t/0 into a Operate.Tape.t/0. Returns the result in an :ok / :error tuple pair.

Optionally specifcy the output index of the tape. If not specified, the first OP_RETURN output is returned as the tape.

Link to this function

from_bpu!(tx, index \\ nil)

View Source

Specs

from_bpu!(Operate.BPU.Transaction.t(), integer()) :: t()

As from_bpu/1, but returns the result or raises an exception.

Link to this function

get_op_refs(tape, aliases \\ %{})

View Source

Specs

get_op_refs(t(), map()) :: list()

Returns a list of Op references from the tape's cells. If a map of aliases is specifed, this is used to alias references to alternative values.

Examples

iex> %Operate.Tape{cells: [
...>   %Operate.Cell{ref: "aabbccdd"},
...>   %Operate.Cell{ref: "eeff1122"},
...>   %Operate.Cell{ref: "33445500"}
...> ]}
...> |> Operate.Tape.get_op_refs(%{"33445500" => "MyAliasReference"})
["aabbccdd", "eeff1122", "MyAliasReference"]
Link to this function

run(tape, vm, options \\ [])

View Source

Specs

run(t(), Operate.VM.t(), keyword()) :: {:ok, t()} | {:error, t()}

Runs the tape in the given VM state.

Options

The accepted options are:

  • :state - Specifiy the state passed to the first cell procedure. Defaults to nil.
  • :strict - By default the tape runs in struct mode - meaning if any cell has an error the entire tape fails. Disable strict mode by setting to false.

Examples

iex> {:ok, tape} = %Operate.Tape{cells: [
...>   %Operate.Cell{op: "return function(state, a) return (state or '') .. a end", params: ["b"]},
...>   %Operate.Cell{op: "return function(state, a) return (state or '') .. a end", params: ["c"]},
...>   %Operate.Cell{op: "return function(state) return string.reverse(state) end", params: []}
...> ]}
...> |> Operate.Tape.run(Operate.VM.init, state: "a")
...> tape.result
"cba"
Link to this function

run!(tape, vm, options \\ [])

View Source

Specs

run!(t(), Operate.VM.t(), keyword()) :: t()

As run/3, but returns the tape or raises an exception.

Options

The accepted options are:

  • :state - Specifiy the state passed to the first cell procedure. Defaults to nil.
  • :strict - By default the tape runs in struct mode - meaning if any cell has an error the entire tape fails. Disable strict mode by setting to false.
Link to this function

set_cell_ops(tape, ops, aliases \\ %{})

View Source

Specs

set_cell_ops(t(), [Operate.Op.t(), ...], map()) :: t()

Sets the given Ops into the cells of the given tape. If a map of aliases is specifed, this is used to reverse map any procedure scripts onto aliased cells.

Specs

valid?(t()) :: boolean()

Validates the given tape. Returns true if all the tape's cells are valid.