Skip to content

Commit

Permalink
Merge pull request #248 from earncef/Nil-Coalescing
Browse files Browse the repository at this point in the history
Nil coalescing operator
  • Loading branch information
mattn authored Jun 5, 2018
2 parents eda3e4e + 55307a8 commit 3a1acce
Show file tree
Hide file tree
Showing 8 changed files with 941 additions and 788 deletions.
7 changes: 7 additions & 0 deletions ast/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ type BinOpExpr struct {
Rhs Expr
}

// NilCoalescingOpExpr provide if invalid operator expression.
type NilCoalescingOpExpr struct {
ExprImpl
Lhs Expr
Rhs Expr
}

// TernaryOpExpr provide ternary operator expression.
type TernaryOpExpr struct {
ExprImpl
Expand Down
13 changes: 12 additions & 1 deletion parser/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,17 @@ retry:
tok = int(ch)
lit = string(ch)
}
case '?':
s.next()
switch s.peek() {
case '?':
tok = NILCOALESCE
lit = "??"
default:
s.back()
tok = int(ch)
lit = string(ch)
}
case '+':
s.next()
switch s.peek() {
Expand Down Expand Up @@ -291,7 +302,7 @@ retry:
tok = int(ch)
lit = string(ch)
}
case '\n', '(', ')', ':', ';', '%', '?', '{', '}', '[', ']', ',', '^':
case '\n', '(', ')', ':', ';', '%', '{', '}', '[', ']', ',', '^':
tok = int(ch)
lit = string(ch)
default:
Expand Down
Loading

0 comments on commit 3a1acce

Please sign in to comment.