All Projects → d5 → Tengo

d5 / Tengo

Licence: mit
A fast script language for Go

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to Tengo

Mtail
extract internal monitoring data from application logs for collection in a timeseries database
Stars: ✭ 3,028 (+19.78%)
Mutual labels:  compiler, vm
Tinyrb
A tiny subset of Ruby with a Lua'esc VM
Stars: ✭ 452 (-82.12%)
Mutual labels:  compiler, vm
Smlvm
Smallrepo Virtual Machine
Stars: ✭ 265 (-89.52%)
Mutual labels:  compiler, vm
Customasm
💻 An assembler for custom, user-defined instruction sets! https://hlorenzi.github.io/customasm/web/
Stars: ✭ 211 (-91.65%)
Mutual labels:  compiler, vm
Rubyx
RubyX compiles ruby to binary (in ruby), hoping to be that X times faster
Stars: ✭ 78 (-96.91%)
Mutual labels:  compiler, vm
Ark
ArkScript is a small, fast, functional and scripting language for C++ projects
Stars: ✭ 312 (-87.66%)
Mutual labels:  compiler, vm
Passerine
A small extensible programming language designed for concise expression with little code.
Stars: ✭ 341 (-86.51%)
Mutual labels:  compiler, vm
Luago Book
《自己动手实现Lua》随书源代码
Stars: ✭ 514 (-79.67%)
Mutual labels:  compiler, vm
Typhon
A virtual machine for Monte.
Stars: ✭ 49 (-98.06%)
Mutual labels:  compiler, vm
Radon
A scripting language.
Stars: ✭ 22 (-99.13%)
Mutual labels:  compiler, vm
Quickjs
QuickJS是一个小型并且可嵌入的Javascript引擎,它支持ES2020规范,包括模块,异步生成器和代理器。
Stars: ✭ 2,199 (-13.01%)
Mutual labels:  compiler, vm
Quickjs
The official repo is at bellard/quickjs.
Stars: ✭ 1,429 (-43.47%)
Mutual labels:  compiler, vm
Ape
Ape Programming Language
Stars: ✭ 195 (-92.29%)
Mutual labels:  compiler, vm
Whileycompiler
The Whiley Compiler (WyC)
Stars: ✭ 181 (-92.84%)
Mutual labels:  compiler
Hybridizer Basic Samples
Examples of C# code compiled to GPU by hybridizer
Stars: ✭ 186 (-92.64%)
Mutual labels:  compiler
Varjo
Lisp to GLSL Language Translator
Stars: ✭ 181 (-92.84%)
Mutual labels:  compiler
Snapdragon
snapdragon is an extremely pluggable, powerful and easy-to-use parser-renderer factory.
Stars: ✭ 180 (-92.88%)
Mutual labels:  compiler
Splash
Simple Programming LAnguage for SHortcuts
Stars: ✭ 192 (-92.41%)
Mutual labels:  compiler
Minijit
A basic x86-64 JIT compiler written from scratch in stock Python
Stars: ✭ 185 (-92.68%)
Mutual labels:  compiler
Llvm Guide Zh
User Guides For those new to the LLVM system.(LLVM系统的新用户指南,中文翻译版)
Stars: ✭ 180 (-92.88%)
Mutual labels:  compiler

The Tengo Language

GoDoc test Go Report Card

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) Language (Type)
Tengo 2,315ms 3ms Tengo (VM)
go-lua 4,028ms 3ms Lua (VM)
GopherLua 4,409ms 3ms Lua (VM)
goja 5,194ms 4ms JavaScript (VM)
starlark-go 6,954ms 3ms Starlark (Interpreter)
gpython 11,324ms 4ms Python (Interpreter)
Yaegi 11,715ms 10ms Yaegi (Interpreter)
otto 48,539ms 6ms JavaScript (Interpreter)
Anko 52,821ms 6ms Anko (Interpreter)
- - - -
Go 47ms 2ms Go (Native)
Lua 756ms 2ms Lua (Native)
Python 1,907ms 14ms Python2 (Native)

* 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() {
	// create a new Script instance
	script := tengo.NewScript([]byte(
`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
})`))

	// 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"
}

Or, if you need to evaluate a simple expression, you can use Eval function instead:

res, err := tengo.Eval(ctx,
	`input ? "success" : "fail"`,
	map[string]interface{}{"input": 1})
if err != nil {
	panic(err)
}
fmt.Println(res) // "success"

References

♥️ Like writing Go code? Come work at Skool. We're hiring!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].