Skip to content

Commit db8980f

Browse files
committed
refactor: more idiomatic golang using embedded structs
1 parent c487216 commit db8980f

File tree

1 file changed

+36
-57
lines changed

1 file changed

+36
-57
lines changed

internal/parser/ast.go

Lines changed: 36 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -18,50 +18,42 @@ func (*RatioLiteral) literal() {}
1818
func (*NumberLiteral) literal() {}
1919
func (*StringLiteral) literal() {}
2020

21-
func (l *AssetLiteral) GetRange() Range { return l.Range }
22-
func (l *MonetaryLiteral) GetRange() Range { return l.Range }
23-
func (l *AccountLiteral) GetRange() Range { return l.Range }
24-
func (l *VariableLiteral) GetRange() Range { return l.Range }
25-
func (l *RatioLiteral) GetRange() Range { return l.Range }
26-
func (l *NumberLiteral) GetRange() Range { return l.Range }
27-
func (l *StringLiteral) GetRange() Range { return l.Range }
28-
2921
type (
3022
AssetLiteral struct {
31-
Range Range
23+
Range
3224
Asset string
3325
}
3426

3527
NumberLiteral struct {
36-
Range Range
28+
Range
3729
Number int
3830
}
3931

4032
StringLiteral struct {
41-
Range Range
33+
Range
4234
String string
4335
}
4436

4537
MonetaryLiteral struct {
46-
Range Range
38+
Range
4739
Asset Literal
4840
Amount Literal
4941
}
5042

5143
AccountLiteral struct {
52-
Range Range
53-
Name string
44+
Range
45+
Name string
5446
}
5547

5648
RatioLiteral struct {
57-
Range Range
49+
Range
5850
Numerator *big.Int
5951
Denominator *big.Int
6052
}
6153

6254
VariableLiteral struct {
63-
Range Range
64-
Name string
55+
Range
56+
Name string
6557
}
6658
)
6759

@@ -70,7 +62,7 @@ func (r RatioLiteral) ToRatio() *big.Rat {
7062
}
7163

7264
type RemainingAllotment struct {
73-
Range Range
65+
Range
7466
}
7567

7668
func (a *AccountLiteral) IsWorld() bool {
@@ -90,34 +82,28 @@ func (*SourceAccount) source() {}
9082
func (*SourceCapped) source() {}
9183
func (*SourceOverdraft) source() {}
9284

93-
func (s *SourceAccount) GetRange() Range { return s.Literal.GetRange() }
94-
func (s *SourceInorder) GetRange() Range { return s.Range }
95-
func (s *SourceAllotment) GetRange() Range { return s.Range }
96-
func (s *SourceCapped) GetRange() Range { return s.Range }
97-
func (s *SourceOverdraft) GetRange() Range { return s.Range }
98-
9985
type (
10086
SourceAccount struct {
101-
Literal Literal
87+
Literal
10288
}
10389

10490
SourceInorder struct {
105-
Range Range
91+
Range
10692
Sources []Source
10793
}
10894
SourceAllotment struct {
109-
Range Range
95+
Range
11096
Items []SourceAllotmentItem
11197
}
11298

11399
SourceCapped struct {
114-
Range Range
115-
From Source
116-
Cap Literal
100+
Range
101+
From Source
102+
Cap Literal
117103
}
118104

119105
SourceOverdraft struct {
120-
Range Range
106+
Range
121107
Address Literal
122108
Bounded *Literal
123109
}
@@ -130,48 +116,44 @@ func (*RatioLiteral) allotmentValue() {}
130116
func (*VariableLiteral) allotmentValue() {}
131117

132118
type SourceAllotmentItem struct {
133-
Range Range
119+
Range
134120
Allotment AllotmentValue
135121
From Source
136122
}
137123

138124
// Destination exprs
139125
type Destination interface {
140126
destination()
141-
GetRange() Range
127+
Ranged
142128
}
143129

144130
func (*DestinationInorder) destination() {}
145131
func (*DestinationAccount) destination() {}
146132
func (*DestinationAllotment) destination() {}
147133

148-
func (d *DestinationAccount) GetRange() Range { return d.Literal.GetRange() }
149-
func (d *DestinationInorder) GetRange() Range { return d.Range }
150-
func (d *DestinationAllotment) GetRange() Range { return d.Range }
151-
152134
type (
153135
DestinationAccount struct {
154-
Literal Literal
136+
Literal
155137
}
156138

157139
DestinationInorder struct {
158-
Range Range
140+
Range
159141
Clauses []DestinationInorderClause
160142
Remaining KeptOrDestination
161143
}
162144

163145
DestinationInorderClause struct {
164-
Range Range
165-
Cap Literal
166-
To KeptOrDestination
146+
Range
147+
Cap Literal
148+
To KeptOrDestination
167149
}
168150
)
169151

170152
type KeptOrDestination interface {
171153
keptOrDestination()
172154
}
173155
type DestinationKept struct {
174-
Range Range
156+
Range
175157
}
176158
type DestinationTo struct {
177159
Destination Destination
@@ -181,12 +163,12 @@ func (*DestinationKept) keptOrDestination() {}
181163
func (*DestinationTo) keptOrDestination() {}
182164

183165
type DestinationAllotment struct {
184-
Range Range
166+
Range
185167
Items []DestinationAllotmentItem
186168
}
187169

188170
type DestinationAllotmentItem struct {
189-
Range Range
171+
Range
190172
Allotment AllotmentValue
191173
To KeptOrDestination
192174
}
@@ -195,22 +177,19 @@ type DestinationAllotmentItem struct {
195177

196178
type Statement interface {
197179
statement()
198-
GetRange() Range
180+
Ranged
199181
}
200182

201183
func (*FnCall) statement() {}
202184
func (*SendStatement) statement() {}
203185

204-
func (s *FnCall) GetRange() Range { return s.Range }
205-
func (s *SendStatement) GetRange() Range { return s.Range }
206-
207186
type FnCallIdentifier struct {
208-
Range Range
209-
Name string
187+
Range
188+
Name string
210189
}
211190

212191
type FnCall struct {
213-
Range Range
192+
Range
214193
Caller *FnCallIdentifier
215194
Args []Literal
216195
}
@@ -220,27 +199,27 @@ type SentValueLiteral struct {
220199
Monetary Literal
221200
}
222201
type SentValueAll struct {
223-
Range Range
202+
Range
224203
Asset Literal
225204
}
226205

227206
func (*SentValueLiteral) sentValue() {}
228207
func (*SentValueAll) sentValue() {}
229208

230209
type SendStatement struct {
231-
Range Range
210+
Range
232211
SentValue SentValue
233212
Source Source
234213
Destination Destination
235214
}
236215

237216
type TypeDecl struct {
238-
Range Range
239-
Name string
217+
Range
218+
Name string
240219
}
241220

242221
type VarDeclaration struct {
243-
Range Range
222+
Range
244223
Name *VariableLiteral
245224
Type *TypeDecl
246225
Origin *FnCall

0 commit comments

Comments
 (0)