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

feat(gnolang): add support for octals without 'o' (eg. 0755) #1331

Merged
merged 11 commits into from
Dec 18, 2023
60 changes: 60 additions & 0 deletions gnovm/pkg/gnolang/gno_test.go
notJoon marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,66 @@ func main() {
m.RunMain()
}

func TestDoOpEvalOctal(t *testing.T) {
m := NewMachine("test", nil)

m.PushExpr(&BasicLitExpr{
Kind: INT,
Value: "0o42",
})
m.doOpEval()
v1 := m.PopValue()
assert.Equal(t, v1.V.String(), "34")
notJoon marked this conversation as resolved.
Show resolved Hide resolved

m.PushExpr(&BasicLitExpr{
Kind: INT,
Value: "0O0000042",
})

m.doOpEval()
v2 := m.PopValue()
assert.Equal(t, v2.V.String(), "34")

// Push a basic literal expression onto the stack.
m.PushExpr(&BasicLitExpr{
Kind: INT,
Value: "042",
})
m.doOpEval()
v3 := m.PopValue()
assert.Equal(t, v3.V.String(), "34")

m.PushExpr((&BasicLitExpr{
Kind: INT,
Value: "048",
}))
assert.Panics(t, func() {
m.doOpEval()
},
"8 is not an octal digit",
)

m.PushExpr((&BasicLitExpr{
Kind: INT,
Value: "0o",
}))
assert.Panics(t, func() {
m.doOpEval()
},
"No digits after 0o",
)

m.PushExpr((&BasicLitExpr{
Kind: INT,
Value: "0OXXXX",
}))
assert.Panics(t, func() {
m.doOpEval()
},
"Non-octal digits(0-7) after 0o",
)
}

func TestEval(t *testing.T) {
t.Parallel()

Expand Down
6 changes: 4 additions & 2 deletions gnovm/pkg/gnolang/op_eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,17 @@ func (m *Machine) doOpEval() {
bi := big.NewInt(0)
// TODO optimize.
// TODO deal with base.
if len(x.Value) > 2 && x.Value[0] == '0' {
var ok bool = false
var ok bool
if len(x.Value) >= 2 && x.Value[0] == '0' {
switch x.Value[1] {
case 'b', 'B':
_, ok = bi.SetString(x.Value[2:], 2)
case 'o', 'O':
_, ok = bi.SetString(x.Value[2:], 8)
case 'x', 'X':
_, ok = bi.SetString(x.Value[2:], 16)
case '0', '1', '2', '3', '4', '5', '6', '7':
_, ok = bi.SetString(x.Value, 8)
notJoon marked this conversation as resolved.
Show resolved Hide resolved
default:
ok = false
}
Expand Down
Loading