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
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:
@@ -1,17 +1,18 @@
|
||||
;; lib/go/parse.sx — Go parser. Tokenises via go-tokenize (lib/go/lex.sx),
|
||||
;; builds canonical AST nodes per lib/guest/ast.sx, and uses the operator
|
||||
;; entry shape from lib/guest/pratt.sx for precedence climbing (Pratt).
|
||||
;; builds canonical AST nodes per lib/guest/ast.sx, and uses
|
||||
;; pratt-op-lookup from lib/guest/pratt.sx for operator-precedence climbing.
|
||||
;;
|
||||
;; First slice: primary expressions only —
|
||||
;; int / float / imag / string / rune literal → (ast-literal VALUE)
|
||||
;; identifier → (ast-var NAME)
|
||||
;; Slices so far:
|
||||
;; 1. Primary expressions — literal / identifier → ast-literal / ast-var
|
||||
;; 2. Binary operators — Pratt precedence climbing against
|
||||
;; go-precedence-table; binary application
|
||||
;; emitted as (ast-app (ast-var OP) [LHS RHS]).
|
||||
;;
|
||||
;; Subsequent slices add binary operators (via gp-precedence-table +
|
||||
;; pratt-op-lookup), function calls, type expressions, declarations,
|
||||
;; and statements.
|
||||
;; The climbing loop is per-language (see lib/guest/pratt.sx header on why)
|
||||
;; but the entry shape and lookup are shared.
|
||||
;;
|
||||
;; All scanner locals are gp- prefixed (mirrors lib/go/lex.sx's gl- prefix):
|
||||
;; SX host primitives silently shadow guest-language defines.
|
||||
;; All scanner locals are gp- prefixed: SX host primitives silently shadow
|
||||
;; guest-language defines.
|
||||
|
||||
(define
|
||||
go-precedence-table
|
||||
@@ -63,4 +64,39 @@
|
||||
(= ty "ident")
|
||||
(do (gp-advance!) (ast-var v))
|
||||
:else nil))))
|
||||
(gp-parse-primary))))
|
||||
(define
|
||||
gp-parse-expr
|
||||
(fn
|
||||
(min-prec)
|
||||
(let ((left (gp-parse-primary))) (gp-pratt-loop left min-prec))))
|
||||
(define
|
||||
gp-pratt-loop
|
||||
(fn
|
||||
(left min-prec)
|
||||
(cond
|
||||
(= left nil) nil
|
||||
:else
|
||||
(let
|
||||
((tok (gp-cur)))
|
||||
(cond
|
||||
(not (= (get tok :type) "op"))
|
||||
left
|
||||
:else (let
|
||||
((entry (pratt-op-lookup go-precedence-table (get tok :value))))
|
||||
(cond
|
||||
(= entry nil)
|
||||
left
|
||||
(< (pratt-op-prec entry) min-prec)
|
||||
left
|
||||
:else (do
|
||||
(gp-advance!)
|
||||
(let
|
||||
((next-min (if (= (pratt-op-assoc entry) :left) (+ (pratt-op-prec entry) 1) (pratt-op-prec entry))))
|
||||
(let
|
||||
((right (gp-parse-expr next-min)))
|
||||
(gp-pratt-loop
|
||||
(ast-app
|
||||
(ast-var (get tok :value))
|
||||
(list left right))
|
||||
min-prec)))))))))))
|
||||
(gp-parse-expr 1))))
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
{
|
||||
"language": "go",
|
||||
"total_pass": 146,
|
||||
"total": 146,
|
||||
"total_pass": 155,
|
||||
"total": 155,
|
||||
"suites": [
|
||||
{"name":"lex","pass":129,"total":129,"status":"ok"},
|
||||
{"name":"parse","pass":17,"total":17,"status":"ok"},
|
||||
{"name":"parse","pass":26,"total":26,"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"},
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
# Go-on-SX Scoreboard
|
||||
|
||||
**Total: 146 / 146 tests passing**
|
||||
**Total: 155 / 155 tests passing**
|
||||
|
||||
| | Suite | Pass | Total |
|
||||
|---|---|---|---|
|
||||
| ✅ | lex | 129 | 129 |
|
||||
| ✅ | parse | 17 | 17 |
|
||||
| ✅ | parse | 26 | 26 |
|
||||
| ⬜ | types | 0 | 0 |
|
||||
| ⬜ | eval | 0 | 0 |
|
||||
| ⬜ | runtime | 0 | 0 |
|
||||
|
||||
@@ -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))
|
||||
|
||||
Reference in New Issue
Block a user