September 12, 2019

409 words 2 mins read

d5/tengo

d5/tengo

A fast script language for Go

repo name d5/tengo
repo link https://github.com/d5/tengo
homepage https://tengolang.com
language Go
size (curr.) 1108 kB
stars (curr.) 1568
created 2019-01-09
license MIT License

The Tengo Language

GoDoc Go Report Card CircleCI

Tengo is a small, dynamic, fast, secure script language for Go.

Tengo is fast and secure because it’s compiled/executed as bytecode on stack-based VM that’s written in native Go.

/* The Tengo Language */
fmt := import("fmt")

each := func(seq, fn) {
    for x in seq { fn(x) }
}

sum := func(init, seq) {
    each(seq, func(x) { init += x })
    return init
}

fmt.println(sum(0, [1, 2, 3]))   // "6"
fmt.println(sum("", [1, 2, 3]))  // "123"

Test this Tengo code in the Tengo Playground

Features

Benchmark

fib(35) fibt(35) Type
Go 48ms 3ms Go (native)
Tengo 2,349ms 5ms VM on Go
Lua 1,416ms 3ms Lua (native)
go-lua 4,402ms 5ms Lua VM on Go
GopherLua 4,023ms 5ms Lua VM on Go
Python 2,588ms 26ms Python (native)
starlark-go 11,126ms 6ms Python-like Interpreter on Go
gpython 15,035ms 4ms Python Interpreter on Go
goja 5,089ms 5ms JS VM on Go
otto 68,377ms 11ms JS Interpreter on Go
Anko 92,579ms 18ms Interpreter on Go

* fib(35): Fibonacci(35)
* fibt(35): tail-call version of Fibonacci(35)
* Go does not read the source code from file, while all other cases do
* See here for commands/codes used

Quick Start

go get github.com/d5/tengo/v2

A simple Go example code that compiles/runs Tengo script code with some input/output values:

package main

import (
	"context"
	"fmt"

	"github.com/d5/tengo/v2"
)

func main() {
	// Tengo script code
	src := `
each := func(seq, fn) {
    for x in seq { fn(x) }
}

sum := 0
mul := 1
each([a, b, c, d], func(x) {
	sum += x
	mul *= x
})`

	// create a new Script instance
	script := tengo.NewScript([]byte(src))

	// set values
	_ = script.Add("a", 1)
	_ = script.Add("b", 9)
	_ = script.Add("c", 8)
	_ = script.Add("d", 4)

	// run the script
	compiled, err := script.RunContext(context.Background())
	if err != nil {
		panic(err)
	}

	// retrieve values
	sum := compiled.Get("sum")
	mul := compiled.Get("mul")
	fmt.Println(sum, mul) // "22 288"
}

References

comments powered by Disqus