Files
llgo/c/lua/_demo/stack/stack.go

105 lines
2.5 KiB
Go
Raw Normal View History

2024-06-27 18:19:45 +08:00
package main
import (
"github.com/goplus/llgo/c"
"github.com/goplus/llgo/c/lua"
)
// printStack prints the current stack of the given Lua state.
2024-06-30 19:35:45 +08:00
func printStack(L *lua.State, stateName *c.Char) {
2024-07-13 22:57:01 +08:00
top := L.Gettop()
2024-06-27 18:19:45 +08:00
c.Printf(c.Str("%s stack (top=%d):"), stateName, top)
for i := 1; i <= int(top); i++ {
2024-07-13 22:57:01 +08:00
c.Printf(c.Str("%s "), L.Tostring(c.Int(i)))
2024-06-27 18:19:45 +08:00
}
c.Printf(c.Str("\n"))
}
func main() {
// Create a new Lua state and open libraries
2024-09-16 23:12:33 +08:00
L := lua.Newstate__1()
2024-06-27 18:19:45 +08:00
defer L.Close()
2024-07-13 22:57:01 +08:00
L.Openlibs()
2024-06-27 18:19:45 +08:00
// Push initial values onto the stack
2024-07-13 22:57:01 +08:00
L.Pushstring(c.Str("Hello"))
L.Pushstring(c.Str("LLGO"))
L.Pushnumber(2024)
2024-06-27 18:19:45 +08:00
// Print initial stack
c.Printf(c.Str("Initial stack:\n"))
printStack(L, c.Str("L1"))
// Use absindex to ensure the index is positive
idx := -2
2024-07-13 22:57:01 +08:00
absIdx := L.Absindex(c.Int(idx))
2024-06-27 18:19:45 +08:00
c.Printf(c.Str("Absolute index of 'LLGO': %d\n"), absIdx)
// Copy 'LLGO' to the top of the stack
2024-07-13 22:57:01 +08:00
L.Pushvalue(absIdx)
2024-06-27 18:19:45 +08:00
c.Printf(c.Str("\nAfter pushing 'LLGO' to the top:\n"))
printStack(L, c.Str("L1"))
// Rotate stack elements
L.Rotate(c.Int(1), c.Int(-1))
c.Printf(c.Str("\nAfter rotating the stack:\n"))
printStack(L, c.Str("L1"))
// Copy the top element to index 2
L.Copy(c.Int(-1), c.Int(2))
c.Printf(c.Str("\nAfter copying the top element to index 2:\n"))
printStack(L, c.Str("L1"))
// Check if we can grow the stack
2024-07-13 22:57:01 +08:00
if L.Checkstack(c.Int(2)) == 0 {
2024-06-27 18:19:45 +08:00
c.Printf(c.Str("Cannot grow stack\n"))
return
}
// Push additional elements
2024-07-13 22:57:01 +08:00
L.Pushnumber(3.14)
L.Pushstring(c.Str("Lua"))
2024-06-27 18:19:45 +08:00
c.Printf(c.Str("\nAfter pushing more elements:\n"))
printStack(L, c.Str("L1"))
// Set the top of the stack, clearing extra elements
2024-07-13 22:57:01 +08:00
L.Settop(c.Int(5))
2024-06-27 18:19:45 +08:00
c.Printf(c.Str("\nAfter setting top to 5:\n"))
printStack(L, c.Str("L1"))
// Create a second Lua state
2024-09-16 23:12:33 +08:00
L1 := lua.Newstate__1()
2024-06-27 18:19:45 +08:00
defer L1.Close()
// Move two elements to the new state
2024-07-13 22:57:01 +08:00
L.Xmove(L1, c.Int(2))
2024-06-27 18:19:45 +08:00
c.Printf(c.Str("\nAfter moving two elements to L1:\n"))
printStack(L, c.Str("L1"))
printStack(L1, c.Str("L2"))
}
2024-07-13 22:57:01 +08:00
/* Expected output:
Initial stack:
L1 stack (top=3):Hello LLGO 2024.0
Absolute index of 'LLGO': 2
After pushing 'LLGO' to the top:
L1 stack (top=4):Hello LLGO 2024.0 LLGO
After rotating the stack:
L1 stack (top=4):LLGO 2024.0 LLGO Hello
After copying the top element to index 2:
L1 stack (top=4):LLGO Hello LLGO Hello
After pushing more elements:
L1 stack (top=6):LLGO Hello LLGO Hello 3.14 Lua
After setting top to 5:
L1 stack (top=5):LLGO Hello LLGO Hello 3.14
After moving two elements to L1:
L1 stack (top=3):LLGO Hello LLGO
L2 stack (top=2):Hello 3.14
*/