Categorygithub.com/gad-lang/gad
repositorypackage
0.0.0-20240620131300-afb8c20837d2
Repository: https://github.com/gad-lang/gad.git
Documentation: pkg.go.dev

# Packages

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# README

The Gad Language

Go Reference Go Report Card Gad Test Gad Dev Test Maintainability

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)

Features

  • Written in native Go (no cgo).
  • Supports Go 1.15 and above.
  • if else statements.
  • for and for in statements.
  • try catch finally statements.
  • param, global, var and const 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.

Why Gad

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.

Quick Start

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

repl-gif

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]
}

TODO

  • 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

Documentation

LICENSE

Gad is licensed under the MIT License.

See LICENSE for the full license text.

Acknowledgements

Gad is inspired by script language uGo by Ozan Hacıbekiroğlu. A special thanks to uGo's creater and contributors.