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
8 changes: 8 additions & 0 deletions parser/cst/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,16 @@ type (
Ident string
Type any
}
ParenthesizedExpr struct {
Expr any
}
)

func MakeParenthesizedExpr(expr any) (ParenthesizedExpr, error) {
result := ParenthesizedExpr{expr}
return result, nil
}

func MakeIndexExpr(baseExpr, indexExpr any) (IndexExpr, error) {
result := IndexExpr{
BaseExpr: baseExpr,
Expand Down
25 changes: 25 additions & 0 deletions parser/expr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,31 @@ func Test_BinOp(t *testing.T) {

testStmts(t, tcs)
}
func Test_ParenthesizedExpr(t *testing.T) {

tcs := []TestForStmt{
{
Name: "parenthesized uint literal",
Input: `(1);`,
Output: cst.ParenthesizedExpr{
Expr: cst.UintLit{Value: 1},
},
},
{
Name: "parenthesized operand in binary operation",
Input: `1 + (2);`,
Output: cst.BinOp{
R: cst.UintLit{Value: 1},
Op: "+",
L: cst.ParenthesizedExpr{
Expr: cst.UintLit{Value: 2},
},
},
},
}

testStmts(t, tcs)
}

func Test_IndexExpr(t *testing.T) {

Expand Down
77 changes: 36 additions & 41 deletions parser/grammar.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ Stmt
;

ExprStmt
: Expr1 << $0, nil >>
: Expr << $0, nil >>
;

VarDecl
: var identifier Type "=" Expr1 << cst.MakeVarDecl($1, $2, $4) >>
: var identifier Type "=" Expr << cst.MakeVarDecl($1, $2, $4) >>
;

Type
Expand Down Expand Up @@ -88,16 +88,16 @@ Param
;

ConstDecl
: const identifier Type "=" Expr1 << cst.MakeConstDecl($1, $2, $4) >>
: const identifier Type "=" Expr << cst.MakeConstDecl($1, $2, $4) >>
;

Assignment
: identifier "=" Expr1 << cst.MakeAssignment($0, $2) >>
: identifier "=" Expr << cst.MakeAssignment($0, $2) >>
;

Return
: return << cst.MakeReturn(nil) >>
| return Expr1 << cst.MakeReturn($1) >>
| return Expr << cst.MakeReturn($1) >>
;

Block
Expand All @@ -110,47 +110,33 @@ StmtList
;

IfStmt
: if Expr1 Block << cst.MakeIf($1, $2, nil) >>
| if Expr1 Block else Block << cst.MakeIf($1, $2, $4) >>
| if Expr1 Block else IfStmt << cst.MakeIf($1, $2, $4) >>
: if Expr Block << cst.MakeIf($1, $2, nil) >>
| if Expr Block else Block << cst.MakeIf($1, $2, $4) >>
| if Expr Block else IfStmt << cst.MakeIf($1, $2, $4) >>
;

Expr1
: Expr1 LogOp Expr2 << cst.MakeBinOp($0, $1, $2) >>
| Expr2 << $0, nil >>
// Binary operator precedence is not represented in the grammar.
// It is enforced by compiler passes later on.
Expr
: Expr BinaryOperator Term << cst.MakeBinOp($0, $1, $2) >>
| Term << $0, nil >>
;

Expr2
: Expr2 CmpOp Expr3 << cst.MakeBinOp($0, $1, $2) >>
| Expr3 << $0, nil >>
;

Expr3
: Expr3 AddOp Expr4 << cst.MakeBinOp($0, $1, $2) >>
| Expr4 << $0, nil >>
;

Expr4
: Expr4 MulOp Expr5 << cst.MakeBinOp($0, $1, $2) >>
| Expr5 << $0, nil >>
;

Expr5
: UnaryOp Expr5 << cst.MakeUnaryOp($0, $1) >>
Term
: UnaryOperator Term << cst.MakeUnaryOp($0, $1) >>
| PrimaryExpr << $0, nil >>
;

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

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

Arguments
Expand All @@ -159,14 +145,18 @@ Arguments
;

ExpressionList
: Expr1 << []any{$0}, nil >>
| ExpressionList "," Expr1 << append($0.([]any), $2), nil >>
: Expr << []any{$0}, nil >>
| ExpressionList "," Expr << append($0.([]any), $2), nil >>
;

Operand
: Lit << $0, nil >>
| OperandName << $0, nil >>
| "(" Expr1 ")" << $1, nil >>
: Lit << $0, nil >>
| OperandName << $0, nil >>
| ParenthesizedExpr << $0, nil >>
;

ParenthesizedExpr
: "(" Expr ")" << cst.MakeParenthesizedExpr($1) >>
;

OperandName
Expand All @@ -181,8 +171,13 @@ Lit
| uintLiteral << cst.MakeUintLit($0) >>
;

LogOp: "||" | "&&";
CmpOp: "==" | "!=" | ">" | "<" | ">=" | "<=";
AddOp: "+" | "-" | "|" | "^";
MulOp: "*" | "/" | "%" | "<<" | ">>" | "&";
UnaryOp: "-" | "!";
BinaryOperator:
"||" | "&&" |
"==" | "!=" |
">" | "<" | ">=" | "<=" |
"+" | "-" |
"|" | "&" | "^" |
"*" | "/" | "%"
;

UnaryOperator: "-" | "!" | "~";
66 changes: 0 additions & 66 deletions parser/precedence_test.go

This file was deleted.