Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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

…wing conditional dispatch to modules vs classes
  • Loading branch information...
commit 3bf8a84ed4ac6a012eba8f910c1f58a1d9c811d9 1 parent de889e3
Tony Arcieri authored
View
28 src/compiler/reia_bytecode.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,13 +50,11 @@ 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)).
@@ -64,18 +62,24 @@ 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) ->
View
2  src/compiler/reia_nodes.hrl
@@ -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
View
6 src/compiler/reia_r2e.erl
@@ -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])
};
Please sign in to comment.
Something went wrong with that request. Please try again.