goScript

package module
v0.0.0-...-caab901 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 21, 2017 License: MIT Imports: 7 Imported by: 0

README

#goScript

Golang like scripting language

Uses go/ast and reflect to allow arbitrary expression evaluation in a given context.

#Highlights

  • Simple and hackable code.

  • Fully reflect function call including variable number of args.

  • Automatic casting

    • Numeric casting always to the bigger more generic representation.
    • True && 0 = false
    • Left operand governs the type 1 + "1" = 2, "1" + 1 = "11"
  • Map, slice, array and string indexer access

  • Full control over evaluation context.

    • Generic map[string]interface{} / *map[string]interface{} contexts
    • Any struct with its fields as variables
    • Custom Context for implentation/user defined context.
  • Mostly tested ;)

#Roadmap

    1. Rock solid expression evaluation.
    1. Enrich evaluation context
    1. Full script program implementation
    1. Performance optimizations

#Examples


import (
	"fmt"
	"time"

	"github.com/japm/goScript"
)

//Example type
type ab struct {
	A int
}

//Function as identifier
func T() int {
	return 0
}

func (a ab) Test(x int, z ...interface{}) []interface{} {
	return z
}


func (a ab) Test2(x int, z ...int) []int {
	return z
}

func (a ab) Test3(x float64, y float64) float64 {
	return x + y
}

//Custom dummy context, the ident value is its own name
func (a ab) GetIdent(name string) (val interface{}, err error) {
	return name, nil
}

func examples() {

	d := make(map[string]interface{})
	d["a"] = 1
	ctxt := make(map[string]interface{})
	ctxt["a"] = time.Now()
	ctxt["b"], _ = time.ParseDuration("3h")
	ctxt["c"] = []int{0, 1, 2, 3}
	ctxt["d"] = d
	ctxt["e"] = &ab{45}
	ctxt["f"] = ab{45}
	ctxt["g"] = []interface{}{nil, nil}

	i := new(int)
	*i = 3
	ctxt["h"] = i
	ctxt["i"] = T //A Function

	exp := &goScript.Expr{}
	err := exp.Prepare("(*e).A")
	//err := exp.Prepare("f.A")
	//err := exp.Prepare("a.Day() + 1.4")
	//err := exp.Prepare("f.Test(1,2)")
	//err := exp.Prepare("f.Test2(3,4,c[3])")
	//err := exp.Prepare("*e")
	//err := exp.Prepare("g[0]")
	//err := exp.Prepare("f.Test3(2,3)")
	//err := exp.Prepare("(1 + c[2]) * 3")
	//err := exp.Prepare("3.5 + len(c)")
	//err := exp.Prepare("i()")
	//err := exp.Prepare("d[\"a\"]")
	//err := exp.Prepare("*h + 2")

	if err != nil {
		fmt.Println(err)
		return
	}


  val, err := exp.Eval(ctxt)
  //val, err := exp.Eval(&ctxt)
  
  //Custom context examples
	//err := exp.Prepare("Name1")
	//val, err := exp.Eval(ab{1})

	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println("Result...", val)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Eval

func Eval(expr string, context interface{}) (val interface{}, err error)

Eval expression within a context

func EvalInt

func EvalInt(expr string, context interface{}) (int, error)

EvalInt convenient function casting return type to int

Types

type Callable

type Callable interface {
	Call(args []interface{}) (val interface{}, err error)
}

A Callable function

type CallableContext

type CallableContext interface {
	GetCallable(name string) (val Callable, err error)
}

Context allows custom identification resolver

type Context

type Context interface {
	GetIdent(name string) (val interface{}, err error)
}

Context allows custom identification resolver

type Expr

type Expr struct {
	// contains filtered or unexported fields
}

Expr expresion holder, allows sentence preparation

func (*Expr) Eval

func (e *Expr) Eval(context interface{}) (val interface{}, err error)

Eval a prepared sentence

func (*Expr) EvalInt

func (e *Expr) EvalInt(context interface{}) (val int, err error)

EvalInt convenient function casting return type to int

func (*Expr) EvalNoRecover

func (e *Expr) EvalNoRecover(context interface{}) (interface{}, error)

EvalNoRecover without recover on defer

func (*Expr) Prepare

func (e *Expr) Prepare(expr string) error

Prepare sentence for evaluation and reuse

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL