================================================================================
listings2/listing3
================================================================================

// subscript, this, super, dynamic binding

import "pkl:test"
import "pkl:math"

local x = new Listing {
  when (false) { 0 }
  "one"
  "two"
  "th" + "ree"
}

res1 = x[0]
res2 = x[1]
res3 = x[2]
res4 = test.catch(() -> x[-1])
res5 = test.catch(() -> x[3])
res6 = test.catch(() -> x[math.minInt])
res7 = test.catch(() -> x[math.maxInt])

local y = new Listing {
  when (false) { 0 }
  new Listing {
    when (false) { 0 }
    "one"
  }
 new Listing {
    when (false) { 0 }
    "two"
    "th" + "ree"
  }
  new Mapping {
    when (false) { 0 }
    ["four"] = 4
  }
}

res8 = y[0][0]
res9 = y[1][0]
res10 = y[1][1]
res11 = y[2]["four"]
res12 = test.catch(() -> y[3][0])
res13 = test.catch(() -> y[1][2])
res14 = test.catch(() -> y[2]["other"])

res15 = new Listing {
  when (false) { 0 }
  "one"
  this[0]
  "two"
  this[2]
  this[1]
}

res16 = (res15) {
  when (false) { 0 }
  "three"
  this[0]
  this[5]
}

res17 = new Listing {
  when (false) { 0 }
  "item"
  this[0] + "2"
}

res18 = (res17) {
  when (false) { 0 }
  [0] = "element"
  this[0] + "3"
}

res19 = (res18) {
  when (false) { 0 }
  [0] = "object"
  this[0] + "4"
}

res20 = (x) {
  when (false) { 0 }
  [0] = super[0] + "2"
  [1] = super[0] + "3"
  super[0] + "4"
  this[0] + "4"
}

local err0 = new Listing {
  when (false) { 0 }
  "foo"
  this[3]
}
res21 = test.catch(() -> err0[1])

// currently, referencing a non-existing super element returns dynamic object
res22 = new Listing {
  when (false) { 0 }
  super[0]
  super[3]
}

--------------------------------------------------------------------------------

