GUEST: step 4 — lib/guest/pratt.sx operator-table format + lookup

Extracted the data-half of Pratt-style precedence parsing: the operator
table format and lookup. The climbing loop stays per-language because
the two canaries use opposite conventions (lua: higher prec = tighter;
prolog: lower prec = tighter, with xfx/xfy/yfx assoc tags) — forcing
one shared loop adds callback indirection that obscures more than it
shares. The brief's literal ask is "Grammar is a dict, not hardcoded
cond" and that's what gets shared.

Entry shape: (NAME PREC ASSOC). Three accessors: pratt-op-name /
pratt-op-prec / pratt-op-assoc. One traversal: pratt-op-lookup.

Ported lua/parser.sx — replaced 18-clause cond and the
lua-binop-right? hardcoded `or` with a 15-entry lua-op-table, now
queried via pratt-op-lookup. Ported prolog/parser.sx — pl-op-find
(linear walk reimpl) deleted; pl-op-lookup wraps pratt-op-lookup;
pl-token-op simplified to return the entry directly.

Verification:
- lua/test.sh: 185/185 = baseline.
- prolog/conformance.sh: 590/590 = baseline (timestamp-only diff).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-07 17:17:17 +00:00
parent d27622d45e
commit da27958d67
7 changed files with 62 additions and 42 deletions

View File

@@ -3,28 +3,33 @@
(define lua-tok-value (fn (t) (if (= t nil) nil (get t :value))))
(define
lua-binop-prec
(fn
(op)
(cond
((= op "or") 1)
((= op "and") 2)
((= op "<") 3)
((= op ">") 3)
((= op "<=") 3)
((= op ">=") 3)
((= op "==") 3)
((= op "~=") 3)
((= op "..") 5)
((= op "+") 6)
((= op "-") 6)
((= op "*") 7)
((= op "/") 7)
((= op "%") 7)
((= op "^") 10)
(else 0))))
lua-op-table
(list
(list "or" 1 :left)
(list "and" 2 :left)
(list "<" 3 :left)
(list ">" 3 :left)
(list "<=" 3 :left)
(list ">=" 3 :left)
(list "==" 3 :left)
(list "~=" 3 :left)
(list ".." 5 :right)
(list "+" 6 :left)
(list "-" 6 :left)
(list "*" 7 :left)
(list "/" 7 :left)
(list "%" 7 :left)
(list "^" 10 :right)))
(define lua-binop-right? (fn (op) (or (= op "..") (= op "^"))))
(define lua-binop-prec
(fn (op)
(let ((entry (pratt-op-lookup lua-op-table op)))
(if (= entry nil) 0 (pratt-op-prec entry)))))
(define lua-binop-right?
(fn (op)
(let ((entry (pratt-op-lookup lua-op-table op)))
(and (not (= entry nil)) (= (pratt-op-assoc entry) :right)))))
(define
lua-parse

View File

@@ -30,6 +30,7 @@ cat > "$TMPFILE" << 'EPOCHS'
(epoch 1)
(load "lib/guest/lex.sx")
(load "lib/guest/prefix.sx")
(load "lib/guest/pratt.sx")
(load "lib/lua/tokenizer.sx")
(epoch 2)
(load "lib/lua/parser.sx")