;; Go parser tests.

(define go-parse-test-count 0)
(define go-parse-test-pass 0)
(define go-parse-test-fails (list))

(define
  go-parse-test
  (fn
    (name actual expected)
    (set! go-parse-test-count (+ go-parse-test-count 1))
    (if
      (= actual expected)
      (set! go-parse-test-pass (+ go-parse-test-pass 1))
      (append! go-parse-test-fails {:name name :expected expected :actual actual}))))

;; ── primary: literals ─────────────────────────────────────────────
(go-parse-test "int literal" (go-parse "42") (ast-literal "42"))
(go-parse-test "zero literal" (go-parse "0") (ast-literal "0"))
(go-parse-test "hex literal" (go-parse "0xFF") (ast-literal "0xFF"))
(go-parse-test "float literal" (go-parse "3.14") (ast-literal "3.14"))
(go-parse-test "leading-dot float" (go-parse ".5") (ast-literal ".5"))
(go-parse-test "exponent float" (go-parse "1e10") (ast-literal "1e10"))
(go-parse-test "imag literal" (go-parse "2i") (ast-literal "2i"))
(go-parse-test "string literal" (go-parse "\"hi\"") (list :literal-string "hi"))
(go-parse-test "empty string" (go-parse "\"\"") (list :literal-string ""))
(go-parse-test "raw string" (go-parse "`a\nb`") (list :literal-string "a\nb"))
(go-parse-test "rune literal" (go-parse "'a'") (list :literal-string "a"))

;; ── primary: identifiers ──────────────────────────────────────────
(go-parse-test "ident: simple" (go-parse "x") (ast-var "x"))
(go-parse-test "ident: underscore" (go-parse "_foo") (ast-var "_foo"))
(go-parse-test "ident: mixed case" (go-parse "fooBar") (ast-var "fooBar"))
(go-parse-test "ident: with digit" (go-parse "x123") (ast-var "x123"))

;; ── primary: non-primary returns nil ──────────────────────────────
(go-parse-test
  "bin: a + b"
  (go-parse "a + b")
  (ast-app (ast-var "+") (list (ast-var "a") (ast-var "b"))))
(go-parse-test
  "bin: int + int"
  (go-parse "1 + 2")
  (ast-app (ast-var "+") (list (ast-literal "1") (ast-literal "2"))))

;; ── report ────────────────────────────────────────────────────────
(go-parse-test
  "bin: left-assoc a + b + c"
  (go-parse "a + b + c")
  (ast-app
    (ast-var "+")
    (list
      (ast-app (ast-var "+") (list (ast-var "a") (ast-var "b")))
      (ast-var "c"))))

(go-parse-test
  "bin: * tighter than + → a + b * c"
  (go-parse "a + b * c")
  (ast-app
    (ast-var "+")
    (list
      (ast-var "a")
      (ast-app (ast-var "*") (list (ast-var "b") (ast-var "c"))))))

(go-parse-test
  "bin: * tighter than + → a * b + c"
  (go-parse "a * b + c")
  (ast-app
    (ast-var "+")
    (list
      (ast-app (ast-var "*") (list (ast-var "a") (ast-var "b")))
      (ast-var "c"))))

(go-parse-test
  "bin: && tighter than || → a || b && c"
  (go-parse "a || b && c")
  (ast-app
    (ast-var "||")
    (list
      (ast-var "a")
      (ast-app (ast-var "&&") (list (ast-var "b") (ast-var "c"))))))

(go-parse-test
  "bin: comparison tighter than &&"
  (go-parse "a == b && c < d")
  (ast-app
    (ast-var "&&")
    (list
      (ast-app (ast-var "==") (list (ast-var "a") (ast-var "b")))
      (ast-app (ast-var "<") (list (ast-var "c") (ast-var "d"))))))

(go-parse-test
  "bin: long left-assoc chain a + b - c + d"
  (go-parse "a + b - c + d")
  (ast-app
    (ast-var "+")
    (list
      (ast-app
        (ast-var "-")
        (list
          (ast-app (ast-var "+") (list (ast-var "a") (ast-var "b")))
          (ast-var "c")))
      (ast-var "d"))))

(go-parse-test
  "bin: equal-prec left-assoc — a | b ^ c → (a | b) ^ c"
  (go-parse "a | b ^ c")
  (ast-app
    (ast-var "^")
    (list
      (ast-app (ast-var "|") (list (ast-var "a") (ast-var "b")))
      (ast-var "c"))))

(go-parse-test
  "unary: -a"
  (go-parse "-a")
  (ast-app (ast-var "-") (list (ast-var "a"))))

(go-parse-test
  "unary: +a"
  (go-parse "+a")
  (ast-app (ast-var "+") (list (ast-var "a"))))

(go-parse-test
  "unary: !x"
  (go-parse "!x")
  (ast-app (ast-var "!") (list (ast-var "x"))))

(go-parse-test
  "unary: ^x (bitwise NOT)"
  (go-parse "^x")
  (ast-app (ast-var "^") (list (ast-var "x"))))

(go-parse-test
  "unary: *p (pointer deref)"
  (go-parse "*p")
  (ast-app (ast-var "*") (list (ast-var "p"))))

