Skip to content

How to get output result for simple arithmetic expressions? #339

@Dentrax

Description

@Dentrax

I know this would be a tricky to implement or may require some workaround. The question actually quite simple: With the following expression structure, how to get arithmetic result along with evaluation result?

<ARITHMATRIC EXPRESSION> <OPERATOR> <VALUE>

For example, what's best to way to return the value of foo+bar+baz = 6 with true evaluation result?

package main

import (
	"fmt"

	"github.com/antonmedv/expr"
)

func main() {
	env := map[string]interface{}{
		"foo": 1,
		"bar": 2,
		"baz": 3,
	}

	code := `foo + bar + baz > 5`

	program, err := expr.Compile(code, expr.Env(env))
	if err != nil {
		panic(err)
	}

	output, err := expr.Run(program, env)
	if err != nil {
		panic(err)
	}

	fmt.Println(output)
}

I know this is a way too open to bugs and unexpected cases, but still, any workaround or ideas here? What should I do?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions