-
Notifications
You must be signed in to change notification settings - Fork 0
/
Divide.go
68 lines (56 loc) · 2.09 KB
/
Divide.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// =================================================================
//
// Copyright (C) 2019 Spatial Current, Inc. - All Rights Reserved
// Released as open source under the MIT License. See LICENSE file.
//
// =================================================================
package dfl
import (
"github.com/pkg/errors"
"github.com/spatialcurrent/go-adaptive-functions/pkg/af"
)
// Divide is a NumericBinaryOperator that represents the mathematical division of two nodes.
type Divide struct {
*NumericBinaryOperator
}
// Dfl returns the DFL representation of this node as a string
func (d Divide) Dfl(quotes []string, pretty bool, tabs int) string {
return d.BinaryOperator.Dfl("/", quotes, pretty, tabs)
}
// Sql returns the SQL representation of this node as a string
func (d Divide) Sql(pretty bool, tabs int) string {
return d.BinaryOperator.Sql("/", pretty, tabs)
}
// Map returns a map representation of this node.
func (d Divide) Map() map[string]interface{} {
return d.BinaryOperator.Map("divide", d.Left, d.Right)
}
// Compile returns a compiled version of this node.
func (d Divide) Compile() Node {
left := d.Left.Compile()
right := d.Right.Compile()
switch left.(type) {
case Literal:
switch right.(type) {
case Literal:
v, err := af.Divide.ValidateRun([]interface{}{left.(Literal).Value, right.(Literal).Value})
if err != nil {
return &Divide{&NumericBinaryOperator{&BinaryOperator{Left: left, Right: right}}}
}
return Literal{Value: v}
}
}
return &Divide{&NumericBinaryOperator{&BinaryOperator{Left: left, Right: right}}}
}
// Evaluate evaluates this node given the variables "vars" and context "ctx" and returns the output, and an error if any.
func (d Divide) Evaluate(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error) {
vars, lv, rv, err := d.EvaluateLeftAndRight(vars, ctx, funcs, quotes)
if err != nil {
return vars, 0, err
}
v, err := af.Divide.ValidateRun(lv, rv)
if err != nil {
return vars, 0, errors.Wrap(err, ErrorEvaluate{Node: d, Quotes: quotes}.Error())
}
return vars, v, err
}