this feature is another one that uses the pre processor in erlang, and since I don't want to implement a preprocessor I had to find a way to do it differently.
but still there was a thing...
the main use of -include is to include record definitions and those record definitions are written in erlang.
another obvious thing is that there is not much code on efene that declares records and efene provides structs, that don't require including external files, this make includes kind of useless in efene.
also there is this "erlang <3 efene, efene <3 erlang" that promotes complete interoperability between both languages, not being able to include erlang in efene would make less true...
last but not least, if I included an ifene module in an efene module "as is", this is, as text before compiling, it won't compile because they are different languajes. This would make include pretty useless.
to avoid all this problems, and avoid including a pre processor I implemented @import (efene version of -include) as an attribute that does some magic at compile time (like @type, @spec and @rec), basically what it does is:
- guess the type of the file by it's extension
- call the correct compiler depending on the type
- get the AST of the imported module and clean up stuff like module and file definitions, @public attributes and eof marks
- insert the remaining ast in the module where it was imported
toimport.ifn
@public
to_import_ifn = fn ()
io.format("i'm a function from module toimport.ifn~n")
toimport.fn
@public
to_import_fn = fn () {
io.format("i'm a function from module toimport.fn~n")
}
toimport.erl
-module(toimport).
-export([to_import_erl/0]).
to_import_erl() ->
io:format("i'm a function from module toimport.erl~n").
toimport.hrl
-module(toimport).
-export([to_import_hrl/0]).
to_import_hrl() ->
io:format("i'm a function from module toimport.hrl~n").
importall.ifn
@import("toimport.ifn")
@import("toimport.fn")
@import("toimport.erl")
@import("toimport.hrl")
@public
run = fn ()
to_import_ifn()
to_import_fn()
to_import_erl()
to_import_hrl()
here we declare 4 modules to be imported by a module called importall, each module is written in a different language (well, hrl is erlang..)
we import all and use the functions, nice uh?
now compile and run
$ fnc importall.ifn Compiling importall.ifn $ fnc -r importall run i'm a function from module toimport.ifn i'm a function from module toimport.fn i'm a function from module toimport.erl i'm a function from module toimport.hrl
other cool things that this allows is to build your own importers on top of the default imported, do import and transformation/filtering on the imported code and any thing you can think of
we could easily do an import version that allows to load modules from the web, from a database or some other place..
even imports that allow to load DSLs and compile them to erlang bytecode, the sky is the limit!
let me know what you think
No hay comentarios:
Publicar un comentario en la entrada