lua: io stub (buffered) + print/tostring/tonumber +12 tests
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Has been cancelled

This commit is contained in:
2026-04-24 19:07:31 +00:00
parent 40439cf0e1
commit c6b7e19892
3 changed files with 152 additions and 1 deletions

View File

@@ -1129,3 +1129,112 @@
(dict-set! table "maxn" lua-table-maxn)
(define unpack lua-unpack)
;; ── io library (minimal stub — buffered; no real stdio) ───────
(define io {})
(define __io-buffer "")
(define
lua-io-write
(fn (&rest args)
(begin
(define
loop
(fn (i)
(when (< i (len args))
(begin
(set! __io-buffer (str __io-buffer (lua-concat-coerce (nth args i))))
(loop (+ i 1))))))
(loop 0)
io)))
(define
lua-io-read
(fn (&rest args) nil))
(define
lua-io-open
(fn (&rest args) nil))
(define
lua-io-lines
(fn (&rest args) (fn (&rest __) nil)))
(define
lua-io-close
(fn (&rest args) true))
(define
lua-io-flush
(fn (&rest args) nil))
(define
lua-io-buffer
(fn ()
(let ((out __io-buffer))
(begin
(set! __io-buffer "")
out))))
;; print(a, b, c) — args joined by tab, trailing newline
(define
lua-print
(fn (&rest args)
(begin
(define
loop
(fn (i)
(when (< i (len args))
(begin
(when (> i 0) (set! __io-buffer (str __io-buffer "\t")))
(set! __io-buffer (str __io-buffer (lua-to-display (nth args i))))
(loop (+ i 1))))))
(loop 0)
(set! __io-buffer (str __io-buffer "\n"))
nil)))
(define
lua-to-display
(fn (v)
(cond
((= v nil) "nil")
((= v true) "true")
((= v false) "false")
((= (type-of v) "string") v)
((= (type-of v) "number") (str v))
((= (type-of v) "dict") (str "table"))
((or (= (type-of v) "function") (= (type-of v) "lambda")) "function")
(else (str v)))))
(define
lua-tostring
(fn (v)
(cond
((= (type-of v) "dict")
(let ((m (lua-get-mm v "__tostring")))
(cond
((not (= m nil)) (lua-first (m v)))
(else (lua-to-display v)))))
(else (lua-to-display v)))))
(define
lua-tonumber
(fn (&rest args)
(let ((v (first args))
(base (if (> (len args) 1) (nth args 1) nil)))
(cond
((= (type-of v) "number") v)
((= (type-of v) "string") (lua-to-number v))
(else nil)))))
(dict-set! io "write" lua-io-write)
(dict-set! io "read" lua-io-read)
(dict-set! io "open" lua-io-open)
(dict-set! io "lines" lua-io-lines)
(dict-set! io "close" lua-io-close)
(dict-set! io "flush" lua-io-flush)
(dict-set! io "__buffer" lua-io-buffer)
(define print lua-print)
(define tostring lua-tostring)
(define tonumber lua-tonumber)