Skip to content

Commit

Permalink
Simplify binding power for now
Browse files Browse the repository at this point in the history
  • Loading branch information
eatonphil committed Apr 15, 2020
1 parent 1250f83 commit e7ff8cb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
16 changes: 8 additions & 8 deletions lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,29 +63,29 @@ type token struct {
loc location
}

func (t token) bindingPower() (uint, uint) {
func (t token) bindingPower() uint {
switch t.kind {
case keywordKind:
switch keyword(t.value) {
case andKeyword:
return 1, 1
fallthrough
case orKeyword:
return 1, 1
return 1
}
case symbolKind:
switch symbol(t.value) {
case eqSymbol:
return 2, 2
fallthrough
case neqSymbol:
return 2, 2
fallthrough
case concatSymbol:
return 3, 3
fallthrough
case plusSymbol:
return 3, 3
return 3
}
}

return 0, 0
return 0
}

type cursor struct {
Expand Down
10 changes: 5 additions & 5 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func parseLiteralExpression(tokens []*token, initialCursor uint) (*expression, u
return nil, initialCursor, false
}

func parseExpression(tokens []*token, initialCursor uint, delimiters []token, min_bp uint) (*expression, uint, bool) {
func parseExpression(tokens []*token, initialCursor uint, delimiters []token, minBp uint) (*expression, uint, bool) {
cursor := initialCursor

var exp *expression
Expand All @@ -85,7 +85,7 @@ func parseExpression(tokens []*token, initialCursor uint, delimiters []token, mi
cursor = newCursor
rightParenToken := tokenFromSymbol(rightParenSymbol)

exp, cursor, ok = parseExpression(tokens, cursor, append(delimiters, rightParenToken), min_bp)
exp, cursor, ok = parseExpression(tokens, cursor, append(delimiters, rightParenToken), minBp)
if !ok {
helpMessage(tokens, cursor, "Expected expression after opening paren")
return nil, initialCursor, false
Expand Down Expand Up @@ -137,13 +137,13 @@ outer:
return nil, initialCursor, false
}

l_bp, r_bp := op.bindingPower()
if l_bp < min_bp {
bp := op.bindingPower()
if bp < minBp {
cursor = lastCursor
break
}

b, newCursor, ok := parseExpression(tokens, cursor, delimiters, r_bp)
b, newCursor, ok := parseExpression(tokens, cursor, delimiters, bp)
if !ok {
helpMessage(tokens, cursor, "Expected right operand")
return nil, initialCursor, false
Expand Down

0 comments on commit e7ff8cb

Please sign in to comment.