-
Notifications
You must be signed in to change notification settings - Fork 0
/
common_test.go
59 lines (49 loc) · 1.21 KB
/
common_test.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
package lr0
import (
"strconv"
"github.com/pkg/errors"
)
const (
tInt Id = iota + 1
tZero
tOne
tPlus
tMinus
tMul
tDiv
tIdent
tInc
nVal
nProd
nSum
nGoal
)
var errDivZero = errors.New("division by zero")
func matchIdentifier(state *State) (next *State, value any) {
if state.IsEOF() {
return
}
if next, _ = state.TakeByteFunc(isAlpha); next == nil {
return
}
next, _ = next.TakeBytesFunc(isAlphaNum)
value = string(state.BytesTo(next))
return
}
func isDigit(b byte) bool { return b >= '0' && b <= '9' }
func byteIsNotSpace(b byte) bool { return b != ' ' }
func runeIsNotSpace(r rune) bool { return r != ' ' }
func isAlphaNum(b byte) bool { return isAlpha(b) || isDigit(b) }
func isAlpha(b byte) bool {
switch {
case b >= 'A' && b <= 'Z', b >= 'a' && b <= 'z', b == '_':
return true
default:
return false
}
}
func bytesToInt(b []byte) (int, error) { return strconv.Atoi(string(b)) }
func calc3AnyNil(any, any, any) (any, error) { return nil, nil }
func calc3StrTrace(a, op, b string) (any, error) { return "(" + a + " " + op + " " + b + ")", nil }
func calc2IntSum(a, b int) int { return a + b }
func calc2IntSub(a, b int) int { return a - b }