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
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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user