Skip to content

Macro Extension

Extension introduced in TethysL 1.8.0 (2019)

  • Nested macros supported since 3.9.7; decreasing ranges since 4.4.0.

Syntax

A macro generates a sequence of aggregates from a single "template" aggregate, one for each value of the macro variable in the given range:

macro $k = 1..4 {
  aggregate Wpt$k {
    run in sequence
    behavior Guidance:Waypoint {
      run in sequence
      set latitude = Lat[$k]
      set longitude = Lon[$k]
    }
  }
}

This is equivalent to writing the aggregates Wpt1, Wpt2, Wpt3 and Wpt4, each with the corresponding Lat[..] and Lon[..] array entries.

  • The variable name must start with $, followed by a letter, then letters, digits or _.
  • The body of the macro is exactly one aggregate.
  • The range limits can be numbers or constants, e.g., macro $j = 2..$LAST.
  • The range can be decreasing, e.g., macro $j = 10..1, generating the aggregates in that order.

Use of the macro variable

Within the template aggregate, the macro variable can be used in:

  • The name of the aggregate (and of nested aggregates), e.g., aggregate Leg$j. An unnamed aggregate is also allowed, although a warning is reported about the multiple resulting unnamed aggregates.

  • Array indexes, e.g., Lat[$j], including static expressions, e.g., Lat[$j - 1].

  • Numbers, either directly, e.g., $j count, or via a static expression grouped with $( ... ):

    macro $k = 2..5 {
      aggregate Leg$k {
        run in sequence
        break if ( leg <= $( $k - 1 ) count )
    
        assign in sequence Lat[$k - 1] = NaN degree
        assign in sequence Lon[$k - 1] = NaN degree
      }
    }
    

A static expression is a number, a macro variable (of any enclosing macro), a constant, or an addition/subtraction of these.

Nested macros

Macros can be nested:

macro $j = 1..5 {
  aggregate Leg$j {
    run in sequence
    macro $k = 1..7 {
      aggregate Wpt$k {
        run in sequence
        behavior Guidance:Waypoint {
          run in sequence
          set latitude = Lat[$k]
          set longitude = Lon[$k]
        }
      }
    }
  }
}

Within nested macros, any of the enclosing macro variables can be used (since 5.0.6; before, only the innermost one), e.g.:

macro $j = 1..2 {
  aggregate Leg$j {
    run in sequence
    macro $k = 1..3 {
      aggregate Sample$j$k {
        run in sequence
        assign in sequence Depth[$j + $k] = $j meter
      }
    }
  }
}
  • An aggregate name can include several macro variables, e.g., Sample$j$k, but it must include the variable of its own macro, so the names don't repeat.
  • The range of a macro is static: it can use numbers and constants, but not the variable of an enclosing macro (e.g., macro $k = 1..$j is not allowed).

Lint

The lint reports groups of aggregates that could be replaced with a macro:

* Possible use of 'macro' extension

  These 4 aggregates:

      aggregate Wpt1 { ... }
      ...
      aggregate Wpt4 { ... }

  could probably be replaced with:

      macro $k = 1..4 {
        aggregate Wpt$k { ... }
      }