diff --git a/docs/content/faq.md b/docs/content/faq.md index fd32e45..6cca90d 100644 --- a/docs/content/faq.md +++ b/docs/content/faq.md @@ -165,7 +165,7 @@ draft: false Press e on the highlighted coin to enter holdings and add to your portfolio. - This dialog supports basic expressions including `+` `-` `*` etc. + This dialog supports basic expressions including `+` `-` `*` `/` etc. ## How do I edit the holdings of a coin in my portfolio? diff --git a/pkg/eval/eval.go b/pkg/eval/eval.go index cd061fe..bbf8ba7 100644 --- a/pkg/eval/eval.go +++ b/pkg/eval/eval.go @@ -5,25 +5,37 @@ import ( "strings" "github.com/antonmedv/expr" + "github.com/antonmedv/expr/ast" ) +// AST Visitor that changes Integer to Float +type patcher struct{} + +func (p *patcher) Enter(_ *ast.Node) {} +func (p *patcher) Exit(node *ast.Node) { + n, ok := (*node).(*ast.IntegerNode) + if ok { + ast.Patch(node, &ast.FloatNode{Value: float64(n.Value)}) + } +} + // EvaluateExpression evaulates a simple math expression string to a float64 func EvaluateExpressionToFloat64(input string) (float64, error) { - input = strings.TrimSpace(input) // remove trailing \0s + input = strings.TrimSpace(input) if input == "" { return 0, nil } - result, err := expr.Eval(input, nil) + program, err := expr.Compile(input, expr.Env(nil), expr.Patch(&patcher{})) + if err != nil { + return 0, err + } + result, err := expr.Run(program, nil) if err != nil { return 0, err } f64, ok := result.(float64) if !ok { - ival, ok := result.(int) - if !ok { - return 0, errors.New("could not type assert float64") - } - f64 = float64(ival) + return 0, errors.New("could not type assert float64") } return f64, nil }