Skip to content

Commit c128772

Browse files
committed
Support star (pointer) *.
1 parent d754ec2 commit c128772

4 files changed

Lines changed: 40 additions & 4 deletions

File tree

parse/expression.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const (
2222
ExpressionTypeArray
2323
ExpressionTypeCall
2424
ExpressionTypeIndex
25+
ExpressionTypeStar
2526
ExpressionTypeParameter
2627
ExpressionTypeResult
2728
)
@@ -45,6 +46,9 @@ func (expr *Expression) String() string {
4546
case ExpressionTypeIndex:
4647
// array[123]
4748
res = fmt.Sprintf("%s[%s]", expr.Name, expr.Children)
49+
case ExpressionTypeStar:
50+
// *flag
51+
res = fmt.Sprintf("*%s", expr.Children[0])
4852
default:
4953
res = expr.Name
5054
}

parse/expression_parse.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func parseArrayExpression(expr *ast.CompositeLit) *Expression {
7373
// []int{1, 2}
7474
nameType := parseNameType(&expr.Type)
7575
res := &Expression{
76-
Type: ExpressionTypeArray,
76+
Type: ExpressionTypeArray,
7777
NameType: nameType, // int*
7878
}
7979

@@ -112,6 +112,18 @@ func parseIndexExpression(expr *ast.IndexExpr) *Expression {
112112
return res
113113
}
114114

115+
func parseStarExpression(expr *ast.StarExpr) *Expression {
116+
// Star expression like:
117+
// *flag
118+
inside := parseExpression(&expr.X)
119+
120+
res := &Expression{
121+
Type: ExpressionTypeStar,
122+
Children: Expressions{inside}, // flag
123+
}
124+
return res
125+
}
126+
115127
func parseExpression(expr *ast.Expr) *Expression {
116128
e, ok := (*expr).(*ast.BasicLit)
117129
if ok {
@@ -148,5 +160,10 @@ func parseExpression(expr *ast.Expr) *Expression {
148160
return parseIndexExpression(e7)
149161
}
150162

163+
e8, ok := (*expr).(*ast.StarExpr)
164+
if ok {
165+
return parseStarExpression(e8)
166+
}
167+
151168
panic("parseExpression(): unknown expression type")
152169
}

parse/name_type_parse.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,21 @@ package parse
66
import "go/ast"
77

88
func parseIdentifierNameType(nameType *ast.Ident) string {
9+
// Normal type like:
10+
// int, string, ..
911
return nameType.Name
1012
}
1113

1214
func parseArrayNameType(nameType *ast.ArrayType) string {
13-
return nameType.Elt.(*ast.Ident).Name + "*"
15+
// Array type like:
16+
// []int
17+
return parseNameType(&nameType.Elt) + "*"
18+
}
19+
20+
func parseStarNameType(nameType *ast.StarExpr) string {
21+
// Star type like:
22+
// *int
23+
return parseNameType(&nameType.X) + "*"
1424
}
1525

1626
func parseNameType(nameType *ast.Expr) string {
@@ -24,5 +34,10 @@ func parseNameType(nameType *ast.Expr) string {
2434
return parseArrayNameType(e2)
2535
}
2636

37+
e3, ok := (*nameType).(*ast.StarExpr)
38+
if ok {
39+
return parseStarNameType(e3)
40+
}
41+
2742
panic("parseNameType(): unknown name type")
2843
}

parse/test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ package parse
22

33
import "fmt"
44

5-
func test(a string, bb []int) int {
5+
func test(a string, bb []*int) int {
66
fmt.Printf("Hello, World!\n")
77
res := 0
88
array := []int{1, 2}
99
for i := 0; i < 10; i++ {
1010
res += 10 / 2
1111
array = append(array, 1)
12-
if res != 10 && (array[0] >= 10 || bb[3] == 5) {
12+
if res != 10 && (array[0] >= 10 || *bb[3] == 5) {
1313
continue
1414
}
1515
}

0 commit comments

Comments
 (0)