lua: extend patterns to match/gmatch/gsub; gsub with string/function/table repl +6 tests
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Has been cancelled

This commit is contained in:
2026-04-24 23:13:05 +00:00
parent bd0377b6a3
commit e670e914e7
5 changed files with 102 additions and 60 deletions

View File

@@ -968,11 +968,16 @@
(define (define
lua-string-match lua-string-match
(fn (&rest args) (fn (&rest args)
(let ((s (first args)) (pat (nth args 1))) (let ((s (first args)) (pat (nth args 1))
(let ((idx (index-of s pat))) (init (if (> (len args) 2) (nth args 2) 1)))
(cond (let ((start-i0 (cond
((< idx 0) nil) ((< init 0) (let ((v (+ (len s) init))) (if (< v 0) 0 v)))
(else pat)))))) ((= init 0) 0)
(else (- init 1)))))
(let ((r (lua-pat-find pat s start-i0)))
(cond
((= r nil) nil)
(else (substring s (first r) (nth r 1)))))))))
;; Literal-only string.gmatch: iterator producing each literal match of pat. ;; Literal-only string.gmatch: iterator producing each literal match of pat.
(define (define
@@ -983,14 +988,14 @@
(cond (cond
((> pos (len s)) nil) ((> pos (len s)) nil)
(else (else
(let ((rest-str (if (= pos 0) s (substring s pos (len s))))) (let ((r (lua-pat-find pat s pos)))
(let ((idx (index-of rest-str pat))) (cond
(cond ((= r nil) (begin (set! pos (+ (len s) 1)) nil))
((< idx 0) (begin (set! pos (+ (len s) 1)) nil)) (else
(else (let ((start (first r)) (end (nth r 1)))
(begin (begin
(set! pos (+ pos idx (len pat))) (set! pos (if (= end start) (+ end 1) end))
pat))))))))))) (substring s start end))))))))))))
;; Literal-only string.gsub: replace all occurrences of pat with repl (string only for now). ;; Literal-only string.gsub: replace all occurrences of pat with repl (string only for now).
(define (define
@@ -1006,28 +1011,42 @@
(let ((out "") (pos 0) (count 0) (done false)) (let ((out "") (pos 0) (count 0) (done false))
(begin (begin
(define (define
loop gsub-loop
(fn () (fn ()
(when (and (not done) (<= pos (len s))) (when (and (not done) (<= pos (len s)))
(let ((rest-str (if (= pos 0) s (substring s pos (len s))))) (let ((r (lua-pat-find pat s pos)))
(let ((idx (index-of rest-str pat))) (cond
(cond ((= r nil)
((< idx 0) (begin
(begin (set! out (str out (substring s pos (len s))))
(set! out (str out rest-str)) (set! done true)))
(set! done true))) ((and (>= max-n 0) (>= count max-n))
((and (>= max-n 0) (>= count max-n)) (begin
(begin (set! out (str out (substring s pos (len s))))
(set! out (str out rest-str)) (set! done true)))
(set! done true))) (else
(else (let ((start (first r)) (end (nth r 1)))
(let ((before (substring rest-str 0 idx))) (let ((matched (substring s start end)))
(begin (let ((replacement
(set! out (str out before (if (= (type-of repl) "string") repl (str repl)))) (cond
(set! pos (+ pos idx (len pat))) ((= (type-of repl) "string") repl)
(set! count (+ count 1)) ((or (= (type-of repl) "function") (= (type-of repl) "lambda"))
(loop)))))))))) (let ((rv (lua-call repl matched)))
(loop) (cond
((or (= rv nil) (= rv false)) matched)
(else (str rv)))))
((= (type-of repl) "dict")
(let ((v (get repl matched)))
(cond
((= v nil) matched)
(else (str v)))))
(else (str repl)))))
(begin
(set! out (str out (substring s pos start) replacement))
(set! pos (if (= end start) (+ end 1) end))
(set! count (+ count 1))
(gsub-loop)))))))))))
(gsub-loop)
(list (quote lua-multi) out count)))))))) (list (quote lua-multi) out count))))))))
;; Basic string.format: %s %d %f (%%.Nf ignored), %%. ;; Basic string.format: %s %d %f (%%.Nf ignored), %%.