(go-parse-test
  "unary: &v (address-of)"
  (go-parse "&v")
  (ast-app (ast-var "&") (list (ast-var "v"))))

(go-parse-test
  "unary: <-ch (channel recv)"
  (go-parse "<-ch")
  (ast-app (ast-var "<-") (list (ast-var "ch"))))

(go-parse-test
  "unary: -1 (on literal)"
  (go-parse "-1")
  (ast-app (ast-var "-") (list (ast-literal "1"))))

(go-parse-test
  "unary: !!x (chained, right-recursive)"
  (go-parse "!!x")
  (ast-app
    (ast-var "!")
    (list (ast-app (ast-var "!") (list (ast-var "x"))))))

(go-parse-test
  "unary: -a + b → ((-a) + b) — unary tighter than binary"
  (go-parse "-a + b")
  (ast-app
    (ast-var "+")
    (list (ast-app (ast-var "-") (list (ast-var "a"))) (ast-var "b"))))

(go-parse-test
  "unary: a + -b → unary applies to RHS"
  (go-parse "a + -b")
  (ast-app
    (ast-var "+")
    (list (ast-var "a") (ast-app (ast-var "-") (list (ast-var "b"))))))

(go-parse-test
  "call: f() (no args)"
  (go-parse "f()")
  (ast-app (ast-var "f") (list)))

(go-parse-test
  "call: f(x)"
  (go-parse "f(x)")
  (ast-app (ast-var "f") (list (ast-var "x"))))

(go-parse-test
  "call: f(x, y, z)"
  (go-parse "f(x, y, z)")
  (ast-app (ast-var "f") (list (ast-var "x") (ast-var "y") (ast-var "z"))))

(go-parse-test
  "call: f(1, 2)"
  (go-parse "f(1, 2)")
  (ast-app (ast-var "f") (list (ast-literal "1") (ast-literal "2"))))

(go-parse-test
  "call: f(a + b) — arg can be a binary expr"
  (go-parse "f(a + b)")
  (ast-app
    (ast-var "f")
    (list (ast-app (ast-var "+") (list (ast-var "a") (ast-var "b"))))))

(go-parse-test
  "call: f(g(x)) — nested"
  (go-parse "f(g(x))")
  (ast-app
    (ast-var "f")
    (list (ast-app (ast-var "g") (list (ast-var "x"))))))

(go-parse-test
  "select: x.y"
  (go-parse "x.y")
  (list :select (ast-var "x") "y"))

(go-parse-test
  "select: x.y.z (chained left-assoc)"
  (go-parse "x.y.z")
  (list :select (list :select (ast-var "x") "y") "z"))

(go-parse-test
  "method: obj.method()"
  (go-parse "obj.method()")
  (ast-app (list :select (ast-var "obj") "method") (list)))

(go-parse-test
  "method: obj.method(x, y)"
  (go-parse "obj.method(x, y)")
  (ast-app
    (list :select (ast-var "obj") "method")
    (list (ast-var "x") (ast-var "y"))))

(go-parse-test
  "postfix: -f(x) → unary applies after call"
  (go-parse "-f(x)")
  (ast-app
    (ast-var "-")
    (list (ast-app (ast-var "f") (list (ast-var "x"))))))

(go-parse-test
  "postfix: f(x) + 1 → call binds tighter than binary +"
  (go-parse "f(x) + 1")
  (ast-app
    (ast-var "+")
    (list (ast-app (ast-var "f") (list (ast-var "x"))) (ast-literal "1"))))

(go-parse-test
  "index: a[0]"
  (go-parse "a[0]")
  (list :index (ast-var "a") (ast-literal "0")))

(go-parse-test
  "index: a[i]"
  (go-parse "a[i]")
  (list :index (ast-var "a") (ast-var "i")))

(go-parse-test
  "index: a[i + 1] (expr index)"
  (go-parse "a[i + 1]")
  (list
    :index (ast-var "a")
    (ast-app (ast-var "+") (list (ast-var "i") (ast-literal "1")))))

(go-parse-test
  "index: m[\"key\"] (string index)"
  (go-parse "m[\"key\"]")
  (list :index (ast-var "m") (list :literal-string "key")))

(go-parse-test
  "index: a[0][1] (chained)"
  (go-parse "a[0][1]")
  (list
    :index (list :index (ast-var "a") (ast-literal "0"))
    (ast-literal "1")))

(go-parse-test
  "index: a[0].field (mixed with selector)"
  (go-parse "a[0].field")
  (list :select (list :index (ast-var "a") (ast-literal "0")) "field"))

(go-parse-test
  "slice: a[:]"
  (go-parse "a[:]")
  (list :slice (ast-var "a") nil nil nil))

(go-parse-test
  "slice: a[1:]"
  (go-parse "a[1:]")
  (list :slice (ast-var "a") (ast-literal "1") nil nil))

(go-parse-test
  "slice: a[:2]"
  (go-parse "a[:2]")
  (list :slice (ast-var "a") nil (ast-literal "2") nil))

(go-parse-test
  "slice: a[1:2]"
  (go-parse "a[1:2]")
  (list :slice (ast-var "a") (ast-literal "1") (ast-literal "2") nil))

