[erlang-questions] some language changes

Vladislav Titov me@REDACTED
Tue May 22 03:58:04 CEST 2007

On 5/21/07, Joe Armstrong <erlang@REDACTED> wrote:
> 3. Extended string syntax: idea - put an atom *before the string quote
>     to say what the string means and to *change* the syntax rules that apply
>     to the string content.
> 4. Simple string substitutions

These two sound like they'd be perfectly matched by something like
CL's reader macro concept. This can be done by, for example, extending
the code preprocessor to allow specifying "reader atoms" and a
callback module to handle them (so it's just a fancy code transform).
Upon encountering such atom (prefixed by some kind of a control
character, presumably) the preprocessor would call into the callback,
passing in an epp handle.

The epp module can then be extended with something along the lines of
peek_form/peek_char, pop_form/pop_char/pop_space, push_form/push_char.
Then, for example, the regexp example can be implemented (in a perl
way, so that code can do :regexp /"(\w+)"/) as:

callback(Epp) ->
  StartChar = epp:pop_char(Epp),
  RegexpStr = find_end(Epp, StartChar, []),
  epp:push_form({string, epp:get_line(Epp), RegexpStr}).

find_end(Epp, X, Acc) ->
  Y = epp:pop_char(Epp),
  if Y /= X -> find_end(Epp, X, [Y | Acc]);
  true -> lists:reverse(Acc).

Likewise, string substitution would be fairly straightforward (apart
from manually parsing the string).

This would allow for a range of nice features, from extending the
language with new literal type (:pid <0,1,2>), to some fancy
metaprogramming tricks (:bitcount(X) would turn into element(X,
{0,1,1,2,1,...})). It brings in some of the functionality of CL macros
without all the nasty AST stuff.

I'll stop now.


More information about the erlang-questions mailing list