Files
rose-ash/lib/go/tests/parse.sx
giles 750035d543
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 39s
go: parse.sx — binary operators via Pratt precedence climbing + 9 tests [consumes-pratt]
gp-parse-expr / gp-pratt-loop implement classic Pratt climbing
against go-precedence-table (entry shape from lib/guest/pratt.sx).
The kit gives us pratt-op-lookup + accessors; the climbing loop
itself stays per-language (per kit header — Lua and Prolog have
opposite conventions).

Left-associative ops raise the right-recursion min by 1; right-
associative would keep prec. All Go binary operators are left-assoc.

AST shape: a binary node is emitted as
  (ast-app (ast-var OP) [LHS RHS])
— canonical ast-app rather than a Go-specific binary node, since a
future evaluator can recognise operator-named apps without losing
information.

Coverage: equal-prec left-to-right, * tighter than +, && tighter
than ||, comparison tighter than &&, long left-assoc chains, mixed
literal+ident operands.

parse 26/26, total 155/155.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-27 07:39:03 +00:00

121 lines
4.0 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 "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))