program main
  enum Reading
    variant Missing
    variant Value i32
    variant Offset i32
  enum WideReading
    variant Missing
    variant Value i64
  enum RatioReading
    variant Missing
    variant Value f64
  enum FlagReading
    variant Missing
    variant Value bool
  enum LabelReading
    variant Missing
    variant Value string
  struct PayloadRecord
    field reading: Reading
    field wide: WideReading
    field ratio: RatioReading
    field flag: FlagReading
    field label: LabelReading
  fn value(payload: i32) -> Reading
    enum-variant Reading.Value
      var payload
  fn echo_reading(reading: Reading) -> Reading
    var reading
  fn local_reading(payload: i32) -> Reading
    local let reading: Reading
      enum-variant Reading.Value
        var payload
    var reading
  fn call_reading(payload: i32) -> Reading
    call echo_reading
      call local_reading
        var payload
  fn reading_code(reading: Reading) -> i32
    match
      subject
        var reading
      arm Reading.Missing
        int 0
      arm Reading.Value payload
        var payload
      arm Reading.Offset payload
        binary +
          var payload
          int 100
  fn wide_value(payload: i64) -> WideReading
    enum-variant WideReading.Value
      var payload
  fn echo_wide(reading: WideReading) -> WideReading
    var reading
  fn local_wide(payload: i64) -> WideReading
    local let reading: WideReading
      enum-variant WideReading.Value
        var payload
    var reading
  fn call_wide(payload: i64) -> WideReading
    call echo_wide
      call local_wide
        var payload
  fn wide_code(reading: WideReading) -> i64
    match
      subject
        var reading
      arm WideReading.Missing
        i64 0
      arm WideReading.Value payload
        var payload
  fn ratio_value(payload: f64) -> RatioReading
    enum-variant RatioReading.Value
      var payload
  fn ratio_code(reading: RatioReading) -> f64
    match
      subject
        var reading
      arm RatioReading.Missing
        float 0
      arm RatioReading.Value payload
        var payload
  fn flag_value(payload: bool) -> FlagReading
    enum-variant FlagReading.Value
      var payload
  fn flag_code(reading: FlagReading) -> bool
    match
      subject
        var reading
      arm FlagReading.Missing
        bool false
      arm FlagReading.Value payload
        var payload
  fn label_value(payload: string) -> LabelReading
    enum-variant LabelReading.Value
      var payload
  fn echo_label(reading: LabelReading) -> LabelReading
    var reading
  fn local_label(payload: string) -> LabelReading
    local let reading: LabelReading
      enum-variant LabelReading.Value
        var payload
    var reading
  fn call_label(payload: string) -> LabelReading
    call echo_label
      call local_label
        var payload
  fn label_text(reading: LabelReading) -> string
    match
      subject
        var reading
      arm LabelReading.Missing
        string ""
      arm LabelReading.Value payload
        var payload
  fn pack_record(reading: Reading, wide: WideReading, ratio: RatioReading, flag: FlagReading, label: LabelReading) -> PayloadRecord
    construct PayloadRecord
      field reading
        var reading
      field wide
        var wide
      field ratio
        var ratio
      field flag
        var flag
      field label
        var label
  fn make_record() -> PayloadRecord
    call pack_record
      enum-variant Reading.Offset
        int 5
      enum-variant WideReading.Value
        i64 4294967296
      enum-variant RatioReading.Value
        float 3.5
      enum-variant FlagReading.Value
        bool true
      enum-variant LabelReading.Value
        string "hello"
  fn echo_record(record: PayloadRecord) -> PayloadRecord
    var record
  fn local_record() -> PayloadRecord
    local let record: PayloadRecord
      call make_record
    call echo_record
      var record
  fn record_reading_code(record: PayloadRecord) -> i32
    call reading_code
      field-access reading
        var record
  fn record_wide_code(record: PayloadRecord) -> i64
    call wide_code
      field-access wide
        var record
  fn record_ratio_code(record: PayloadRecord) -> f64
    call ratio_code
      field-access ratio
        var record
  fn record_flag_code(record: PayloadRecord) -> bool
    call flag_code
      field-access flag
        var record
  fn record_label_text(record: PayloadRecord) -> string
    call label_text
      field-access label
        var record
  fn main() -> i32
    call record_reading_code
      call local_record
  test "i32 enum constructor equality"
    binary =
      enum-variant Reading.Value
        int 7
      call value
        int 7
  test "i32 enum equality compares payload"
    if
      binary =
        enum-variant Reading.Value
          int 7
        enum-variant Reading.Value
          int 8
      bool false
      bool true
  test "i32 enum payloadless equality"
    binary =
      enum-variant Reading.Missing
      call echo_reading
        enum-variant Reading.Missing
  test "i32 enum local return call flow"
    binary =
      call call_reading
        int 9
      enum-variant Reading.Value
        int 9
  test "i64 enum constructor equality"
    binary =
      enum-variant WideReading.Value
        i64 4294967296
      call wide_value
        i64 4294967296
  test "i64 enum local return call flow"
    binary =
      call call_wide
        i64 4294967297
      enum-variant WideReading.Value
        i64 4294967297
  test "f64 enum constructor equality"
    binary =
      enum-variant RatioReading.Value
        float 3.5
      call ratio_value
        float 3.5
  test "f64 enum equality compares payload"
    if
      binary =
        enum-variant RatioReading.Value
          float 3.5
        enum-variant RatioReading.Value
          float 4.5
      bool false
      bool true
  test "bool enum constructor equality"
    binary =
      enum-variant FlagReading.Value
        bool true
      call flag_value
        bool true
  test "bool enum match value"
    call flag_code
      enum-variant FlagReading.Value
        bool true
  test "string enum constructor equality"
    binary =
      enum-variant LabelReading.Value
        string "hello"
      call label_value
        string "hello"
  test "string enum equality compares payload"
    if
      binary =
        enum-variant LabelReading.Value
          string "left"
        enum-variant LabelReading.Value
          string "right"
      bool false
      bool true
  test "string enum local return call flow"
    binary =
      call call_label
        string "hello"
      enum-variant LabelReading.Value
        string "hello"
  test "struct field enum i32 flow"
    binary =
      call record_reading_code
        call local_record
      int 105
  test "struct field enum i64 flow"
    binary =
      call record_wide_code
        call local_record
      i64 4294967296
  test "struct field enum f64 flow"
    binary =
      call record_ratio_code
        call local_record
      float 3.5
  test "struct field enum bool flow"
    call record_flag_code
      call local_record
  test "struct field enum string flow"
    binary =
      call record_label_text
        call local_record
      string "hello"
