-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathcmd_calc.go
254 lines (208 loc) · 4.25 KB
/
cmd_calc.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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package main
import (
"fmt"
"io"
"strings"
"github.com/peterh/liner"
"github.com/skx/subcommands"
"github.com/skx/sysbox/calc"
)
// Structure for our options and state.
type calcCommand struct {
// We embed the NoFlags option, because we accept no command-line flags.
subcommands.NoFlags
}
// Info returns the name of this subcommand.
func (c *calcCommand) Info() (string, string) {
return "calc", `A simple (floating-point) calculator.
Details:
This command allows you to evaluate simple mathematical operations,
with support for floating-point operations - something the standard
'expr' command does not support.
Example:
$ sysbox calc 3 + 3
$ sysbox calc '1 / 3 * 9'
Note here we can join arguments, or accept a quoted string. The arguments
must be quoted if you use '*' because otherwise the shell's globbing would
cause surprises.
Repl:
If you execute this command with no arguments you'll be dropped into a REPL
environment. This environment is almost 100% identical to the non-interactive
use, with the exception that you can define variables:
$ sysbox calc
calc> let a = 3
3
calc> a * 3
9
calc> a / 9
0.3333
calc> exit
If you prefer you can handle assignments without "let":
calc> a = 1; b = 2 ; c = 3
3
calc> a + b * c
7
calc> exit
The result of the previous calculation is always stored in the variable 'result':
calc> 1 / 3
0.3333
calc> result * 3
1
`
}
// Show the result of a calculation
func (c *calcCommand) showResult(out *calc.Token) error {
if out == nil {
return fmt.Errorf("nil result")
}
if out.Type == calc.ERROR {
return fmt.Errorf("%s", out.Value.(string))
}
if out.Type != calc.NUMBER {
return fmt.Errorf("unexpected output (not a number): %v", out)
}
//
// Show the result as an int, if possible.
//
result := out.Value.(float64)
if float64(int(result)) == result {
fmt.Printf("%d\n", int(result))
return nil
}
//
// strip trailing "0"
//
// First convert to string, then remove each
// final zero.
output := fmt.Sprintf("%f", result)
for strings.HasSuffix(output, "0") {
output = strings.TrimSuffix(output, "0")
}
fmt.Printf("%s\n", output)
return nil
}
// Execute is invoked if the user specifies `calc` as the subcommand.
func (c *calcCommand) Execute(args []string) int {
//
// Join all arguments, in case we have been given "3", "+", "4".
//
input := ""
for _, arg := range args {
input += arg
input += " "
}
//
// Create a new evaluator
//
cal := calc.New()
//
// If we have no arguments then we're in the repl.
//
// Otherwise we process the input.
//
if len(input) > 0 {
//
// Load the script
//
cal.Load(input)
//
// Run it.
//
out := cal.Run()
//
// Show the result.
//
err := c.showResult(out)
if err != nil {
fmt.Printf("error: %s\n", err)
return 1
}
return 0
}
//
// Repl uses command-history
//
line := liner.NewLiner()
defer line.Close()
//
// Tab completion
//
complete := []string{"exit", "help", "result", "quit"}
line.SetCompleter(func(line string) (c []string) {
for _, n := range complete {
if strings.HasPrefix(n, strings.ToLower(line)) {
c = append(c, n)
}
}
return
})
//
// Ctrl-C will abort input of a line, not the whole program.
//
line.SetCtrlCAborts(false)
//
// Loop until we should stop
//
run := true
for run {
input, err := line.Prompt("calc>")
if err == nil {
//
// Trim the input
//
input = strings.TrimSpace(input)
//
// Exit ?
//
if strings.HasPrefix(input, "exit") ||
strings.HasPrefix(input, "quit") {
run = false
continue
}
//
// Help ?
//
if strings.HasPrefix(input, "help") {
_, txt := c.Info()
fmt.Printf("%s\n", txt)
continue
}
//
// Is the input empty?
//
if input == "" {
continue
}
//
// Load the script
//
cal.Load(input)
//
// Run it.
//
out := cal.Run()
//
// Show the result.
//
err = c.showResult(out)
if err != nil {
fmt.Printf("error: %s\n", err)
}
//
// Add the input to our history.
//
// NOTE: Our history is deliberately not persisted.
//
line.AppendHistory(input)
}
// Ctrl-d
if io.EOF == err {
run = false
fmt.Printf("\n")
}
}
//
// All done
//
return 0
}