-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
190 lines (166 loc) · 4.04 KB
/
main.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
package main
import (
"bufio"
"fmt"
"math"
"os"
"strings"
"github.com/tpillow/apc/pkg/apc"
)
type Executable interface {
Execute() float64
}
type Operator string
const (
OpAdd Operator = "+"
OpSub Operator = "-"
OpMul Operator = "*"
OpDiv Operator = "/"
OpExp Operator = "^"
)
var (
opAddParser = apc.Bind(apc.ExactStr(string(OpAdd)), OpAdd)
opSubParser = apc.Bind(apc.ExactStr(string(OpSub)), OpSub)
opMulParser = apc.Bind(apc.ExactStr(string(OpMul)), OpMul)
opDivParser = apc.Bind(apc.ExactStr(string(OpDiv)), OpDiv)
opExpParser = apc.Bind(apc.ExactStr(string(OpExp)), OpExp)
factorParser apc.Parser[rune, Executable]
factorParserRef = apc.Ref(&factorParser)
exponentTermParser = apc.Named("exponential term",
apc.Map(
apc.Seq2(
factorParserRef,
apc.ZeroOrMore(
apc.Seq2(
opExpParser,
factorParserRef))),
func(node *apc.Seq2Node[Executable, []*apc.Seq2Node[Operator, Executable]]) Executable {
left := node.Result1
for _, seqRes := range node.Result2 {
left = BinOpNode{
Operator: seqRes.Result1,
Left: left,
Right: seqRes.Result2,
}
}
return left
}))
termParser = apc.Named("term",
apc.Map(
apc.Seq2(
exponentTermParser,
apc.ZeroOrMore(
apc.Seq2(
apc.Any(opMulParser, opDivParser),
exponentTermParser))),
func(node *apc.Seq2Node[Executable, []*apc.Seq2Node[Operator, Executable]]) Executable {
left := node.Result1
for _, seqRes := range node.Result2 {
left = BinOpNode{
Operator: seqRes.Result1,
Left: left,
Right: seqRes.Result2,
}
}
return left
}))
exprParser = apc.Named("expression",
apc.Map(
apc.Seq2(
termParser,
apc.ZeroOrMore(
apc.Seq2(
apc.Any(opAddParser, opSubParser),
termParser))),
func(node *apc.Seq2Node[Executable, []*apc.Seq2Node[Operator, Executable]]) Executable {
left := node.Result1
for _, seqRes := range node.Result2 {
left = BinOpNode{
Operator: seqRes.Result1,
Left: left,
Right: seqRes.Result2,
}
}
return left
}))
maybeExprParser = apc.Maybe(exprParser)
)
func initParser() {
factorParser = apc.Named("factor",
apc.Any(
apc.Map(
apc.FloatParser,
func(node float64) Executable {
return ValueNode{Value: node}
}),
apc.Map(
apc.Seq3(
apc.ExactStr("("),
exprParser,
apc.ExactStr(")")),
func(node *apc.Seq3Node[string, Executable, string]) Executable {
return node.Result2
})))
}
func executeInput(input string) {
ctx := apc.NewStringContext("<user_input>", input)
ctx.AddSkipParser(apc.CastToAny(apc.WhitespaceParser))
node, err := apc.Parse[rune](ctx, maybeExprParser, apc.DefaultParseConfig)
if err != nil {
fmt.Printf("Error parsing input: %v\n", err)
return
}
if node.IsNil() {
return
}
fmt.Printf("Parse Node: %v\n", node)
fmt.Printf("Result: %v\n\n", node.Value().Execute())
}
func main() {
initParser()
inputPrompt := "Input expression: "
input := "1 + 2 * 3 ^ (5 - 3)"
fmt.Print("Welcome to the APC calculator. Type 'q' to quit.\n")
fmt.Printf("Here, I'll do one first:\n\n%v%v\n", inputPrompt, input)
executeInput(input)
stdin := bufio.NewReader(os.Stdin)
for {
fmt.Print(inputPrompt)
input, err := stdin.ReadString('\n')
if err != nil {
fmt.Printf("Error reading input: %v\n", err)
continue
}
if strings.TrimSpace(input) == "q" {
break
}
executeInput(input)
}
fmt.Printf("Goodbye!\n")
}
type ValueNode struct {
Value float64
}
func (n ValueNode) Execute() float64 {
return n.Value
}
type BinOpNode struct {
Operator Operator
Left Executable
Right Executable
}
func (n BinOpNode) Execute() float64 {
switch n.Operator {
case OpAdd:
return n.Left.Execute() + n.Right.Execute()
case OpSub:
return n.Left.Execute() - n.Right.Execute()
case OpMul:
return n.Left.Execute() * n.Right.Execute()
case OpDiv:
return n.Left.Execute() / n.Right.Execute()
case OpExp:
return math.Pow(n.Left.Execute(), n.Right.Execute())
}
panic("unknown operator")
}