Skip to content

Commit

Permalink
Inline node-specific literals.
Browse files Browse the repository at this point in the history
  • Loading branch information
christhekeele committed Mar 3, 2023
1 parent 8553c57 commit b05713a
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 19 deletions.
20 changes: 14 additions & 6 deletions lib/dice/die.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
defmodule Dice.Die do
defstruct [:faces]

@symbol "d"
@compile {:inline, symbol: 0}
def symbol, do: @symbol

@range_symbol ".."
@compile {:inline, range_symbol: 0}
def range_symbol, do: @range_symbol

import Dice.Parser.Builder

defparser do
Expand All @@ -9,7 +17,7 @@ defmodule Dice.Die do
dice_range_descriptor =
ignore(left_bracket_literal())
|> concat(unwrap_and_tag(integer_literal(), :first))
|> concat(ignore(range_literal()))
|> concat(ignore(string(@range_symbol)))
|> concat(unwrap_and_tag(integer_literal(), :last))
|> concat(right_bracket_literal())
|> post_traverse({__MODULE__, :dice_range_constructor, []})
Expand All @@ -27,7 +35,7 @@ defmodule Dice.Die do
dice_set_descriptor
]

concat(ignore(dice_literal()), unwrap_and_tag(choice(numeric_dice_descriptor), :faces))
concat(ignore(string(@symbol)), unwrap_and_tag(choice(numeric_dice_descriptor), :faces))
|> post_traverse({__MODULE__, :from_parse, []})
end

Expand Down Expand Up @@ -64,10 +72,10 @@ defmodule Dice.Die do

defimpl String.Chars do
def to_string(%Dice.Die{} = die) do
case die.faces do
number when is_integer(number) -> "d#{number}"
%Range{} = range -> "d[#{range.first}..#{range.last}]"
faces when is_list(faces) -> "d{#{Enum.join(faces, ", ")}}"
Dice.Die.symbol() <> case die.faces do
number when is_integer(number) -> Integer.to_string(number)
%Range{} = range -> Enum.join([Integer.to_string(range.first), Dice.Die.range_symbol(), Integer.to_string(range.last)])
faces when is_list(faces) -> Enum.join(faces |> Enum.map(&Integer.to_string/1), ", ")
end
end
end
Expand Down
8 changes: 6 additions & 2 deletions lib/dice/operators/addition.ex
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
defmodule Dice.Operator.Addition do
defstruct [:left, :right]

@symbol "+"
@compile {:inline, symbol: 0}
def symbol, do: @symbol

import Dice.Parser.Builder

defparser do
unwrap_and_tag(parsec({Dice.Expression.Term.Parser, :combinator}), :left)
|> concat(optional(whitespace_literal()))
|> concat(ignore(positive_literal()))
|> concat(ignore(string(@symbol)))
|> concat(optional(whitespace_literal()))
|> unwrap_and_tag(parsec({Dice.Expression.Term.Parser, :combinator}), :right)
|> post_traverse({__MODULE__, :from_parse, []})
Expand Down Expand Up @@ -34,7 +38,7 @@ defmodule Dice.Operator.Addition do
def to_string(%Dice.Operator.Addition{} = addition) do
Enum.join([
Kernel.to_string(addition.left),
"+",
Dice.Operator.Addition.symbol(),
Kernel.to_string(addition.right)
], " ")
end
Expand Down
8 changes: 6 additions & 2 deletions lib/dice/operators/subtraction.ex
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
defmodule Dice.Operator.Subtraction do
defstruct [:left, :right]

@symbol "-"
@compile {:inline, symbol: 0}
def symbol, do: @symbol

import Dice.Parser.Builder

defparser do
unwrap_and_tag(parsec({Dice.Expression.Term.Parser, :combinator}), :left)
|> concat(optional(whitespace_literal()))
|> concat(ignore(negative_literal()))
|> concat(ignore(string(@symbol)))
|> concat(optional(whitespace_literal()))
|> unwrap_and_tag(parsec({Dice.Expression.Term.Parser, :combinator}), :right)
|> post_traverse({__MODULE__, :from_parse, []})
Expand Down Expand Up @@ -34,7 +38,7 @@ defmodule Dice.Operator.Subtraction do
def to_string(%Dice.Operator.Subtraction{} = subtraction) do
Enum.join([
Kernel.to_string(subtraction.left),
"-",
Dice.Operator.Subtraction.symbol(),
Kernel.to_string(subtraction.right)
], " ")
end
Expand Down
9 changes: 1 addition & 8 deletions lib/dice/parser/helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,10 @@ defmodule Dice.Parser.Helpers do

def integer_literal,
do:
optional(unwrap_and_tag(negative_literal(), :negative))
optional(unwrap_and_tag(string("-"), :negative))
|> concat(unwrap_and_tag(non_negative_integer_literal(), :number))
|> post_traverse({__MODULE__, :integer_literal_constructor, []})

def negative_literal, do: string("-")
def positive_literal, do: string("+")

def dice_literal, do: string("d")

def range_literal, do: string("..")

def comma_literal,
do:
optional(whitespace_literal())
Expand Down
7 changes: 6 additions & 1 deletion lib/dice/parser/module.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ defmodule Dice.Parser.Module do
env.module
|> Module.get_attribute(:combinator)
|> Macro.postwalk(fn
{:__MODULE__, _meta, _context} -> {:__aliases__, [{:alias, false}], base_module_alias}
{:__MODULE__, _, _} -> {:__aliases__, [{:alias, false}], base_module_alias}
other -> other
end)
|> Macro.postwalk(fn
{:@, _, [{attribute, _, _}]} -> {{:., [], [{:__aliases__, [alias: false], [:Module]}, :get_attribute]}, [],
[{:__aliases__, [alias: false], base_module_alias}, attribute]}
other -> other
end)

Expand Down

0 comments on commit b05713a

Please sign in to comment.