[erlang-questions] Writing a DSL in ERlang (ILYA Khlopotov)

ILYA Khlopotov ilya.khlopotov@REDACTED
Fri Feb 8 20:05:37 CET 2013

You might want to consider elixir for this simple task. Since it compiles
into beam you can use generated modules from erlang.
If you really want erlang code you could extract generated erlang from
compiled beam (you need debug_info for that though).
I know that it's not elixir mailing list so I'm apologize for providing a
solution in this language. However it might save a lot of effort compared
to writing DSL in erlang.

Your dsl code might look like (for complete code see
 defmacro action(match, [do: body]) do
     quote do
        def handle_info(unquote(match), state) do
          {:ok, state}
 defmacro createhandler(name, [do: body]) do
     quote do
        defmodule unquote(name) do

Having that you can write your definition files using your own macro
(action and createhandler) as:
createhandler TestHandler do
   action {:x, x}, do: IO.puts "x: #{inspect x}"
   action {:y, y, seed} do
      # example of calling erlang modules
      value = :random.uniform(y)
      IO.puts "random.uniform(${y}) -> #{value} "

If your actions can be a single expression you can go further and define
macro for ::: (or something else) and then you might be able turn it into
(WARNING pseudo code)
createhandler TestHandler do
   {:x, x} ::: IO.puts("x: #{inspect x}")
   {:y, y} ::: IO.puts("random.uniform(${y}) -> #{value} ")

Best regards,

> I'm am interested in creating an external DSL with Erlang and what are
> the steps involved. So very basically I want to convert some English
> like language into running erlang code. My goal is to build up a full
> erlang source file relative to the dsl file I have as input. I don't
> necessarily need to print out the source file to the file system,
> holding it in memory is sufficient and allowing its execution.

> Sample DSL syntax; {createhandler, "TestHandler", {handles, [ {x,
> {action, printToScreen}}, {y, {action, writeToStorage}} ]} }

> So from above I want to create a gen_event handler with a handle_event
> function which pattern matches on 'x' and 'y' and performs the actions
> defined. I have used this to just show what I would like to do, what I'm
> interested in is the steps and approaches to doing this.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20130208/ac8e9363/attachment.htm>

More information about the erlang-questions mailing list