-
Notifications
You must be signed in to change notification settings - Fork 10
/
eval2.go
261 lines (231 loc) · 4.74 KB
/
eval2.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
255
256
257
258
259
260
261
// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
// Generic expression parser/evaluator
type Value interface {
String() string
BinaryOp(op string, y Value) Value
}
type Parser struct {
precTab map[string]int
newVal func(string) Value
src string
pos int
tok string
}
const alphanum = "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
func (p *Parser) stop(c uint8) bool {
switch {
case p.pos >= len(p.src):
return true
case c == '"':
if p.src[p.pos] == '"' {
p.pos++
return true
}
return false
case strings.IndexRune(alphanum, int(c)) >= 0:
return strings.IndexRune(alphanum, int(p.src[p.pos])) < 0
}
return true
}
func (p *Parser) next() {
// skip blanks
for ; p.pos < len(p.src) && p.src[p.pos] <= ' '; p.pos++ {
}
if p.pos >= len(p.src) {
p.tok = ""
return
}
start := p.pos
c := p.src[p.pos]
for p.pos < len(p.src) {
p.pos++
if p.stop(c) {
break
}
}
p.tok = p.src[start:p.pos]
}
func (p *Parser) binaryExpr(prec1 int) Value {
x := p.newVal(p.tok)
p.next()
for prec := p.precTab[p.tok]; prec >= prec1; prec-- {
for p.precTab[p.tok] == prec {
op := p.tok
p.next()
y := p.binaryExpr(prec + 1)
x = x.BinaryOp(op, y)
}
}
return x
}
func Eval(precTab map[string]int, newVal func(string) Value, src string) Value {
var p Parser
p.precTab = precTab
p.newVal = newVal
p.src = src
p.next()
return p.binaryExpr(1)
}
// Command-line expression evaluator
func main() {
r := bufio.NewReader(os.Stdin)
for {
fmt.Printf("> ")
line, err := r.ReadString('\n')
if err != nil {
break
}
fmt.Printf("%s\n", Eval(precTab, trace(newVal), line))
}
}
// Custom grammar and values
var precTab = map[string]int{
"&&": 1,
"||": 2,
"==": 3,
"!=": 3,
"<": 3,
"<=": 3,
">": 3,
">=": 3,
"+": 4,
"-": 4,
"*": 5,
"/": 5,
"%": 5,
}
func newVal(lit string) Value {
x, err := strconv.Atoi(lit)
if err == nil {
return Int(x)
}
b, err := strconv.Atob(lit)
if err == nil {
return Bool(b)
}
s, err := strconv.Unquote(lit)
if err == nil {
return String(s)
}
return Error(fmt.Sprintf("illegal literal '%s'", lit))
}
type Error string
func (e Error) String() string { return string(e) }
func (e Error) BinaryOp(op string, y Value) Value { return e }
type Int int
func (x Int) String() string { return strconv.Itoa(int(x)) }
func (x Int) BinaryOp(op string, y Value) Value {
switch y := y.(type) {
case Error:
return y
case String:
switch op {
case "*":
return String(strings.Repeat(string(y), int(x)))
}
case Int:
switch op {
case "+":
return x + y
case "-":
return x - y
case "*":
return x * y
case "/":
return x / y
case "%":
return x % y
case "==":
return Bool(x == y)
case "!=":
return Bool(x != y)
case "<":
return Bool(x < y)
case "<=":
return Bool(x <= y)
case ">":
return Bool(x > y)
case ">=":
return Bool(x >= y)
}
}
return Error(fmt.Sprintf("illegal operation: '%v %s %v'", x, op, y))
}
type Bool bool
func (x Bool) String() string { return strconv.Btoa(bool(x)) }
func (x Bool) BinaryOp(op string, y Value) Value {
switch y := y.(type) {
case Error:
return y
case Bool:
switch op {
case "&&":
return Bool(x && y)
case "||":
return Bool(x || y)
case "==":
return Bool(x == y)
case "!=":
return Bool(x != y)
}
}
return Error(fmt.Sprintf("illegal operation: '%v %s %v'", x, op, y))
}
type String string
func (x String) String() string { return strconv.Quote(string(x)) }
func (x String) BinaryOp(op string, y Value) Value {
switch y := y.(type) {
case Error:
return y
case Int:
switch op {
case "*":
return String(strings.Repeat(string(x), int(y)))
}
case String:
switch op {
case "+":
return x + y
case "<":
return Bool(x < y)
}
}
return Error(fmt.Sprintf("illegal operation: '%v %s %v'", x, op, y))
}
func trace(newVal func(string) Value) func(string) Value {
return func(s string) Value {
v := newVal(s)
fmt.Printf("\tnewVal(%q) = %s\n", s, fmtv(v))
return &traceValue{v}
}
}
type traceValue struct {
Value
}
func (x *traceValue) BinaryOp(op string, y Value) Value {
z := x.Value.BinaryOp(op, y.(*traceValue).Value)
fmt.Printf("\t%s.BinaryOp(%q, %s) = %s\n", fmtv(x.Value), op, fmtv(y.(*traceValue).Value), fmtv(z))
return &traceValue{z}
}
func (x *traceValue) String() string {
s := x.Value.String()
fmt.Printf("\t%s.String() = %#v\n", fmtv(x.Value), s)
return s
}
func fmtv(v Value) string {
t := fmt.Sprintf("%T", v)
if i := strings.LastIndex(t, "."); i >= 0 { // strip package
t = t[i+1:]
}
return fmt.Sprintf("%s(%#v)", t, v)
}