/ Lang / src / _off.jl
_off.jl
 1  macro evalmod(files...)
 2      quote
 3          with_logger(NullLogger()) do
 4              for f in $files
 5                  eval(:(include(joinpath(@__DIR__, $f))))
 6              end
 7          end
 8      end
 9  end
10  
11  @doc "Export all instances of an enum type."
12  macro exportenum(enums...)
13      expr = quote end
14      for enum in enums
15          push!(
16              expr.args,
17              :(Core.eval(
18                  $__module__, Expr(:export, map(Symbol, instances($(esc(enum))))...)
19              )),
20          )
21      end
22      expr
23  end
24  function firsthead(expr::Expr, sym::Symbol)
25      if sym == expr.head
26          return expr
27      end
28      for arg in expr.args
29          if isa(arg, Expr)
30              e = firsthead(arg, sym)
31              isnothing(e) || return e
32          end
33      end
34      return nothing
35  end
36  
37  @doc "Import all instances of an enum type."
38  macro importenum(stmt)
39      ex = quote
40          $stmt
41      end
42      @assert stmt.head ∈ (:import, :using)
43      enums = firsthead(stmt, :(:))
44      for e in enums.args
45          push!(
46              ex.args,
47              quote
48                  for i in instances($e)
49                      using i: i
50                  end
51              end,
52          )
53      end
54      # for val in instances(Core.eval(__module__, T))
55      #     str = Meta.parse("import .$mod.$val")
56      #     ex = quote
57      #         Core.eval($__module__, $str)
58      #         $ex
59      #     end
60      # end
61      return stmt
62  end
63  
64  # FIXME: untested
65  @doc "Unroll expression `exp` for every element in `fields` assign each element to symbol defined by `asn`.
66  
67  `exp` must use the name defined by `asn`(default to `el`) as the variable name of the loop."
68  macro unroll(exp, fields, asn=:el)
69      ex = esc(exp)
70      Expr(:block, (:($(esc(asn)) = $el; $ex) for el in fields.args)...)
71  end