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:
110
lib/lua/test.sh
110
lib/lua/test.sh
@@ -633,6 +633,116 @@ check 482 "while i<5 count" '5'
|
||||
check 483 "repeat until i>=3" '3'
|
||||
check 484 "for 1..100 sum" '5050'
|
||||
|
||||
# ── Phase 3: stdlib — math, string, table ──────────────────────────────────
|
||||
|
||||
cat >> "$TMPFILE" << 'EPOCHS2'
|
||||
|
||||
;; ── math library ───────────────────────────────────────────────
|
||||
(epoch 500)
|
||||
(eval "(lua-eval-ast \"return math.abs(-7)\")")
|
||||
(epoch 501)
|
||||
(eval "(lua-eval-ast \"return math.floor(3.9)\")")
|
||||
(epoch 502)
|
||||
(eval "(lua-eval-ast \"return math.ceil(3.1)\")")
|
||||
(epoch 503)
|
||||
(eval "(lua-eval-ast \"return math.sqrt(9)\")")
|
||||
(epoch 504)
|
||||
(eval "(lua-eval-ast \"return math.sin(0)\")")
|
||||
(epoch 505)
|
||||
(eval "(lua-eval-ast \"return math.cos(0)\")")
|
||||
(epoch 506)
|
||||
(eval "(lua-eval-ast \"return math.max(3, 7)\")")
|
||||
(epoch 507)
|
||||
(eval "(lua-eval-ast \"return math.min(3, 7)\")")
|
||||
(epoch 508)
|
||||
(eval "(lua-eval-ast \"return math.pi > 3\")")
|
||||
(epoch 509)
|
||||
(eval "(lua-eval-ast \"return math.huge > 0\")")
|
||||
|
||||
;; ── string library ─────────────────────────────────────────────
|
||||
(epoch 520)
|
||||
(eval "(lua-eval-ast \"return string.len(\\\"hello\\\")\")")
|
||||
(epoch 521)
|
||||
(eval "(lua-eval-ast \"return string.upper(\\\"hello\\\")\")")
|
||||
(epoch 522)
|
||||
(eval "(lua-eval-ast \"return string.lower(\\\"WORLD\\\")\")")
|
||||
(epoch 523)
|
||||
(eval "(lua-eval-ast \"return string.sub(\\\"hello\\\", 2, 4)\")")
|
||||
(epoch 524)
|
||||
(eval "(lua-eval-ast \"return string.rep(\\\"ab\\\", 3)\")")
|
||||
(epoch 525)
|
||||
(eval "(lua-eval-ast \"return string.reverse(\\\"hello\\\")\")")
|
||||
(epoch 526)
|
||||
(eval "(lua-eval-ast \"return string.byte(\\\"A\\\")\")")
|
||||
(epoch 527)
|
||||
(eval "(lua-eval-ast \"return string.char(72, 105)\")")
|
||||
(epoch 528)
|
||||
(eval "(lua-eval-ast \"return string.find(\\\"hello\\\", \\\"ll\\\")\")")
|
||||
(epoch 529)
|
||||
(eval "(lua-eval-ast \"return string.match(\\\"hello\\\", \\\"ell\\\")\")")
|
||||
(epoch 530)
|
||||
(eval "(lua-eval-ast \"return string.gsub(\\\"hello\\\", \\\"l\\\", \\\"r\\\")\")")
|
||||
|
||||
;; ── table library ──────────────────────────────────────────────
|
||||
(epoch 540)
|
||||
(eval "(lua-eval-ast \"local t = {10, 20, 30} t = table.insert(t, 40) return t[4]\")")
|
||||
(epoch 541)
|
||||
(eval "(lua-eval-ast \"local t = {10, 20, 30} t = table.remove(t) return t[3]\")")
|
||||
(epoch 542)
|
||||
(eval "(lua-eval-ast \"local t = {\\\"a\\\", \\\"b\\\", \\\"c\\\"} return table.concat(t, \\\",\\\")\")")
|
||||
(epoch 543)
|
||||
(eval "(lua-eval-ast \"local t = {3, 1, 2} t = table.sort(t) return t[1]\")")
|
||||
(epoch 544)
|
||||
(eval "(lua-eval-ast \"local t = {3, 1, 2} t = table.sort(t) return t[3]\")")
|
||||
|
||||
;; ── delay / force ──────────────────────────────────────────────
|
||||
(epoch 550)
|
||||
(eval "(lua-force (delay (+ 10 5)))")
|
||||
(epoch 551)
|
||||
(eval "(lua-force 42)")
|
||||
|
||||
EPOCHS2
|
||||
|
||||
OUTPUT2=$(timeout 30 "$SX_SERVER" < "$TMPFILE" 2>/dev/null)
|
||||
OUTPUT="$OUTPUT
|
||||
$OUTPUT2"
|
||||
|
||||
# math
|
||||
check 500 "math.abs(-7)" '7'
|
||||
check 501 "math.floor(3.9)" '3'
|
||||
check 502 "math.ceil(3.1)" '4'
|
||||
check 503 "math.sqrt(9)" '3'
|
||||
check 504 "math.sin(0)" '0'
|
||||
check 505 "math.cos(0)" '1'
|
||||
check 506 "math.max(3,7)" '7'
|
||||
check 507 "math.min(3,7)" '3'
|
||||
check 508 "math.pi > 3" 'true'
|
||||
check 509 "math.huge > 0" 'true'
|
||||
|
||||
# string
|
||||
check 520 "string.len" '5'
|
||||
check 521 "string.upper" '"HELLO"'
|
||||
check 522 "string.lower" '"world"'
|
||||
check 523 "string.sub(2,4)" '"ell"'
|
||||
check 524 "string.rep(ab,3)" '"ababab"'
|
||||
check 525 "string.reverse" '"olleh"'
|
||||
check 526 "string.byte(A)" '65'
|
||||
check 527 "string.char(72,105)" '"Hi"'
|
||||
check 528 "string.find ll" '3'
|
||||
check 529 "string.match ell" '"ell"'
|
||||
check 530 "string.gsub l->r" '"herro"'
|
||||
|
||||
# table
|
||||
check 540 "table.insert" '40'
|
||||
check 541 "table.remove" 'nil'
|
||||
check 542 "table.concat ," '"a,b,c"'
|
||||
check 543 "table.sort [1]" '1'
|
||||
check 544 "table.sort [3]" '3'
|
||||
|
||||
# delay/force
|
||||
check 550 "lua-force delay" '15'
|
||||
check 551 "lua-force non-promise" '42'
|
||||
|
||||
TOTAL=$((PASS + FAIL))
|
||||
if [ $FAIL -eq 0 ]; then
|
||||
echo "ok $PASS/$TOTAL Lua-on-SX tests passed"
|
||||
|
||||
Reference in New Issue
Block a user