Skip to content

Commit

Permalink
fix: integer division assign possibly resulting in float (#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
serkonda7 authored Jan 3, 2024
1 parent c351e09 commit 808bae1
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ _unreleased_
### Type Checks
- Ensure function parameter type exists

### General Fixes
- Fix integer division assign possibly resulting in a float


## 0.0.6
_26 December 2023_
Expand Down
15 changes: 13 additions & 2 deletions lib/bait/parser/stmt.bt
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,20 @@ fun (mut p Parser) assign_stmt() ast.AssignStmt {
}

fun (mut p Parser) partial_assign_stmt(left ast.Expr) ast.AssignStmt {
op := p.tok
mut op := p.tok
p.next()
right := p.expr(.lowest)
mut right := p.expr(.lowest)

// Convert math assign to normal assign
if op.is_math_assign() {
right = ast.InfixExpr{
op = op.math_from_assign()
left = left
right = right
}
op = .assign
}

return ast.AssignStmt{
op = op
left = left
Expand Down
20 changes: 17 additions & 3 deletions lib/bait/token/token.bt
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ pub fun kind_from_string(name string) Token {
const COMPARE_KINDS := [Token.eq, .ne, .lt, .gt, .le, .ge, .key_is]
const MATH_KINDS := [Token.plus, .minus, .mul, .div, .mod]
const OTHER_INFIXES := [Token.key_and, .key_or]
const MATH_ASSIGNS := [Token.plus_assign, .minus_assign, .mul_assign, .div_assign, .mod_assign]

pub fun (kind Token) is_compare() bool {
return COMPARE_KINDS.contains(kind)
Expand All @@ -119,10 +120,23 @@ pub fun (kind Token) is_infix() bool {
return kind.is_compare() or MATH_KINDS.contains(kind) or OTHER_INFIXES.contains(kind)
}

const ASSIGN_KINDS := [Token.decl_assign, .assign, .plus_assign, .minus_assign, .mul_assign, .div_assign, .mod_assign]

pub fun (kind Token) is_assign() bool {
return ASSIGN_KINDS.contains(kind)
return kind == .decl_assign or kind == .assign or kind.is_math_assign()
}

pub fun (tok Token) is_math_assign() bool {
return MATH_ASSIGNS.contains(tok)
}

pub fun (tok Token) math_from_assign() Token {
return match tok {
.plus_assign { .plus }
.minus_assign { .minus }
.mul_assign { .mul }
.div_assign { .div }
.mod_assign { .mod }
else { panic('invalid math assign token') }
}
}

pub fun (kind Token) js_repr() string {
Expand Down
File renamed without changes.
15 changes: 15 additions & 0 deletions tests/number/int_division_test.bt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// SPDX-FileCopyrightText: 2024-present Lukas Neubert <[email protected]>
// SPDX-License-Identifier: MPL-2.0

fun test_integer_division() {
i := 10
res := i / 3
assert res == 3

mut n := 2
n /= 2
assert n == 1

n /= 2
assert n == 0
}

0 comments on commit 808bae1

Please sign in to comment.