github.com/hirochachacha/plua@v0.0.0-20170217012138-c82f520cc725/README.md (about)

     1  plua
     2  ====
     3  
     4  [![GoDoc](https://godoc.org/github.com/hirochachacha/plua.svg?status.svg)](http://godoc.org/github.com/hirochachacha/plua)
     5  [![Build Status](https://travis-ci.org/hirochachacha/plua.svg?branch=master)](https://travis-ci.org/hirochachacha/plua)
     6  [![Code Climate](https://codeclimate.com/github/hirochachacha/plua/badges/gpa.svg)](https://codeclimate.com/github/hirochachacha/plua)
     7  [![Test Coverage](https://codeclimate.com/github/hirochachacha/plua/badges/coverage.svg)](https://codeclimate.com/github/hirochachacha/plua/coverage)
     8  
     9  Description
    10  -----------
    11  
    12  Lua 5.3 implementation.
    13  
    14  ```go
    15  package main
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  
    21  	"github.com/hirochachacha/plua/compiler"
    22  	"github.com/hirochachacha/plua/runtime"
    23  	"github.com/hirochachacha/plua/stdlib"
    24  )
    25  
    26  var input = `
    27  -- example code is taken from https://tour.golang.org/concurrency/5
    28  
    29  function fibonacci(ch, quit)
    30    local x, y = 0, 1
    31    while true do
    32      local chosen, recv, recvOK = goroutine.select(
    33        goroutine.case("send", ch, x),
    34        goroutine.case("recv", quit)
    35      )
    36  
    37      if chosen == 1 then
    38        x, y = y, x+y
    39      elseif chosen == 2 then
    40  	  print("quit")
    41        return
    42      end
    43    end
    44  end
    45  
    46  ch = goroutine.newchannel()
    47  quit = goroutine.newchannel()
    48  
    49  goroutine.wrap(function()
    50    for i = 1, 10 do
    51      print(ch:recv())
    52    end
    53    quit:send(nil)
    54  end)()
    55  
    56  fibonacci(ch, quit)
    57  
    58  return "ok"
    59  `
    60  
    61  func main() {
    62  	c := compiler.NewCompiler()
    63  
    64  	proto, err := c.Compile(strings.NewReader(input), "=input.lua", compiler.Text)
    65  	if err != nil {
    66  		panic(err)
    67  	}
    68  
    69  	p := runtime.NewProcess()
    70  
    71  	p.Require("", stdlib.Open)
    72  
    73  	rets, err := p.Exec(proto)
    74  	if err != nil {
    75  		// object.PrintError(err) // print traceback from error
    76  		panic(err)
    77  	}
    78  
    79  	fmt.Println(rets[0])
    80  }
    81  ```
    82  Output:
    83  ```
    84  0	true
    85  1	true
    86  1	true
    87  2	true
    88  3	true
    89  5	true
    90  8	true
    91  13	true
    92  21	true
    93  34	true
    94  quit
    95  ok
    96  ```