Skip to content

Commit

Permalink
Fix number formatting on non-64bit architectures (#235)
Browse files Browse the repository at this point in the history
Fix number formatting on non-64bit architectures (#234)

Run CI tests with GOARCH=386 (-race is not supported when
running tests on Linux/386.)

---------

Co-authored-by: Ben Hoyt <[email protected]>
  • Loading branch information
guilherme-puida and benhoyt authored Jun 11, 2024
1 parent 1de11b9 commit 63ba648
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 11 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,29 @@ jobs:
gawk --version
go test -race ./...
build-linux-386:
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
go: ['1.22']

name: Go ${{ matrix.go }} on Linux 386

steps:
- uses: actions/checkout@v2

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go }}

- name: Build and Test
run: |
gawk --version
GOARCH=386 go test ./...
build-windows:
runs-on: windows-latest

Expand Down
4 changes: 2 additions & 2 deletions internal/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,8 @@ type NumExpr struct {
}

func (e *NumExpr) String() string {
if e.Value == float64(int(e.Value)) {
return strconv.Itoa(int(e.Value))
if e.Value == float64(int64(e.Value)) {
return strconv.FormatInt(int64(e.Value), 10)
} else {
return fmt.Sprintf("%.6g", e.Value)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1135,10 +1135,10 @@ func (c *compiler) binaryOp(op lexer.Token) {
// Generate an array index, handling multi-indexes properly.
func (c *compiler) index(index []ast.Expr) {
for _, expr := range index {
if e, ok := expr.(*ast.NumExpr); ok && e.Value == float64(int(e.Value)) {
if e, ok := expr.(*ast.NumExpr); ok && e.Value == float64(int64(e.Value)) {
// If index expression is integer constant, optimize to string "n"
// to avoid toString() at runtime.
s := strconv.Itoa(int(e.Value))
s := strconv.FormatInt(int64(e.Value), 10)
c.expr(&ast.StrExpr{Value: s})
continue
}
Expand Down
4 changes: 2 additions & 2 deletions internal/compiler/disassembler.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ func (d *disassembler) disassemble(prefix string) error {
case Num:
index := d.fetch()
num := d.program.Nums[index]
if num == float64(int(num)) {
d.writeOpf("Num %d (%d)", int(num), index)
if num == float64(int64(num)) {
d.writeOpf("Num %d (%d)", int64(num), index)
} else {
d.writeOpf("Num %.6g (%d)", num, index)
}
Expand Down
4 changes: 2 additions & 2 deletions interp/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,11 +405,11 @@ func (p *interp) sprintf(format string, args []value) (string, error) {
case 's':
v = p.toString(a)
case 'd':
v = int(a.num())
v = int64(a.num())
case 'f':
v = a.num()
case 'u':
v = uint(a.num())
v = uint64(a.num())
case 'c':
var c []byte
n, isStr := a.isTrueStr()
Expand Down
4 changes: 2 additions & 2 deletions interp/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ func (v value) str(floatFormat string) string {
} else {
return "inf"
}
case v.n == float64(int(v.n)):
return strconv.Itoa(int(v.n))
case v.n == float64(int64(v.n)):
return strconv.FormatInt(int64(v.n), 10)
default:
if floatFormat == "%.6g" {
return strconv.FormatFloat(v.n, 'g', 6, 64)
Expand Down
2 changes: 1 addition & 1 deletion interp/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -980,7 +980,7 @@ func (p *interp) callBuiltin(builtinOp compiler.BuiltinOp) error {
p.replaceTop(num(float64(index + 1)))

case compiler.BuiltinInt:
p.replaceTop(num(float64(int(p.peekTop().num()))))
p.replaceTop(num(float64(int64(p.peekTop().num()))))

case compiler.BuiltinLength:
p.push(num(float64(len(p.line))))
Expand Down

0 comments on commit 63ba648

Please sign in to comment.