All Projects → apaxa-go → Eval

apaxa-go / Eval

Licence: apache-2.0
Package eval implements evaluation of GoLang expression at runtime.

Programming Languages

go
31211 projects - #10 most used programming language
golang
3204 projects

Projects that are alternatives of or similar to Eval

Olingo Odata4 Js
Mirror of Apache Olingo
Stars: ✭ 38 (-9.52%)
Mutual labels:  library
Rxdownloader
- Reactive Extension Library for Android to download files
Stars: ✭ 40 (-4.76%)
Mutual labels:  library
8bp
8 bits de poder ( 8 bits of power)
Stars: ✭ 41 (-2.38%)
Mutual labels:  library
Stealtool
📚 盗取手机敏感信息,Android 6.0之上兼容
Stars: ✭ 39 (-7.14%)
Mutual labels:  library
Localize and translate
Flutter localization in easy steps, really simple
Stars: ✭ 40 (-4.76%)
Mutual labels:  library
Redash
Tiny functional programming suite for JavaScript.
Stars: ✭ 40 (-4.76%)
Mutual labels:  library
Blink Mind React
A mind map library for react that based on immutable.js.
Stars: ✭ 38 (-9.52%)
Mutual labels:  library
Concentriconboarding
Android Concentric Onboarding library
Stars: ✭ 42 (+0%)
Mutual labels:  library
Easygo
基于Kotlin、OkHttp的声明式网络框架,像写HTML界面一样写网络调用代码
Stars: ✭ 40 (-4.76%)
Mutual labels:  library
Depressurizer
A Steam library categorizing tool.
Stars: ✭ 1,008 (+2300%)
Mutual labels:  library
Lara Eye
Filter your Query\Builder using a structured query language
Stars: ✭ 39 (-7.14%)
Mutual labels:  library
Chakra Ui Vue
⚡️ Build scalable and accessible Vue.js applications with ease.
Stars: ✭ 993 (+2264.29%)
Mutual labels:  library
Webrix
Powerful building blocks for React-based web applications
Stars: ✭ 41 (-2.38%)
Mutual labels:  library
Cordova Plugin Inappbrowser
Apache Cordova Plugin inappbrowser
Stars: ✭ 994 (+2266.67%)
Mutual labels:  library
Gmtwitch
Lightweight, open source Twitch interface for Game Maker: Studio
Stars: ✭ 41 (-2.38%)
Mutual labels:  library
Gifloader
An Android Library to load your GIF files
Stars: ✭ 38 (-9.52%)
Mutual labels:  library
Xna.js
WebGL framework strongly inspired by the XNA library
Stars: ✭ 40 (-4.76%)
Mutual labels:  library
Astnorm
AST normalization experiment
Stars: ✭ 42 (+0%)
Mutual labels:  library
Cordova Firefoxos
[DEPRECATED] Apache Cordova firefoxos
Stars: ✭ 41 (-2.38%)
Mutual labels:  library
Nulldefense
Removes invalid objects during Gson parsing which are marked as required, yet null/empty.
Stars: ✭ 41 (-2.38%)
Mutual labels:  library

eval

Build Status Coverage Status Go Report Card GoDoc

Package eval implements evaluation of GoLang expression at runtime.

THIS LIB NO MORE MAINTAINED!

For whose who wants to implement golang eval

Suggestions:

  1. Implement 2-steps algorithm: first step - analogue of golang compilation (resolve all types; compute all constants; convert all untyped constant and untyped variables to typed; also simplify AST tree here / convert to your own format), second step - analogue of golang runtime (here will be passed values of external variables and computed final result).
  2. You need to use golang eval package, but keep in mind that it can simplify break your lib on update (golang's backward compatibility does not save you).
  3. Follow language specification (it is hard to implement some additional custom behaviour without breaking compatibility with language accepted expressions).
  4. Language specification may omit some details (so also test with golang compiler).

Requirements for expression:

  1. expression itself and all of subexpression must return exactly one value,
  2. see documentation Bugs section for other requirements/restrictions.

What does supported:

  1. types (by passing predefined/custom types and by defining unnamed types in expression itself),
  2. named arguments (regular variables, typed & untyped constants, untyped boolean variable),
  3. "package.SomeThing" notation,
  4. evaluate expression as if it is evaluates in specified package (even write access to private fields),
  5. evaluate expression like Go evaluates it (following most of specification rules),
  6. position of error in source on evaluation.

Examples:

Simple:

src:="int8(1*(1+2))"
expr,err:=ParseString(src,"")
if err!=nil{
	return err
}
r,err:=expr.EvalToInterface(nil)
if err!=nil{
	return err
}
fmt.Printf("%v %T", r, r)	// "3 int8"

Complicated:

type exampleString string

func (s exampleString) String() exampleString { return "!" + s + "!" }

type exampleStruct struct {
	A, B int
}

func (s exampleStruct) Sum() int { return s.A + s.B }

func main(){
	c := make(chan int64, 10)
	c <- 2

	src := `exampleString(fmt.Sprint(interface{}(math.MaxInt64/exampleStruct(struct{ A, B int }{3, 5}).Sum()+int(<-(<-chan int64)(c))-cap(make([]string, 1, 100))))).String().String() + "."`

	expr, err := ParseString(src, "")
	if err != nil {
		return
	}
	a := Args{
		"exampleString": MakeTypeInterface(exampleString("")),
		"fmt.Sprint":    MakeDataRegularInterface(fmt.Sprint),
		"math.MaxInt64": MakeDataUntypedConst(constanth.MakeUint(math.MaxInt64)),
		"exampleStruct": MakeTypeInterface(exampleStruct{}),
		"c":             MakeDataRegularInterface(c),
	}
	r, err := expr.EvalToInterface(a)
	if err != nil {
		return
	}
	if r != testR {
		return
	}
	fmt.Printf("%v %T\n", r, r)	// "!!1152921504606846877!!. exampleString"
	return
}

With error:

src := `exampleString(fmt.Sprint(interface{}(math.MaxInt64/exampleStruct(struct{ A, B int }{3, 5}).Sum()+int(<-(<-chan int64)(c))-cap(make([]string, 1, 100))))).String().String() + "."`
expr, err := ParseString(src, "")
if err != nil {
	t.Error(err)
}
a := Args{
	"exampleString": MakeTypeInterface(exampleString("")),
	"fmt.Sprint":    MakeDataRegularInterface(fmt.Sprint),
	"math.MaxInt64": MakeDataUntypedConst(constanth.MakeUint(math.MaxInt64)),
	"exampleStruct": MakeTypeInterface(exampleStruct{}),
	// Remove "c" from passed arguments:
	// "c":             MakeDataRegularInterface(c),
}
_, err = expr.EvalToInterface(a)
fmt.Println(err)	// "expression:1:119: undefined: c"

Bugs

Please report bug if you found it.

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