enso/docs/syntax/macros.md
Ara Adkins 2f404b7f08
Fix a bug with compiler metadata (#838)
1.  The metadata objects weren't being duplicated when duplicating
    the IR. This meant that the later passes would write metadata
    multiples times into one store (reference), causing wrong
    behaviour at codegen time.
2020-06-16 13:52:11 +01:00

2.3 KiB

layout title category tags order
developer-doc The Enso Macro Syntax syntax
syntax
macro
8

The Enso Macro Syntax

Enso provides a macro system that allows users to perform AST to AST transformations on the provided pieces of code. While many languages' macros provide their users with access to the compilation and type-checking phases (scala, for example), there are a few reasons that we don't want to:

  • The power of a dependently-typed language obviates the need for the ability to manipulate types at compile time.
  • Syntactic macros are far more predictable than those that can perform type manipulation and compute values.
  • We do not want to introduce a metaprogramming system that is too complex.

The actionables for this section are:

  • Fully specify the macro system.
  • Fully specify the interactions between the parser-based macro system and the runtime.

Annotations

Much like annotations on the JVM, annotations in Enso are tags that perform a purely syntactic transformation on the entity to which they are applied. The implementation of this requires both parser changes and support for user-defined macros, but for now it would be possible to work only with a set of hard-coded annotation macros.

Annotations can be arbitrarily nested, so a set of annotation macros become implicitly nested inside each other:

@derive Eq Debug
@make_magic
type Maybe a
    use Nothing
    type Just

The above example is logically translated to:

derive Eq Debug
    make_magic
        type Maybe a
            use Nothing
            type Just (value : a)

In the presence of annotations and macros, it becomes more and more important that we are able to reserve words such as type to ensure that users can always have a good sense of what the most common constructs in the language mean, rather than allowing them to be overridden outside of the stdlib.

Automatic Deriving

In order to make the language easier to debug, we have all types automatically derive an interface DebugShow. This interface provides a function that will print all the significant information about the value (e.g. locations, types, source information, etc).