Skip to content

Commit e0992ff

Browse files
committed
Refactor visitor
1 parent 686baab commit e0992ff

File tree

9 files changed

+185
-279
lines changed

9 files changed

+185
-279
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ product.Stock < 15
3131
* Static typing ([example](https://godoc.org/github.com/antonmedv/expr#example-Env)).
3232
```go
3333
out, err := expr.Eval("'hello' + 10")
34-
// err: invalid operation + (mismatched types string and int64)
34+
// err: invalid operation + (mismatched types string and int)
3535
// | 'hello' + 10
3636
// | ........^
3737
```

ast/base_visitor.go

Lines changed: 0 additions & 45 deletions
This file was deleted.

ast/visitor.go

Lines changed: 23 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,8 @@ package ast
33
import "fmt"
44

55
type Visitor interface {
6-
NilNode(node *NilNode)
7-
IdentifierNode(node *IdentifierNode)
8-
IntegerNode(node *IntegerNode)
9-
FloatNode(node *FloatNode)
10-
BoolNode(node *BoolNode)
11-
StringNode(node *StringNode)
12-
UnaryNode(node *UnaryNode)
13-
BinaryNode(node *BinaryNode)
14-
MatchesNode(node *MatchesNode)
15-
PropertyNode(node *PropertyNode)
16-
IndexNode(node *IndexNode)
17-
MethodNode(node *MethodNode)
18-
FunctionNode(node *FunctionNode)
19-
BuiltinNode(node *BuiltinNode)
20-
ClosureNode(node *ClosureNode)
21-
PointerNode(node *PointerNode)
22-
ConditionalNode(node *ConditionalNode)
23-
ArrayNode(node *ArrayNode)
24-
MapNode(node *MapNode)
25-
PairNode(node *PairNode)
26-
Node(node *Node)
6+
Enter(node *Node)
7+
Exit(node *Node)
278
}
289

2910
type walker struct {
@@ -38,79 +19,79 @@ func Walk(node *Node, visitor Visitor) {
3819
}
3920

4021
func (w *walker) walk(node *Node) {
41-
w.visitor.Node(node)
22+
w.visitor.Enter(node)
4223

4324
switch n := (*node).(type) {
4425
case *NilNode:
45-
w.visitor.NilNode(n)
26+
w.visitor.Exit(node)
4627
case *IdentifierNode:
47-
w.visitor.IdentifierNode(n)
28+
w.visitor.Exit(node)
4829
case *IntegerNode:
49-
w.visitor.IntegerNode(n)
30+
w.visitor.Exit(node)
5031
case *FloatNode:
51-
w.visitor.FloatNode(n)
32+
w.visitor.Exit(node)
5233
case *BoolNode:
53-
w.visitor.BoolNode(n)
34+
w.visitor.Exit(node)
5435
case *StringNode:
55-
w.visitor.StringNode(n)
36+
w.visitor.Exit(node)
5637
case *UnaryNode:
5738
w.walk(&n.Node)
58-
w.visitor.UnaryNode(n)
39+
w.visitor.Exit(node)
5940
case *BinaryNode:
6041
w.walk(&n.Left)
6142
w.walk(&n.Right)
62-
w.visitor.BinaryNode(n)
43+
w.visitor.Exit(node)
6344
case *MatchesNode:
6445
w.walk(&n.Left)
6546
w.walk(&n.Right)
66-
w.visitor.MatchesNode(n)
47+
w.visitor.Exit(node)
6748
case *PropertyNode:
6849
w.walk(&n.Node)
69-
w.visitor.PropertyNode(n)
50+
w.visitor.Exit(node)
7051
case *IndexNode:
7152
w.walk(&n.Node)
7253
w.walk(&n.Index)
73-
w.visitor.IndexNode(n)
54+
w.visitor.Exit(node)
7455
case *MethodNode:
7556
w.walk(&n.Node)
7657
for _, arg := range n.Arguments {
7758
w.walk(&arg)
7859
}
79-
w.visitor.MethodNode(n)
60+
w.visitor.Exit(node)
8061
case *FunctionNode:
8162
for _, arg := range n.Arguments {
8263
w.walk(&arg)
8364
}
84-
w.visitor.FunctionNode(n)
65+
w.visitor.Exit(node)
8566
case *BuiltinNode:
8667
for _, arg := range n.Arguments {
8768
w.walk(&arg)
8869
}
89-
w.visitor.BuiltinNode(n)
70+
w.visitor.Exit(node)
9071
case *ClosureNode:
9172
w.walk(&n.Node)
92-
w.visitor.ClosureNode(n)
73+
w.visitor.Exit(node)
9374
case *PointerNode:
94-
w.visitor.PointerNode(n)
75+
w.visitor.Exit(node)
9576
case *ConditionalNode:
9677
w.walk(&n.Cond)
9778
w.walk(&n.Exp1)
9879
w.walk(&n.Exp2)
99-
w.visitor.ConditionalNode(n)
80+
w.visitor.Exit(node)
10081
case *ArrayNode:
10182
for _, node := range n.Nodes {
10283
w.walk(&node)
10384
}
104-
w.visitor.ArrayNode(n)
85+
w.visitor.Exit(node)
10586
case *MapNode:
10687
var pair Node
10788
for _, pair = range n.Pairs {
10889
w.walk(&pair)
10990
}
110-
w.visitor.MapNode(n)
91+
w.visitor.Exit(node)
11192
case *PairNode:
11293
w.walk(&n.Value)
113-
w.visitor.PairNode(n)
94+
w.visitor.Exit(node)
11495
default:
11596
panic(fmt.Sprintf("undefined node type (%T)", node))
11697
}

ast/visitor_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ import (
77
)
88

99
type visitor struct {
10-
ast.BaseVisitor
1110
identifiers []string
1211
}
1312

14-
func (v *visitor) IdentifierNode(node *ast.IdentifierNode) {
15-
v.identifiers = append(v.identifiers, node.Value)
13+
func (v *visitor) Enter(node *ast.Node) {}
14+
func (v *visitor) Exit(node *ast.Node) {
15+
if n, ok := (*node).(*ast.IdentifierNode); ok {
16+
v.identifiers = append(v.identifiers, n.Value)
17+
}
1618
}
1719

1820
func TestWalk(t *testing.T) {
@@ -28,15 +30,14 @@ func TestWalk(t *testing.T) {
2830
assert.Equal(t, []string{"foo", "bar"}, visitor.identifiers)
2931
}
3032

31-
type patcher struct {
32-
ast.BaseVisitor
33-
}
33+
type patcher struct{}
3434

35-
func (p *patcher) Node(node *ast.Node) {
35+
func (p *patcher) Enter(node *ast.Node) {
3636
if _, ok := (*node).(*ast.IdentifierNode); ok {
3737
*node = &ast.NilNode{}
3838
}
3939
}
40+
func (p *patcher) Exit(node *ast.Node) {}
4041

4142
func TestWalk_patch(t *testing.T) {
4243
var node ast.Node

checker/checker.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,6 @@ func Check(tree *parser.Tree, config *conf.Config) (t reflect.Type, err error) {
3131

3232
t = v.visit(tree.Node)
3333

34-
// TODO: Move patch into visitor.
35-
if v.operators != nil {
36-
patchOperators(tree, config)
37-
}
38-
3934
if v.expect != reflect.Invalid {
4035
switch v.expect {
4136
case reflect.Int64, reflect.Float64:

checker/patcher.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import (
77
)
88

99
type operatorPatcher struct {
10-
ast.BaseVisitor
1110
ops map[string][]string
1211
types conf.TypesTable
1312
}
1413

15-
func (p *operatorPatcher) Node(node *ast.Node) {
14+
func (p *operatorPatcher) Enter(node *ast.Node) {}
15+
func (p *operatorPatcher) Exit(node *ast.Node) {
1616
binaryNode, ok := (*node).(*ast.BinaryNode)
1717
if !ok {
1818
return
@@ -40,7 +40,7 @@ func (p *operatorPatcher) Node(node *ast.Node) {
4040
}
4141
}
4242

43-
func patchOperators(tree *parser.Tree, config *conf.Config) {
43+
func PatchOperators(tree *parser.Tree, config *conf.Config) {
4444
if len(config.Operators) == 0 {
4545
return
4646
}

0 commit comments

Comments
 (0)