(go-parse-test
  "slice: a[1:2:3] (full slice)"
  (go-parse "a[1:2:3]")
  (list
    :slice (ast-var "a")
    (ast-literal "1")
    (ast-literal "2")
    (ast-literal "3")))

(go-parse-test
  "slice: a[i:j] (var bounds)"
  (go-parse "a[i:j]")
  (list :slice (ast-var "a") (ast-var "i") (ast-var "j") nil))

(go-parse-test
  "assert: v.(int)"
  (go-parse "v.(int)")
  (list :assert (ast-var "v") (list :ty-name "int")))

(go-parse-test
  "assert: v.(string)"
  (go-parse "v.(string)")
  (list :assert (ast-var "v") (list :ty-name "string")))

(go-parse-test
  "assert: v.(MyType) (user-defined)"
  (go-parse "v.(MyType)")
  (list :assert (ast-var "v") (list :ty-name "MyType")))

(go-parse-test
  "assert: v.(*T) (pointer type)"
  (go-parse "v.(*T)")
  (list :assert (ast-var "v") (list :ty-ptr (list :ty-name "T"))))

(go-parse-test
  "assert: v.(**T) (pointer-to-pointer)"
  (go-parse "v.(**T)")
  (list
    :assert (ast-var "v")
    (list :ty-ptr (list :ty-ptr (list :ty-name "T")))))

(go-parse-test
  "assert: v.(pkg.T) (qualified type)"
  (go-parse "v.(pkg.T)")
  (list :assert (ast-var "v") (list :ty-sel "pkg" "T")))

(go-parse-test
  "assert: f().(int) (on call result)"
  (go-parse "f().(int)")
  (list :assert (ast-app (ast-var "f") (list)) (list :ty-name "int")))

(go-parse-test
  "assert: obj.field.(int) (after selector)"
  (go-parse "obj.field.(int)")
  (list
    :assert (list :select (ast-var "obj") "field")
    (list :ty-name "int")))

(go-parse-test
  "assert: v.(int) + 1 (assert binds tighter than binary +)"
  (go-parse "v.(int) + 1")
  (ast-app
    (ast-var "+")
    (list
      (list :assert (ast-var "v") (list :ty-name "int"))
      (ast-literal "1"))))

(go-parse-test
  "ty: []int (slice)"
  (go-parse "v.([]int)")
  (list :assert (ast-var "v") (list :ty-slice (list :ty-name "int"))))

(go-parse-test
  "ty: [10]int (array)"
  (go-parse "v.([10]int)")
  (list
    :assert (ast-var "v")
    (list :ty-array (ast-literal "10") (list :ty-name "int"))))

(go-parse-test
  "ty: map[string]int"
  (go-parse "v.(map[string]int)")
  (list
    :assert (ast-var "v")
    (list :ty-map (list :ty-name "string") (list :ty-name "int"))))

(go-parse-test
  "ty: chan int (bidirectional)"
  (go-parse "v.(chan int)")
  (list :assert (ast-var "v") (list :ty-chan :both (list :ty-name "int"))))

(go-parse-test
  "ty: chan<- int (send-only)"
  (go-parse "v.(chan<- int)")
  (list :assert (ast-var "v") (list :ty-chan :send (list :ty-name "int"))))

(go-parse-test
  "ty: <-chan int (recv-only)"
  (go-parse "v.(<-chan int)")
  (list :assert (ast-var "v") (list :ty-chan :recv (list :ty-name "int"))))

(go-parse-test
  "ty: []*T (slice of pointers)"
  (go-parse "v.([]*T)")
  (list
    :assert (ast-var "v")
    (list :ty-slice (list :ty-ptr (list :ty-name "T")))))

(go-parse-test
  "ty: [][]int (slice of slice)"
  (go-parse "v.([][]int)")
  (list
    :assert (ast-var "v")
    (list :ty-slice (list :ty-slice (list :ty-name "int")))))

(go-parse-test
  "ty: map[string][]int (map with slice value)"
  (go-parse "v.(map[string][]int)")
  (list
    :assert (ast-var "v")
    (list
      :ty-map (list :ty-name "string")
      (list :ty-slice (list :ty-name "int")))))

(go-parse-test
  "ty: chan map[K]V (chan of map type)"
  (go-parse "v.(chan map[K]V)")
  (list
    :assert (ast-var "v")
    (list
      :ty-chan :both
      (list :ty-map (list :ty-name "K") (list :ty-name "V")))))

(go-parse-test
  "ty: *[]int (pointer to slice)"
  (go-parse "v.(*[]int)")
  (list
    :assert (ast-var "v")
    (list :ty-ptr (list :ty-slice (list :ty-name "int")))))

(go-parse-test
  "ty: func() (no params, no return)"
  (go-parse "v.(func())")
  (list :assert (ast-var "v") (list :ty-func (list) (list))))

(go-parse-test
  "ty: func() int (no params, one return)"
  (go-parse "v.(func() int)")
  (list
    :assert (ast-var "v")
    (list :ty-func (list) (list (list :ty-name "int")))))

