lua: raw table access — mutating set!, len via has-key?, +19 tests
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Has been cancelled
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Has been cancelled
This commit is contained in:
@@ -123,7 +123,7 @@
|
||||
(fn
|
||||
(i)
|
||||
(if
|
||||
(has? a (str i))
|
||||
(has-key? a (str i))
|
||||
(begin (set! n i) (count-loop (+ i 1)))
|
||||
n)))
|
||||
(count-loop 1))))
|
||||
@@ -168,7 +168,7 @@
|
||||
(t k)
|
||||
(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)))
|
||||
(define lua-set! (fn (t k v) (dict-set! t (str k) v)))
|
||||
|
||||
(define
|
||||
lua-multi?
|
||||
|
||||
@@ -539,6 +539,61 @@ cat > "$TMPFILE" << 'EPOCHS'
|
||||
(epoch 670)
|
||||
(eval "(lua-eval-ast \"local t = {fn = function(x) return x * 2 end} return t.fn(5)\")")
|
||||
|
||||
;; ── Phase 3: raw table access (read + write + #) ───────────────
|
||||
;; Write then read array index
|
||||
(epoch 700)
|
||||
(eval "(lua-eval-ast \"local t = {} t[1] = \\\"a\\\" t[2] = \\\"b\\\" return t[1]\")")
|
||||
(epoch 701)
|
||||
(eval "(lua-eval-ast \"local t = {} t[1] = 10 t[2] = 20 return t[1] + t[2]\")")
|
||||
|
||||
;; Write then read field
|
||||
(epoch 710)
|
||||
(eval "(lua-eval-ast \"local t = {} t.x = 100 return t.x\")")
|
||||
(epoch 711)
|
||||
(eval "(lua-eval-ast \"local t = {} t.name = \\\"alice\\\" t.age = 30 return t.name\")")
|
||||
(epoch 712)
|
||||
(eval "(lua-eval-ast \"local t = {x = 1} t.x = 99 return t.x\")")
|
||||
|
||||
;; Missing key is nil
|
||||
(epoch 720)
|
||||
(eval "(lua-eval-ast \"local t = {x = 1} if t.y == nil then return 99 else return 0 end\")")
|
||||
(epoch 721)
|
||||
(eval "(lua-eval-ast \"local t = {} if t[1] == nil then return 1 else return 0 end\")")
|
||||
|
||||
;; Length operator
|
||||
(epoch 730)
|
||||
(eval "(lua-eval-ast \"local t = {10, 20, 30, 40, 50} return #t\")")
|
||||
(epoch 731)
|
||||
(eval "(lua-eval-ast \"local t = {} return #t\")")
|
||||
(epoch 732)
|
||||
(eval "(lua-eval-ast \"local t = {} t[1] = 5 t[2] = 10 return #t\")")
|
||||
(epoch 733)
|
||||
(eval "(lua-eval-ast \"return #\\\"hello\\\"\")")
|
||||
|
||||
;; Mixed read/write
|
||||
(epoch 740)
|
||||
(eval "(lua-eval-ast \"local t = {count = 0} t.count = t.count + 1 t.count = t.count + 1 return t.count\")")
|
||||
(epoch 741)
|
||||
(eval "(lua-eval-ast \"local t = {} for i = 1, 5 do t[i] = i * i end return t[3]\")")
|
||||
(epoch 742)
|
||||
(eval "(lua-eval-ast \"local t = {} for i = 1, 10 do t[i] = i end return #t\")")
|
||||
|
||||
;; Chained field read/write
|
||||
(epoch 750)
|
||||
(eval "(lua-eval-ast \"local t = {a = {b = {c = 42}}} return t.a.b.c\")")
|
||||
(epoch 751)
|
||||
(eval "(lua-eval-ast \"local t = {a = {}} t.a.x = 7 return t.a.x\")")
|
||||
|
||||
;; Computed key read/write
|
||||
(epoch 760)
|
||||
(eval "(lua-eval-ast \"local k = \\\"foo\\\" local t = {} t[k] = 88 return t.foo\")")
|
||||
(epoch 761)
|
||||
(eval "(lua-eval-ast \"local t = {} t[\\\"x\\\" .. \\\"y\\\"] = 7 return t.xy\")")
|
||||
|
||||
;; Reference semantics
|
||||
(epoch 770)
|
||||
(eval "(lua-eval-ast \"local t = {} local s = t s.x = 42 return t.x\")")
|
||||
|
||||
EPOCHS
|
||||
|
||||
OUTPUT=$(timeout 60 "$SX_SERVER" < "$TMPFILE" 2>/dev/null)
|
||||
@@ -823,6 +878,27 @@ check 660 "dynamic pos values" '14'
|
||||
check 661 "function calls as values" '4'
|
||||
check 670 "function-valued field + call" '10'
|
||||
|
||||
# ── Phase 3: raw table access ─────────────────────────────────
|
||||
check 700 "t[1]=v then read" '"a"'
|
||||
check 701 "t[1]+t[2] after writes" '30'
|
||||
check 710 "t.x=100 persists" '100'
|
||||
check 711 "t.name=... persists" '"alice"'
|
||||
check 712 "overwrite existing field" '99'
|
||||
check 720 "missing field is nil" '99'
|
||||
check 721 "missing index is nil" '1'
|
||||
check 730 "#t on 5-element array" '5'
|
||||
check 731 "#t on empty" '0'
|
||||
check 732 "#t after inserts" '2'
|
||||
check 733 "#\"hello\"" '5'
|
||||
check 740 "t.count mutate chain" '2'
|
||||
check 741 "fill via for-num then read" '9'
|
||||
check 742 "#t after 10 inserts" '10'
|
||||
check 750 "chained t.a.b.c read" '42'
|
||||
check 751 "chained t.a.x write" '7'
|
||||
check 760 "t[k]=v reads via t.foo" '88'
|
||||
check 761 "t[concat]=v reads via t.xy" '7'
|
||||
check 770 "reference semantics t=s" '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