(module
  (lineComment)
  (importClause
    (stringConstant))
  (importClause
    (stringConstant))
  (classProperty
    (modifier)
    (identifier)
    (newExpr
      (type
        (qualifiedIdentifier
          (identifier)))
      (objectBody
        (whenGenerator
          (falseLiteral)
          (objectBody
            (objectElement
              (intLiteral))))
        (objectElement
          (slStringLiteral))
        (objectElement
          (slStringLiteral))
        (objectElement
          (binaryExpr
            (slStringLiteral)
            (slStringLiteral))))))
  (classProperty
    (identifier)
    (subscriptExpr
      (variableExpr
        (identifier))
      (intLiteral)))
  (classProperty
    (identifier)
    (subscriptExpr
      (variableExpr
        (identifier))
      (intLiteral)))
  (classProperty
    (identifier)
    (subscriptExpr
      (variableExpr
        (identifier))
      (intLiteral)))
  (classProperty
    (identifier)
    (methodCallExpr
      (variableExpr
        (identifier))
      (identifier)
      (argumentList
        (functionLiteral
          (parameterList)
          (subscriptExpr
            (variableExpr
              (identifier))
            (unaryExpr
              (intLiteral)))))))
  (classProperty
    (identifier)
    (methodCallExpr
      (variableExpr
        (identifier))
      (identifier)
      (argumentList
        (functionLiteral
          (parameterList)
          (subscriptExpr
            (variableExpr
              (identifier))
            (intLiteral))))))
  (classProperty
    (identifier)
    (methodCallExpr
      (variableExpr
        (identifier))
      (identifier)
      (argumentList
        (functionLiteral
          (parameterList)
          (subscriptExpr
            (variableExpr
              (identifier))
            (propertyCallExpr
              (variableExpr
                (identifier))
              (identifier)))))))
  (classProperty
    (identifier)
    (methodCallExpr
      (variableExpr
        (identifier))
      (identifier)
      (argumentList
        (functionLiteral
          (parameterList)
          (subscriptExpr
            (variableExpr
              (identifier))
            (propertyCallExpr
              (variableExpr
                (identifier))
              (identifier)))))))
  (classProperty
    (modifier)
    (identifier)
    (newExpr
      (type
        (qualifiedIdentifier
          (identifier)))
      (objectBody
        (whenGenerator
          (falseLiteral)
          (objectBody
            (objectElement
              (intLiteral))))
        (objectElement
          (newExpr
            (type
              (qualifiedIdentifier
                (identifier)))
            (objectBody
              (whenGenerator
                (falseLiteral)
                (objectBody
                  (objectElement
                    (intLiteral))))
              (objectElement
                (slStringLiteral)))))
        (objectElement
          (newExpr
            (type
              (qualifiedIdentifier
                (identifier)))
            (objectBody
              (whenGenerator
                (falseLiteral)
                (objectBody
                  (objectElement
                    (intLiteral))))
              (objectElement
                (slStringLiteral))
              (objectElement
                (binaryExpr
                  (slStringLiteral)
                  (slStringLiteral))))))
        (objectElement
          (newExpr
            (type
              (qualifiedIdentifier
                (identifier)))
            (objectBody
              (whenGenerator
                (falseLiteral)
                (objectBody
                  (objectElement
                    (intLiteral))))
              (objectEntry
                (slStringLiteral)
                (intLiteral))))))))
  (classProperty
    (identifier)
    (subscriptExpr
      (subscriptExpr
        (variableExpr
          (identifier))
        (intLiteral))
      (intLiteral)))
  (classProperty
    (identifier)
    (subscriptExpr
      (subscriptExpr
        (variableExpr
          (identifier))
        (intLiteral))
      (intLiteral)))
  (classProperty
    (identifier)
    (subscriptExpr
      (subscriptExpr
        (variableExpr
          (identifier))
        (intLiteral))
      (intLiteral)))
  (classProperty
    (identifier)
    (subscriptExpr
      (subscriptExpr
        (variableExpr
          (identifier))
        (intLiteral))
      (slStringLiteral)))
  (classProperty
    (identifier)
    (methodCallExpr
      (variableExpr
        (identifier))
      (identifier)
      (argumentList
        (functionLiteral
          (parameterList)
          (subscriptExpr
            (subscriptExpr
              (variableExpr
                (identifier))
              (intLiteral))
            (intLiteral))))))
  (classProperty
    (identifier)
    (methodCallExpr
      (variableExpr
        (identifier))
      (identifier)
      (argumentList
        (functionLiteral
          (parameterList)
          (subscriptExpr
            (subscriptExpr
              (variableExpr
                (identifier))
              (intLiteral))
            (intLiteral))))))
  (classProperty
    (identifier)
    (methodCallExpr
      (variableExpr
        (identifier))
      (identifier)
      (argumentList
        (functionLiteral
          (parameterList)
          (subscriptExpr
            (subscriptExpr
              (variableExpr
                (identifier))
              (intLiteral))
            (slStringLiteral))))))
  (classProperty
    (identifier)
    (newExpr
      (type
        (qualifiedIdentifier
          (identifier)))
      (objectBody
        (whenGenerator
          (falseLiteral)
          (objectBody
            (objectElement
              (intLiteral))))
        (objectElement
          (slStringLiteral))
        (objectElement
          (subscriptExpr
            (thisExpr)
            (intLiteral)))
        (objectElement
          (slStringLiteral))
        (objectElement
          (subscriptExpr
            (thisExpr)
            (intLiteral)))
        (objectElement
          (subscriptExpr
            (thisExpr)
            (intLiteral))))))
  (classProperty
    (identifier)
    (objectLiteral
      (parenthesizedExpr
        (variableExpr
          (identifier)))
      (objectBody
        (whenGenerator
          (falseLiteral)
          (objectBody
            (objectElement
              (intLiteral))))
        (objectElement
          (slStringLiteral))
        (objectElement
          (subscriptExpr
            (thisExpr)
            (intLiteral)))
        (objectElement
          (subscriptExpr
            (thisExpr)
            (intLiteral))))))
  (classProperty
    (identifier)
    (newExpr
      (type
        (qualifiedIdentifier
          (identifier)))
      (objectBody
        (whenGenerator
          (falseLiteral)
          (objectBody
            (objectElement
              (intLiteral))))
        (objectElement
          (slStringLiteral))
        (objectElement
          (binaryExpr
            (subscriptExpr
              (thisExpr)
              (intLiteral))
            (slStringLiteral))))))
  (classProperty
    (identifier)
    (objectLiteral
      (parenthesizedExpr
        (variableExpr
          (identifier)))
      (objectBody
        (whenGenerator
          (falseLiteral)
          (objectBody
            (objectElement
              (intLiteral))))
        (objectEntry
          (intLiteral)
          (slStringLiteral))
        (objectElement
          (binaryExpr
            (subscriptExpr
              (thisExpr)
              (intLiteral))
            (slStringLiteral))))))
  (classProperty
    (identifier)
    (objectLiteral
      (parenthesizedExpr
        (variableExpr
          (identifier)))
      (objectBody
        (whenGenerator
          (falseLiteral)
          (objectBody
            (objectElement
              (intLiteral))))
        (objectEntry
          (intLiteral)
          (slStringLiteral))
        (objectElement
          (binaryExpr
            (subscriptExpr
              (thisExpr)
              (intLiteral))
            (slStringLiteral))))))
  (classProperty
    (identifier)
    (objectLiteral
      (parenthesizedExpr
        (variableExpr
          (identifier)))
      (objectBody
        (whenGenerator
          (falseLiteral)
          (objectBody
            (objectElement
              (intLiteral))))
        (objectEntry
          (intLiteral)
          (binaryExpr
            (subscriptExpr
              (intLiteral))
            (slStringLiteral)))
        (objectEntry
          (intLiteral)
          (binaryExpr
            (subscriptExpr
              (intLiteral))
            (slStringLiteral)))
        (objectElement
          (binaryExpr
            (subscriptExpr
              (intLiteral))
            (slStringLiteral)))
        (objectElement
          (binaryExpr
            (subscriptExpr
              (thisExpr)
              (intLiteral))
            (slStringLiteral))))))
  (classProperty
    (modifier)
    (identifier)
    (newExpr
      (type
        (qualifiedIdentifier
          (identifier)))
      (objectBody
        (whenGenerator
          (falseLiteral)
          (objectBody
            (objectElement
              (intLiteral))))
        (objectElement
          (slStringLiteral))
        (objectElement
          (subscriptExpr
            (thisExpr)
            (intLiteral))))))
  (classProperty
    (identifier)
    (methodCallExpr
      (variableExpr
        (identifier))
      (identifier)
      (argumentList
        (functionLiteral
          (parameterList)
          (subscriptExpr
            (variableExpr
              (identifier))
            (intLiteral))))))
  (lineComment)
  (classProperty
    (identifier)
    (newExpr
      (type
        (qualifiedIdentifier
          (identifier)))
      (objectBody
        (whenGenerator
          (falseLiteral)
          (objectBody
            (objectElement
              (intLiteral))))
        (objectElement
          (subscriptExpr
            (intLiteral)))
        (objectElement
          (subscriptExpr
            (intLiteral)))))))