(go-parse-test
  "ty: func(int)"
  (go-parse "v.(func(int))")
  (list
    :assert (ast-var "v")
    (list :ty-func (list (list :ty-name "int")) (list))))

(go-parse-test
  "ty: func(int, string)"
  (go-parse "v.(func(int, string))")
  (list
    :assert (ast-var "v")
    (list
      :ty-func (list (list :ty-name "int") (list :ty-name "string"))
      (list))))

(go-parse-test
  "ty: func(int) string"
  (go-parse "v.(func(int) string)")
  (list
    :assert (ast-var "v")
    (list
      :ty-func (list (list :ty-name "int"))
      (list (list :ty-name "string")))))

(go-parse-test
  "ty: func() (int, error) (multi return)"
  (go-parse "v.(func() (int, error))")
  (list
    :assert (ast-var "v")
    (list
      :ty-func (list)
      (list (list :ty-name "int") (list :ty-name "error")))))

(go-parse-test
  "ty: func(*T) []int (pointer param, slice return)"
  (go-parse "v.(func(*T) []int)")
  (list
    :assert (ast-var "v")
    (list
      :ty-func (list (list :ty-ptr (list :ty-name "T")))
      (list (list :ty-slice (list :ty-name "int"))))))

(go-parse-test
  "ty: func() func() (nested func type as return)"
  (go-parse "v.(func() func())")
  (list
    :assert (ast-var "v")
    (list :ty-func (list) (list (list :ty-func (list) (list))))))

(go-parse-test
  "ty: chan func() int (chan of func type)"
  (go-parse "v.(chan func() int)")
  (list
    :assert (ast-var "v")
    (list
      :ty-chan :both
      (list :ty-func (list) (list (list :ty-name "int"))))))

(go-parse-test
  "ty: struct {} (empty)"
  (go-parse "v.(struct {})")
  (list :assert (ast-var "v") (list :ty-struct (list))))

(go-parse-test
  "ty: struct { x int }"
  (go-parse "v.(struct { x int })")
  (list
    :assert (ast-var "v")
    (list :ty-struct (list (list :field (list "x") (list :ty-name "int"))))))

(go-parse-test
  "ty: struct { x int; y string } (multiple fields)"
  (go-parse "v.(struct { x int; y string })")
  (list
    :assert (ast-var "v")
    (list
      :ty-struct (list
        (list :field (list "x") (list :ty-name "int"))
        (list :field (list "y") (list :ty-name "string"))))))

(go-parse-test
  "ty: struct { x, y int } (shared type)"
  (go-parse "v.(struct { x, y int })")
  (list
    :assert (ast-var "v")
    (list
      :ty-struct (list (list :field (list "x" "y") (list :ty-name "int"))))))

(go-parse-test
  "ty: struct { p *T } (pointer field)"
  (go-parse "v.(struct { p *T })")
  (list
    :assert (ast-var "v")
    (list
      :ty-struct (list (list :field (list "p") (list :ty-ptr (list :ty-name "T")))))))

(go-parse-test
  "ty: struct { items []int } (slice field)"
  (go-parse "v.(struct { items []int })")
  (list
    :assert (ast-var "v")
    (list
      :ty-struct (list
        (list :field (list "items") (list :ty-slice (list :ty-name "int")))))))

(go-parse-test
  "ty: struct { a int; b, c string; d *T } (mixed)"
  (go-parse "v.(struct { a int; b, c string; d *T })")
  (list
    :assert (ast-var "v")
    (list
      :ty-struct (list
        (list :field (list "a") (list :ty-name "int"))
        (list :field (list "b" "c") (list :ty-name "string"))
        (list :field (list "d") (list :ty-ptr (list :ty-name "T")))))))

(go-parse-test
  "ty: nested struct { inner struct { x int } }"
  (go-parse "v.(struct { inner struct { x int } })")
  (list
    :assert (ast-var "v")
    (list
      :ty-struct (list
        (list
          :field (list "inner")
          (list
            :ty-struct (list (list :field (list "x") (list :ty-name "int")))))))))

(go-parse-test
  "ty: interface {} (empty)"
  (go-parse "v.(interface {})")
  (list :assert (ast-var "v") (list :ty-interface (list))))

(go-parse-test
  "ty: interface { Close() } (single method, no params, no return)"
  (go-parse "v.(interface { Close() })")
  (list
    :assert (ast-var "v")
    (list :ty-interface (list (list :method "Close" (list) (list))))))

(go-parse-test
  "ty: interface { String() string } (single return)"
  (go-parse "v.(interface { String() string })")
  (list
    :assert (ast-var "v")
    (list
      :ty-interface (list (list :method "String" (list) (list (list :ty-name "string")))))))

(go-parse-test
  "ty: interface { Read([]byte) (int, error) } (multi return)"
  (go-parse "v.(interface { Read([]byte) (int, error) })")
  (list
    :assert (ast-var "v")
    (list
      :ty-interface (list
        (list
          :method "Read"
          (list (list :ty-slice (list :ty-name "byte")))
          (list (list :ty-name "int") (list :ty-name "error")))))))

(go-parse-test
  "ty: interface { Stringer } (embedded interface)"
  (go-parse "v.(interface { Stringer })")
  (list
    :assert (ast-var "v")
    (list :ty-interface (list (list :embed (list :ty-name "Stringer"))))))

