xgo/objects/bool.go

65 lines
1.2 KiB
Go
Raw Normal View History

2019-01-09 10:17:42 +03:00
package objects
import (
2019-01-11 13:27:28 +03:00
"github.com/d5/tengo/compiler/token"
2019-01-09 10:17:42 +03:00
)
// Bool represents a boolean value.
2019-01-09 10:17:42 +03:00
type Bool struct {
// this is intentionally non-public to force using objects.TrueValue and FalseValue always
value bool
2019-01-09 10:17:42 +03:00
}
func (o *Bool) String() string {
if o.value {
2019-01-09 10:17:42 +03:00
return "true"
}
return "false"
}
// TypeName returns the name of the type.
2019-01-09 10:17:42 +03:00
func (o *Bool) TypeName() string {
return "bool"
}
// BinaryOp returns another object that is the result of
// a given binary operator and a right-hand side object.
2019-01-09 10:17:42 +03:00
func (o *Bool) BinaryOp(op token.Token, rhs Object) (Object, error) {
return nil, ErrInvalidOperator
}
// Copy returns a copy of the type.
2019-01-09 10:17:42 +03:00
func (o *Bool) Copy() Object {
return o
2019-01-09 10:17:42 +03:00
}
// IsFalsy returns true if the value of the type is falsy.
2019-01-09 10:17:42 +03:00
func (o *Bool) IsFalsy() bool {
return !o.value
2019-01-09 10:17:42 +03:00
}
// Equals returns true if the value of the type
// is equal to the value of another object.
2019-01-09 10:17:42 +03:00
func (o *Bool) Equals(x Object) bool {
return o == x
}
// GobDecode decodes bool value from input bytes.
func (o *Bool) GobDecode(b []byte) (err error) {
o.value = b[0] == 1
return
}
// GobEncode encodes bool values into bytes.
func (o *Bool) GobEncode() (b []byte, err error) {
if o.value {
b = []byte{1}
} else {
b = []byte{0}
2019-01-09 10:17:42 +03:00
}
return
2019-01-09 10:17:42 +03:00
}