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

Robert Virding robert.virding@REDACTED
Sun Feb 10 19:46:07 CET 2013


Or use one of the lisp front-ends for erlang, LFE or Joxa, to get ultimate macro facilities. 

Robert 

----- Original Message -----

> From: "ILYA Khlopotov" <ilya.khlopotov@REDACTED>
> To: erlang-questions@REDACTED
> Sent: Friday, 8 February, 2013 8:05:37 PM
> Subject: Re: [erlang-questions] Writing a DSL in ERlang (ILYA
> Khlopotov)

> 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
> https://gist.github.com/khia/4741045 ):

> defmacro action(match, [do: body]) do
> quote do
> def handle_info(unquote(match), state) do
> unquote(body)
> {:ok, state}
> end
> end
> end
> defmacro createhandler(name, [do: body]) do
> quote do
> defmodule unquote(name) do
> unquote(body)
> end
> end
> end

> 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
> :random.seed(seed)
> value = :random.uniform(y)
> IO.puts "random.uniform(${y}) -> #{value} "
> end
> end

> 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} ")
> end

> Best regards,
> ILYA

> > 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.
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20130210/d342da6e/attachment.htm>


More information about the erlang-questions mailing list