Permalink
Browse files

Include a 'module_type' attribute in each generated Reia module, allo…

…wing conditional dispatch to modules vs classes
  • Loading branch information...
1 parent de889e3 commit 3bf8a84ed4ac6a012eba8f910c1f58a1d9c811d9 Tony Arcieri committed Dec 25, 2010
Showing with 21 additions and 15 deletions.
  1. +16 −12 src/compiler/reia_bytecode.erl
  2. +1 −1 src/compiler/reia_nodes.hrl
  3. +4 −2 src/compiler/reia_r2e.erl
@@ -1,6 +1,6 @@
%
% reia_bytecode: Generate bytecode from compiled Reia AST
-% Copyright (C)2009 Tony Arcieri
+% Copyright (C)2009-10 Tony Arcieri
%
% Redistribution is permitted under the MIT license. See LICENSE for details.
%
@@ -35,7 +35,7 @@ load(Bin, Args) ->
compile(Filename, Exprs) ->
compile(Filename, Exprs, #compile_options{}).
-compile(Filename, Exprs, Options) ->
+compile(Filename, Exprs, Options) ->
case compile_expressions(Filename, Exprs, Options) of
{ok, _Module, Bin} ->
Module = #reia_module{filename=Filename, base_module=Bin},
@@ -50,32 +50,36 @@ compile_expressions(Filename, Exprs, Options) ->
true -> wrapped_module(Filename, Exprs);
false -> unwrapped_module(Exprs)
end,
-
+
+ Header = module_header(Module#module.name, Filename, Module#module.attrs, Options),
Submodules2 = compile_submodules(Submodules, Filename, Options),
-
- Header = module_header(Module#module.name, Filename, Options),
- ParentAttr = {attribute, 1, parent, list_to_atom(filename:rootname(Filename))},
SubmoduleAttr = {attribute, 1, submodules, Submodules2},
- ErlModule = lists:flatten([Header, ParentAttr, SubmoduleAttr, Module#module.exprs]),
+ ErlModule = lists:flatten([Header, SubmoduleAttr, Module#module.exprs]),
compile:forms(ErlModule, compile_options(Options)).
compile_submodules(Submodules, Filename, Options) ->
[compile_submodule(Submodule, Filename, Options) || Submodule <- Submodules].
compile_submodule(Module, Filename, Options) ->
- Header = module_header(Module#module.name, Filename, Options),
- ParentAttr = {attribute, 1, parent, list_to_atom(filename:rootname(Filename))},
- ErlModule = lists:flatten([Header, ParentAttr, Module#module.exprs]),
+ Header = module_header(Module#module.name, Filename, Module#module.attrs, Options),
+ ErlModule = Header ++ Module#module.exprs,
{ok, Name, Bin} = compile:forms(ErlModule, compile_options(Options)),
{static, Name, Bin}.
-module_header(Name, Filename, Options) ->
+module_header(Name, Filename, CustomAttrs, Options) ->
+ ParentAttr = {attribute, 1, parent, list_to_atom(filename:rootname(Filename))},
+ ErlAttrs = lists:map(
+ fun({AttrName, Value}) -> {attribute, 1, AttrName, Value} end,
+ CustomAttrs
+ ),
+
[
{attribute, 1, module, Name},
{attribute, 1, file, {Filename, 1}},
- {attribute, 1, code, Options#compile_options.code}
+ {attribute, 1, code, Options#compile_options.code},
+ ParentAttr|ErlAttrs
].
wrapped_module(Filename, Exprs) ->
@@ -22,7 +22,7 @@
-record(self, {line}).
% Modules and Classes
--record(module, {line, name, exprs}).
+-record(module, {line, name, attrs=[], exprs}).
-record(class, {line, name, parent='Object', exprs}).
% Functions
@@ -38,8 +38,9 @@ transform(#module{line=Line, name=Name, exprs=Exprs}) ->
% Modules need some postprocessing, so we leave them in a similar form but
% with their subtrees converted
#module{
- line = Line,
- name = Name,
+ line = Line,
+ name = Name,
+ attrs = [{module_type, module}],
exprs = group_clauses([transform(Expr) || Expr <- Exprs])
};
@@ -50,6 +51,7 @@ transform(#class{line=Line, name=Name, exprs=Exprs}) ->
#module{
line = Line,
name = Name,
+ attrs = [{module_type, class}],
exprs = group_clauses([transform(Expr) || Expr <- Exprs])
};

0 comments on commit 3bf8a84

Please sign in to comment.