(go-parse-test
  "ty: interface { io.Reader } (qualified embedded)"
  (go-parse "v.(interface { io.Reader })")
  (list
    :assert (ast-var "v")
    (list :ty-interface (list (list :embed (list :ty-sel "io" "Reader"))))))

(go-parse-test
  "ty: interface with embed + methods (io.ReadWriter style)"
  (go-parse "v.(interface { io.Reader; Close() error })")
  (list
    :assert (ast-var "v")
    (list
      :ty-interface (list
        (list :embed (list :ty-sel "io" "Reader"))
        (list :method "Close" (list) (list (list :ty-name "error")))))))

(go-parse-test
  "ty: interface with multiple methods"
  (go-parse "v.(interface { Read([]byte) int; Write([]byte) int; Close() })")
  (list
    :assert (ast-var "v")
    (list
      :ty-interface (list
        (list
          :method "Read"
          (list (list :ty-slice (list :ty-name "byte")))
          (list (list :ty-name "int")))
        (list
          :method "Write"
          (list (list :ty-slice (list :ty-name "byte")))
          (list (list :ty-name "int")))
        (list :method "Close" (list) (list))))))

(go-parse-test
  "comp: Point{} (empty)"
  (go-parse "Point{}")
  (list :composite (ast-var "Point") (list)))

(go-parse-test
  "comp: Point{1, 2} (positional)"
  (go-parse "Point{1, 2}")
  (list
    :composite (ast-var "Point")
    (list (ast-literal "1") (ast-literal "2"))))

(go-parse-test
  "comp: Point{X: 1, Y: 2} (keyed)"
  (go-parse "Point{X: 1, Y: 2}")
  (list
    :composite (ast-var "Point")
    (list
      (list :kv (ast-var "X") (ast-literal "1"))
      (list :kv (ast-var "Y") (ast-literal "2")))))

(go-parse-test
  "comp: []int{1, 2, 3} (slice literal)"
  (go-parse "[]int{1, 2, 3}")
  (list
    :composite (list :ty-slice (list :ty-name "int"))
    (list (ast-literal "1") (ast-literal "2") (ast-literal "3"))))

(go-parse-test
  "comp: [3]int{1, 2, 3} (array literal)"
  (go-parse "[3]int{1, 2, 3}")
  (list
    :composite (list :ty-array (ast-literal "3") (list :ty-name "int"))
    (list (ast-literal "1") (ast-literal "2") (ast-literal "3"))))

(go-parse-test
  "comp: map[string]int{\"a\": 1, \"b\": 2} (map literal)"
  (go-parse "map[string]int{\"a\": 1, \"b\": 2}")
  (list
    :composite (list :ty-map (list :ty-name "string") (list :ty-name "int"))
    (list
      (list :kv (list :literal-string "a") (ast-literal "1"))
      (list :kv (list :literal-string "b") (ast-literal "2")))))

(go-parse-test
  "comp: pkg.Point{1, 2} (qualified type)"
  (go-parse "pkg.Point{1, 2}")
  (list
    :composite (list :select (ast-var "pkg") "Point")
    (list (ast-literal "1") (ast-literal "2"))))

(go-parse-test
  "comp: nested — []Point{Point{1,2}, Point{3,4}}"
  (go-parse "[]Point{Point{1, 2}, Point{3, 4}}")
  (list
    :composite (list :ty-slice (list :ty-name "Point"))
    (list
      (list
        :composite (ast-var "Point")
        (list (ast-literal "1") (ast-literal "2")))
      (list
        :composite (ast-var "Point")
        (list (ast-literal "3") (ast-literal "4"))))))

(go-parse-test
  "decl: package main"
  (go-parse "package main")
  (list :package "main"))

(go-parse-test
  "decl: import \"fmt\""
  (go-parse "import \"fmt\"")
  (ast-import "fmt"))

(go-parse-test
  "decl: var x int (type only, no init)"
  (go-parse "var x int")
  (list :var-decl (list :field (list "x") (list :ty-name "int")) nil))

(go-parse-test
  "decl: var x = 5 (init only, type inferred)"
  (go-parse "var x = 5")
  (list :var-decl (list :field (list "x") nil) (list (ast-literal "5"))))

(go-parse-test
  "decl: var x int = 5 (both type and init)"
  (go-parse "var x int = 5")
  (list
    :var-decl (list :field (list "x") (list :ty-name "int"))
    (list (ast-literal "5"))))

(go-parse-test
  "decl: var x, y int = 1, 2 (multi-name shared type)"
  (go-parse "var x, y int = 1, 2")
  (list
    :var-decl (list :field (list "x" "y") (list :ty-name "int"))
    (list (ast-literal "1") (ast-literal "2"))))

(go-parse-test
  "decl: const Pi = 3.14"
  (go-parse "const Pi = 3.14")
  (list
    :const-decl (list :field (list "Pi") nil)
    (list (ast-literal "3.14"))))

(go-parse-test
  "decl: const C int = 42 (typed const)"
  (go-parse "const C int = 42")
  (list
    :const-decl (list :field (list "C") (list :ty-name "int"))
    (list (ast-literal "42"))))

