lua: scoreboard iteration — rawget/rawset/raweq/rawlen + loadstring/load + select/assert + _G/_VERSION
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Has been cancelled

This commit is contained in:
2026-04-24 19:29:08 +00:00
parent 24fde8aa2f
commit cf4d19fb94
4 changed files with 171 additions and 59 deletions

View File

@@ -1340,3 +1340,104 @@
(else (error (str "lua: module '" name "' not found"))))))
(define require lua-require)
;; ── raw operations + loadstring ───────────────────────────────
(define
lua-rawget
(fn (t k)
(cond
((not (= (type-of t) "dict")) nil)
((has-key? t (str k)) (get t (str k)))
(else nil))))
(define
lua-rawset
(fn (t k v)
(begin (dict-set! t (str k) v) t)))
(define
lua-rawequal
(fn (a b)
(cond
((and (= a nil) (= b nil)) true)
((or (= a nil) (= b nil)) false)
((and (= (type-of a) (type-of b)) (= a b)) true)
(else false))))
(define
lua-rawlen
(fn (v)
(cond
((= (type-of v) "string") (len v))
((= (type-of v) "dict")
(let ((n 0))
(begin
(define
rl-count
(fn (i)
(if (has-key? v (str i))
(begin (set! n i) (rl-count (+ i 1)))
n)))
(rl-count 1)
n)))
(else 0))))
(define rawget lua-rawget)
(define rawset lua-rawset)
(define rawequal lua-rawequal)
(define rawlen lua-rawlen)
;; loadstring(src) returns a function that, when called, evaluates src.
(define
lua-loadstring
(fn (src)
(fn (&rest args) (lua-eval-ast src))))
(define loadstring lua-loadstring)
(define load lua-loadstring)
;; select(n, ...) — Lua 5.1 built-in. select("#", ...) is arg count; select(i, ...) returns args from i on.
(define
lua-select
(fn (&rest args)
(let ((n (first args)) (rest-args (rest args)))
(cond
((= n "#") (len rest-args))
((= (type-of n) "number")
(let ((i (- n 1)))
(cond
((< i 0) (error "lua: bad argument to select"))
((>= i (len rest-args)) nil)
(else
(let ((out (list (quote lua-multi))))
(begin
(define
sel-loop
(fn (j)
(when (< j (len rest-args))
(begin
(set! out (append out (list (nth rest-args j))))
(sel-loop (+ j 1))))))
(sel-loop i)
out))))))
(else (error "lua: bad argument to select"))))))
(define select lua-select)
;; assert(v, msg) — errors with msg if v is falsy.
(define
lua-assert
(fn (&rest args)
(let ((v (first args)))
(cond
((lua-truthy? v)
(cond
((= (len args) 1) v)
(else (cons (quote lua-multi) args))))
(else
(error (if (> (len args) 1) (nth args 1) "assertion failed!")))))))
(define assert lua-assert)
(define _G {})
(define _VERSION "Lua 5.1")