View File

@@ -51,7 +51,7 @@
"name": "attrib.lua", "name": "attrib.lua",
"status": "fail", "status": "fail",
"reason": "other: Unhandled exception: \\\"Unhandled exception: \\\\\\\"lua: module 'C' not found\\\\\\\"\\", "reason": "other: Unhandled exception: \\\"Unhandled exception: \\\\\\\"lua: module 'C' not found\\\\\\\"\\",
"ms": 6112 "ms": 6137
}, },
{ {
"name": "big.lua", "name": "big.lua",
@@ -63,7 +63,7 @@
"name": "calls.lua", "name": "calls.lua",
"status": "fail", "status": "fail",
"reason": "undefined symbol: fat\\", "reason": "undefined symbol: fat\\",
"ms": 4800 "ms": 4744
}, },
{ {
"name": "checktable.lua", "name": "checktable.lua",
@@ -87,7 +87,7 @@
"name": "constructs.lua", "name": "constructs.lua",
"status": "fail", "status": "fail",
"reason": "other: Unhandled exception: \\\"Unhandled exception: \\\\\\\"lua: attempt to compare incompat", "reason": "other: Unhandled exception: \\\"Unhandled exception: \\\\\\\"lua: attempt to compare incompat",
"ms": 6618 "ms": 6638
}, },
{ {
"name": "db.lua", "name": "db.lua",
@@ -99,13 +99,13 @@
"name": "errors.lua", "name": "errors.lua",
"status": "fail", "status": "fail",
"reason": "other: Unhandled exception: \\\"Unhandled exception: \\\\\\\"assertion failed!\\\\\\\"\\", "reason": "other: Unhandled exception: \\\"Unhandled exception: \\\\\\\"assertion failed!\\\\\\\"\\",
"ms": 3080 "ms": 3109
}, },
{ {
"name": "events.lua", "name": "events.lua",
"status": "fail", "status": "fail",
"reason": "other: Unhandled exception: \\\"Unhandled exception: \\\\\\\"assertion failed!\\\\\\\"\\", "reason": "other: Unhandled exception: \\\"Unhandled exception: \\\\\\\"assertion failed!\\\\\\\"\\",
"ms": 7192 "ms": 7069
}, },
{ {
"name": "files.lua", "name": "files.lua",
@@ -123,13 +123,13 @@
"name": "literals.lua", "name": "literals.lua",
"status": "fail", "status": "fail",
"reason": "other: Unhandled exception: \\\"Unhandled exception: \\\\\\\"assertion failed!\\\\\\\"\\", "reason": "other: Unhandled exception: \\\"Unhandled exception: \\\\\\\"assertion failed!\\\\\\\"\\",
"ms": 1777 "ms": 1866
}, },
{ {
"name": "locals.lua", "name": "locals.lua",
"status": "fail", "status": "fail",
"reason": "other: Unhandled exception: \\\"Unhandled exception: \\\\\\\"lua: attempt to call non-functio", "reason": "other: Unhandled exception: \\\"Unhandled exception: \\\\\\\"lua: attempt to call non-functio",
"ms": 1644 "ms": 1612
}, },
{ {
"name": "main.lua", "name": "main.lua",
@@ -141,43 +141,43 @@
"name": "math.lua", "name": "math.lua",
"status": "fail", "status": "fail",
"reason": "other: Unhandled exception: \\\"Unhandled exception: \\\\\\\"assertion failed!\\\\\\\"\\", "reason": "other: Unhandled exception: \\\"Unhandled exception: \\\\\\\"assertion failed!\\\\\\\"\\",
"ms": 3999 "ms": 3983
}, },
{ {
"name": "nextvar.lua", "name": "nextvar.lua",
"status": "fail", "status": "fail",
"reason": "other: Unhandled exception: \\\"Unhandled exception: \\\\\\\"assertion failed!\\\\\\\"\\", "reason": "other: Unhandled exception: \\\"Unhandled exception: \\\\\\\"assertion failed!\\\\\\\"\\",
"ms": 6948 "ms": 6968
}, },
{ {
"name": "pm.lua", "name": "pm.lua",
"status": "fail", "status": "fail",
"reason": "other: Unhandled exception: \\\"Unhandled exception: \\\\\\\"assertion failed!\\\\\\\"\\", "reason": "other: Unhandled exception: \\\"Unhandled exception: \\\\\\\"assertion failed!\\\\\\\"\\",
"ms": 6326 "ms": 5852
}, },
{ {
"name": "sort.lua", "name": "sort.lua",
"status": "timeout", "status": "timeout",
"reason": "per-test timeout", "reason": "per-test timeout",
"ms": 8008 "ms": 8007
}, },
{ {
"name": "strings.lua", "name": "strings.lua",
"status": "fail", "status": "fail",
"reason": "other: Unhandled exception: \\\"Unhandled exception: \\\\\\\"assertion failed!\\\\\\\"\\", "reason": "other: Unhandled exception: \\\"Unhandled exception: \\\\\\\"assertion failed!\\\\\\\"\\",
"ms": 3975 "ms": 3925
}, },
{ {
"name": "vararg.lua", "name": "vararg.lua",
"status": "fail", "status": "fail",
"reason": "other: Unhandled exception: \\\"Unhandled exception: \\\\\\\"assertion failed!\\\\\\\"\\", "reason": "other: Unhandled exception: \\\"Unhandled exception: \\\\\\\"assertion failed!\\\\\\\"\\",
"ms": 2152 "ms": 2195
}, },
{ {
"name": "verybig.lua", "name": "verybig.lua",
"status": "pass", "status": "pass",
"reason": "", "reason": "",
"ms": 562 "ms": 930
} }
] ]
} }

