Skip to content

Commit

Permalink
feat: support and & or boolean operators
Browse files Browse the repository at this point in the history
  • Loading branch information
saffage committed May 7, 2024
1 parent a24d20f commit ab27de0
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 9 deletions.
2 changes: 2 additions & 0 deletions ast/operator_kind.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ const (
OperatorBitXor // ^
OperatorBitShl // <<
OperatorBitShr // >>
OperatorAnd // and
OperatorOr // or

// Postfix.

Expand Down
10 changes: 6 additions & 4 deletions ast/operator_kind_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions checker/type_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,15 @@ func (check *Checker) typeOfInfixOp(node *ast.InfixOp) types.Type {
return types.Primitives[types.Bool]
}

case ast.OperatorAnd, ast.OperatorOr:
switch primitive.Kind() {
case types.UntypedBool:
return types.Primitives[types.UntypedBool]

case types.Bool:
return types.Primitives[types.Bool]
}

default:
panic("unreachable")
}
Expand Down
8 changes: 7 additions & 1 deletion parser/parse_ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,16 @@ func (p *Parser) parseBinaryExpr(lhs ast.Node, precedence token.Precedence) ast.

case token.Shl:
binaryOpKind = ast.OperatorBitShl
case token.Shr:

case token.Shr:
binaryOpKind = ast.OperatorBitShr

case token.KwAnd:
binaryOpKind = ast.OperatorAnd

case token.KwOr:
binaryOpKind = ast.OperatorOr

default:
p.errorf(
tok.Start,
Expand Down
4 changes: 4 additions & 0 deletions token/kind.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ const (
KwReturn // keyword 'return'
KwBreak // keyword 'break'
KwContinue // keyword 'continue'
KwAnd // keyword 'and'
KwOr // keyword 'or'
)

const (
Expand Down Expand Up @@ -241,4 +243,6 @@ var representableKinds = map[Kind]string{
KwReturn: "return",
KwBreak: "break",
KwContinue: "continue",
KwAnd: "and",
KwOr: "or",
}
6 changes: 4 additions & 2 deletions token/kind_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions token/kind_user_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const (
LowestPrec Precedence = iota
AssignPrec
ArrowPrec
BooleanOpPrec
BitwisePrec
CmpPrec
ShiftPrec
Expand Down Expand Up @@ -69,6 +70,9 @@ func (t Token) Precedence() Precedence {
case EqOp, NeOp, LtOp, GtOp, LeOp, GeOp:
return CmpPrec

case KwAnd, KwOr:
return BooleanOpPrec

case Arrow, FatArrow, Dot2, Dot2Less:
return ArrowPrec

Expand Down

0 comments on commit ab27de0

Please sign in to comment.