Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gnolang): allow floats in inc/dec statements #1221

Merged
merged 2 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 45 additions & 2 deletions gnovm/pkg/gnolang/op_inc_dec.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package gnolang

import (
"fmt"
"math/big"

"github.com/cockroachdb/apd"
)

func (m *Machine) doOpInc() {
s := m.PopStmt().(*IncDecStmt)

Expand Down Expand Up @@ -42,8 +49,26 @@
lv.SetUint32(lv.GetUint32() + 1)
case Uint64Type:
lv.SetUint64(lv.GetUint64() + 1)
case Float32Type:
lv.SetFloat32(lv.GetFloat32() + 1)
case Float64Type:
lv.SetFloat64(lv.GetFloat64() + 1)
case BigintType, UntypedBigintType:
lb := lv.GetBigInt()
lb = big.NewInt(0).Add(lb, big.NewInt(1))
lv.V = BigintValue{V: lb}
case BigdecType, UntypedBigdecType:
lb := lv.GetBigDec()
sum := apd.New(0, 0)
cond, err := apd.BaseContext.WithPrecision(0).Add(sum, lb, apd.New(1, 0))
if err != nil {
panic(fmt.Sprintf("bigdec addition error: %v", err))
} else if cond.Inexact() {
panic(fmt.Sprintf("bigdec addition inexact: %v + 1", lb))

Check warning on line 67 in gnovm/pkg/gnolang/op_inc_dec.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/op_inc_dec.go#L52-L67

Added lines #L52 - L67 were not covered by tests
}
lv.V = BigdecValue{V: sum}

Check warning on line 69 in gnovm/pkg/gnolang/op_inc_dec.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/op_inc_dec.go#L69

Added line #L69 was not covered by tests
default:
panic("unexpected type in in operation")
panic(fmt.Sprintf("unexpected type %s in inc/dec operation", lv.T))

Check warning on line 71 in gnovm/pkg/gnolang/op_inc_dec.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/op_inc_dec.go#L71

Added line #L71 was not covered by tests
}

// Mark dirty in realm.
Expand Down Expand Up @@ -94,8 +119,26 @@
lv.SetUint32(lv.GetUint32() - 1)
case Uint64Type:
lv.SetUint64(lv.GetUint64() - 1)
case Float32Type:
lv.SetFloat32(lv.GetFloat32() - 1)
case Float64Type:
lv.SetFloat64(lv.GetFloat64() - 1)
case BigintType, UntypedBigintType:
lb := lv.GetBigInt()
lb = big.NewInt(0).Sub(lb, big.NewInt(1))
lv.V = BigintValue{V: lb}
case BigdecType, UntypedBigdecType:
lb := lv.GetBigDec()
sum := apd.New(0, 0)
cond, err := apd.BaseContext.WithPrecision(0).Sub(sum, lb, apd.New(1, 0))
if err != nil {
panic(fmt.Sprintf("bigdec addition error: %v", err))
} else if cond.Inexact() {
panic(fmt.Sprintf("bigdec addition inexact: %v + 1", lb))

Check warning on line 137 in gnovm/pkg/gnolang/op_inc_dec.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/op_inc_dec.go#L122-L137

Added lines #L122 - L137 were not covered by tests
}
lv.V = BigdecValue{V: sum}

Check warning on line 139 in gnovm/pkg/gnolang/op_inc_dec.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/op_inc_dec.go#L139

Added line #L139 was not covered by tests
default:
panic("unexpected type in in operation")
panic(fmt.Sprintf("unexpected type %s in inc/dec operation", lv.T))

Check warning on line 141 in gnovm/pkg/gnolang/op_inc_dec.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/op_inc_dec.go#L141

Added line #L141 was not covered by tests
}

// Mark dirty in realm.
Expand Down
File renamed without changes.
15 changes: 15 additions & 0 deletions gnovm/tests/files/inc1.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

func main() {
var i float64 = 899
i++
println(i)

j := float32(901)
j--
println(j)
}

// Output:
// 900
// 900
Loading