Skip to content

Macro extensions in TethysL

The TethysL array and macro syntax extensions allow capturing certain similar constructs (for example, multiple similar arguments or aggregates) with a very concise, readable, and less error-prone syntax when compared to how they would be (directly) written in XML.

Although both syntax extensions can be used separately, they work together very nicely.

As an example, the "Source" tab below demonstrates the use of both of these extensions in TethysL, while the "Expanded" tab shows the corresponding, semantically equivalent TethysL that uses directly supported elements according to the Tethys Schema.

Note

The expanded TethysL below is only included for illustration. The TethysL compiler will translate the given source directly to XML with the effect of any array/macro expansions in place.

mission {
  arguments {
    Lat[1..3] = NaN degree   """Latitude {$}"""
    Lon[1..3] = NaN degree   """Longitude {$}"""
  }

  macro $i = 1..3 {
    aggregate Wpt$i {
      run in sequence
      behavior Guidance:Waypoint {
        run in sequence
        set latitude = Lat[$i]
        set longitude = Lon[$i]
      }
    }
  }
}
mission {
  arguments {
    Lat1 = NaN degree   """Latitude 1"""
    Lat2 = NaN degree   """Latitude 2"""
    Lat3 = NaN degree   """Latitude 3"""

    Lon1 = NaN degree   """Longitude 1"""
    Lon2 = NaN degree   """Longitude 2"""
    Lon3 = NaN degree   """Longitude 3"""
  }

  aggregate Wpt1 {
    run in sequence
    behavior Guidance:Waypoint {
      run in sequence
      set latitude = Lat1
      set longitude = Lon1
    }
  }

  aggregate Wpt2 {
    run in sequence
    behavior Guidance:Waypoint {
      run in sequence
      set latitude = Lat2
      set longitude = Lon2
    }
  }

  aggregate Wpt3 {
    run in sequence
    behavior Guidance:Waypoint {
      run in sequence
      set latitude = Lat3
      set longitude = Lon3
    }
  }
}
  • The {$} fragment in the description of an argument or output declaration, expands to the corresponding index as indicated by the array range ([1..3] in the example above).

  • One can use a static, simple addition/subtraction expression in two general cases:

    • Anywhere where a number expression can be used, e.g., $( $k - 1 ) count.

      Note: The $( ... ) construct is to enclose the static expression itself, and only needed if the expression involves an actual addition or substraction. (In other words, this is not needed for a single term, e.g., $k count is valid.)

    • Array index expression, e.g., Latitude[$k - 1].

      Note: In this case, the brackets ([...]) serve as the enclosing construct for the static expression.

    Since the expression is to be evaluated prior to translating to XML, "static" here means that it can only involve literal numbers (e.g., 1, 5, 42) and any macro variable in scope.

    Here's an example (as used in mbts_sci2.tl):

    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
      }
    }
    

    So, in expanded form, the 4 aggregates generated by the macro above would be:

    aggregate Leg2 {
      run in sequence
      break if ( leg <= 1 count )
    
      assign in sequence Lat[1] = NaN degree
      assign in sequence Lon[1] = NaN degree
    }
    aggregate Leg3 {
      run in sequence
      break if ( leg <= 2 count )
    
      assign in sequence Lat[2] = NaN degree
      assign in sequence Lon[2] = NaN degree
    }
    aggregate Leg4 {
      run in sequence
      break if ( leg <= 3 count )
    
      assign in sequence Lat[3] = NaN degree
      assign in sequence Lon[3] = NaN degree
    }
    aggregate Leg5 {
      run in sequence
      break if ( leg <= 4 count )
    
      assign in sequence Lat[4] = NaN degree
      assign in sequence Lon[4] = NaN degree
    }