View File

@@ -18,25 +18,25 @@ fail=12 timeout=3 skip=8 total=24
|---|---|---|---:| |---|---|---|---:|
| all.lua | skip | driver uses dofile to chain other tests | 0 | | all.lua | skip | driver uses dofile to chain other tests | 0 |
| api.lua | skip | requires testC (C debug library) | 0 | | api.lua | skip | requires testC (C debug library) | 0 |
| attrib.lua | fail | other: Unhandled exception: \"Unhandled exception: \\\"lua: module 'C' not found\\\"\ | 6112 | | attrib.lua | fail | other: Unhandled exception: \"Unhandled exception: \\\"lua: module 'C' not found\\\"\ | 6137 |
| big.lua | timeout | per-test timeout | 8008 | | big.lua | timeout | per-test timeout | 8008 |
| calls.lua | fail | undefined symbol: fat\ | 4800 | | calls.lua | fail | undefined symbol: fat\ | 4744 |
| checktable.lua | skip | internal debug helpers | 0 | | checktable.lua | skip | internal debug helpers | 0 |
| closure.lua | timeout | per-test timeout | 8008 | | closure.lua | timeout | per-test timeout | 8008 |
| code.lua | skip | bytecode inspection via debug library | 0 | | code.lua | skip | bytecode inspection via debug library | 0 |
| constructs.lua | fail | other: Unhandled exception: \"Unhandled exception: \\\"lua: attempt to compare incompat | 6618 | | constructs.lua | fail | other: Unhandled exception: \"Unhandled exception: \\\"lua: attempt to compare incompat | 6638 |
| db.lua | skip | debug library | 0 | | db.lua | skip | debug library | 0 |
| errors.lua | fail | other: Unhandled exception: \"Unhandled exception: \\\"assertion failed!\\\"\ | 3080 | | errors.lua | fail | other: Unhandled exception: \"Unhandled exception: \\\"assertion failed!\\\"\ | 3109 |
| events.lua | fail | other: Unhandled exception: \"Unhandled exception: \\\"assertion failed!\\\"\ | 7192 | | events.lua | fail | other: Unhandled exception: \"Unhandled exception: \\\"assertion failed!\\\"\ | 7069 |
| files.lua | skip | io library | 0 | | files.lua | skip | io library | 0 |
| gc.lua | skip | collectgarbage / finalisers | 0 | | gc.lua | skip | collectgarbage / finalisers | 0 |
| literals.lua | fail | other: Unhandled exception: \"Unhandled exception: \\\"assertion failed!\\\"\ | 1777 | | literals.lua | fail | other: Unhandled exception: \"Unhandled exception: \\\"assertion failed!\\\"\ | 1866 |
| locals.lua | fail | other: Unhandled exception: \"Unhandled exception: \\\"lua: attempt to call non-functio | 1644 | | locals.lua | fail | other: Unhandled exception: \"Unhandled exception: \\\"lua: attempt to call non-functio | 1612 |
| main.lua | skip | standalone interpreter driver | 0 | | main.lua | skip | standalone interpreter driver | 0 |
| math.lua | fail | other: Unhandled exception: \"Unhandled exception: \\\"assertion failed!\\\"\ | 3999 | | math.lua | fail | other: Unhandled exception: \"Unhandled exception: \\\"assertion failed!\\\"\ | 3983 |
| nextvar.lua | fail | other: Unhandled exception: \"Unhandled exception: \\\"assertion failed!\\\"\ | 6948 | | nextvar.lua | fail | other: Unhandled exception: \"Unhandled exception: \\\"assertion failed!\\\"\ | 6968 |
| pm.lua | fail | other: Unhandled exception: \"Unhandled exception: \\\"assertion failed!\\\"\ | 6326 | | pm.lua | fail | other: Unhandled exception: \"Unhandled exception: \\\"assertion failed!\\\"\ | 5852 |
| sort.lua | timeout | per-test timeout | 8008 | | sort.lua | timeout | per-test timeout | 8007 |
| strings.lua | fail | other: Unhandled exception: \"Unhandled exception: \\\"assertion failed!\\\"\ | 3975 | | strings.lua | fail | other: Unhandled exception: \"Unhandled exception: \\\"assertion failed!\\\"\ | 3925 |
| vararg.lua | fail | other: Unhandled exception: \"Unhandled exception: \\\"assertion failed!\\\"\ | 2152 | | vararg.lua | fail | other: Unhandled exception: \"Unhandled exception: \\\"assertion failed!\\\"\ | 2195 |
| verybig.lua | pass | - | 562 | | verybig.lua | pass | - | 930 |