(go-parse-test
  "decl: type T int (named type)"
  (go-parse "type T int")
  (list :type-decl "T" (list :ty-name "int")))

(go-parse-test
  "decl: type Point struct { x, y int }"
  (go-parse "type Point struct { x, y int }")
  (list
    :type-decl "Point"
    (list
      :ty-struct (list (list :field (list "x" "y") (list :ty-name "int"))))))

(go-parse-test
  "fdecl: func main() {}"
  (go-parse "func main() {}")
  (list :func-decl "main" (list) (list) (list :block (list))))

(go-parse-test
  "fdecl: func add(x, y int) int { return x + y }"
  (go-parse "func add(x, y int) int { return x + y }")
  (list
    :func-decl "add"
    (list (list :field (list "x" "y") (list :ty-name "int")))
    (list (list :ty-name "int"))
    (list :block
      (list
        (list :return
          (list
            (ast-app (ast-var "+") (list (ast-var "x") (ast-var "y")))))))))

(go-parse-test
  "fdecl: generic identity func with one type param [T any]"
  (go-parse "func Id[T any](x T) T { return x }")
  (list
    :func-decl "Id"
    (list (list :field (list "x") (list :ty-name "T")))
    (list (list :ty-name "T"))
    (list :block (list (list :return (list (list :var "x")))))
    (list (list :field (list "T") (list :ty-name "any")))))

(go-parse-test
  "fdecl: generic with two type params [T, U any]"
  (go-parse "func Map[T, U any](x T) U { return x }")
  (list
    :func-decl "Map"
    (list (list :field (list "x") (list :ty-name "T")))
    (list (list :ty-name "U"))
    (list :block (list (list :return (list (list :var "x")))))
    (list (list :field (list "T" "U") (list :ty-name "any")))))

(go-parse-test
  "fdecl: generic with multi-group type params"
  (go-parse "func F[T any, U comparable]() {}")
  (list
    :func-decl "F"
    (list)
    (list)
    (list :block (list))
    (list
      (list :field (list "T") (list :ty-name "any"))
      (list :field (list "U") (list :ty-name "comparable")))))

(go-parse-test
  "fdecl: func with multi-group params"
  (go-parse "func mix(x int, y string) {}")
  (list
    :func-decl "mix"
    (list
      (list :field (list "x") (list :ty-name "int"))
      (list :field (list "y") (list :ty-name "string")))
    (list)
    (list :block (list))))

(go-parse-test
  "fdecl: func with multi-return"
  (go-parse "func divmod(a, b int) (int, int) {}")
  (list
    :func-decl "divmod"
    (list (list :field (list "a" "b") (list :ty-name "int")))
    (list (list :ty-name "int") (list :ty-name "int"))
    (list :block (list))))

(go-parse-test
  "fdecl: func with no body (signature only)"
  (go-parse "func sig(x int) int")
  (list
    :func-decl "sig"
    (list (list :field (list "x") (list :ty-name "int")))
    (list (list :ty-name "int"))
    nil))

(go-parse-test
  "mdecl: method on pointer receiver"
  (go-parse "func (p *Point) String() string { return p.x }")
  (list
    :method-decl (list :field (list "p") (list :ty-ptr (list :ty-name "Point")))
    "String"
    (list)
    (list (list :ty-name "string"))
    (list
      :block (list (list :return (list (list :select (ast-var "p") "x")))))))

(go-parse-test
  "mdecl: method on value receiver"
  (go-parse "func (s Stack) Len() int { return 0 }")
  (list
    :method-decl (list :field (list "s") (list :ty-name "Stack"))
    "Len"
    (list)
    (list (list :ty-name "int"))
    (list :block (list (list :return (list (ast-literal "0")))))))

(go-parse-test
  "fdecl: body with return"
  (go-parse "func ret() { return 42 }")
  (list
    :func-decl "ret"
    (list)
    (list)
    (list :block (list (list :return (list (ast-literal "42")))))))

(go-parse-test
  "stmt: short-decl x := 5"
  (go-parse "x := 5")
  (list :short-decl (list (ast-var "x")) (list (ast-literal "5"))))

(go-parse-test
  "stmt: short-decl multi a, b := 1, 2"
  (go-parse "a, b := 1, 2")
  (list
    :short-decl (list (ast-var "a") (ast-var "b"))
    (list (ast-literal "1") (ast-literal "2"))))

(go-parse-test
  "stmt: assign x = 5"
  (go-parse "x = 5")
  (list :assign (list (ast-var "x")) (list (ast-literal "5"))))

(go-parse-test
  "stmt: compound assign x += 1"
  (go-parse "x += 1")
  (list :assign-op "+=" (list (ast-var "x")) (list (ast-literal "1"))))

(go-parse-test
  "stmt: return (no value)"
  (go-parse "return")
  (list :return (list)))

(go-parse-test
  "stmt: return x + y"
  (go-parse "return x + y")
  (list
    :return (list (ast-app (ast-var "+") (list (ast-var "x") (ast-var "y"))))))

(go-parse-test
  "stmt: return multi a, b"
  (go-parse "return a, b")
  (list :return (list (ast-var "a") (ast-var "b"))))

