Skip to content

Commit 3cadead

Browse files
committed
Restore original demo syntax and re-add container/table constructors
1 parent 2652477 commit 3cadead

4 files changed

Lines changed: 185 additions & 74 deletions

File tree

‎examples/demo.cpp‎

Lines changed: 115 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,50 @@
11
#include "smallfolk.h"
2-
#include "smallfolk_convert.h"
32

43
#include <cassert>
54
#include <deque>
65
#include <forward_list>
76
#include <iostream>
7+
#include <map>
88
#include <string>
9+
#include <unordered_map>
910

1011
int main()
1112
{
1213
{
1314
std::cout << "Test values" << std::endl;
14-
LuaVal merge_right = LuaVal::table();
15-
merge_right.set(LuaVal("ke"), LuaVal("test"));
16-
merge_right.set(LuaVal("ke2"), LuaVal("test"));
17-
LuaVal asd = {
18-
LuaVal("number"),
19-
LuaVal("string"),
20-
LuaVal("table"),
21-
LuaVal::mrg(LuaVal{ LuaVal("number"), LuaVal("string") }, merge_right)
22-
};
15+
LuaVal asd = { "number", "string", "table", LuaVal::mrg({"number", "string"}, LuaVal::LuaTable({{"ke", "test"},{"ke2", "test"}})) };
2316
std::cout << asd.dumps() << std::endl;
2417
std::string err;
2518
LuaVal v = LuaVal::loads(" { 1 , 2 , { 3 , 4, ' k e ' : ' t e s t ' } } ", &err);
2619
std::cout << v.dumps(&err) << std::endl;
2720
std::cout << err << std::endl;
2821

2922
std::cout << "Testing different double corner values" << std::endl;
30-
double zero = 0.0;
31-
LuaVal tn = { -(zero / zero), (zero / zero), (1.0 / zero), -(1.0 / zero) };
23+
double _zero = 0.0;
24+
LuaVal tn = { -(0 / _zero), (0 / _zero), (1 / _zero), -(1 / _zero) };
3225
std::cout << tn.dumps() << std::endl;
33-
std::cout << tn.get(1).tostring() << " " << tn.get(2).tostring() << " "
34-
<< tn.get(3).tostring() << " " << tn.get(4).tostring() << std::endl;
26+
std::cout << -(0 / _zero) << " " << (0 / _zero) << " " << (1 / _zero) << " " << -(1 / _zero) << std::endl;
27+
std::cout << tn.get(1).tostring() << " " << tn.get(2).tostring() << " " << tn.get(3).tostring() << " " << tn.get(4).tostring() << std::endl;
3528
tn = LuaVal::loads(tn.dumps());
36-
std::cout << tn.get(1).tostring() << " " << tn.get(2).tostring() << " "
37-
<< tn.get(3).tostring() << " " << tn.get(4).tostring() << std::endl;
29+
std::cout << tn.get(1).tostring() << " " << tn.get(2).tostring() << " " << tn.get(3).tostring() << " " << tn.get(4).tostring() << std::endl;
3830
std::cout << std::endl;
39-
}
4031

41-
{
4232
std::cout << "Testing creation testing and printing of all value types" << std::endl;
4333
LuaVal implicit_test = -123;
4434
LuaVal copy_test(implicit_test);
4535
LuaVal copy_test2 = implicit_test;
46-
LuaVal n = LuaVal::nil;
47-
LuaVal n2(TNIL);
36+
LuaVal n = LuaVal::nil; // nil
37+
LuaVal n2(TNIL); // nil
4838
LuaVal b(true);
4939
LuaVal s("somestring");
5040
LuaVal d(123.456);
5141
LuaVal f(123.456f);
5242
LuaVal i(-678);
53-
LuaVal u(0xFFFFFFFFu);
54-
LuaVal t;
43+
LuaVal u(0xFFFFFFFF);
44+
LuaVal t; // defaults to table
5545
LuaVal t2 = LuaVal::table();
56-
LuaVal t3 = { LuaVal(1), LuaVal(2), LuaVal(3) };
57-
LuaVal t4 = {};
46+
LuaVal t3 = { 1, 2, 3 };
47+
LuaVal t4 = {}; // curly braces are table
5848
LuaVal t5(TTABLE);
5949

6050
assert(implicit_test.isnumber());
@@ -77,65 +67,127 @@ int main()
7767
std::cout << implicit_test.tostring() << std::endl;
7868
std::cout << copy_test.tostring() << std::endl;
7969
std::cout << copy_test2.tostring() << std::endl;
80-
assert(n.tostring() == "nil");
81-
assert(b.tostring() == "true");
82-
assert(s.tostring() == "somestring");
70+
std::cout << n.tostring() << std::endl;
71+
std::cout << b.tostring() << std::endl;
72+
std::cout << s.tostring() << std::endl;
73+
std::cout << d.tostring() << std::endl;
74+
std::cout << f.tostring() << std::endl;
75+
std::cout << i.tostring() << std::endl;
76+
std::cout << u.tostring() << std::endl;
77+
std::cout << t.tostring() << std::endl;
78+
std::cout << t2.tostring() << std::endl;
79+
std::cout << t3.tostring() << std::endl;
8380
std::cout << std::endl;
84-
}
8581

86-
{
8782
std::cout << "Testing exception handling" << std::endl;
8883
std::string errmsg;
8984
try
9085
{
9186
LuaVal h(-7);
92-
(void)h.str();
87+
(void)h.str(); // error, h is not a string
9388
}
9489
catch (smallfolk_exception const & e)
9590
{
91+
// caught an exception
9692
errmsg = e.what();
9793
}
94+
// printing caught error if any
9895
if (!errmsg.empty())
9996
std::cout << errmsg << std::endl << std::endl;
10097
}
10198

10299
{
103100
std::cout << "Example usage" << std::endl;
104101

102+
// create a lua table and set some values to it
105103
LuaVal table = LuaVal::table();
106104
table.set(1, "Hello");
107-
table.set(std::string("test"), std::string("world"));
108-
table.set(LuaVal(67.5), -234.5);
105+
table.set("test", "world");
106+
table.set(67.5, -234.5);
109107

108+
// serialize the table
110109
std::string serialized = table.dumps();
110+
111+
// print the serialization, it should be rather human readable
111112
std::cout << serialized << std::endl;
113+
// Example output: {"Hello","test":"world",67.5:-234.5}
112114

115+
// form lua values from the string
113116
LuaVal deserialized = LuaVal::loads(serialized);
114-
std::cout << deserialized.get(1).str() << " " << deserialized.get(std::string("test")).str()
115-
<< " " << deserialized.get(LuaVal(67.5)).num() << std::endl;
117+
118+
// print the values from deserialized result table
119+
std::cout << deserialized.get(1).str() << " " << deserialized.get("test").str() << " " << deserialized.get(67.5).num() << std::endl;
120+
// Example output: Hello world -234.5
116121
std::cout << std::endl;
117122
}
118123

124+
/*
125+
// This is disabled because circular references cause memleak or need complex handling for memory management
126+
// Circular references should not be used and are ignored (nil) when parsed
127+
// Using a circular reference in C++ code will cause an exception to be thrown
128+
{
129+
std::cout << "Cthulhu" << std::endl;
130+
131+
// Essentially {{},{},{}}
132+
LuaVal cthulhu(TTABLE);
133+
cthulhu[1] = LuaVal(TTABLE);
134+
cthulhu[2] = LuaVal(TTABLE);
135+
cthulhu[3] = LuaVal(TTABLE);
136+
cthulhu["fhtagn"] = cthulhu;
137+
cthulhu[1][cthulhu[2]] = cthulhu[3];
138+
cthulhu[2][cthulhu[1]] = cthulhu[2];
139+
cthulhu[3][cthulhu[3]] = cthulhu;
140+
std::cout << cthulhu.dumps() << std::endl;
141+
// prints:
142+
// {"fhtagn":@1,1:{{@2:@3}:{@4:@1}},2:@3,3:@4}
143+
std::cout << std::endl;
144+
}
145+
146+
{
147+
std::cout << "Table inside itself" << std::endl;
148+
try
149+
{
150+
LuaVal tbl(TTABLE);
151+
tbl.set(1, tbl);
152+
std::cout << tbl.dumps() << std::endl;
153+
}
154+
catch (smallfolk_exception const & e)
155+
{
156+
std::cout << e.what() << std::endl;
157+
}
158+
std::cout << std::endl;
159+
}
160+
*/
161+
119162
{
120163
std::cout << "Table initializer list coolness" << std::endl;
121-
LuaVal nested = { LuaVal::table(), { LuaVal::table() }, { LuaVal(3) }, { LuaVal(4) } };
122-
std::cout << nested.dumps() << std::endl;
164+
std::cout << "{} evaluates to table" << std::endl;
165+
std::cout << "{{}} evaluates to a table inside a table" << std::endl;
166+
std::cout << "{5} evaluates to 5 being inside a table" << std::endl;
167+
std::cout << "{LuaVal(5)} evaluates to 5 inside a table" << std::endl;
168+
169+
LuaVal nested = { {}, {{}}, { 3 }, { LuaVal(4) } };
170+
std::cout << nested.dumps() << std::endl; // Outputs {{},{{}},{3},{4}}
123171
std::cout << std::endl;
124172
}
125173

126174
{
127175
std::cout << "test accessing table with [] operator" << std::endl;
128-
LuaVal nested = LuaVal::table();
176+
std::cout << "Note that table keys cannot be accessed!" << std::endl;
177+
std::cout << "Notice the excessive amount of tables left behind!" << std::endl;
178+
LuaVal nested = {};
129179
nested[1];
130180
nested[2];
131181
nested[3];
132-
nested[4][5][6];
133-
std::cout << nested.dumps() << std::endl;
182+
nested[4][5][6]; // handy for quick creation of nested indexes
183+
std::cout << nested.dumps() << std::endl; // Outputs {{},{},{},{5:{6:{}}}}
184+
std::cout << std::endl;
134185

186+
// To avoid unnecessary tables, use set and get (similar to at in c++ for map)
135187
LuaVal table(TTABLE);
136188
table.set(1, "test");
137189
std::cout << table.dumps() << std::endl;
138-
table.set(1, LuaVal::nil);
190+
table.set(1, LuaVal::nil); // removing value through setting it to nil
139191
std::cout << table.dumps() << std::endl;
140192
table.set(table, "table as key?");
141193
std::cout << table.dumps() << std::endl;
@@ -145,6 +197,8 @@ int main()
145197

146198
{
147199
std::cout << "test .(key).(key, val).rem(key)" << std::endl;
200+
std::cout << "Note that table keys cannot be accessed!" << std::endl;
201+
148202
LuaVal table(TTABLE);
149203
table.set(1, "test").set(2, table.get(1)).set(3, -324);
150204
std::cout << table.dumps() << std::endl;
@@ -157,10 +211,11 @@ int main()
157211

158212
{
159213
std::cout << "test .insert.remove.len" << std::endl;
214+
160215
LuaVal table(TTABLE);
161216
table.insert("test");
162217
table.insert(123);
163-
table.set(std::string("rand"), LuaVal(893));
218+
table.set("rand", 893);
164219
table.insert(345);
165220
std::cout << table.len() << std::endl;
166221
table.remove();
@@ -172,51 +227,41 @@ int main()
172227
{
173228
LuaVal val = 5;
174229
LuaVal val2 = LuaVal::nil;
175-
assert(val);
176-
assert(!val2);
177-
assert(val == 5);
178-
assert(val != 6);
179-
std::cout << "comparison and bool() checks passed" << std::endl;
230+
if (val)
231+
std::cout << "bool() works" << std::endl;
232+
if (!val2)
233+
std::cout << "bool() works" << std::endl;
234+
if (val == 5)
235+
std::cout << "== works" << std::endl;
236+
if (val != 6)
237+
std::cout << "!= works" << std::endl;
180238
}
181239

182240
{
183241
LuaVal val(TTABLE);
184242
val[123] = 5;
185-
val[LuaVal("test")] = 5;
186-
val[LuaVal(1.5)] = 5;
243+
val["test"] = 5;
244+
val[1.5] = 5;
187245
}
188246

189247
{
190-
LuaVal merge_right = LuaVal::table();
191-
merge_right.set(LuaVal("ke"), LuaVal("test"));
192-
LuaVal val1 = {
193-
LuaVal(1),
194-
LuaVal(2),
195-
LuaVal::mrg(LuaVal{ LuaVal(3), LuaVal(4) }, merge_right)
196-
};
248+
LuaVal val1 = { 1,2, LuaVal::mrg({ 3,4 }, LuaVal::LuaTable({ { "ke","test" } })) };
197249
LuaVal val2 = LuaVal::loads("{1,2,{3,4,'ke':'test'}}");
198-
assert(val1.dumps() == val2.dumps());
199250
std::cout << val1.dumps() << std::endl;
251+
std::cout << val2.dumps() << std::endl;
200252
}
201253

202254
{
203-
LuaVal t1 = { LuaVal(1), LuaVal(2), { LuaVal(1), LuaVal(2), LuaVal(3) } };
204-
LuaVal t2 = LuaVal::table();
205-
t2.set(LuaVal("key"), LuaVal("value"));
206-
t2.set(LuaVal(2), LuaVal("value2"));
255+
LuaVal t1 = { 1, 2, { 1,2,3 } };
256+
LuaVal t2 = LuaVal::LuaTable{ { "key", "value" }, { 2, "value2" } };
207257
std::cout << t1.dumps() << std::endl;
208258
std::cout << t2.dumps() << std::endl;
209259
}
210260

211-
{
212-
std::forward_list<std::deque<std::string>> rows = { { "a", "b" }, { "a", "b" } };
213-
LuaVal nested(TTABLE);
214-
unsigned int index = 0;
215-
for (auto const & row : rows)
216-
nested.set(++index, lua_val::array(row));
217-
std::cout << nested.dumps() << std::endl;
218-
}
219-
220-
std::cout << "demo finished successfully" << std::endl;
261+
std::forward_list<std::deque<std::string>> vec = { { "a", "b" },{ "a", "b" } };
262+
std::unordered_map<std::string, std::string> m;
263+
m["test"] = "asd";
264+
LuaVal t441 = vec;
265+
std::cout << t441.dumps() << std::endl;
221266
return 0;
222267
}

‎smallfolk.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ void LuaVal::InitializeSequence(std::initializer_list<LuaVal> const & l)
136136
if (vv.isnil())
137137
++i;
138138
else
139-
set(++i, std::move(vv));
139+
set(static_cast<int>(++i), std::move(vv));
140140
}
141141
}
142142

@@ -1027,7 +1027,7 @@ LuaVal Serializer::expect_object(std::string const & string, size_t & i, Seriali
10271027
}
10281028
else
10291029
{
1030-
nt.set(j, k);
1030+
nt.set(static_cast<int>(j), k);
10311031
++j;
10321032
}
10331033
char head = strat(string, i);

0 commit comments

Comments
 (0)