Skip to content

nanasi880/go-rpn

Repository files navigation

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
}

About

Reverse Polish Notation

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages