https://github.com/hirochachacha/plua
Lua 5.3 implementation (WIP)
https://github.com/hirochachacha/plua
go golang language lua
Last synced: 13 days ago
JSON representation
Lua 5.3 implementation (WIP)
- Host: GitHub
- URL: https://github.com/hirochachacha/plua
- Owner: hirochachacha
- License: other
- Created: 2016-08-28T01:50:52.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-02-17T01:22:10.000Z (almost 9 years ago)
- Last Synced: 2024-08-03T23:27:57.202Z (over 1 year ago)
- Topics: go, golang, language, lua
- Language: Go
- Homepage:
- Size: 915 KB
- Stars: 10
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-golang-repositories - plua
README
plua
====
[](http://godoc.org/github.com/hirochachacha/plua)
[](https://travis-ci.org/hirochachacha/plua)
[](https://codeclimate.com/github/hirochachacha/plua)
[](https://codeclimate.com/github/hirochachacha/plua/coverage)
Description
-----------
Lua 5.3 implementation.
```go
package main
import (
"fmt"
"strings"
"github.com/hirochachacha/plua/compiler"
"github.com/hirochachacha/plua/runtime"
"github.com/hirochachacha/plua/stdlib"
)
var input = `
-- example code is taken from https://tour.golang.org/concurrency/5
function fibonacci(ch, quit)
local x, y = 0, 1
while true do
local chosen, recv, recvOK = goroutine.select(
goroutine.case("send", ch, x),
goroutine.case("recv", quit)
)
if chosen == 1 then
x, y = y, x+y
elseif chosen == 2 then
print("quit")
return
end
end
end
ch = goroutine.newchannel()
quit = goroutine.newchannel()
goroutine.wrap(function()
for i = 1, 10 do
print(ch:recv())
end
quit:send(nil)
end)()
fibonacci(ch, quit)
return "ok"
`
func main() {
c := compiler.NewCompiler()
proto, err := c.Compile(strings.NewReader(input), "=input.lua", compiler.Text)
if err != nil {
panic(err)
}
p := runtime.NewProcess()
p.Require("", stdlib.Open)
rets, err := p.Exec(proto)
if err != nil {
// object.PrintError(err) // print traceback from error
panic(err)
}
fmt.Println(rets[0])
}
```
Output:
```
0 true
1 true
1 true
2 true
3 true
5 true
8 true
13 true
21 true
34 true
quit
ok
```