View File

@@ -964,6 +964,20 @@ cat > "$TMPFILE" << 'EPOCHS'
(epoch 1881) (epoch 1881)
(eval "(lua-eval-ast \"local function f() return 10, 20 end return (f()) + 1\")") (eval "(lua-eval-ast \"local function f() return 10, 20 end return (f()) + 1\")")
;; ── Lua patterns in match/gmatch/gsub ────────────────────────
(epoch 1890)
(eval "(lua-eval-ast \"return string.match(\\\"hello123world\\\", \\\"%d+\\\")\")")
(epoch 1891)
(eval "(lua-eval-ast \"return string.match(\\\"name=bob\\\", \\\"%a+\\\")\")")
(epoch 1892)
(eval "(lua-eval-ast \"local c = 0 for w in string.gmatch(\\\"a b c d\\\", \\\"%a\\\") do c = c + 1 end return c\")")
(epoch 1893)
(eval "(lua-eval-ast \"local r, n = string.gsub(\\\"1 + 2 = 3\\\", \\\"%d\\\", \\\"X\\\") return r .. \\\":\\\" .. n\")")
(epoch 1894)
(eval "(lua-eval-ast \"if string.find(\\\"hello\\\", \\\"^hel\\\") then return 1 else return 0 end\")")
(epoch 1895)
(eval "(lua-eval-ast \"if string.find(\\\"hello\\\", \\\"^wor\\\") then return 0 else return 1 end\")")
EPOCHS EPOCHS
OUTPUT=$(timeout 60 "$SX_SERVER" < "$TMPFILE" 2>/dev/null) OUTPUT=$(timeout 60 "$SX_SERVER" < "$TMPFILE" 2>/dev/null)
@@ -1460,6 +1474,14 @@ check 1870 "a:add():add():add().x chain" '60'
check 1880 "(f()) scalar coerce" '1' check 1880 "(f()) scalar coerce" '1'
check 1881 "(f()) + 1 scalar" '11' check 1881 "(f()) + 1 scalar" '11'
# ── Lua patterns in match/gmatch/gsub ────────────────────────
check 1890 "match %d+" '"123"'
check 1891 "match %a+" '"name"'
check 1892 "gmatch %a count" '4'
check 1893 "gsub %d → X" '"X + X = X:3"'
check 1894 "find ^ anchor hit" '1'
check 1895 "find ^ anchor miss" '1'
TOTAL=$((PASS + FAIL)) TOTAL=$((PASS + FAIL))
if [ $FAIL -eq 0 ]; then if [ $FAIL -eq 0 ]; then
echo "ok $PASS/$TOTAL Lua-on-SX tests passed" echo "ok $PASS/$TOTAL Lua-on-SX tests passed"

