Skip to content

Commit

Permalink
Adds demo of using a macro to generate specialized macros (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
popematt authored Nov 27, 2024
1 parent f887ae7 commit fc2ed0d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions conformance/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ declarative domain-specific language.
* `ion_encoding/` – test cases for encoding directives
* `module/`
* `system_macros/` – test cases for each of the system macros
* `demos/` – contains demonstrations of interesting and/or useful strategies

> [!WARNING]
> This test suite and its DSL are **Work In Progress**.
Expand Down
35 changes: 35 additions & 0 deletions conformance/demos/metaprogramming.ion
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

(ion_1_1 "a macro that can create a monomorphized variant of the values macro"
(mactab (macro tiny_decimal (int8::a int8::b) (.make_decimal a b))
(macro tagless_values (mod_or_type type?)
(macro
// Macro name
(.$ion::make_symbol (.. (%mod_or_type) (.$ion::if_some (%type) "_") (%type) "_values"))
// Signature
((.$ion::annotate (.. (%mod_or_type) (%type)) vals) *)
// Body
((.$ion::values %) vals))
))
(then "for a built-in encoding type"
(toplevel ('#$:$ion::add_macros' ('#$:tagless_values' uint8)))
(then "when invoked in Ion text"
(text "(:uint8_values 1 2 3 4 5)")
(produces 1 2 3 4 5))
(then "when invoked in Ion binary"
// Invoke our new "uint8_values" macro
(binary "02 02 0B 01 02 03 04 05")
(produces 1 2 3 4 5)))
(then "for a macro-shape"
(toplevel ('#$:$ion::add_macros' ('#$:tagless_values' tiny_decimal)))
// TODO: Add case demoing binary
(then "when invoked in Ion text"
(text "(:tiny_decimal_values (1 2) (3 4))")
(produces 1d2 3d4)))
(then "for a qualified type name"
(toplevel ('#$:$ion::add_macros' ('#$:tagless_values' $ion make_decimal)))
// TODO: Add case demoing binary
(then "when invoked in Ion text"
(text "(:$ion_make_decimal_values (1 2) (3 4))")
(produces 1d2 3d4))))

0 comments on commit fc2ed0d

Please sign in to comment.