chili

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2020 License: MIT Imports: 5 Imported by: 0

README

🌶️ chili

Currently in development, Unstable (API may change in future)

Simple expression evaluation engine.

Expression is one liner that evalutes into single value

Features
  • Number accuracy (using github.com/shopspring/decimal pkg)
  • Extensible
  • Simple grammer
Installation
$ go get github.com/5anthosh/chili
Examples
package main

import (
    "fmt"
    "github.com/5anthosh/chili"
)
func main() {
    expression := "val > 50 ? 'Greater than or 50' : 'Smaller than 50'"
    values := map[string]interface{}{
        "val": 60,
    }
    result, err := chili.Eval(expression, values)
    if err != nil {
        panic(err)
    }
    println(fmt.Sprintf("%v result", result))
}
package main

import (
	"fmt"
    
	"github.com/5anthosh/chili/environment"
	"github.com/5anthosh/chili/evaluator"
	"github.com/5anthosh/chili/parser"
	"github.com/shopspring/decimal"
)

func main() {
	source := "PI*R^2 + abs(45.345)"
    
	env := environment.New()
	env.SetDefaultFunctions()
	env.SetNumberVariable("PI", decimal.RequireFromString("3.1415926535897932385"))
	env.SetNumberVariable("R", decimal.RequireFromString("2"))
    
	chiliParser := parser.New(source)
	expression, err := chiliParser.Parse()
	if err != nil {
        panic(err)
	}
    
	chiliEvaluator := evaluator.New(env)
	value, err := chiliEvaluator.Run(expression)
	if err != nil {
        panic(err)
	}
    
    println(fmt.Sprintf("%v result", value))
}

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Eval

func Eval(expression string, data map[string]interface{}) (interface{}, error)

Eval the expression

Types

This section is empty.

Directories

Path Synopsis
ast

Jump to

Keyboard shortcuts

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