View File

@@ -82,6 +82,7 @@ Each item: implement → tests → tick box → update progress log.
_Newest first. Agent appends on every commit._ _Newest first. Agent appends on every commit._
- 2026-04-24: lua: scoreboard iteration — extended pattern engine to `string.match`/`gmatch`/`gsub`. `gsub` now supports string/function/table replacement modes. 381/381 green (+6 pattern tests).
- 2026-04-24: lua: scoreboard iteration — **Lua pattern engine (minimal)** for `string.find`. Supports character classes (`%d`/`%a`/`%s`/`%w`/`%p`/`%l`/`%u`/`%c`/`%x` + complements), `.` any, `^`/`$` anchors, quantifiers `*`/`+`/`-`/`?`, literal chars, `%%`. Added `plain` arg pathway. match/gmatch/gsub still literal. Scoreboard unchanged (pattern-using tests still hit other issues downstream). - 2026-04-24: lua: scoreboard iteration — **Lua pattern engine (minimal)** for `string.find`. Supports character classes (`%d`/`%a`/`%s`/`%w`/`%p`/`%l`/`%u`/`%c`/`%x` + complements), `.` any, `^`/`$` anchors, quantifiers `*`/`+`/`-`/`?`, literal chars, `%%`. Added `plain` arg pathway. match/gmatch/gsub still literal. Scoreboard unchanged (pattern-using tests still hit other issues downstream).
- 2026-04-24: lua: scoreboard iteration — `package.cpath`/`config`/`loaders`/`searchers`/`searchpath` stubs. attrib.lua moves from #9 (checking `package.cpath` is a string) to "module 'C' not found" — test requires filesystem-based module loading, not tractable. Most remaining failures need Lua pattern matching (pm.lua/strings.lua), env tracking (locals.lua/events.lua), or filesystem (attrib.lua). - 2026-04-24: lua: scoreboard iteration — `package.cpath`/`config`/`loaders`/`searchers`/`searchpath` stubs. attrib.lua moves from #9 (checking `package.cpath` is a string) to "module 'C' not found" — test requires filesystem-based module loading, not tractable. Most remaining failures need Lua pattern matching (pm.lua/strings.lua), env tracking (locals.lua/events.lua), or filesystem (attrib.lua).
- 2026-04-24: lua: scoreboard iteration — **parenthesized expressions truncate multi-return** (Lua spec: `(f())` forces single value even if `f` returns multi). Parser wraps `(expr)` in a new `lua-paren` AST node; transpile emits `(lua-first inner)`. Fixes `constructs.lua`@30 (`a,b,c = (f())` expects `a=1, b=nil, c=nil`) and `math.lua`@13. 375/375 green (+2 paren tests). Scoreboard: 8× asserts (was 10). - 2026-04-24: lua: scoreboard iteration — **parenthesized expressions truncate multi-return** (Lua spec: `(f())` forces single value even if `f` returns multi). Parser wraps `(expr)` in a new `lua-paren` AST node; transpile emits `(lua-first inner)`. Fixes `constructs.lua`@30 (`a,b,c = (f())` expects `a=1, b=nil, c=nil`) and `math.lua`@13. 375/375 green (+2 paren tests). Scoreboard: 8× asserts (was 10).