Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.

Commit bbde613

Browse files
committed
initial public commit of presentation
Removed work-specific stuff just in case
0 parents  commit bbde613

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+4907
-0
lines changed

assets/gopherlua.png

201 KB
Loading

assets/luapgo.png

45.9 KB
Loading

assets/userdata_cpp.png

58.9 KB
Loading

examples/classes/classes.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package main
2+
3+
import (
4+
"github.com/yuin/gopher-lua"
5+
)
6+
7+
// START OMIT
8+
const scr = `
9+
-- luastart // OMIT
10+
local Player = {}
11+
Player.__index = Player -- magic!
12+
13+
function Player.new(name)
14+
local self = setmetatable({}, Player) -- more magic!
15+
self.name = name
16+
self.health = 100
17+
return self
18+
end
19+
20+
function Player:hit(dmg) self.health = self.health - dmg end
21+
function Player:alive() return self.health > 0 end
22+
23+
-- create a new Player
24+
local p = Player.new("chrsm")
25+
p:hit(150)
26+
if not p:alive() then
27+
print(p.name .. " is dead!")
28+
end -- output: chrsm is dead!
29+
-- luaend // OMIT
30+
`
31+
32+
// END OMIT
33+
34+
func main() {
35+
vm := lua.NewState()
36+
defer vm.Close()
37+
38+
if err := vm.DoString(scr); err != nil {
39+
panic(err)
40+
}
41+
}

examples/classes/classes.lua

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
local Player = {}
2+
Player.__index = Player
3+
4+
function Player.new(name)
5+
local self = setmetatable({}, Player)
6+
self.name = name
7+
self.health = 100
8+
return self
9+
end
10+
11+
function Player:hit(dmg) self.health = self.health - dmg end
12+
function Player:alive() return self.health > 0 end
13+
14+
-- create a new Player
15+
local p = Player.new("chrsm")
16+
p:hit(150)
17+
if not p:alive() then
18+
print(p.name .. " is dead!")
19+
end -- output: chrsm is dead!

examples/cond/cond.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package main
2+
3+
import (
4+
"github.com/yuin/gopher-lua"
5+
)
6+
7+
// START OMIT
8+
const scr = `
9+
-- luastart // OMIT
10+
if a then
11+
print("a exists!")
12+
elseif not a then
13+
print("a doesn't exist!")
14+
end
15+
16+
b = 2
17+
if b ~= 1 then
18+
print("b != 1")
19+
end
20+
21+
a = 10
22+
while a > 0 do
23+
a = a-1
24+
if a % 2 == 0 then print("a = ", a) end
25+
end
26+
27+
b = 10
28+
repeat
29+
b = b - 1
30+
if b % 2 == 0 then print("b = ", b) end
31+
until b == 0
32+
-- luaend // OMIT
33+
`
34+
35+
// END OMIT
36+
37+
func main() {
38+
vm := lua.NewState()
39+
defer vm.Close()
40+
41+
if err := vm.DoString(scr); err != nil {
42+
panic(err)
43+
}
44+
}

examples/coro/coro.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package main
2+
3+
import (
4+
"github.com/yuin/gopher-lua"
5+
)
6+
7+
// START OMIT
8+
const scr = `
9+
-- luastart // OMIT
10+
local yield, resume, status =
11+
coroutine.yield, coroutine.resume, coroutine.status
12+
13+
local coro = coroutine.create(function(initial, max)
14+
local v = initial
15+
while v < max do
16+
yield(v)
17+
v = v + 1
18+
end
19+
20+
return v -- get to max
21+
end)
22+
23+
while status(coro) ~= "dead" do
24+
ok, v = resume(coro, 10, 15)
25+
print("v =", v, ok, status(coro))
26+
end
27+
28+
-- luaend // OMIT
29+
`
30+
31+
// END OMIT
32+
33+
func main() {
34+
vm := lua.NewState()
35+
defer vm.Close()
36+
37+
if err := vm.DoString(scr); err != nil {
38+
panic(err)
39+
}
40+
}

examples/coro/coro.lua

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
local c = coroutine.create(function(a, b)
2+
print("args: " .. tostring(a) .. "," .. tostring(b))
3+
4+
a, b = coroutine.yield(a+b, 0)
5+
print("back inside: " .. tostring(a) .. "," .. tostring(b))
6+
7+
return "done"
8+
end)
9+
10+
local ok, inita, initb = coroutine.resume(c, 1, 2)
11+
print("initial: " .. tostring(inita) .. "," .. tostring(initb))
12+
ok, inita, initb = coroutine.resume(c, 3, 4)
13+
print("second: " .. tostring(inita) .. "," .. tostring(initb))
14+
print("ret: " .. tostring(coroutine.status(c)))
15+
print("typ: " .. tostring(type(c)))

examples/fn/fn.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
// START OMIT
4+
import (
5+
"github.com/yuin/gopher-lua"
6+
)
7+
8+
const scr = `
9+
-- luastart // OMIT
10+
function add(a, b)
11+
return a+b
12+
end
13+
14+
function run(fn, ...)
15+
return fn(...)
16+
end
17+
18+
print(run(add, 1, 2))
19+
20+
run(function(a, b)
21+
print(a, b)
22+
end, 1, 2)
23+
-- luaend // OMIT
24+
`
25+
26+
func main() {
27+
vm := lua.NewState()
28+
defer vm.Close()
29+
30+
if err := vm.DoString(scr); err != nil {
31+
panic(err)
32+
}
33+
}
34+
35+
//END OMIT

examples/forl/forl.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package main
2+
3+
import (
4+
"github.com/yuin/gopher-lua"
5+
)
6+
7+
// START OMIT
8+
const scr = `
9+
-- luastart // OMIT
10+
-- for init,expr do
11+
-- note: init is explicitly local!
12+
for i=1,10 do
13+
if i % 2 == 0 then
14+
print("i =", i)
15+
end
16+
end
17+
print("i =", i)
18+
19+
-- for init,expr,postexpr
20+
for j=1,10,2 do
21+
print("j =", j)
22+
end
23+
24+
-- break
25+
for k=1,10 do
26+
print("k =", k)
27+
break
28+
end
29+
30+
-- luaend // OMIT
31+
`
32+
33+
// END OMIT
34+
35+
func main() {
36+
vm := lua.NewState()
37+
defer vm.Close()
38+
39+
if err := vm.DoString(scr); err != nil {
40+
panic(err)
41+
}
42+
}

0 commit comments

Comments
 (0)