compiling a module from a string

Mats Cronqvist mats.cronqvist@REDACTED
Mon Jul 10 12:54:28 CEST 2006


Ulf Wiger (AL/EAB) wrote:
> I want to effectively call
> 
> Str = "-module(m).\n-export([foo/0]).\nfoo() -> foo.\n".
> compile:string(Str, [binary]).

   this should work iff there are no #@#$&* macros. if there is, it's pretty 
easy to write a subset of epp and call it between scanning and parsing.

   mats

cl(Text) ->
     Forms = scan_and_parse(Text,1,[]),
     {ok,Mod,Bin} = compile:forms(Forms),
     code:load_binary(Mod,"generated",Bin).

scan_and_parse([],_Line,Forms) -> lists:reverse(Forms);
scan_and_parse(Text,Line,Forms) ->
     {done,{ok,Toks,NLine},Cont} = erl_scan:tokens([],Text,Line),
     {ok,Form} = erl_parse:parse_form(Toks),
     scan_and_parse(Cont,NLine,[Form|Forms]).




More information about the erlang-questions mailing list