generated from mcpplibs/templates
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable.cpp
More file actions
95 lines (81 loc) · 2.91 KB
/
table.cpp
File metadata and controls
95 lines (81 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import std;
import mcpplibs.capi.lua;
namespace lua = mcpplibs::capi::lua;
int main() {
std::println("=== mcpplibs.capi.lua table example ===\n");
auto* L = lua::L_newstate();
lua::L_openlibs(L);
// --- Create a table from C++ ---
std::println("[1] Create table from C++:");
lua::newtable(L);
lua::pushstring(L, "Alice");
lua::setfield(L, -2, "name");
lua::pushinteger(L, 30);
lua::setfield(L, -2, "age");
lua::pushstring(L, "alice@example.com");
lua::setfield(L, -2, "email");
lua::setglobal(L, "person");
lua::L_dostring(L,
"print(' name = ' .. person.name)\n"
"print(' age = ' .. person.age)\n"
"print(' email = ' .. person.email)\n"
);
// --- Array-style table ---
std::println("[2] Array table:");
lua::newtable(L);
const char* fruits[] { "apple", "banana", "cherry", "date" };
for (int i { 0 }; i < 4; i++) {
lua::pushstring(L, fruits[i]);
lua::rawseti(L, -2, i + 1);
}
lua::setglobal(L, "fruits");
lua::L_dostring(L,
"for i, fruit in ipairs(fruits) do\n"
" print(' [' .. i .. '] = ' .. fruit)\n"
"end\n"
);
// --- Nested tables ---
std::println("[3] Nested tables:");
lua::newtable(L);
lua::newtable(L);
lua::pushnumber(L, 1.0);
lua::setfield(L, -2, "x");
lua::pushnumber(L, 2.0);
lua::setfield(L, -2, "y");
lua::setfield(L, -2, "position");
lua::newtable(L);
lua::pushnumber(L, 10.0);
lua::setfield(L, -2, "width");
lua::pushnumber(L, 20.0);
lua::setfield(L, -2, "height");
lua::setfield(L, -2, "size");
lua::setglobal(L, "rect");
lua::L_dostring(L,
"print(' rect.position = (' .. rect.position.x .. ', ' .. rect.position.y .. ')')\n"
"print(' rect.size = ' .. rect.size.width .. ' x ' .. rect.size.height)\n"
);
// --- Table traversal from C++ ---
std::println("[4] Traverse table from C++:");
lua::L_dostring(L, "config = { debug=true, level=5, name='test' }");
lua::getglobal(L, "config");
lua::pushnil(L);
while (lua::next(L, -2) != 0) {
const char* key = lua::tostring(L, -2);
const char* typeName = lua::L_typename(L, -1);
if (lua::isstring(L, -1)) {
std::println(" {} ({}) = {}", key, typeName, lua::tostring(L, -1));
} else if (lua::isinteger(L, -1)) {
std::println(" {} ({}) = {}", key, typeName, lua::tointeger(L, -1));
} else if (lua::isboolean(L, -1)) {
std::println(" {} ({}) = {}", key, typeName, lua::toboolean(L, -1) ? "true" : "false");
} else if (lua::isnumber(L, -1)) {
std::println(" {} ({}) = {}", key, typeName, lua::tonumber(L, -1));
} else {
std::println(" {} ({}) = <{}>", key, typeName, typeName);
}
lua::pop(L, 1);
}
lua::close(L);
std::println("\ndone.");
return 0;
}