Files
rose-ash/lib/go/tests/parse.sx
giles e64d72f554
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 28s
go: parse.sx — index x[i] + slice x[a:b]/x[a:b:c] + 12 tests [proposes-ast]
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>
2026-05-27 07:53:10 +00:00

320 lines
8.6 KiB
Plaintext

;; 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\"") (ast-literal "hi"))
(go-parse-test "empty string" (go-parse "\"\"") (ast-literal ""))
(go-parse-test "raw string" (go-parse "`a\nb`") (ast-literal "a\nb"))
(go-parse-test "rune literal" (go-parse "'a'") (ast-literal "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") (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)
(define
go-parse-test-summary
(str "parse " go-parse-test-pass "/" go-parse-test-count))