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