go: parse.sx — index x[i] + slice x[a:b]/x[a:b:c] + 12 tests [proposes-ast]
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 28s

Adds the bracket postfix branch:
  a[0] / a[i] / a[i+1] / m["key"]             → (list :index OBJ IDX)
  a[:] / a[1:] / a[:2] / a[1:2] / a[1:2:3]    → (list :slice OBJ LOW HIGH MAX)

LOW/HIGH/MAX are AST nodes or nil for omitted indices. The 4th MAX
slot is only populated by the three-index full-slice form.

Two new lib/guest/ast.sx kit gaps surfaced (logged in plans/go-on-sx.md
Blockers):

  * No :index node — universal across guests with arrays/maps.
  * No :slice node — Python/Rust/Swift/JS/Ruby all need at minimum the
    two-index form. Go's three-index variant is more specialised but
    fits in the same shape with an optional fourth slot.

Parser is permissive on a[1::3] (strict Go rejects, but the type phase
can enforce the grammar; lexer/parser stays loose).

Chained (a[0][1]) and mixed-with-selector (a[0].field) cases work via
the existing left-associative postfix loop.

parse 61/61, total 190/190.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-27 07:53:10 +00:00
parent e1c5fdae53
commit e64d72f554
5 changed files with 148 additions and 11 deletions

View File

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