tags:

views:

276

answers:

2

I have a generated string that contains the code for an erlang module.

Is there a way to compile the generated module straight from the string?

Or is there a way to convert the string into the format needed for compile:forms/1?

Or will I have to save it to a temp file first and then compile it with compile:file/1?

Alternatively, I can add support to the compile module, but there must be a reason why the good people that write erlang haven't added it.

+6  A: 

You need to scan your string into tokens, and then parse the tokens into forms. Unfortunately it is only possible to parse one form at a time, so you will need to either cut your string, or your tokens at form boundaries. Here is a short example:

% create tokens from strings containing forms
> {ok, MTs, _} = erl_scan:string("-module(z).").
> {ok, ETs, _} = erl_scan:string("-export([f/0]).").
> {ok, FTs, _} = erl_scan:string("f() -> hello_world.").
% tokens to erl_parse trees
> {ok,MF} = erl_parse:parse_form(MTs).
> {ok,EF} = erl_parse:parse_form(ETs).
> {ok,FF} = erl_parse:parse_form(FTs).

% compile forms to binary
> {ok, z, Bin} = compile:forms([MF,EF,FF]).
{ok,z,<<70,79,82,49,0,0,1,164,66,69,65,77,65,116,111,109,0,0,0,...>>}

% load module from binary
> code:load_binary(z, "nofile", Bin).
{module,z}

% test
> z:f().
hello_world

Alternatively you can scan your string, and then cut the resulting token list at {dot, _} tokens apart.

Zed
Looks good, thanks, didn't know I had to spilt it all up :D
monkey_p
Fixed the typo. I guess you could put your whole source code into a single string, scan it, and then cut the token list at each `{dot, _}`.
Zed
Also, note that includes do not work with this approach (see: http://stackoverflow.com/questions/2013119/compile-forms-with-included-header-files)
Zed
Exactly what I ended up doing, scanned for the {dot, _} :D
monkey_p
Unfortunately it is `erl_parse` which needs the spliting, `erl_scan:string` has no problems. I am working on a fix to yecc, which is used to generate `erl_parse`, so it can handle both to few and too many tokens in a sensible way.
rvirding
http://www.trapexit.org/String_Eval(same as Zed's answer)
Gene T
A: 

Store the content of the string in a file with a module name (if not there in the code) and compile it using compile module [http://ftp.sunet.se/pub/lang/erlang/doc/man/compile.html#file-1].

Code becomes available in the VM. I am using this technique to convert a configuration file into a module. This way there not much binary copy going on during the execution.

Yogish Baliga