go: parse.sx — binary operators via Pratt precedence climbing + 9 tests [consumes-pratt]
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 39s

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>
This commit is contained in:
2026-05-27 07:39:03 +00:00
parent 976c6dd0ef
commit 750035d543
5 changed files with 143 additions and 21 deletions

View File

@@ -34,10 +34,87 @@
(go-parse-test "ident: with digit" (go-parse "x123") (ast-var "x123"))
;; ── primary: non-primary returns nil ──────────────────────────────
(go-parse-test "non-primary: '+'" (go-parse "+") nil)
(go-parse-test "non-primary: empty" (go-parse "") 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))