further improvements
This commit is contained in:
@@ -4,10 +4,22 @@ import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
var (
|
||||
TRUE = Boolean{Value: true}
|
||||
FALSE = Boolean{Value: false}
|
||||
)
|
||||
|
||||
type Boolean struct {
|
||||
Value bool
|
||||
}
|
||||
|
||||
func NewBoolean(value bool) Boolean {
|
||||
if value {
|
||||
return TRUE
|
||||
}
|
||||
return FALSE
|
||||
}
|
||||
|
||||
func (b Boolean) Bool() bool {
|
||||
return b.Value
|
||||
}
|
||||
|
||||
@@ -7,18 +7,18 @@ type Builtin struct {
|
||||
Fn BuiltinFunction
|
||||
}
|
||||
|
||||
func (b *Builtin) Bool() bool {
|
||||
func (b Builtin) Bool() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (b *Builtin) Type() Type {
|
||||
func (b Builtin) Type() Type {
|
||||
return BuiltinType
|
||||
}
|
||||
|
||||
func (b *Builtin) Inspect() string {
|
||||
func (b Builtin) Inspect() string {
|
||||
return fmt.Sprintf("<built-in function %s>", b.Name)
|
||||
}
|
||||
|
||||
func (b *Builtin) String() string {
|
||||
func (b Builtin) String() string {
|
||||
return b.Inspect()
|
||||
}
|
||||
|
||||
@@ -27,17 +27,21 @@ func (i Integer) String() string {
|
||||
}
|
||||
|
||||
func (i Integer) Add(other Object) (Object, error) {
|
||||
if !AssertTypes(other, IntegerType) {
|
||||
switch obj := other.(type) {
|
||||
case Integer:
|
||||
return Integer{i.Value + obj.Value}, nil
|
||||
default:
|
||||
return nil, NewBinaryOpError(i, other, "+")
|
||||
}
|
||||
return Integer{i.Value + other.(Integer).Value}, nil
|
||||
}
|
||||
|
||||
func (i Integer) Sub(other Object) (Object, error) {
|
||||
if !AssertTypes(other, IntegerType) {
|
||||
switch obj := other.(type) {
|
||||
case Integer:
|
||||
return Integer{i.Value - obj.Value}, nil
|
||||
default:
|
||||
return nil, NewBinaryOpError(i, other, "-")
|
||||
}
|
||||
return Integer{i.Value - other.(Integer).Value}, nil
|
||||
}
|
||||
|
||||
func (i Integer) Mul(other Object) (Object, error) {
|
||||
@@ -115,15 +119,16 @@ func (i Integer) Negate() Object {
|
||||
}
|
||||
|
||||
func (i Integer) Compare(other Object) int {
|
||||
if obj, ok := other.(Integer); ok {
|
||||
switch {
|
||||
case i.Value < obj.Value:
|
||||
switch obj := other.(type) {
|
||||
case Integer:
|
||||
if i.Value < obj.Value {
|
||||
return -1
|
||||
case i.Value > obj.Value:
|
||||
return 1
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
if i.Value > obj.Value {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
default:
|
||||
return -1
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package object
|
||||
|
||||
var NULL = Null{}
|
||||
|
||||
type Null struct{}
|
||||
|
||||
func (n Null) Bool() bool {
|
||||
|
||||
Reference in New Issue
Block a user