-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.go
66 lines (58 loc) · 1.67 KB
/
string.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
package diceprob
import (
"fmt"
"strings"
)
// String - Output the dice Expression as a string; top level of recursive output functions.
func (e *Expression) String() string {
out := []string{e.Left.string()}
for _, r := range e.Right {
out = append(out, r.string())
}
return strings.Join(out, " ")
}
// String - Output the Operator as a string; part of the recursive output functions.
func (o Operator) string() string {
switch o {
case OpMul:
return "*"
case OpDiv:
return "/"
case OpSub:
return "-"
case OpAdd:
return "+"
}
panic("unsupported operator") // TODO - We can do better here.
}
// String - Output the Operator and Term as a string; part of the recursive output functions.
func (o *OpTerm) string() string {
return fmt.Sprintf("%s %s", o.Operator.string(), o.Term.string())
}
// String - Output the Term as a string; part of the recursive output functions.
func (t *Term) string() string {
out := []string{t.Left.string()}
for _, r := range t.Right {
out = append(out, r.string())
}
return strings.Join(out, " ")
}
// String - Output the Operator and Atom as a string; part of the recursive output functions.
func (o *OpAtom) string() string {
return fmt.Sprintf("%s %s", o.Operator.string(), o.Atom.string())
}
// String - Output the Atom as a string; part of the recursive output functions.
func (a *Atom) string() string {
if a.Modifier != nil {
return fmt.Sprintf("%d", *a.Modifier)
}
if a.RollExpr != nil {
return a.RollExpr.string()
}
return "(" + a.SubExpression.String() + ")"
}
// String - Output the DiceRoll as a string; the deepest of the recursive output functions.
func (s *DiceRoll) string() string {
ret := string(*s)
return ret
}