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

@@ -95,12 +95,54 @@
:else nil)))
(gp-args-rest)
args)))))
(define
gp-parse-bracket-expr
;; Optional expression inside brackets — returns nil if next token
;; is ':' or ']' (the slice "omitted" cases).
(fn
()
(cond
(and (= (gp-tok-type) "op")
(or (= (gp-tok-value) ":") (= (gp-tok-value) "]")))
nil
:else (gp-parse-expr 1))))
(define
gp-parse-bracket
;; Caller has consumed '['. Parses index or slice and ']'.
;; x[i] → (list :index BASE i)
;; x[a:b] → (list :slice BASE LOW HIGH nil) (LOW/HIGH may be nil)
;; x[a:b:c] → (list :slice BASE LOW HIGH MAX)
;; Returns the AST node based on BASE.
(fn
(base)
(let ((low (gp-parse-bracket-expr)))
(cond
(and (= (gp-tok-type) "op") (= (gp-tok-value) "]"))
(do (gp-advance!) (list :index base low))
(and (= (gp-tok-type) "op") (= (gp-tok-value) ":"))
(do
(gp-advance!)
(let ((high (gp-parse-bracket-expr)))
(cond
(and (= (gp-tok-type) "op") (= (gp-tok-value) "]"))
(do (gp-advance!) (list :slice base low high nil))
(and (= (gp-tok-type) "op") (= (gp-tok-value) ":"))
(do
(gp-advance!)
(let ((maxe (gp-parse-bracket-expr)))
(when (and (= (gp-tok-type) "op")
(= (gp-tok-value) "]"))
(gp-advance!))
(list :slice base low high maxe)))
:else (list :slice base low high nil))))
:else base))))
(define
gp-parse-postfix
;; Left-associative postfix loop on top of gp-parse-primary:
;; x.field → (list :select x "field") — Go-specific node,
;; no kit shape covers selector access
;; f(args...) → (ast-app f args)canonical
;; x.field → (list :select x "field") — Go-specific
;; f(args...) → (ast-app f args) — canonical
;; x[i] → (list :index x i) Go-specific
;; x[a:b] → (list :slice x low high max) — Go-specific
(fn
()
(let ((base (gp-parse-primary)))
@@ -129,6 +171,10 @@
(do
(gp-advance!)
(gp-postfix-loop (ast-app base (gp-parse-call-args))))
(and (= (get tok :type) "op") (= (get tok :value) "["))
(do
(gp-advance!)
(gp-postfix-loop (gp-parse-bracket base)))
:else base)))))
(define
gp-unary-ops

View File

@@ -1,10 +1,10 @@
{
"language": "go",
"total_pass": 178,
"total": 178,
"total_pass": 190,
"total": 190,
"suites": [
{"name":"lex","pass":129,"total":129,"status":"ok"},
{"name":"parse","pass":49,"total":49,"status":"ok"},
{"name":"parse","pass":61,"total":61,"status":"ok"},
{"name":"types","pass":0,"total":0,"status":"pending"},
{"name":"eval","pass":0,"total":0,"status":"pending"},
{"name":"runtime","pass":0,"total":0,"status":"pending"},

View File

@@ -1,11 +1,11 @@
# Go-on-SX Scoreboard
**Total: 178 / 178 tests passing**
**Total: 190 / 190 tests passing**
| | Suite | Pass | Total |
|---|---|---|---|
| ✅ | lex | 129 | 129 |
| ✅ | parse | 49 | 49 |
| ✅ | parse | 61 | 61 |
| ⬜ | types | 0 | 0 |
| ⬜ | eval | 0 | 0 |
| ⬜ | runtime | 0 | 0 |

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)