go: parse.sx — package/import/var/const/type declarations + 10 tests [consumes-ast]
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 33s

First slice of Phase 2 declarations:
  package main                      →  (list :package "main")
  import "fmt"                      →  (ast-import "fmt")    [from kit]
  var x int                         →  var-decl + :field binding
  var x = 5                         →  init only (type inferred)
  var x int = 5                     →  both type and init
  var x, y int = 1, 2               →  multi-name shared type
  const Pi = 3.14                   →  const-decl
  const C int = 42                  →  typed const
  type T int                        →  named alias
  type Point struct { x, y int }    →  named struct

New gp-parse-top dispatches on the leading keyword: routes
package/import/var/const/type to gp-parse-decl; everything else
still goes through gp-parse-expr. Existing expression tests are
unaffected (cur won't be a decl keyword at expression start).

var/const decls use the (:field NAMES TYPE) shape from the
ast-binding-group proposal — first concrete cross-deliverable use:
struct fields, var decls, const decls all envelope through the
same node. That's the smell test for whether the kit shape is
right; so far it's clean.

import uses the canonical ast-import from lib/guest/ast.sx — first
direct use of a kit constructor for a declaration shape.

Grouped/parenthesized decls (var (...), import (...), const (...),
type (...)) and func decls (with method receivers + named params)
deferred to subsequent iterations.

parse 124/124, total 253/253.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-27 19:44:24 +00:00
parent 632e06d3cf
commit 4922b6e987
5 changed files with 202 additions and 9 deletions

View File

@@ -714,6 +714,67 @@
: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 "non-primary: '+'" (go-parse "+") nil)
(go-parse-test "non-primary: empty" (go-parse "") nil)