README.md
1<p align="center">
2 <img src="https://raw.githubusercontent.com/d5/tengolang-share/master/logo_400.png" width="200" height="200">
3</p>
4
5# The Tengo Language
6
7[![GoDoc](https://godoc.org/github.com/d5/tengo?status.svg)](https://godoc.org/github.com/d5/tengo)
8![test](https://github.com/d5/tengo/workflows/test/badge.svg)
9[![Go Report Card](https://goreportcard.com/badge/github.com/d5/tengo)](https://goreportcard.com/report/github.com/d5/tengo)
10
11**Tengo is a small, dynamic, fast, secure script language for Go.**
12
13Tengo is **[fast](#benchmark)** and secure because it's compiled/executed as
14bytecode on stack-based VM that's written in native Go.
15
16```golang
17/* The Tengo Language */
18fmt := import("fmt")
19
20each := func(seq, fn) {
21 for x in seq { fn(x) }
22}
23
24sum := func(init, seq) {
25 each(seq, func(x) { init += x })
26 return init
27}
28
29fmt.println(sum(0, [1, 2, 3])) // "6"
30fmt.println(sum("", [1, 2, 3])) // "123"
31```
32
33> Test this Tengo code in the
34> [Tengo Playground](https://tengolang.com/?s=0c8d5d0d88f2795a7093d7f35ae12c3afa17bea3)
35
36## Features
37
38- Simple and highly readable
39 [Syntax](https://github.com/d5/tengo/blob/master/docs/tutorial.md)
40 - Dynamic typing with type coercion
41 - Higher-order functions and closures
42 - Immutable values
43- [Securely Embeddable](https://github.com/d5/tengo/blob/master/docs/interoperability.md)
44 and [Extensible](https://github.com/d5/tengo/blob/master/docs/objects.md)
45- Compiler/runtime written in native Go _(no external deps or cgo)_
46- Executable as a
47 [standalone](https://github.com/d5/tengo/blob/master/docs/tengo-cli.md)
48 language / REPL
49- Use cases: rules engine, [state machine](https://github.com/d5/go-fsm),
50 data pipeline, [transpiler](https://github.com/d5/tengo2lua)
51
52## Benchmark
53
54| | fib(35) | fibt(35) | Language (Type) |
55| :--- | ---: | ---: | :---: |
56| [**Tengo**](https://github.com/d5/tengo) | `2,931ms` | `4ms` | Tengo (VM) |
57| [go-lua](https://github.com/Shopify/go-lua) | `4,824ms` | `4ms` | Lua (VM) |
58| [GopherLua](https://github.com/yuin/gopher-lua) | `5,365ms` | `4ms` | Lua (VM) |
59| [goja](https://github.com/dop251/goja) | `5,533ms` | `5ms` | JavaScript (VM) |
60| [starlark-go](https://github.com/google/starlark-go) | `11,495ms` | `5ms` | Starlark (Interpreter) |
61| [Yaegi](https://github.com/containous/yaegi) | `15,645ms` | `12ms` | Yaegi (Interpreter) |
62| [gpython](https://github.com/go-python/gpython) | `16,322ms` | `5ms` | Python (Interpreter) |
63| [otto](https://github.com/robertkrimen/otto) | `73,093ms` | `10ms` | JavaScript (Interpreter) |
64| [Anko](https://github.com/mattn/anko) | `79,809ms` | `8ms` | Anko (Interpreter) |
65| - | - | - | - |
66| Go | `53ms` | `3ms` | Go (Native) |
67| Lua | `1,612ms` | `3ms` | Lua (Native) |
68| Python | `2,632ms` | `23ms` | Python 2 (Native) |
69
70_* [fib(35)](https://github.com/d5/tengobench/blob/master/code/fib.tengo):
71Fibonacci(35)_
72_* [fibt(35)](https://github.com/d5/tengobench/blob/master/code/fibtc.tengo):
73[tail-call](https://en.wikipedia.org/wiki/Tail_call) version of Fibonacci(35)_
74_* **Go** does not read the source code from file, while all other cases do_
75_* See [here](https://github.com/d5/tengobench) for commands/codes used_
76
77## Quick Start
78
79```
80go get github.com/d5/tengo/v2
81```
82
83A simple Go example code that compiles/runs Tengo script code with some input/output values:
84
85```golang
86package main
87
88import (
89 "context"
90 "fmt"
91
92 "github.com/d5/tengo/v2"
93)
94
95func main() {
96 // Tengo script code
97 src := `
98each := func(seq, fn) {
99 for x in seq { fn(x) }
100}
101
102sum := 0
103mul := 1
104each([a, b, c, d], func(x) {
105 sum += x
106 mul *= x
107})`
108
109 // create a new Script instance
110 script := tengo.NewScript([]byte(src))
111
112 // set values
113 _ = script.Add("a", 1)
114 _ = script.Add("b", 9)
115 _ = script.Add("c", 8)
116 _ = script.Add("d", 4)
117
118 // run the script
119 compiled, err := script.RunContext(context.Background())
120 if err != nil {
121 panic(err)
122 }
123
124 // retrieve values
125 sum := compiled.Get("sum")
126 mul := compiled.Get("mul")
127 fmt.Println(sum, mul) // "22 288"
128}
129```
130
131## References
132
133- [Language Syntax](https://github.com/d5/tengo/blob/master/docs/tutorial.md)
134- [Object Types](https://github.com/d5/tengo/blob/master/docs/objects.md)
135- [Runtime Types](https://github.com/d5/tengo/blob/master/docs/runtime-types.md)
136 and [Operators](https://github.com/d5/tengo/blob/master/docs/operators.md)
137- [Builtin Functions](https://github.com/d5/tengo/blob/master/docs/builtins.md)
138- [Interoperability](https://github.com/d5/tengo/blob/master/docs/interoperability.md)
139- [Tengo CLI](https://github.com/d5/tengo/blob/master/docs/tengo-cli.md)
140- [Standard Library](https://github.com/d5/tengo/blob/master/docs/stdlib.md)
141- Syntax Highlighters: [VSCode](https://github.com/lissein/vscode-tengo), [Atom](https://github.com/d5/tengo-atom)
142- **Why the name Tengo?** It's from [1Q84](https://en.wikipedia.org/wiki/1Q84).
143
144##
145
146:hearts: Like writing Go code? Come work at Skool. [We're hiring!](https://jobs.lever.co/skool)
147
148