[erlang-questions] dynamic module compilation

Hynek Vychodil <>
Sun Jun 8 21:40:54 CEST 2008


List constructor is AST node too.

1> erl_parse:parse(element(2, erl_scan:string("strings() -> [\"foo\",
\"bar\"]."))).
{ok,{function,1,strings,0,
              [{clause,1,[],[],
                       [{cons,1,
                              {string,1,"foo"},
                              {cons,1,{string,1,"bar"},{nil,1}}}]}]}}

Anyway, ets is better solution for you, it will be faster for most of your
tasks. It is easier and you don't need learn AST :-)

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
>



-- 
--Hynek (Pichi) Vychodil
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20080608/130d7f7c/attachment.html>


More information about the erlang-questions mailing list