Permalink
Browse files

Rename __MAIN__ to Elixir, closes #390

  • Loading branch information...
1 parent 7d1ef13 commit 117d09509c5ba49dd8ce6a45b2f76ee9cd6283e4 @josevalim josevalim committed Jul 22, 2012
View
@@ -1,8 +1,8 @@
* incompatible changes
+ * [Kernel] Compiled files now follow `Elixir-ModuleName` convention to solve issues with Erlang embedded mode. This removes the `__MAIN__` pseudo-variable as modules are now located inside `Elixir` namespace;
* [Kernel] `__using__` callback triggered by `use` now receives just one argument. Caller information can be accessed via macros using `__CALLER__`;
* [Module] Removed data functions in favor of unifying the attributes API;
* [Kernel] Comprehensions syntax changed to be more compatible with Erlang behavior;
- * [Kernel] Compiled files now follow "__MAIN__-ModuleName" convention to solve issues with Erlang embedded mode;
* [Kernel] loop and recur were removed in favor of recursion with named functions;
* deprecations
View
@@ -10,9 +10,9 @@ VERSION:=0.6.0.dev
#==> Templates
define TASK_TEMPLATE
-$(1): lib/$(1)/ebin/__MAIN__-$(2).beam
+$(1): lib/$(1)/ebin/Elixir-$(2).beam
-lib/$(1)/ebin/__MAIN__-$(2).beam: $(wildcard lib/$(1)/lib/*.ex) $(wildcard lib/$(1)/lib/*/*.ex) $(wildcard lib/$(1)/lib/*/*/*.ex) $$(FORCE)
+lib/$(1)/ebin/Elixir-$(2).beam: $(wildcard lib/$(1)/lib/*.ex) $(wildcard lib/$(1)/lib/*/*.ex) $(wildcard lib/$(1)/lib/*/*/*.ex) $$(FORCE)
@ echo "==> $(1) (compile)"
@ $$(ELIXIRC) "lib/$(1)/lib/**/*.ex" -o lib/$(1)/ebin
@ cd lib/$(1) && ../../bin/mix compile.app
@@ -23,7 +23,7 @@ test_$(1): $(1)
endef
#==> Compilation tasks
-KERNEL:=lib/elixir/ebin/__MAIN__-Kernel.beam
+KERNEL:=lib/elixir/ebin/Elixir-Kernel.beam
compile: lib/elixir/src/elixir.app.src erlang elixir
@@ -33,33 +33,34 @@ defimpl Binary.Inspect, for: Atom do
inspect(Foo.Bar) #=> "Foo.Bar"
"""
- def inspect(false), do: "false"
- def inspect(true), do: "true"
- def inspect(nil), do: "nil"
- def inspect(:""), do: ":\"\""
+ def inspect(false), do: "false"
+ def inspect(true), do: "true"
+ def inspect(nil), do: "nil"
+ def inspect(:""), do: ":\"\""
+ def inspect(Elixir), do: "Elixir"
def inspect(atom) do
binary = atom_to_binary(atom)
cond do
- atom in Macro.binary_ops or atom in Macro.unary_ops ->
- ":" <> binary
valid_atom_identifier?(binary) ->
":" <> binary
valid_ref_identifier?(binary) ->
- "__MAIN__-" <> rest = binary
+ "Elixir-" <> rest = binary
bc <<r>> inbits rest, do: <<to_dot(r)>>
+ atom in Macro.binary_ops or atom in Macro.unary_ops ->
+ ":" <> binary
true ->
":" <> Binary.escape(binary, ?")
end
end
- # Detect if atom is an atom alias (__MAIN__-Foo-Bar-Baz)
+ # Detect if atom is an atom alias (Elixir-Foo-Bar-Baz)
defp to_dot(?-), do: ?.
defp to_dot(l), do: l
- defp valid_ref_identifier?("__MAIN__" <> rest) do
+ defp valid_ref_identifier?("Elixir" <> rest) do
valid_ref_piece?(rest)
end
@@ -234,7 +235,7 @@ defimpl Binary.Inspect, for: Tuple do
## Helpers
defp is_record?(name) do
- is_atom(name) and match?("__MAIN__-" <> _, atom_to_binary(name, :utf8)) and
+ is_atom(name) and match?("Elixir-" <> _, atom_to_binary(name, :utf8)) and
function_exported?(name, :__record__, 1)
end
@@ -2207,7 +2207,7 @@ defmodule Kernel do
"""
defmacro :"..".(first, last) do
- { :{}, 0, [__MAIN__.Range, first, last] }
+ { :{}, 0, [Elixir.Range, first, last] }
end
@doc """
@@ -4,14 +4,14 @@ defmodule Kernel.SpecialForms do
special forms because they cannot be overridden by the developer
and sometimes have lexical scope (like `alias`, `import`, etc).
- This module also documents Elixir's pseudo variable (`__MODULE__`,
+ This module also documents Elixir's pseudo variables (`__MODULE__`,
`__FILE__`, `__ENV__` and `__CALLER__`) which return information
about Elixir's compilation environment.
Finally, it also documents 3 special forms (`__block__`,
`__scope__` and `__aliases__`), which are not intended to be
called directly by the developer but they appear in quoted
- contents since they are important for Elixir's functioning.
+ contents since they are essential in Elixir's constructions.
"""
@doc """
@@ -60,10 +60,10 @@ defmodule Kernel.SpecialForms do
automatically replaced by `MyKeyword`.
In case one wants to access the original `Keyword`, it can be done
- by accessing __MAIN__:
+ by accessing Elixir:
Keyword.values #=> uses MyKeyword.values
- __MAIN__.Keyword.values #=> uses Keyword.values
+ Elixir.Keyword.values #=> uses Keyword.values
Notice that calling `alias` without the `as:` option automatically
sets an alias based on the last part of the module. For example:
@@ -270,12 +270,12 @@ defmodule Kernel.SpecialForms do
a #=> 11
Notice that aliases are not hygienic in Elixir, ambiguity
- must be solved by prepending __MAIN__:
+ must be solved by prepending Elixir:
quote do
- __MAIN__.Foo #=> Access the root Foo
- Foo #=> Access the Foo alias in the current module
- (if any is set), then fallback to __MAIN__.Foo
+ Elixir.Foo #=> Access the root Foo
+ Foo #=> Access the Foo alias in the current module
+ (if any is set), then fallback to Elixir.Foo
end
## Options
@@ -283,17 +283,18 @@ defmodule Macro do
# aliases just contain one item, we are sure it is
# an atom, so we just expand it based on the aliases
# dict.
- def expand({ :__aliases__, _, [h] }, env) do
+ def expand({ :__aliases__, _, [h] }, env) when h != Elixir do
expand_alias(h, env)
end
# In case aliases contains more than one item, we need
# to loop them checking if they are all atoms or not.
# Macros and pseudo-variables are then expanded.
def expand({ :__aliases__, _, [h|t] }, env) do
- aliases = case is_atom(h) do
- true -> [expand_alias(h, env)|t]
- false -> [h|t]
+ aliases = case h do
+ Elixir -> t
+ x when is_atom(x) -> [expand_alias(x, env)|t]
+ _ -> [h|t]
end
aliases = lc alias inlist aliases, do: expand(alias, env)
@@ -305,7 +306,6 @@ defmodule Macro do
is_atom(atom) and (is_atom(args) or args == []), do: atom
# Expand pseudo-variables
- def expand({ :__MAIN__, _, atom }, _env) when is_atom(atom), do: :__MAIN__
def expand({ :__MODULE__, _, atom }, env) when is_atom(atom), do: env.module
def expand({ :__FILE__, _, atom }, env) when is_atom(atom), do: env.file
def expand({ :__ENV__, _, atom }, env) when is_atom(atom), do: env
@@ -355,7 +355,7 @@ defmodule Macro do
end
defp expand_alias(h, env) do
- atom = list_to_atom('__MAIN__-' ++ atom_to_list(h))
+ atom = list_to_atom('Elixir-' ++ atom_to_list(h))
Erlang.elixir_aliases.lookup(atom, env.aliases)
end
end
@@ -228,7 +228,7 @@ defmodule Protocol do
true -> __MODULE__.Tuple
false ->
case atom_to_list(first) do
- '__MAIN__' ++ _ -> __MODULE__.Record
+ 'Elixir-' ++ _ -> __MODULE__.Record
_ -> __MODULE__.Tuple
end
end
@@ -24,9 +24,9 @@ ensure_loaded(Line, Ref, S) ->
first(Atom) ->
First = first(atom_to_list(Atom), []),
- list_to_atom("__MAIN__-" ++ First).
+ list_to_atom("Elixir-" ++ First).
-first("__MAIN__-" ++ Rest, []) -> first(Rest, []);
+first("Elixir-" ++ Rest, []) -> first(Rest, []);
first([$-|_], Acc) -> lists:reverse(Acc);
first([H|T], Acc) -> first(T, [H|Acc]);
first([], Acc) -> lists:reverse(Acc).
@@ -35,7 +35,7 @@ first([], Acc) -> lists:reverse(Acc).
last(Atom) ->
Last = last(lists:reverse(atom_to_list(Atom)), []),
- list_to_atom("__MAIN__-" ++ Last).
+ list_to_atom("Elixir-" ++ Last).
last([$-|_], Acc) -> Acc;
last([H|T], Acc) -> last(T, [H|Acc]);
@@ -49,11 +49,11 @@ safe_concat(Args) -> list_to_existing_atom(raw_concat(Args)).
raw_concat(Args) ->
Aliases = [to_partial(Arg) || Arg <- Args, Arg /= nil],
- "__MAIN__" ++ lists:concat(Aliases).
+ "Elixir" ++ lists:concat(Aliases).
to_partial(Arg) when is_binary(Arg) -> to_partial(binary_to_list(Arg));
to_partial(Arg) when is_atom(Arg) -> to_partial(atom_to_list(Arg));
-to_partial("__MAIN__" ++ Arg) -> dot_to_dash(Arg);
+to_partial("Elixir-" ++ Arg) -> dot_to_dash([$-|Arg]);
to_partial([$-|_] = Arg) -> dot_to_dash(Arg);
to_partial(Arg) when is_list(Arg) -> [$-|dot_to_dash(Arg)].
@@ -15,7 +15,7 @@
inspect(Atom) when is_atom(Atom) ->
case atom_to_list(Atom) of
- "__MAIN__-" ++ Rest -> [to_dot(R) || R <- Rest];
+ "Elixir-" ++ Rest -> [to_dot(R) || R <- Rest];
Else -> Else
end;
@@ -266,7 +266,6 @@ non_overridable_macros() ->
{'import',1},
{'import',2},
{'import',3},
- {'__MAIN__',0},
{'__ENV__',0},
{'__CALLER__',0},
{'__MODULE__',0},
@@ -274,7 +274,7 @@ translate_macro({ 'var!', Line, [_] }, S) ->
module_ref(_Raw, Module, nil) ->
Module;
-module_ref({ '__aliases__', _, [{ '__MAIN__', _, Atom }|_]}, Module, _Nesting) when is_atom(Atom) ->
+module_ref({ '__aliases__', _, ['Elixir'|_]}, Module, _Nesting) ->
Module;
module_ref(_F, Module, Nesting) ->
@@ -6,9 +6,11 @@ parse_transform(Forms, _) ->
do_transform({ atom, Line, Atom }) ->
case atom_to_list(Atom) of
- "Elixir." ++ Rest ->
- Tokens = string:tokens(Rest, "."),
- Module = string:join(["__MAIN__"|Tokens], "-"),
+ "Elixir." ++ _ = List ->
+ Module = [case T of
+ $. -> $-;
+ _ -> T
+ end || T <- List],
{ atom, Line, list_to_atom(Module) };
_ ->
{ atom, Line, Atom }
@@ -218,9 +218,6 @@ translate_each({'__MODULE__', Line, Atom}, S) when is_atom(Atom) ->
translate_each({'__FILE__', _Line, Atom}, S) when is_atom(Atom) ->
translate_each(S#elixir_scope.file, S);
-translate_each({'__MAIN__', Line, Atom}, S) when is_atom(Atom) ->
- { { atom, Line, '__MAIN__' }, S };
-
translate_each({'__ENV__', Line, Atom}, S) when is_atom(Atom) ->
{ elixir_scope:to_erl_env({ Line, S }), S };
@@ -244,16 +241,18 @@ translate_each({'__LINE__', Line, Atom}, S) when is_atom(Atom) ->
%% Aliases
-translate_each({ '__aliases__', Line, [H] }, S) ->
- Atom = list_to_atom("__MAIN__-" ++ atom_to_list(H)),
+translate_each({ '__aliases__', Line, [H] }, S) when H /= 'Elixir' ->
+ Atom = list_to_atom("Elixir-" ++ atom_to_list(H)),
{ { atom, Line, elixir_aliases:lookup(Atom, S#elixir_scope.aliases) }, S };
translate_each({ '__aliases__', Line, [H|T] }, S) ->
- Aliases = case is_atom(H) of
- true ->
- Atom = list_to_atom("__MAIN__-" ++ atom_to_list(H)),
+ Aliases = if
+ H == 'Elixir' ->
+ T;
+ is_atom(H) ->
+ Atom = list_to_atom("Elixir-" ++ atom_to_list(H)),
[elixir_aliases:lookup(Atom, S#elixir_scope.aliases)|T];
- false ->
+ true ->
[H|T]
end,
@@ -23,7 +23,7 @@ defmodule Binary.Chars.AtomTest do
end
test :alias_atom do
- assert to_binary(Foo.Bar) == "__MAIN__-Foo-Bar"
+ assert to_binary(Foo.Bar) == "Elixir-Foo-Bar"
end
end
@@ -12,8 +12,8 @@ defmodule Kernel.AliasTest do
test :alias_erlang do
alias Erlang.lists, as: MyList
assert MyList.flatten([1,[2],3]) == [1,2,3]
- assert __MAIN__.MyList.Bar == :"__MAIN__-MyList-Bar"
- assert MyList.Bar == :"__MAIN__-lists-Bar"
+ assert Elixir.MyList.Bar == :"Elixir-MyList-Bar"
+ assert MyList.Bar == :"Elixir-lists-Bar"
end
test :double_alias do
@@ -80,7 +80,7 @@ defmodule Kernel.CLI.CompileTest do
fixture = fixture_path "compile_sample.exs"
assert elixirc('#{fixture} -o #{tmp_path}') ==
'Compiled #{fixture}\n'
- assert File.regular?(tmp_path "__MAIN__-CompileSample.beam")
+ assert File.regular?(tmp_path "Elixir-CompileSample.beam")
end
end
@@ -92,8 +92,8 @@ defmodule Kernel.CLI.ParallelCompilerTest do
output = elixirc('#{fixture_path("parallel_compiler")} -o #{tmp_path}')
assert Erlang.string.str(output, 'message_from_foo') > 0,
"Expected #{inspect output} to contain 'message_from_foo'"
- assert File.regular?(tmp_path "__MAIN__-Foo.beam")
- assert File.regular?(tmp_path "__MAIN__-Bar.beam")
+ assert File.regular?(tmp_path "Elixir-Foo.beam")
+ assert File.regular?(tmp_path "Elixir-Bar.beam")
end
test :deadlock_failure do
@@ -156,7 +156,7 @@ defmodule Kernel.ErrorsTest do
test :in_definition_module do
assert "nofile:1: cannot define module Foo because it is currently being defined" ==
- format_rescue 'defmodule Foo, do: (defmodule __MAIN__.Foo, do: true)'
+ format_rescue 'defmodule Foo, do: (defmodule Elixir.Foo, do: true)'
end
test :duplicated_bitstring_size do
@@ -46,7 +46,7 @@ defmodule MacroTest do
end
test :expand_with_main do
- assert Macro.expand(quote(do: __MAIN__), __ENV__) == __MAIN__
+ assert Macro.expand(quote(do: Elixir), __ENV__) == Elixir
end
test :expand_with_simple_alias do
@@ -58,7 +58,7 @@ defmodule MacroTest do
end
test :expand_with_main_plus_alias do
- assert Macro.expand(quote(do: __MAIN__.Foo), __ENV__) == Foo
+ assert Macro.expand(quote(do: Elixir.Foo), __ENV__) == Foo
end
test :expand_with_custom_alias do
@@ -68,7 +68,7 @@ defmodule MacroTest do
test :expand_with_main_plus_custom_alias do
alias Foo, as: Bar
- assert Macro.expand(quote(do: __MAIN__.Bar.Baz), __ENV__) == __MAIN__.Bar.Baz
+ assert Macro.expand(quote(do: Elixir.Bar.Baz), __ENV__) == Elixir.Bar.Baz
end
test :expand_with_erlang do
@@ -93,7 +93,7 @@ defmodule ModuleTest do
end
test :__MODULE__ do
- assert __MODULE__ == :"__MAIN__-ModuleTest"
+ assert __MODULE__ == :"Elixir-ModuleTest"
end
test :compile_callback_hook do
Oops, something went wrong.

0 comments on commit 117d095

Please sign in to comment.