-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
262 lines (234 loc) · 9.2 KB
/
main.cpp
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#include "smallfolk.h"
#include <iostream> // std::cout
#include <cassert> // assert
#include <map>
int main()
{
{
std::cout << "Test values" << std::endl;
LuaVal asd = { "number", "string", "table", LuaVal::mrg({"number", "string"}, LuaVal::LuaTable({{"ke", "test"},{"ke2", "test"}})) };
std::cout << asd.dumps() << std::endl;
std::string err;
LuaVal v = LuaVal::loads(" { 1 , 2 , { 3 , 4, ' k e ' : ' t e s t ' } } ", &err);
std::cout << v.dumps(&err) << std::endl;
std::cout << err << std::endl;
std::cout << "Testing different double corner values" << std::endl;
double _zero = 0.0;
LuaVal tn = { -(0 / _zero), (0 / _zero), (1 / _zero), -(1 / _zero) };
std::cout << tn.dumps() << std::endl;
std::cout << -(0 / _zero) << " " << (0 / _zero) << " " << (1 / _zero) << " " << -(1 / _zero) << std::endl;
std::cout << tn.get(1).tostring() << " " << tn.get(2).tostring() << " " << tn.get(3).tostring() << " " << tn.get(4).tostring() << std::endl;
tn = LuaVal::loads(tn.dumps());
std::cout << tn.get(1).tostring() << " " << tn.get(2).tostring() << " " << tn.get(3).tostring() << " " << tn.get(4).tostring() << std::endl;
std::cout << std::endl;
std::cout << "Testing creation testing and printing of all value types" << std::endl;
LuaVal implicit_test = -123;
LuaVal copy_test(implicit_test);
LuaVal copy_test2 = implicit_test;
LuaVal n = LuaVal::nil; // nil
LuaVal n2(TNIL); // nil
LuaVal b(true);
LuaVal s("somestring");
LuaVal d(123.456);
LuaVal f(123.456f);
LuaVal i(-678);
LuaVal u(0xFFFFFFFF);
LuaVal t; // defaults to table
LuaVal t2 = LuaVal::table();
LuaVal t3 = { 1, 2, 3 };
LuaVal t4 = {}; // curly braces are table
LuaVal t5(TTABLE);
assert(implicit_test.isnumber());
assert(copy_test.isnumber());
assert(copy_test2.isnumber());
assert(n.isnil());
assert(n2.isnil());
assert(b.isbool());
assert(s.isstring());
assert(d.isnumber());
assert(f.isnumber());
assert(i.isnumber());
assert(u.isnumber());
assert(t.istable());
assert(t2.istable());
assert(t3.istable());
assert(t4.istable());
assert(t5.istable());
std::cout << implicit_test.tostring() << std::endl;
std::cout << copy_test.tostring() << std::endl;
std::cout << copy_test2.tostring() << std::endl;
std::cout << n.tostring() << std::endl;
std::cout << b.tostring() << std::endl;
std::cout << s.tostring() << std::endl;
std::cout << d.tostring() << std::endl;
std::cout << f.tostring() << std::endl;
std::cout << i.tostring() << std::endl;
std::cout << u.tostring() << std::endl;
std::cout << t.tostring() << std::endl;
std::cout << t2.tostring() << std::endl;
std::cout << t3.tostring() << std::endl;
std::cout << std::endl;
std::cout << "Testing exception handling" << std::endl;
std::string errmsg;
try
{
LuaVal h(-7);
std::string str = h.str(); // error, h is not a string
}
catch (smallfolk_exception const & e)
{
// caught an exception
errmsg = e.what();
}
// printing caught error if any
if (!errmsg.empty())
std::cout << errmsg << std::endl << std::endl;
}
{
std::cout << "Example usage" << std::endl;
// create a lua table and set some values to it
LuaVal table = LuaVal::table();
table.set(1, "Hello");
table.set("test", "world");
table.set(67.5, -234.5);
// serialize the table
std::string serialized = table.dumps();
// print the serialization, it should be rather human readable
std::cout << serialized << std::endl;
// Example output: {"Hello","test":"world",67.5:-234.5}
// form lua values from the string
LuaVal deserialized = LuaVal::loads(serialized);
// print the values from deserialized result table
std::cout << deserialized.get(1).str() << " " << deserialized.get("test").str() << " " << deserialized.get(67.5).num() << std::endl;
// Example output: Hello world -234.5
std::cout << std::endl;
}
/*
// This is disabled because circular references cause memleak or need complex handling for memory management
// Circular references should not be used and are ignored (nil) when parsed
// Using a circular reference in C++ code will cause an exception to be thrown
{
std::cout << "Cthulhu" << std::endl;
// Essentially {{},{},{}}
LuaVal cthulhu(TTABLE);
cthulhu[1] = LuaVal(TTABLE);
cthulhu[2] = LuaVal(TTABLE);
cthulhu[3] = LuaVal(TTABLE);
cthulhu["fhtagn"] = cthulhu;
cthulhu[1][cthulhu[2]] = cthulhu[3];
cthulhu[2][cthulhu[1]] = cthulhu[2];
cthulhu[3][cthulhu[3]] = cthulhu;
std::cout << cthulhu.dumps() << std::endl;
// prints:
// {"fhtagn":@1,1:{{@2:@3}:{@4:@1}},2:@3,3:@4}
std::cout << std::endl;
}
{
std::cout << "Table inside itself" << std::endl;
try
{
LuaVal tbl(TTABLE);
tbl.set(1, tbl);
std::cout << tbl.dumps() << std::endl;
}
catch (smallfolk_exception const & e)
{
std::cout << e.what() << std::endl;
}
std::cout << std::endl;
}
*/
{
std::cout << "Table initializer list coolness" << std::endl;
std::cout << "{} evaluates to table" << std::endl;
std::cout << "{{}} evaluates to a table inside a table" << std::endl;
std::cout << "{5} evaluates to 5 being inside a table" << std::endl;
std::cout << "{LuaVal(5)} evaluates to 5 inside a table" << std::endl;
LuaVal nested = { {}, {{}}, { 3 }, { LuaVal(4) } };
std::cout << nested.dumps() << std::endl; // Outputs {{},{{}},{3},{4}}
std::cout << std::endl;
}
{
std::cout << "test accessing table with [] operator" << std::endl;
std::cout << "Note that table keys cannot be accessed!" << std::endl;
std::cout << "Notice the excessive amount of tables left behind!" << std::endl;
LuaVal nested = {};
nested[1];
nested[2];
nested[3];
nested[4][5][6]; // handy for quick creation of nested indexes
std::cout << nested.dumps() << std::endl; // Outputs {{},{},{},{5:{6:{}}}}
std::cout << std::endl;
// To avoid unnecessary tables, use set and get (similar to at in c++ for map)
LuaVal table(TTABLE);
table.set(1, "test");
std::cout << table.dumps() << std::endl;
table.set(1, LuaVal::nil); // removing value through setting it to nil
std::cout << table.dumps() << std::endl;
table.set(table, "table as key?");
std::cout << table.dumps() << std::endl;
std::cout << table.get(table).tostring() << std::endl;
std::cout << std::endl;
}
{
std::cout << "test .(key).(key, val).rem(key)" << std::endl;
std::cout << "Note that table keys cannot be accessed!" << std::endl;
LuaVal table(TTABLE);
table.set(1, "test").set(2, table.get(1)).set(3, -324);
std::cout << table.dumps() << std::endl;
table.rem(1).rem(2).set(3, LuaVal());
std::cout << table.dumps() << std::endl;
table.set(table, "table as key?");
std::cout << table.get(table).tostring() << std::endl;
std::cout << std::endl;
}
{
std::cout << "test .insert.remove.len" << std::endl;
LuaVal table(TTABLE);
table.insert("test");
table.insert(123);
table.set("rand", 893);
table.insert(345);
std::cout << table.len() << std::endl;
table.remove();
table.remove();
std::cout << table.len() << std::endl;
std::cout << std::endl;
}
{
LuaVal val = 5;
LuaVal val2 = LuaVal::nil;
if (val)
std::cout << "bool() works" << std::endl;
if (!val2)
std::cout << "bool() works" << std::endl;
if (val == 5)
std::cout << "== works" << std::endl;
if (val != 6)
std::cout << "!= works" << std::endl;
}
{
LuaVal val(TTABLE);
val[123] = 5;
val["test"] = 5;
val[1.5] = 5;
}
{
LuaVal val1 = { 1,2, LuaVal::mrg({ 3,4 }, LuaVal::LuaTable({ { "ke","test" } })) };
LuaVal val2 = LuaVal::loads("{1,2,{3,4,'ke':'test'}}");
std::cout << val1.dumps() << std::endl;
std::cout << val2.dumps() << std::endl;
}
{
LuaVal t1 = { 1, 2, { 1,2,3 } };
LuaVal t2 = LuaVal::LuaTable{ { "key", "value" }, { 2, "value2" } };
std::cout << t1.dumps() << std::endl;
std::cout << t2.dumps() << std::endl;
}
std::forward_list<std::deque<std::string>> vec = { { "a", "b" },{ "a", "b" } };
std::unordered_map<std::string, std::string> m;
m["test"] = "asd";
LuaVal t441 = vec;
std::cout << t441.dumps() << std::endl;
return 0;
}