(go-parse-test
  "stmt: function body with multiple stmts"
  (go-parse "func f() { x := 1; y := 2; return x + y }")
  (list
    :func-decl "f"
    (list)
    (list)
    (list
      :block (list
        (list :short-decl (list (ast-var "x")) (list (ast-literal "1")))
        (list :short-decl (list (ast-var "y")) (list (ast-literal "2")))
        (list
          :return (list
            (ast-app (ast-var "+") (list (ast-var "x") (ast-var "y")))))))))

(go-parse-test
  "stmt: expression statement (just a call)"
  (go-parse "func g() { f(x) }")
  (list
    :func-decl "g"
    (list)
    (list)
    (list :block (list (ast-app (ast-var "f") (list (ast-var "x")))))))

(go-parse-test
  "if: if x { }"
  (go-parse "if x { }")
  (list :if (ast-var "x") (list :block (list)) nil))

(go-parse-test
  "if: if cond { body } else { body }"
  (go-parse "if x { y := 1 } else { z := 2 }")
  (list
    :if (ast-var "x")
    (list
      :block (list
        (list :short-decl (list (ast-var "y")) (list (ast-literal "1")))))
    (list
      :block (list
        (list :short-decl (list (ast-var "z")) (list (ast-literal "2")))))))

(go-parse-test
  "if: chained else-if"
  (go-parse "if a { } else if b { } else { }")
  (list
    :if (ast-var "a")
    (list :block (list))
    (list :if (ast-var "b") (list :block (list)) (list :block (list)))))

(go-parse-test
  "if: comparison condition"
  (go-parse "if x == 0 { return 0 }")
  (list
    :if (ast-app (ast-var "==") (list (ast-var "x") (ast-literal "0")))
    (list :block (list (list :return (list (ast-literal "0")))))
    nil))

(go-parse-test
  "for: infinite — for { }"
  (go-parse "for { }")
  (list :for nil nil nil (list :block (list))))

(go-parse-test
  "for: while-like — for cond { }"
  (go-parse "for x { }")
  (list :for nil (ast-var "x") nil (list :block (list))))

(go-parse-test
  "for: C-style — for i := 0; i < 10; i++ { }"
  (go-parse "for i := 0; i < 10; i++ { }")
  (list
    :for (list :short-decl (list (ast-var "i")) (list (ast-literal "0")))
    (ast-app (ast-var "<") (list (ast-var "i") (ast-literal "10")))
    (list :inc-dec "++" (ast-var "i"))
    (list :block (list))))

(go-parse-test "stmt: break" (go-parse "break") (list :break nil))

(go-parse-test "stmt: continue" (go-parse "continue") (list :continue nil))

(go-parse-test
  "stmt: x++ (inc-dec)"
  (go-parse "x++")
  (list :inc-dec "++" (ast-var "x")))

(go-parse-test
  "control: composite-lit suppression in if cond"
  (go-parse "if Foo {}")
  (list :if (ast-var "Foo") (list :block (list)) nil))

(go-parse-test
  "stmt: go f()"
  (go-parse "go f()")
  (list :go (ast-app (ast-var "f") (list))))

(go-parse-test
  "stmt: go method(x, y)"
  (go-parse "go obj.method(x, y)")
  (list
    :go (ast-app
      (list :select (ast-var "obj") "method")
      (list (ast-var "x") (ast-var "y")))))

(go-parse-test
  "stmt: defer cleanup()"
  (go-parse "defer cleanup()")
  (list :defer (ast-app (ast-var "cleanup") (list))))

(go-parse-test
  "stmt: send ch <- v"
  (go-parse "ch <- 42")
  (list :send (ast-var "ch") (ast-literal "42")))

(go-parse-test
  "for-range: no kv — for range coll { }"
  (go-parse "for range coll { }")
  (list :range-for nil nil nil (ast-var "coll") (list :block (list))))

(go-parse-test
  "for-range: key only — for k := range m { }"
  (go-parse "for k := range m { }")
  (list
    :range-for :short-decl
    (ast-var "k")
    nil
    (ast-var "m")
    (list :block (list))))

(go-parse-test
  "for-range: k, v := range m"
  (go-parse "for k, v := range m { }")
  (list
    :range-for :short-decl
    (ast-var "k")
    (ast-var "v")
    (ast-var "m")
    (list :block (list))))

(go-parse-test
  "for-range: assign form k = range coll"
  (go-parse "for k = range coll { }")
  (list
    :range-for :assign
    (ast-var "k")
    nil
    (ast-var "coll")
    (list :block (list))))

(go-parse-test
  "concurrency: defer + go in func body"
  (go-parse "func main() { defer cleanup(); go worker() }")
  (list
    :func-decl "main"
    (list)
    (list)
    (list
      :block (list
        (list :defer (ast-app (ast-var "cleanup") (list)))
        (list :go (ast-app (ast-var "worker") (list)))))))

(go-parse-test
  "switch: tagged with two cases"
  (go-parse "switch x { case 1: a; case 2: b }")
  (list
    :switch (ast-var "x")
    (list
      (list :case (list (ast-literal "1")) (list (ast-var "a")))
      (list :case (list (ast-literal "2")) (list (ast-var "b"))))))

