-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdemo.lua
203 lines (117 loc) · 2.44 KB
/
demo.lua
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
--[[ DO NOT MODIFY - COMPILED FROM demo.sol --]] -----------------------------------------------------
--[[
# SOL IS:
* Lua + static typing
* Flexible, plausible typing
# SOL IS NOT:
* Waterproof
* More than Lua (yet!)
--]]
-----------------------------------------------------
-- Undefined globals
x = 1337
local a = x
-----------------------------------------------------
-- Argument checking
local Foo = {}
--Foo.member()
Foo:member()
Foo.member(Foo)
--Foo:member(Foo)
-- Note - no forward-declare needed!
function Foo:member()
--self.member()
self:member()
self.member(self)
--self:member(self)
end
-----------------------------------------------------
-- pairs/ipairs
do
local list = { "one", "two", "three" }
for i,v in ipairs(list) do
print(i .. ': ' .. v)
end
local map = { ['one'] = 1, ['two'] = 2, ['three'] = 3 }
for k,v in pairs(map) do
k = k .. '42'
v = v + 42
end
end
-----------------------------------------------------
-- Add a type annotation to the function
local function int_to_string(arg)
return '' .. arg
end
local function string_to_int(arg)
if arg then
return tonumber(arg)
else
return 0
end
end
local i = string_to_int("42")
i = i + 42
local function cmp(a, b)
--return a == b
--return true
return nil
end
-----------------------------------------------------
-- What is the return type of this?
local function win_or_fail()
if math.random() < 0.5 then
return true, nil
else
return false, "bad luck"
end
end
local win, err_msg = win_or_fail()
-----------------------------------------------------
-- var vs local
local local_can_be_anything = require 'unfindable'
--var var_must_be_deducible = some_lua_function()
local var_can_be_explicit = require 'unfindable'
-----------------------------------------------------
-- Maps and lists:
do
local list = {1, 2, 3}
list[1] = 42
local map
= {}
map[1] = 'one'
map[2] = 'two'
--map['three'] = 3
end
local ok
= 'yes'
--var bad = 'wrong' : Tribool
local function do_stuff(how)
end
do_stuff("quickly")
local function filter_number(fp, doWhat)
for i=1,42 do
if fp(i) then
doWhat(i)
end
end
end
filter_number(
function(i) return i%2==1 end,
function(i) print(i .. " is odd") end
)
local function work_on_node(n)
if n.tag == 'Foo' then
local f = n
end
local a = n.wild
end
work_on_node({
tag = 'Foo';
korv = 32
})
--[[
work_on_node({
tag = 'Baz'
})
--]]