-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperation.go
51 lines (41 loc) · 1.05 KB
/
operation.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
package zeno
import "strconv"
// Operator is the representation of a math expression operator.
type Operator interface {
Operate(x, y *Operation) (float64, error)
LaTeX(x, y *Operation) string
}
// Constant represents a numeric constant expression
type Constant struct {
Value float64
}
func (c *Constant) Operate(x, y *Operation) (float64, error) {
return c.Value, nil
}
func (c *Constant) LaTeX(x, y *Operation) string {
return strconv.FormatFloat(c.Value, 'f', -1, 64)
}
// Operation is a binary tree containing the operands
// and the operation to perform
type Operation struct {
// can be constant, variable, func or operator
Operator Operator
// operands
Left *Operation
Right *Operation
}
func (o *Operation) Operate() (float64, error) {
return o.Operator.Operate(o.Left, o.Right)
}
func (o *Operation) LaTeX() string {
return o.Operator.LaTeX(o.Left, o.Right)
}
func signedOperation(op *Operation, negate bool) *Operation {
if negate {
return &Operation{
Operator: &Function{Name: "neg"},
Left: nil, Right: op,
}
}
return op
}