Skip to content

Latest commit

 

History

History
67 lines (53 loc) · 903 Bytes

README.md

File metadata and controls

67 lines (53 loc) · 903 Bytes

go-rpn

https://godoc.org/go.nanasi880.dev/rpn

Simply

package main

import (
    "fmt"
    
    "github.com/shopspring/decimal"
    "go.nanasi880.dev/rpn"
)

func main() {
	const expr = `5 * (3 + 7) + 50`
	r, err := rpn.Parse(expr)
	if err != nil {
		panic(err)
	}

	v, err := r.Eval(nil)
	if err != nil {
		panic(err)
	}

	fmt.Println(v.String())
	// Output:
	// 100
}

Variable value

package main

import (
    "fmt"
    
    "github.com/shopspring/decimal"
    "go.nanasi880.dev/rpn"
)

func main() {
	const expr = `5 * (3 + 7) + 50 + x`
	r, err := rpn.Parse(expr)
	if err != nil {
		panic(err)
	}

	v, err := r.Eval(func(name string) (decimal.Decimal, error) {
		if name == "x" {
			return decimal.New(10, 0), nil
		}
		return decimal.Zero, fmt.Errorf("undefined variable: %s", name)
	})
	if err != nil {
		panic(err)
	}

	fmt.Println(v.String())
	// Output:
	// 110
}