|
1 | 1 | defmodule ElixirScript.Translate.Forms.Receive do |
2 | 2 | @moduledoc false |
3 | 3 | alias ESTree.Tools.Builder, as: J |
4 | | - alias ElixirScript.Translate.Helpers |
| 4 | + alias ElixirScript.Translate.{Helpers, Form, Function, Clause} |
5 | 5 |
|
6 | 6 | @doc """ |
7 | 7 | receive is not supported just yet, but we compile it |
8 | 8 | to a stub function for now |
9 | 9 | """ |
10 | 10 | def compile(blocks, state) do |
11 | | - _receive_block = Keyword.get(blocks, :do) |
12 | | - _after_block = Keyword.get(blocks, :after, nil) |
| 11 | + receive_block = Keyword.get(blocks, :do) |
| 12 | + after_block = Keyword.get(blocks, :after, nil) |
| 13 | + |
| 14 | + receive_block = Enum.map(receive_block, fn x -> Clause.compile(x, state) |> elem(0) end) |
| 15 | + |> List.flatten |
| 16 | + |> J.array_expression() |
13 | 17 |
|
14 | 18 | receive_function = J.member_expression( |
15 | 19 | Helpers.special_forms(), |
16 | 20 | J.identifier("receive") |
17 | 21 | ) |
18 | 22 |
|
| 23 | + args = [receive_block] ++ process_after(after_block, state) |
| 24 | + |
19 | 25 | ast = Helpers.call( |
20 | 26 | receive_function, |
21 | | - [] |
| 27 | + args |
22 | 28 | ) |
23 | 29 |
|
24 | 30 | { ast, state } |
25 | 31 | end |
| 32 | + |
| 33 | + defp process_after(nil, _) do |
| 34 | + [] |
| 35 | + end |
| 36 | + |
| 37 | + defp process_after([{:->, _, [[timeout], body]}], state) do |
| 38 | + timeout = Form.compile!(timeout, state) |
| 39 | + {body, _state} = Function.compile_block(body, state) |
| 40 | + |
| 41 | + function = Helpers.arrow_function( |
| 42 | + [], |
| 43 | + J.block_statement(List.wrap(body)) |
| 44 | + ) |
| 45 | + |
| 46 | + [timeout, function] |
| 47 | + end |
26 | 48 | end |
0 commit comments