This repository has been archived on 2023-08-07. You can view files and clone it, but cannot push or open issues or pull requests.
temple/lib/temple/ast/do_expressions.ex

35 lines
761 B
Elixir
Raw Normal View History

defmodule Temple.Ast.DoExpressions do
2021-01-02 18:21:48 +00:00
@moduledoc false
@behaviour Temple.Parser
use TypedStruct
2021-04-09 03:04:26 +00:00
typedstruct do
field :elixir_ast, Macro.t()
field :children, [map()]
end
@impl true
def applicable?({_, _, args}) when is_list(args) do
Enum.any?(args, fn arg -> match?([{:do, _} | _], arg) end)
end
def applicable?(_), do: false
@impl true
2021-03-25 03:01:13 +00:00
def run({name, meta, args}) do
{do_and_else, args} = Temple.Ast.Utils.split_args(args)
2021-03-25 03:01:13 +00:00
do_body = Temple.Parser.parse(do_and_else[:do])
2021-04-09 03:04:26 +00:00
else_body =
if do_and_else[:else] == nil do
nil
else
Temple.Parser.parse(do_and_else[:else])
end
2021-03-25 03:01:13 +00:00
Temple.Ast.new(__MODULE__, elixir_ast: {name, meta, args}, children: [do_body, else_body])
2021-03-25 03:01:13 +00:00
end
end