-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
154d056
commit 6bd499e
Showing
14 changed files
with
240 additions
and
194 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,85 @@ | ||
defprotocol Dice.Expression.Evaluate do | ||
def evaluate(node) | ||
end | ||
|
||
defmodule Dice.Expression.Term do | ||
import Dice.Parser.Builder | ||
|
||
defparser do | ||
choice([ | ||
parsec({Dice.Pool.Parser, :combinator}), | ||
parsec({Dice.Constant.Parser, :combinator}), | ||
]) | ||
end | ||
|
||
end | ||
|
||
defmodule Dice.Expression.Operation do | ||
import Dice.Parser.Builder | ||
|
||
defparser do | ||
choice([ | ||
parsec({Dice.Operator.Addition.Parser, :combinator}), | ||
parsec({Dice.Operator.Subtraction.Parser, :combinator}), | ||
]) | ||
end | ||
|
||
end | ||
|
||
defmodule Dice.Expression do | ||
defstruct terms: [] | ||
defstruct [:expression] | ||
|
||
import Dice.Parser.Builder | ||
|
||
defparser do | ||
|
||
expression = [ | ||
parsec({Dice.Expression.Operation.Parser, :combinator}), | ||
parsec({Dice.Expression.Term.Parser, :combinator}), | ||
] | ||
|
||
optional(whitespace_literal()) | ||
|> concat(unwrap_and_tag(Dice.Term.Parser.maybe_signed_term_combinator(), :term)) | ||
|> concat( | ||
repeat( | ||
concat( | ||
optional(whitespace_literal()), | ||
unwrap_and_tag(Dice.Term.Parser.signed_term_combinator(), :term) | ||
) | ||
) | ||
) | ||
|> unwrap_and_tag(choice(expression), :expression) | ||
|> optional(whitespace_literal()) | ||
|> post_traverse({__MODULE__, :from_parse, []}) | ||
end | ||
|
||
# import NimbleParsec | ||
# defparsec :parse, parsec({__MODULE__.Parser, :parse}) | ||
|
||
def from_parse(unparsed, parsed, context, _line, _offset) do | ||
terms = :lists.reverse(Keyword.get_values(parsed, :term)) | ||
expression = Keyword.fetch!(parsed, :expression) | ||
|
||
expression = %__MODULE__{ | ||
terms: terms | ||
expression: expression | ||
} | ||
|
||
{unparsed, [expression], context} | ||
end | ||
|
||
def new(expression) when is_binary(expression) do | ||
expression |> parse | ||
end | ||
|
||
def evaluate(%__MODULE__{} = expression) do | ||
expression.terms | ||
|> Enum.map(&Dice.Term.evaluate(&1)) | ||
|> Enum.sum() | ||
defimpl Dice.Expression.Evaluate do | ||
def evaluate(%Dice.Expression{} = expression) do | ||
Dice.Expression.Evaluate.evaluate(expression.expression) | ||
end | ||
end | ||
|
||
def to_string(%__MODULE__{} = expression) do | ||
expression.terms | ||
|> Enum.map(&Dice.Term.to_string(&1)) | ||
|> Enum.join() | ||
|> String.trim_leading("+ ") | ||
|> String.replace_leading("- ", "-") | ||
defimpl String.Chars do | ||
def to_string(%Dice.Expression{} = expression) do | ||
Kernel.to_string(expression.expression) | ||
end | ||
end | ||
|
||
defimpl Inspect do | ||
import Inspect.Algebra | ||
|
||
def inspect(expression, _opts) do | ||
[first_term | rest] = expression.terms | ||
|
||
concat( | ||
List.flatten([ | ||
"#{inspect(__MODULE__)}.new(\"", | ||
break(""), | ||
first_term | ||
|> Dice.Term.to_string() | ||
|> String.trim_leading("+ ") | ||
|> String.replace_leading("- ", "-"), | ||
break(), | ||
rest | ||
|> Enum.map(&Dice.Term.to_string(&1)) | ||
|> Enum.intersperse(break()), | ||
break(""), | ||
"\")" | ||
]) | ||
) | ||
def inspect(%Dice.Expression{} = expression, _opts) do | ||
concat([ | ||
"#{inspect(__impl__(:for))}.new(\"", | ||
break(""), | ||
Kernel.to_string(expression.expression), | ||
break(""), | ||
"\")" | ||
]) | ||
end | ||
end | ||
end |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
defmodule Dice.Operator.Addition do | ||
defstruct [:left, :right] | ||
|
||
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(optional(whitespace_literal())) | ||
|> unwrap_and_tag(parsec({Dice.Expression.Term.Parser, :combinator}), :right) | ||
|> post_traverse({__MODULE__, :from_parse, []}) | ||
end | ||
|
||
def from_parse(unparsed, parsed, context, _line, _offset) do | ||
left = Keyword.fetch!(parsed, :left) | ||
right = Keyword.fetch!(parsed, :right) | ||
|
||
addition = %__MODULE__{ | ||
left: left, | ||
right: right | ||
} | ||
|
||
{unparsed, [addition], context} | ||
end | ||
|
||
defimpl Dice.Expression.Evaluate do | ||
def evaluate(%Dice.Operator.Addition{} = addition) do | ||
Dice.Expression.Evaluate.evaluate(addition.left) + Dice.Expression.Evaluate.evaluate(addition.right) | ||
end | ||
end | ||
|
||
defimpl String.Chars do | ||
def to_string(%Dice.Operator.Addition{} = addition) do | ||
Enum.join([ | ||
Kernel.to_string(addition.left), | ||
"+", | ||
Kernel.to_string(addition.right) | ||
], " ") | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
defmodule Dice.Operator.Subtraction do | ||
defstruct [:left, :right] | ||
|
||
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(optional(whitespace_literal())) | ||
|> unwrap_and_tag(parsec({Dice.Expression.Term.Parser, :combinator}), :right) | ||
|> post_traverse({__MODULE__, :from_parse, []}) | ||
end | ||
|
||
def from_parse(unparsed, parsed, context, _line, _offset) do | ||
left = Keyword.fetch!(parsed, :left) | ||
right = Keyword.fetch!(parsed, :right) | ||
|
||
subtraction = %__MODULE__{ | ||
left: left, | ||
right: right | ||
} | ||
|
||
{unparsed, [subtraction], context} | ||
end | ||
|
||
defimpl Dice.Expression.Evaluate do | ||
def evaluate(%Dice.Operator.Subtraction{} = subtraction) do | ||
Dice.Expression.Evaluate.evaluate(subtraction.left) - Dice.Expression.Evaluate.evaluate(subtraction.right) | ||
end | ||
end | ||
|
||
defimpl String.Chars do | ||
def to_string(%Dice.Operator.Subtraction{} = subtraction) do | ||
Enum.join([ | ||
Kernel.to_string(subtraction.left), | ||
"-", | ||
Kernel.to_string(subtraction.right) | ||
], " ") | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,45 @@ | ||
defmodule Dice.Parser.Builder do | ||
defmacro defparser(_args \\ [], do: combinator) do | ||
alias URI.Error | ||
defmacro defparser(args \\ [], do: combinator) do | ||
Module.put_attribute(__CALLER__.module, :combinator, combinator) | ||
|
||
quote location: :keep do | ||
@behaviour Dice.Parser | ||
variable = args | ||
|> Keyword.get(:variable, __CALLER__.module |> Module.split |> List.last |> String.downcase |> String.to_atom) | ||
|> Macro.var(__CALLER__.module) | ||
|
||
quote location: :keep, generated: true do | ||
@before_compile {Dice.Parser.Module, :build_parser_module} | ||
|
||
def new(expression) when is_binary(expression) do | ||
expression |> parse |> validate! | ||
end | ||
|
||
def parse(input) when is_binary(input) do | ||
{:ok, [result: result], _unparsed, _context, _line, _offset} = | ||
unquote(Module.concat(__CALLER__.module, Parser)).parse(input) | ||
|
||
result | ||
end | ||
|
||
def validate!(unquote(variable)) do | ||
case validate(unquote(variable)) do | ||
{:ok, unquote(variable)} -> unquote(variable) | ||
{:error, reason} -> raise reason | ||
end | ||
end | ||
|
||
def valid?(unquote(variable)) do | ||
case validate(unquote(variable)) do | ||
{:ok, _} -> true | ||
{:error, _reason} -> false | ||
end | ||
end | ||
|
||
def validate(unquote(variable)) do | ||
{:ok, unquote(variable)} | ||
end | ||
defoverridable(validate: 1) | ||
|
||
end | ||
end | ||
end |
Oops, something went wrong.