Skip to content

Commit 93941b0

Browse files
committed
Refactor ast nodes to embed private base struct
1 parent 75e0078 commit 93941b0

File tree

4 files changed

+88
-60
lines changed

4 files changed

+88
-60
lines changed

ast/node.go

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,152 +15,148 @@ type Node interface {
1515
SetType(reflect.Type)
1616
}
1717

18-
type Base struct {
18+
type base struct {
1919
loc file.Location
2020
nodeType reflect.Type
2121
}
2222

23-
func (n *Base) Location() file.Location {
23+
func (n *base) Location() file.Location {
2424
return n.loc
2525
}
2626

27-
func (n *Base) SetLocation(loc file.Location) {
27+
func (n *base) SetLocation(loc file.Location) {
2828
n.loc = loc
2929
}
3030

31-
func (n *Base) Type() reflect.Type {
31+
func (n *base) Type() reflect.Type {
3232
return n.nodeType
3333
}
3434

35-
func (n *Base) SetType(t reflect.Type) {
35+
func (n *base) SetType(t reflect.Type) {
3636
n.nodeType = t
3737
}
3838

39-
func Loc(l file.Location) Base {
40-
return Base{loc: l}
41-
}
42-
4339
type NilNode struct {
44-
Base
40+
base
4541
}
4642

4743
type IdentifierNode struct {
48-
Base
44+
base
4945
Value string
5046
}
5147

5248
type IntegerNode struct {
53-
Base
49+
base
5450
Value int
5551
}
5652

5753
type FloatNode struct {
58-
Base
54+
base
5955
Value float64
6056
}
6157

6258
type BoolNode struct {
63-
Base
59+
base
6460
Value bool
6561
}
6662

6763
type StringNode struct {
68-
Base
64+
base
6965
Value string
7066
}
7167

7268
type ConstantNode struct {
73-
Base
69+
base
7470
Value interface{}
7571
}
7672

7773
type UnaryNode struct {
78-
Base
74+
base
7975
Operator string
8076
Node Node
8177
}
8278

8379
type BinaryNode struct {
84-
Base
80+
base
8581
Operator string
8682
Left Node
8783
Right Node
8884
}
8985

9086
type MatchesNode struct {
91-
Base
87+
base
9288
Regexp *regexp.Regexp
9389
Left Node
9490
Right Node
9591
}
9692

9793
type PropertyNode struct {
98-
Base
94+
base
9995
Node Node
10096
Property string
10197
}
10298

10399
type IndexNode struct {
104-
Base
100+
base
105101
Node Node
106102
Index Node
107103
}
108104

109105
type SliceNode struct {
110-
Base
106+
base
111107
Node Node
112108
From Node
113109
To Node
114110
}
115111

116112
type MethodNode struct {
117-
Base
113+
base
118114
Node Node
119115
Method string
120116
Arguments []Node
121117
}
122118

123119
type FunctionNode struct {
124-
Base
120+
base
125121
Name string
126122
Arguments []Node
127123
Fast bool
128124
}
129125

130126
type BuiltinNode struct {
131-
Base
127+
base
132128
Name string
133129
Arguments []Node
134130
}
135131

136132
type ClosureNode struct {
137-
Base
133+
base
138134
Node Node
139135
}
140136

141137
type PointerNode struct {
142-
Base
138+
base
143139
}
144140

145141
type ConditionalNode struct {
146-
Base
142+
base
147143
Cond Node
148144
Exp1 Node
149145
Exp2 Node
150146
}
151147

152148
type ArrayNode struct {
153-
Base
149+
base
154150
Nodes []Node
155151
}
156152

157153
type MapNode struct {
158-
Base
154+
base
159155
Pairs []Node
160156
}
161157

162158
type PairNode struct {
163-
Base
159+
base
164160
Key Node
165161
Value Node
166162
}

ast/print.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package ast
33
import (
44
"fmt"
55
"reflect"
6+
"regexp"
67
)
78

89
func Dump(node Node) string {
@@ -19,7 +20,7 @@ func dump(v reflect.Value, ident string) string {
1920
out := t.Name() + "{\n"
2021
for i := 0; i < t.NumField(); i++ {
2122
f := t.Field(i)
22-
if f.Name == "Base" {
23+
if isPrivate(f.Name) {
2324
continue
2425
}
2526
s := v.Field(i)
@@ -50,3 +51,9 @@ func dump(v reflect.Value, ident string) string {
5051
return fmt.Sprintf("%v", v)
5152
}
5253
}
54+
55+
var isCapital = regexp.MustCompile("^[A-Z]")
56+
57+
func isPrivate(s string) bool {
58+
return !isCapital.Match([]byte(s))
59+
}

cmd/exe/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import (
44
"bufio"
55
"flag"
66
"fmt"
7+
"github.com/antonmedv/expr/ast"
78
"io/ioutil"
89
"os"
910

1011
"github.com/antonmedv/expr"
11-
"github.com/antonmedv/expr/ast"
1212
"github.com/antonmedv/expr/checker"
1313
"github.com/antonmedv/expr/compiler"
1414
"github.com/antonmedv/expr/optimizer"

0 commit comments

Comments
 (0)