Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions parser/cst/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ type (
QualifiedIdent QualifiedIdent
Args []any
}
IndexExpr struct {
BaseExpr any
IndexExpr any
}
SliceType struct {
Type any
}
Expand Down Expand Up @@ -84,6 +88,14 @@ type (
}
)

func MakeIndexExpr(baseExpr, indexExpr any) (IndexExpr, error) {
result := IndexExpr{
BaseExpr: baseExpr,
IndexExpr: indexExpr,
}
return result, nil
}

func AppendToParamSlice(slice, item any) ([]Param, error) {
s := slice.([]Param)
i := item.(Param)
Expand Down
34 changes: 34 additions & 0 deletions parser/expr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,40 @@ func Test_BinOp(t *testing.T) {
testStmts(t, tcs)
}

func Test_IndexExpr(t *testing.T) {

tcs := []TestForStmt{
{
Name: "identifier indexed with uint literal",
Input: `a[1];`,
Output: cst.IndexExpr{
BaseExpr: cst.QualifiedIdent{Ident: "a"},
IndexExpr: cst.UintLit{Value: 1},
},
},
{
Name: "identifier indexed with identifier",
Input: `a[i];`,
Output: cst.IndexExpr{
BaseExpr: cst.QualifiedIdent{Ident: "a"},
IndexExpr: cst.QualifiedIdent{Ident: "i"},
},
},
{
Name: "function call indexed with uint literal",
Input: `f()[1];`,
Output: cst.IndexExpr{
BaseExpr: cst.Call{
QualifiedIdent: cst.QualifiedIdent{Ident: "f"},
},
IndexExpr: cst.UintLit{Value: 1},
},
},
}

testStmts(t, tcs)
}

func Test_IntLit(t *testing.T) {

tcs := []TestForStmt{
Expand Down
10 changes: 7 additions & 3 deletions parser/grammar.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,19 @@ Expr5
| PrimaryExpr << $0, nil >>
;

PrimaryExpr
: Operand << $0, nil >>
// TODO: // https://go.dev/ref/spec#PrimaryExpr
// | Conversion
// | PrimaryExpr Index
// | PrimaryExpr Slice
PrimaryExpr
: Operand << $0, nil >>
| PrimaryExpr Index << cst.MakeIndexExpr($0, $1) >>
| PrimaryExpr Arguments << cst.MakeCall($0, $1) >>
;

Index
: "[" Expr1 "]" << $1, nil >>
;

Arguments
: "(" ")" << nil, nil >>
| "(" ExpressionList ")" << $1, nil >>
Expand Down