ion-java icon indicating copy to clipboard operation
ion-java copied to clipboard

Evaluating user-defined template macros

Open tgregg opened this issue 1 year ago • 0 comments

Ion 1.1 allows users to define templates--reusable data shapes that can be invoked as many times as needed--that expand to zero or more Ion values.

This issue builds on #656 and #657, and tracks building the machinery needed to evaluate invocations of a user-defined template macro. Such invocations can appear in two places:

  1. In the data stream, encoded using an e-expression.
  2. In the body of another template, encoded as an s-expression.

The latter requires implementing the ability to pass named parameters (i.e. variables) from the host template's environment to the macro being invoked.

For example:

// Template definition
(macro
    greet              // Template name
    (part_of_day name) // Parameter names
    (make_string       // Template body
        "Good "
        part_of_day
        ", "
        name
        "."))

(:greet "morning" "Mr. Cartwright")

// Notionally expands to the intermediate equivalent:
(:make_string "Good " "morning" ", " "Mr. Cartwright" ".")

// Which evaluates to:
"Good morning, Mr. Cartwright." 

In this case, the named parameters part_of_day and name are passed from the environment of the greet invocation to the environment of the make_string invocation.

tgregg avatar Feb 21 '24 23:02 tgregg