Gad is a fast, dynamic scripting language to embed in Go applications. Gad is compiled and executed as bytecode on stack-based VM that's written in native Go.
Gad is actively used in production to evaluate Sigma Rules' conditions, and to perform compromise assessment dynamically.
To see how fast Gad is, please have a look at fibonacci benchmarks (not updated frequently).
Play with Gad via Playground built for WebAssembly.
Fibonacci Example
param arg0
var fib
fib = func(x) {
if x == 0 {
return 0
} else if x == 1 {
return 1
}
return fib(x-1) + fib(x-2)
}
return fib(arg0)
- Written in native Go (no cgo).
- Supports Go 1.15 and above.
if else
statements.for
andfor in
statements.try catch finally
statements.param
,global
,var
andconst
declarations.- Rich builtins.
- Pure Gad and Go Module support.
- Go like syntax with additions.
- Call Gad functions from Go.
- Import Gad modules from any source (file system, HTTP, etc.).
- Create wrapper functions for Go functions using code generation.
Gad
(Hebrew: גָּד, Modern: Gad, Tiberian: Gāḏ, "luck") was, according to the Book of Genesis, the first of the two
sons of Jacob and Zilpah (Jacob's seventh son) and the founder of the Israelite tribe of Gad.[2]
The text of the Book of Genesis implies that the name of Gad means luck/fortunate, in Hebrew.
go get github.com/gad-lang/gad@latest
Gad has a REPL application to learn and test Gad scripts.
go install github.com/gad-lang/gad/cmd/gad@latest
./gad
This example is to show some features of Gad.
https://play.golang.org/p/1Tj6joRmLiX
package main
import (
"fmt"
"github.com/gad-lang/gad"
)
func main() {
script := `
param ...args
mapEach := func(seq, fn) {
if !isArray(seq) {
return error("want array, got " + typeName(seq))
}
var out = []
if sz := len(seq); sz > 0 {
out = repeat([0], sz)
} else {
return out
}
try {
for i, v in seq {
out[i] = fn(v)
}
} catch err {
println(err)
} finally {
return out, err
}
}
global multiplier
v, err := mapEach(args, func(x) { return x*multiplier })
if err != nil {
return err
}
return v
`
bytecode, err := gad.Compile([]byte(script), gad.DefaultCompilerOptions)
if err != nil {
panic(err)
}
globals := gad.Map{"multiplier": gad.Int(2)}
ret, err := gad.NewVM(bytecode).Run(
globals,
gad.Int(1), gad.Int(2), gad.Int(3), gad.Int(4),
)
if err != nil {
panic(err)
}
fmt.Println(ret) // [2, 4, 6, 8]
}
- Dollar as valid ident char
- Nullisch Coalescing
- Named arguments
- Array Expansion
- Array Comprehensions
- Map Expansion
- Map Comprehensions
- Examples for best practices
- Better Playground
- Configurable Stdin, Stdout and Stderr per Virtual Machine
- Deferring function calls
- Concurrency support
Gad is licensed under the MIT License.
See LICENSE for the full license text.
Gad is inspired by script language uGo by Ozan Hacıbekiroğlu. A special thanks to uGo's creater and contributors.