(go-parse-test
  "switch: multi-value case"
  (go-parse "switch x { case 1, 2: a; case 3: b }")
  (list
    :switch (ast-var "x")
    (list
      (list
        :case (list (ast-literal "1") (ast-literal "2"))
        (list (ast-var "a")))
      (list :case (list (ast-literal "3")) (list (ast-var "b"))))))

(go-parse-test
  "switch: tagless (if-else chain)"
  (go-parse "switch { case x > 0: a; case x < 0: b; default: c }")
  (list
    :switch nil
    (list
      (list
        :case (list
          (ast-app (ast-var ">") (list (ast-var "x") (ast-literal "0"))))
        (list (ast-var "a")))
      (list
        :case (list
          (ast-app (ast-var "<") (list (ast-var "x") (ast-literal "0"))))
        (list (ast-var "b")))
      (list :default (list (ast-var "c"))))))

(go-parse-test
  "switch: with default only"
  (go-parse "switch x { default: y }")
  (list :switch (ast-var "x") (list (list :default (list (ast-var "y"))))))

(go-parse-test
  "select: recv-into-var case"
  (go-parse "select { case x := <-ch: a }")
  (list
    :select (list
      (list
        :select-case (list
          :short-decl (list (ast-var "x"))
          (list (ast-app (ast-var "<-") (list (ast-var "ch")))))
        (list (ast-var "a"))))))

(go-parse-test
  "select: send case"
  (go-parse "select { case ch <- v: done() }")
  (list
    :select (list
      (list
        :select-case (list :send (ast-var "ch") (ast-var "v"))
        (list (ast-app (ast-var "done") (list)))))))

(go-parse-test
  "select: recv (discard) case"
  (go-parse "select { case <-ch: a }")
  (list
    :select (list
      (list
        :select-case (ast-app (ast-var "<-") (list (ast-var "ch")))
        (list (ast-var "a"))))))

(go-parse-test
  "select: with default (non-blocking)"
  (go-parse "select { case <-ch: a; default: b }")
  (list
    :select (list
      (list
        :select-case (ast-app (ast-var "<-") (list (ast-var "ch")))
        (list (ast-var "a")))
      (list :default (list (ast-var "b"))))))

(define
  decl-tags
  (fn
    (parsed)
    (cond
      (and (list? parsed) (= (first parsed) :file))
      (map (fn (d) (first d)) (nth parsed 1))
      (list? parsed)
      (list (first parsed))
      :else (list))))

(go-parse-test
  "e2e: hello-world top-level tags"
  (decl-tags
    (go-parse
      "package main\nimport \"fmt\"\nfunc main() { fmt.Println(\"hello, world\") }"))
  (list :package :import :func-decl))

(go-parse-test
  "e2e: recursive fibonacci"
  (decl-tags
    (go-parse
      "package main\nfunc fib(n int) int {\n  if n < 2 { return n }\n  return fib(n-1) + fib(n-2)\n}"))
  (list :package :func-decl))

(go-parse-test
  "e2e: FizzBuzz with for + if-else chain"
  (decl-tags
    (go-parse
      "package main\nimport \"fmt\"\nfunc fizzbuzz(n int) {\n  for i := 1; i <= n; i++ {\n    if i % 15 == 0 { fmt.Println(\"FizzBuzz\") } else if i % 3 == 0 { fmt.Println(\"Fizz\") } else if i % 5 == 0 { fmt.Println(\"Buzz\") } else { fmt.Println(i) }\n  }\n}"))
  (list :package :import :func-decl))

(go-parse-test
  "e2e: goroutine ping-pong (channels)"
  (decl-tags
    (go-parse
      "package main\nfunc sender(ch chan int) { ch <- 1 }\nfunc main() {\n  ch := make(chan int)\n  go sender(ch)\n  x := <-ch\n  print(x)\n}"))
  (list :package :func-decl :func-decl))

(go-parse-test
  "e2e: struct + method"
  (decl-tags
    (go-parse
      "package main\ntype Point struct { x, y int }\nfunc (p Point) Sum() int { return p.x + p.y }\nfunc main() {\n  p := Point{1, 2}\n  print(p.Sum())\n}"))
  (list :package :type-decl :method-decl :func-decl))

(go-parse-test
  "e2e: interface + structural satisfaction setup"
  (decl-tags
    (go-parse
      "package main\ntype Stringer interface { String() string }\ntype T struct { v int }\nfunc (t T) String() string { return \"t\" }"))
  (list :package :type-decl :type-decl :method-decl))

(go-parse-test
  "e2e: defer + select + range"
  (decl-tags
    (go-parse
      "package main\nfunc worker(jobs chan int, results chan int) {\n  defer close(results)\n  for j := range jobs {\n    select {\n    case results <- j * 2:\n    default:\n      return\n    }\n  }\n}"))
  (list :package :func-decl))

(go-parse-test "non-primary: '+'" (go-parse "+") nil)

(go-parse-test "non-primary: empty" (go-parse "") nil)

(define
  go-parse-test-summary
  (str "parse " go-parse-test-pass "/" go-parse-test-count))
