-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperand.go
More file actions
183 lines (142 loc) · 3.33 KB
/
operand.go
File metadata and controls
183 lines (142 loc) · 3.33 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import (
"fmt"
)
type Operand interface {
// Value returns the value of the operand
Value() int
// String returns string representation of the operand
String() string
// NeedsParenthesis indicates if operand needs parenthesis when multiplying
NeedsParenthesis() bool
// OperandsCount returns number of operands inside the one
OperandsCount() int
}
type SingleValueOperand struct {
value int
}
func NewSingleValueOperand(value int) Operand {
return &SingleValueOperand{
value: value,
}
}
func (o *SingleValueOperand) Value() int {
return o.value
}
func (o *SingleValueOperand) String() string {
return fmt.Sprintf("%v", o.value)
}
func (o *SingleValueOperand) NeedsParenthesis() bool {
return false
}
func (o *SingleValueOperand) OperandsCount() int {
return 1
}
type ParenthesisOperand struct {
inner Operand
}
func NewParenthesisOperand(inner Operand) Operand {
return &ParenthesisOperand{
inner: inner,
}
}
func (o *ParenthesisOperand) Value() int {
return o.inner.Value()
}
func (o *ParenthesisOperand) String() string {
return fmt.Sprintf("(%v)", o.inner.String())
}
func (o *ParenthesisOperand) NeedsParenthesis() bool {
return false
}
func (o *ParenthesisOperand) OperandsCount() int {
return o.inner.OperandsCount()
}
type SumOperand struct {
left Operand
right Operand
}
func NewSumOperand(left Operand, right Operand) Operand {
return &SumOperand{
left: left,
right: right,
}
}
func (o *SumOperand) Value() int {
return o.left.Value() + o.right.Value()
}
func (o *SumOperand) String() string {
return fmt.Sprintf("%v + %v", o.left, o.right)
}
func (o *SumOperand) NeedsParenthesis() bool {
return true
}
func (o *SumOperand) OperandsCount() int {
return o.left.OperandsCount() + o.right.OperandsCount()
}
type SubtractOperand struct {
left Operand
right Operand
}
func NewSubtractOperand(left Operand, right Operand) Operand {
return &SubtractOperand{
left: left,
right: right,
}
}
func (o *SubtractOperand) Value() int {
return o.left.Value() - o.right.Value()
}
func (o *SubtractOperand) String() string {
return fmt.Sprintf("%v - %v", o.left, o.right)
}
func (o *SubtractOperand) NeedsParenthesis() bool {
return true
}
func (o *SubtractOperand) OperandsCount() int {
return o.left.OperandsCount() + o.right.OperandsCount()
}
type MultiplyOperand struct {
left Operand
right Operand
}
func NewMultiplyOperand(left Operand, right Operand) Operand {
return &MultiplyOperand{
left: left,
right: right,
}
}
func (o *MultiplyOperand) Value() int {
return o.left.Value() * o.right.Value()
}
func (o *MultiplyOperand) String() string {
return fmt.Sprintf("%v * %v", o.left, o.right)
}
func (o *MultiplyOperand) NeedsParenthesis() bool {
return false
}
func (o *MultiplyOperand) OperandsCount() int {
return o.left.OperandsCount() + o.right.OperandsCount()
}
type DivideOperand struct {
left Operand
right Operand
}
func NewDivideOperand(left Operand, right Operand) Operand {
return &DivideOperand{
left: left,
right: right,
}
}
func (o *DivideOperand) Value() int {
return o.left.Value() / o.right.Value()
}
func (o *DivideOperand) String() string {
return fmt.Sprintf("%v / %v", o.left, o.right)
}
func (o *DivideOperand) NeedsParenthesis() bool {
return false
}
func (o *DivideOperand) OperandsCount() int {
return o.left.OperandsCount() + o.right.OperandsCount()
}