[erlang-questions] dynamic module compilation

Jacob Perkins <>
Sun Jun 8 22:22:06 CEST 2008


Thanks for the help everyone, I got things working using erl_parse:abstract.
Final code turned out pretty simple:
Strings = ["foo", "bar"],
Clause = {clause, LINE, [], [], [erl_parse:abstract(Strings)]},
{function, LINE, strings, 0, [Clause]}.

On Sun, Jun 8, 2008 at 12:38 PM, Ulf Wiger <> wrote:

> There are a few tricks you can use to quickly find out what
> abstract form a certain expression corresponds to:
>
> 1> erl_parse:abstract(["foo","bar"]).
> {cons,0,{string,0,"foo"},{cons,0,{string,0,"bar"},{nil,0}}}
>
> Another trick is to write a dummy module, then compile
> it with the option 'debug_info'. You can then read the
> debug info and view the abstract code that resulted from
> the source in your module. The command for reading
> debug info is
>
> beam_lib:chunks(BeamFile, [abstract_code]).
>
> BR,
> Ulf W
>
> 2008/6/8 Jacob Perkins <>:
> > So I decided to try generating the AST, and got most of the way there. I
> > don't want to use ets because I don't want to have a process dependency.
> > I was able to use compile:forms to generate a module and function, but my
> > function clause isn't correct. The function and clause forms I can get to
> > compile are:
> > {function, LINE, strings, 0,
> >     [{clause, LINE, [], [],
> >         [{string, LINE, "foo"}, {string, LINE, "bar"}]}]}
> > But the above form appears to generate
> > strings() -> "foo", "bar".
> > when what I really want is
> > strings() -> ["foo", "bar"].
> > It's probably a simple fix in the forms, but the obvious change of
> enclosing
> > the string literals in another list causes a compile error:
> > {function, LINE, strings, 0,
> >     [{clause, LINE, [], [],
> >         [[{string, LINE, "foo"}, {string, LINE, "bar"}]]}]}
> > Any help?
> > Thanks,
> > Jacob
> > On Sun, Jun 8, 2008 at 9:45 AM, Howard Yeh <> wrote:
> >>
> >> see ERTS manual chapter 4 to see the abstract format.
> >>
> >> http://www.erlang.org/doc/apps/erts/part_frame.html
> >>
> >> there's also Core Erlang. But I am not sure where it's documented.
> >>
> >>
> >> On 6/8/08, Robert Virding <> wrote:
> >> > There are basically two ways of doing this:
> >> >
> >> > - From you list of files generate a file, grammar.erl, containing the
> >> > module
> >> > grammar with functions adjectives/0 and nouns/0. You then compile with
> >> > compile:file and load with code:load.
> >> >
> >> > - You can generate the code as AST in memory and then compile with
> >> > compile:forms. This is a bit tricky the first time, but easy once you
> >> > get
> >> > the hang of it.
> >> >
> >> > - As someone else mentioned save them in a database on disk.
> >> >
> >> > Robert
> >> >
> >> > 2008/6/8 Jacob Perkins <>:
> >> > > Hi all,
> >> > >
> >> > >
> >> > >
> >> > > I need some help understanding how to dynamically compile/generate
> an
> >> > erlang module. I've seen other projects do this kind of thing, such as
> >> > erlydtl, but my needs are much simpler.
> >> > >
> >> > >
> >> > > I have a few files that are basically lists of words. What I want to
> >> > > do is
> >> > generate a module whose functions will return each list of words. So
> if
> >> > I
> >> > have two files named "adjectives" and "nouns", then the generated
> module
> >> > (let's call it 'grammar') should have two functions, adjectives() and
> >> > nouns(), that return their respective list of words.
> >> > >
> >> > >
> >> > > How can I do this?
> >> > >
> >> > >
> >> > > Thanks,
> >> > > Jacob
> >> > > _______________________________________________
> >> > > erlang-questions mailing list
> >> > > 
> >> > > http://www.erlang.org/mailman/listinfo/erlang-questions
> >> > >
> >> >
> >> >
> >> > _______________________________________________
> >> >  erlang-questions mailing list
> >> >  
> >> >  http://www.erlang.org/mailman/listinfo/erlang-questions
> >> >
> >>
> >>
> >> --
> >> hayeah.wordpress.com
> >>   --metacircular thinking
> >
> >
> > _______________________________________________
> > erlang-questions mailing list
> > 
> > http://www.erlang.org/mailman/listinfo/erlang-questions
> >
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20080608/7db458aa/attachment.html>


More information about the erlang-questions mailing list