Elixir Core Language Elements

99 views
Skip to first unread message

David Simmons

unread,
Sep 5, 2013, 3:31:19 PM9/5/13
to elixir-l...@googlegroups.com
Hi 

In the Design Goals for Elixir (http://elixir-lang.org/blog/2013/08/08/elixir-design-goals/) it is stated that Elixir is made up of a small language core with the rest being implemented in Elixir itself. Just out of curiosity is there a list of those language elements that make up the core (i.e. are not written in Elixir itself)?

cheers

Dave

Alexei Sholik

unread,
Sep 5, 2013, 3:46:22 PM9/5/13
to elixir-l...@googlegroups.com


--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Best regards
Alexei Sholik

David Simmons

unread,
Sep 5, 2013, 4:03:06 PM9/5/13
to elixir-l...@googlegroups.com
Thanks Alexei.

Not quite what I was expecting :-). I was kind of just hoping for a list of those Elixir keywords that were part of the core language.

cheers

Dave

Alexei Sholik

unread,
Sep 5, 2013, 4:11:31 PM9/5/13
to elixir-l...@googlegroups.com

David Simmons

unread,
Sep 5, 2013, 4:15:54 PM9/5/13
to elixir-l...@googlegroups.com
Alexei

Perfect - thanks for your help

cheers

Dave

Alexei Sholik

unread,
Sep 5, 2013, 4:37:19 PM9/5/13
to elixir-l...@googlegroups.com
Note that this list of keywords is not a faithful representation of Elixir's core language. Things like defmodule, def, defmacro are implemented in Erlang, so they are part of the core too. There is no single list of all those special words, because they are special at different levels of abstraction, so to say.

The keywords defined in elixir_tokenizer.erl are there only because the parser treats them in a special way. 'true' is just an atom ':true'. It is made into a keyword so that you don't need to prepend it with a colon. Same goes for other "singleton values".

"do" is a keyword for the same reasons: it tells the parser to collect all expressions that follow into a single __block__ tuple:

iex(5)> {:ok, tokens} = :elixir_tokenizer.tokenize('quote do a; a + 1 end', 1, [])
{:ok,
 [{:do_identifier, 1, :quote}, {:do, 1}, {:identifier, 1, :a}, {:eol, 1, :";"},
  {:identifier, 1, :a}, {:dual_op, 1, :+}, {:number, 1, 1}, {:end, 1}]}

iex(6)> :elixir_parser.parse(tokens)                                              
{:ok,
 [{:quote, [line: 1],
   [[do: {:__block__, [],
      [{:a, [line: 1], nil}, {:+, [line: 1], [{:a, [line: 1], nil}, 1]}]}]]}]}

Notice that the whole body of "do-end" is parsed as a keyword list [do: {:__block__, [], <list of exprs>}]. Writing ;-separated list of expressions in parentheses yields the same thing:

iex(8)> {:ok, tokens} = :elixir_tokenizer.tokenize('quote do: (a; a + 1)', 1, [])
{:ok,
 [{:identifier, 1, :quote}, {:kw_identifier, 1, :do}, {:"(", 1},
  {:identifier, 1, :a}, {:eol, 1, :";"}, {:identifier, 1, :a},
  {:dual_op, 1, :+}, {:number, 1, 1}, {:")", 1}]}

iex(9)> :elixir_parser.parse(tokens)                                             
{:ok,
 [{:quote, [line: 1],
   [[do: {:__block__, [],
      [{:a, [line: 1], nil}, {:+, [line: 1], [{:a, [line: 1], nil}, 1]}]}]]}]}

So, in this case, thanks to "do" being a keyword we can have a nice parenless and ;-less syntax for blocks of code.

---

So, the real answer to your question is this: there is no single place describing all the elements comprising the core of Elixir. There is no formal definition of this core either. Basically, whatever is in the src/ directory can be treated as core. But there are also some low-level (non-user-facing) things that have been reimplemented in Elixir, like dispatch_tracker and record_rewriter. Go figure :)

José Valim

unread,
Sep 5, 2013, 5:21:51 PM9/5/13
to elixir-l...@googlegroups.com
> Note that this list of keywords is not a faithful representation of Elixir's core language. Things like defmodule, def, defmacro are implemented in Erlang, so they are part of the core too. There is no single list of all those special words, because they are special at different levels of abstraction, so to say.
Correct. And even though things like defmodule are written in Erlang, developers can still replace or wrap them. The list of special words that cannot, in any circunstance, be replaced are the keywords linked above and the macros defined in "Kernel.SpecialForms".

Things that are implemented in Erlang, but can be replaced, are all defined in Kernel (and their definitions will be empty).
Reply all
Reply to author
Forward
0 new messages