[erlang-questions] dynamic module compilation

Jacob Perkins <>
Sun Jun 8 21:12:40 CEST 2008


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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20080608/d57e9ced/attachment.html>


More information about the erlang-questions mailing list