program main
  enum PacketState
    variant Missing
    variant Live Packet
    variant Cached Packet
  struct Packet
    field values: (array i32 3)
    field labels: (array string 2)
    field flags: (array bool 2)
  fn make_packet(base: i32, head: string) -> Packet
    construct Packet
      field values
        array i32
          var base
          binary +
            var base
            int 1
          binary +
            var base
            int 2
      field labels
        array string
          var head
          string "tail"
      field flags
        array bool
          bool false
          bool true
  fn live(packet: Packet) -> PacketState
    enum-variant PacketState.Live
      var packet
  fn cached(packet: Packet) -> PacketState
    enum-variant PacketState.Cached
      var packet
  fn echo_state(state: PacketState) -> PacketState
    var state
  fn local_state(base: i32, head: string) -> PacketState
    local let state: PacketState
      enum-variant PacketState.Live
        call make_packet
          var base
          var head
    var state
  fn call_state(base: i32, head: string) -> PacketState
    call echo_state
      call cached
        call make_packet
          var base
          var head
  fn state_value(state: PacketState, i: i32) -> i32
    match
      subject
        var state
      arm PacketState.Missing
        int 0
      arm PacketState.Live payload
        index
          field-access values
            var payload
          var i
      arm PacketState.Cached payload
        index
          field-access values
            var payload
          var i
  fn state_label(state: PacketState, i: i32) -> string
    match
      subject
        var state
      arm PacketState.Missing
        string ""
      arm PacketState.Live payload
        index
          field-access labels
            var payload
          var i
      arm PacketState.Cached payload
        index
          field-access labels
            var payload
          var i
  fn state_flag(state: PacketState, i: i32) -> bool
    match
      subject
        var state
      arm PacketState.Missing
        bool false
      arm PacketState.Live payload
        index
          field-access flags
            var payload
          var i
      arm PacketState.Cached payload
        index
          field-access flags
            var payload
          var i
  fn main() -> i32
    call state_value
      call call_state
        int 7
        string "sun"
      int 2
  test "struct payload missing arm"
    binary =
      call state_value
        enum-variant PacketState.Missing
        int 0
      int 0
  test "struct payload live constructor flow"
    binary =
      call state_value
        call live
          call make_packet
            int 7
            string "sun"
        int 2
      int 9
  test "struct payload cached param return call flow"
    binary =
      call state_value
        call call_state
          int 7
          string "sun"
        int 1
      int 8
  test "struct payload string array field access"
    binary =
      call state_label
        call call_state
          int 7
          string "sun"
        int 0
      string "sun"
  test "struct payload bool array field access"
    call state_flag
      call call_state
        int 7
        string "sun"
      int 1
  test "struct payload local return flow"
    binary =
      call state_label
        call local_state
          int 9
          string "orb"
        int 1
      string "tail"
