~philho/+junk/Lua

1 by plhoste
Initial import of Lua scripts (take 2...)
1
dofile[[../Lua/DumpObject.lua]]
2
3
function Foo(a, b)
4
	return (a + b) / a * b
5
end
6
a = { _ = true, __ = false }
7
b = { 'a', 'b' }
8
9
t =
10
{
11
	a =
12
	{
13
		x = 5.0,
14
		y = 104.3,
15
	},
16
	b =
17
	{
18
		-- Function as key
19
		[Foo] = 'Function!',
20
		-- Numerical index
21
		[5] = 42,
22
		-- Simple string index ('simple' means having the syntax of variables)
23
		ls = [[
24
Multiline
25
Value
26
]],
18 by PhiLho
Improving DumpObject to handle nicely the array part of the tables
27
		-- Complex string index (not identifier like)
1 by plhoste
Initial import of Lua scripts (take 2...)
28
		['Regular Expression'] = [[(?:\d{1,3}\.){3}\d{1,3}]],
29
		keywords =
30
		{
31
			-- Keywords are 'simple' but must be quoted:
32
			['if'] = 1,
33
			['else'] = 1,
34
		},
35
		-- A simple array
36
		sar =
37
		{
38
			"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
39
		},
40
		-- An array of tables
41
		tar =
42
		{
43
			{
44
				kw1 = 'and',
45
				kw2 = 'or',
46
				kw3 = 'not',
47
			},
48
			{
49
				op1 = '*',
50
				op2 = '+',
51
				op3 = '-',
52
			},
53
		},
18 by PhiLho
Improving DumpObject to handle nicely the array part of the tables
54
		-- An array of arrays
55
		abracadabra =
56
		{
57
			{ 'ichi', 'ni', 'san', 'shi', 'go' },
58
			{ 1, 2, 3, 4, 5 },
59
			{ '-', '--', '---', '0', 'Z' }
60
		},
61
		-- Mixed table
62
		mixed = { 'a', 'bb', 'ccc', d = 'dddd', 'eeeee', [7] = 'ffffff', 'ggggggg' },
1 by plhoste
Initial import of Lua scripts (take 2...)
63
		-- Empty table
64
		empty = {},
65
		-- Table with holes
66
		holes = { 1, nil, {}, nil, 2 },
67
	},
68
	others =
69
	{
70
		-- Function storage
71
		functions =
72
		{
73
			f = function () return math.random(100) end,
74
			ff = Foo,
75
		},
76
		-- Table as key
77
		tables =
78
		{
79
			[a] = '',
80
			[b] = "",
81
			-- This one is different of the previous one because the created
82
			-- table is different of b. You cannot reference it directly,
83
			-- it is reachable only by iterating on all keys/values...
84
			[ { 'a', 'b' } ] = { "aa", "bb" },
85
			-- And now for the empty table key
86
			[ {} ] = "tada!",
87
		},
88
		-- Complex strings as key
89
		strings =
90
		{
91
			[ [[C:\Lua\Lua.exe]] ] = true,
92
			[ "O'Neil" ] = false,
93
			[ 'Will "Liam" Cheese Pear' ] = not nil,
94
		},
95
		-- Boolean as key
96
		[true] = a,
97
		[false] = a,
98
	}
99
}
18 by PhiLho
Improving DumpObject to handle nicely the array part of the tables
100
-- References in the table
1 by plhoste
Initial import of Lua scripts (take 2...)
101
t.others.tables.reference = t.a
102
t.others.tables.otherRef = b
103
t[ { t.a, t.b } ] = t.others.functions
104
105
--~ print(t.f(), t.f(), t.f())
106
--~ print(t.reference.x, t.b[5], t.b.ls, t[true])
107
18 by PhiLho
Improving DumpObject to handle nicely the array part of the tables
108
print("--== Formatted dump")
1 by plhoste
Initial import of Lua scripts (take 2...)
109
print("\ndo\n" .. DumpObject(t) .. "\nreturn T\n\nend\n")
18 by PhiLho
Improving DumpObject to handle nicely the array part of the tables
110
111
print("\n\n--== In line dump")
1 by plhoste
Initial import of Lua scripts (take 2...)
112
print("\n" .. DumpObject(t, '', ' '))
113
114
-- This one doesn't work, I don't handle cyclic references!
115
18 by PhiLho
Improving DumpObject to handle nicely the array part of the tables
116
print("\n\n--== Failed attempt to handle cyclic references")
1 by plhoste
Initial import of Lua scripts (take 2...)
117
a =
118
{
119
	x = 5,
120
	y = 42
121
}
122
b =
123
{
124
	a,
125
	"Foo"
126
}
127
a.ref = b
128
129
print("\ndo\n" .. DumpObject(b) .. "\nreturn T\n\nend\n")
130
18 by PhiLho
Improving DumpObject to handle nicely the array part of the tables
131
--~ print(t.b.tar[2].op1) -- Testing syntax of table field access