lua-runtime: add math/string/table stdlib + delay/force (185/185 pass)

math: abs/ceil/floor/sqrt/sin/cos/tan/asin/acos/atan/exp/log/max/min/pi/huge
string: len/sub/upper/lower/rep/reverse/byte/char/find/match/gmatch/gsub
table: insert/remove/concat/sort
lua-force: force promises (delay thunk protocol)
Fix lua-len: replace has? (unavailable in sx_server) with nil-check.
Fix string.byte: use string->list to get char type, not nth on string.
Fix string.char: truncate float codes before integer->char.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-01 21:14:14 +00:00
parent 10c278d073
commit ec3512d63b
2 changed files with 219 additions and 2 deletions

View File

@@ -123,7 +123,7 @@
(fn
(i)
(if
(has? a (str i))
(not (= (get a (str i)) nil))
(begin (set! n i) (count-loop (+ i 1)))
n)))
(count-loop 1))))
@@ -152,7 +152,9 @@
(cond
((= (first f) "pos")
(begin
(set! t (assoc t (str array-idx) (nth f 1)))
(set!
t
(assoc t (str array-idx) (nth f 1)))
(set! array-idx (+ array-idx 1))))
((= (first f) "kv")
(let
@@ -169,3 +171,108 @@
(if (= t nil) nil (let ((v (get t (str k)))) (if (= v nil) nil v)))))
(define lua-set! (fn (t k v) (assoc t (str k) v)))
;; ---------------------------------------------------------------------------
;; Helpers for stdlib
;; ---------------------------------------------------------------------------
;; Apply a char function to every character in a string
(define (lua-str-map s fn) (list->string (map fn (string->list s))))
;; Repeat string s n times
(define
(lua-str-rep s n)
(letrec
((go (fn (acc i) (if (= i 0) acc (go (str acc s) (- i 1))))))
(go "" n)))
;; Force a promise created by delay
(define
(lua-force p)
(if
(and (dict? p) (get p :_promise))
(if (get p :forced) (get p :value) ((get p :thunk)))
p))
;; ---------------------------------------------------------------------------
;; math — Lua math library
;; ---------------------------------------------------------------------------
(define math {:asin asin :floor floor :exp exp :huge 1e+308 :tan tan :sqrt sqrt :log log :abs abs :ceil ceil :sin sin :max (fn (a b) (if (> a b) a b)) :acos acos :min (fn (a b) (if (< a b) a b)) :cos cos :pi 3.14159 :atan atan})
;; ---------------------------------------------------------------------------
;; string — Lua string library
;; ---------------------------------------------------------------------------
(define
(lua-string-find s pat)
(let
((m (regexp-match (make-regexp pat) s)))
(if (= m nil) nil (list (+ (get m :start) 1) (get m :end)))))
(define
(lua-string-match s pat)
(let
((m (regexp-match (make-regexp pat) s)))
(if
(= m nil)
nil
(let
((groups (get m :groups)))
(if (= (len groups) 0) (get m :match) (first groups))))))
(define
(lua-string-gmatch s pat)
(map (fn (m) (get m :match)) (regexp-match-all (make-regexp pat) s)))
(define
(lua-string-gsub s pat repl)
(regexp-replace-all (make-regexp pat) s repl))
(define string {:rep lua-str-rep :sub (fn (s i &rest j-args) (let ((slen (len s)) (j (if (= (len j-args) 0) -1 (first j-args)))) (let ((from (if (< i 0) (let ((r (+ slen i))) (if (< r 0) 0 r)) (- i 1))) (to (if (< j 0) (let ((r (+ slen j 1))) (if (< r 0) 0 r)) (if (> j slen) slen j)))) (if (> from to) "" (substring s from to))))) :len (fn (s) (len s)) :upper (fn (s) (lua-str-map s char-upcase)) :char (fn (&rest codes) (list->string (map (fn (c) (integer->char (truncate c))) codes))) :gmatch lua-string-gmatch :gsub lua-string-gsub :lower (fn (s) (lua-str-map s char-downcase)) :byte (fn (s &rest args) (char->integer (nth (string->list s) (- (if (= (len args) 0) 1 (first args)) 1)))) :match lua-string-match :find lua-string-find :reverse (fn (s) (list->string (reverse (string->list s))))})
;; ---------------------------------------------------------------------------
;; table — Lua table library
;; ---------------------------------------------------------------------------
(define
(lua-table-insert t v)
(assoc t (str (+ (lua-len t) 1)) v))
(define
(lua-table-remove t &rest args)
(let
((n (lua-len t))
(pos (if (= (len args) 0) (lua-len t) (first args))))
(letrec
((slide (fn (t i) (if (< i n) (assoc (slide t (+ i 1)) (str i) (lua-get t (+ i 1))) (assoc t (str n) nil)))))
(slide t pos))))
(define
(lua-table-concat t &rest args)
(let
((sep (if (= (len args) 0) "" (first args)))
(n (lua-len t)))
(letrec
((go (fn (acc i) (if (> i n) acc (go (str acc (if (= i 1) "" sep) (lua-to-string (lua-get t i))) (+ i 1))))))
(go "" 1))))
(define
(lua-table-sort t)
(let
((n (lua-len t)))
(letrec
((collect (fn (i acc) (if (< i 1) acc (collect (- i 1) (cons (lua-get t i) acc)))))
(rebuild
(fn
(t i items)
(if
(= (len items) 0)
t
(rebuild
(assoc t (str i) (first items))
(+ i 1)
(rest items))))))
(rebuild t 1 (sort (collect n (list)))))))
(define table {:sort lua-table-sort :concat lua-table-concat :insert lua-table-insert :remove lua-table-remove})