tengo2lua

package module
v0.0.0-...-eaf3841 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 14, 2019 License: MIT Imports: 9 Imported by: 0

README

Tengo2Lua

An experimental transpiler to convert Tengo source code into Lua code.

Limitations
  • Tengo module system is not implemented.
  • Tengo int and float values are both converted into Number values in Lua.
  • Array slicing is not implemented ([1,2,3,4,5][1:3]). But string slicing is implemented.
  • Character literal is not supported.
  • String indexing is not supported.
  • Bitwise operators is not implemented.
  • Different boolean truthiness
  • Different type coercion logic
Example

Tengo code:

each := func(x, f) { for k, v in x { f(k, v) } }
sum := 0
each([1, 2, 3], func(i, v) { sum += v })

is converted into:

function __iter__(v)
  if v.__a then
    local idx = 0
	return function()
      if v[idx] == nil then
        return nil
      end
      idx = idx + 1
      return idx-1, v[idx-1]
    end
  else
    return pairs(v)
  end
end
getmetatable("").__add=function(a,b) return a..b end
local each=function(x,f)
  for k, v in __iter__(x) do
    local __cont_1__ = false
    repeat
      f(k,v)
      __cont_1__ = true
    until 1
    if not __cont_1__ then break end
  end
end

local sum=0
each({[0]=(1),(2),(3), __a=true},function(i,v)
  sum=sum+v
end
)

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Error

type Error struct {
	// contains filtered or unexported fields
}

func (*Error) Error

func (e *Error) Error() string

type Options

type Options struct {
	// EnableGlobalScope creates global variables if it's set to true.
	// If not, global variables in Tengo code will be transpiled as local
	// variables in Lua.
	EnableGlobalScope bool

	// Indent string is added whenever the block level increases.
	Indent string
}

Options represents a set of options for Transpiler.

func DefaultOptions

func DefaultOptions() *Options

DefaultOptions creates a default option for Transpiler.

type Transpiler

type Transpiler struct {
	// contains filtered or unexported fields
}

Transpiler converts Tengo source code into Lua code.

Example
package main

import (
	"fmt"

	"github.com/d5/tengo2lua"
)

func main() {
	src := []byte(`
each := func(x, f) { for k, v in x { f(k, v) } }
sum := 0
each([1, 2, 3], func(i, v) { sum += v })
`)

	t := tengo2lua.NewTranspiler(src, nil)
	dst, err := t.Convert()
	if err != nil {
		panic(err)
	}

	fmt.Println(string(dst))
}
Output:

func NewTranspiler

func NewTranspiler(src []byte, opts *Options) *Transpiler

NewTranspiler creates a new Transpiler.

func (*Transpiler) Convert

func (t *Transpiler) Convert() (output string, err error)

Convert converts the input Tengo source code.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL