Skip to content

Commit 5feb4ca

Browse files
committed
refactor(ast): centralize logical or detection
Add ast.IsLogicalOr and use it at AST-expression logical OR checks so compiler conditional lowering does not import token just to inspect operator spelling.
1 parent 8155a44 commit 5feb4ca

5 files changed

Lines changed: 14 additions & 6 deletions

File tree

ast/ast.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,15 @@ type InfixExpression struct {
445445
Right Expression
446446
}
447447

448+
// IsLogicalOr returns exp as an infix logical OR expression.
449+
func IsLogicalOr(exp Expression) (*InfixExpression, bool) {
450+
infix, ok := exp.(*InfixExpression)
451+
if !ok || infix.Operator != token.SYM_LOGICAL_OR {
452+
return nil, false
453+
}
454+
return infix, true
455+
}
456+
448457
func (ie *InfixExpression) expressionNode() {}
449458
func (ie *InfixExpression) Tok() token.Token { return ie.Token }
450459
func (ie *InfixExpression) String() string {

compiler/compiler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1705,7 +1705,7 @@ func (c *Compiler) compileCondScalar(op string, left *Symbol, right *Symbol) *Sy
17051705
}
17061706

17071707
func (c *Compiler) compileInfixBasic(expr *ast.InfixExpression, info *ExprInfo) (res []*Symbol) {
1708-
if expr.Operator == token.SYM_LOGICAL_OR && !info.HasFallbackOr() {
1708+
if _, isLogicalOr := ast.IsLogicalOr(expr); isLogicalOr && !info.HasFallbackOr() {
17091709
return c.compileLogicalOrCondition(expr)
17101710
}
17111711

compiler/cond.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"fmt"
55

66
"github.com/thiremani/pluto/ast"
7-
"github.com/thiremani/pluto/token"
87
"tinygo.org/x/go-llvm"
98
)
109

@@ -417,8 +416,8 @@ func (c *Compiler) hasCondExprInTree(expr ast.Expression) bool {
417416
}
418417

419418
func (c *Compiler) logicalOrCondExpr(expr ast.Expression) (*ast.InfixExpression, bool) {
420-
infix, ok := expr.(*ast.InfixExpression)
421-
if !ok || infix.Operator != token.SYM_LOGICAL_OR {
419+
infix, ok := ast.IsLogicalOr(expr)
420+
if !ok {
422421
return nil, false
423422
}
424423

compiler/solver.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1573,7 +1573,7 @@ func (ts *TypeSolver) TypeInfixExpression(expr *ast.InfixExpression) (types []Ty
15731573
return
15741574
}
15751575

1576-
if expr.Operator == token.SYM_LOGICAL_OR {
1576+
if _, isLogicalOr := ast.IsLogicalOr(expr); isLogicalOr {
15771577
return ts.typeLogicalOrExpression(expr, left, right)
15781578
}
15791579

parser/parser.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,7 @@ func (p *StmtParser) isCondition(exp ast.Expression) bool {
791791
return true
792792
}
793793

794-
if infix, ok := exp.(*ast.InfixExpression); ok && infix.Operator == token.SYM_LOGICAL_OR {
794+
if infix, ok := ast.IsLogicalOr(exp); ok {
795795
return p.isCondition(infix.Left) && p.isCondition(infix.Right)
796796
}
797797

0 commit comments

Comments
 (0)