Bug 471380 - Macros
Summary: Macros
Status: ASSIGNED
Alias: None
Product: z_Archived
Classification: Eclipse Foundation
Component: Golo (show other bugs)
Version: unspecified   Edit
Hardware: PC Mac OS X
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Yannick Loiseau CLA
QA Contact:
URL:
Whiteboard:
Keywords: investigate, noteworthy
Depends on:
Blocks:
 
Reported: 2015-06-29 16:31 EDT by Julien Ponge CLA
Modified: 2016-05-25 10:27 EDT (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Julien Ponge CLA 2015-06-29 16:31:42 EDT
(from https://github.com/golo-lang/golo-lang/issues/192, works in progress published by Yannick Loiseau)

Wouldn't it be fun to have (hygienic) macros? :smile:

https://en.wikipedia.org/wiki/Hygienic_macro

----  yloiseau commented on 23 Oct 2014

At which stage would you see the macro expansion ?

----  Artpej commented on 23 Oct 2014

I love this idea, it will be awesome to have something like http://sweetjs.org/ !

At which stage is a good question (as usual :wink:) : i think it can be done at compile time, I have no idea how it can be done at runime ...
At compile time it could be viewed as compiler plugin? (I have in mind the java annotations processors)

----  yloiseau commented on 23 Oct 2014

Also think compile time is the more natural. Seems like just some “simple” AST/IR manipulation (aside from the hygienic problem)

An other reference to harvest ideas from: http://doc.rust-lang.org/0.12.0/guide-macros.html

----  jponge commented on 23 Oct 2014

For me it's an AST/IR manipulation work.


---- yloiseau commented on 23 Oct 2014

Would you want “importable” macros, or just macros defined in the current module ?

----  jponge commented on 23 Oct 2014 

Importable is more interesting, don't you think so?=

----  yloiseau commented on 23 Oct 2014

of course, but also harder to do... probably more than just AST manipulation

----  jponge commented on 23 Oct 2014

Yep, the parser work is more involved...

----  yloiseau commented on 25 Nov 2014

Here are some ideas about a possible macro implementation, for discussion.

A low level macro is a “callable” (static method, or single method interface, to be defined) that is “discoverable” at compile time. This callable take a IR subtree and returns a new (or modified) IR subtree.

The Golo parser is modified to identify macro calls, and thus adds a new node in the AST/IR (say MacroInvocation) containing the macro name and the subtree on which it will be applied.

A new visitor (say MacroExpansionGoloIrVisitor) then transform the IR tree by searching MacroInvocation nodes and replacing them with the result of the application of the corresponding macro. The resulting IR tree is then compiled as usual.

An new macro definition language should be defined (maybe using predefined low level macros) to ease the creation of low level macros, that could be compiled in a preliminary step. See for example Caml, Rust or sweetjs macro languages.

How the available macros are made discoverable to the compiler is yet to be defined.

As a side note, a GoloSourceGoloIrVisitor, serializing an IR tree as golo source code could be useful for debugging macros expansion. It could also be used as a golo pretty printer.

Some references:

* Macros in JS: http://sweetjs.org/ mini language with an independent pre-compilation step,
* Macros in Rust mini language http://doc.rust-lang.org/0.12.0/guide-macros.html macro definition integrated in the main language, no pre-compilation required,
* Macros in Python: https://github.com/lihaoyi/macropy regular python function expanded at import time (use the special import hook),
* Macros in Caml http://caml.inria.fr/pub/docs/tutorial-camlp4/index.html mini language with a separate pre-compile step

----  jponge commented on 25 Nov 2014

Makes sense as an approach :+1:

----  yloiseau commented on 12 Dec 2014

Some (very generic) use cases:

* code generation to avoid boiler plate
* syntax extension: e.g. adding operators
* pattern matching and restructuring