optimizations
Some checks failed
Build / build (push) Successful in 10m47s
Publish Image / publish (push) Failing after 33s
Test / build (push) Failing after 6m42s

This commit is contained in:
Chuck Smith
2024-04-02 14:54:08 -04:00
parent 88e3330856
commit f08b458325
9 changed files with 313 additions and 355 deletions

View File

@@ -11,7 +11,7 @@ type BinaryOpError struct {
}
func (e BinaryOpError) Error() string {
return fmt.Sprintf("unsupported types for binary operation: %s %s %s %s %s", e.left.Type(), e.left.Inspect(), e.op, e.right.Type(), e.right.Inspect())
return fmt.Sprintf("unsupported operator: %s %s %s", e.left.Type(), e.op, e.right.Type())
}
// NewBinaryOpError returns a new BinaryOpError
@@ -25,7 +25,7 @@ type DivisionByZeroError struct {
}
func (e DivisionByZeroError) Error() string {
return fmt.Sprintf("cannot divide %s by zero", e.left)
return fmt.Sprintf("division by zero: %s", e.left)
}
// NewDivisionByZeroError returns a new DivisionByZeroError

View File

@@ -17,6 +17,8 @@ func (f Float) Bool() bool {
func (f Float) Add(other Object) (Object, error) {
switch obj := other.(type) {
case Integer:
return Float{f.Value + float64(obj.Value)}, nil
case Float:
return Float{f.Value + obj.Value}, nil
default:
@@ -26,6 +28,8 @@ func (f Float) Add(other Object) (Object, error) {
func (f Float) Sub(other Object) (Object, error) {
switch obj := other.(type) {
case Integer:
return Float{f.Value - float64(obj.Value)}, nil
case Float:
return Float{f.Value - obj.Value}, nil
default:
@@ -35,6 +39,8 @@ func (f Float) Sub(other Object) (Object, error) {
func (f Float) Mul(other Object) (Object, error) {
switch obj := other.(type) {
case Integer:
return Float{f.Value * float64(obj.Value)}, nil
case Float:
return Float{f.Value * obj.Value}, nil
case String:
@@ -48,6 +54,11 @@ func (f Float) Mul(other Object) (Object, error) {
func (f Float) Div(other Object) (Object, error) {
switch obj := other.(type) {
case Integer:
if obj.Value == 0.0 {
return nil, NewDivisionByZeroError(f)
}
return Float{f.Value / float64(obj.Value)}, nil
case Float:
if obj.Value == 0 {
return nil, NewDivisionByZeroError(f)
@@ -60,6 +71,8 @@ func (f Float) Div(other Object) (Object, error) {
func (f Float) Mod(other Object) (Object, error) {
switch obj := other.(type) {
case Integer:
return Float{math.Mod(f.Value, float64(obj.Value))}, nil
case Float:
return Float{math.Mod(f.Value, obj.Value)}, nil
default:
@@ -95,17 +108,17 @@ func (f Float) String() string {
}
// Copy implements the Copyable interface
func (i Float) Copy() Object {
return Float{Value: i.Value}
func (f Float) Copy() Object {
return Float{Value: f.Value}
}
// Hash implements the Hasher interface
func (i Float) Hash() HashKey {
return HashKey{Type: i.Type(), Value: uint64(i.Value)}
func (f Float) Hash() HashKey {
return HashKey{Type: f.Type(), Value: uint64(f.Value)}
}
// Type returns the type of the object
func (i Float) Type() Type { return FloatType }
func (f Float) Type() Type { return FloatType }
// Inspect returns a stringified version of the object for debugging
func (i Float) Inspect() string { return fmt.Sprintf("%f", i.Value) }
func (f Float) Inspect() string { return fmt.Sprintf("%f", f.Value) }

View File

@@ -35,6 +35,8 @@ func (i Integer) Add(other Object) (Object, error) {
switch obj := other.(type) {
case Integer:
return Integer{i.Value + obj.Value}, nil
case Float:
return Float{float64(i.Value) + obj.Value}, nil
default:
return nil, NewBinaryOpError(i, other, "+")
}
@@ -44,6 +46,8 @@ func (i Integer) Sub(other Object) (Object, error) {
switch obj := other.(type) {
case Integer:
return Integer{i.Value - obj.Value}, nil
case Float:
return Float{float64(i.Value) - obj.Value}, nil
default:
return nil, NewBinaryOpError(i, other, "-")
}
@@ -53,6 +57,8 @@ func (i Integer) Mul(other Object) (Object, error) {
switch obj := other.(type) {
case Integer:
return Integer{i.Value * obj.Value}, nil
case Float:
return Float{float64(i.Value) * obj.Value}, nil
case String:
return obj.Mul(i)
case *Array:
@@ -69,6 +75,11 @@ func (i Integer) Div(other Object) (Object, error) {
return nil, NewDivisionByZeroError(i)
}
return Integer{i.Value / obj.Value}, nil
case Float:
if obj.Value == 0.0 {
return nil, NewDivisionByZeroError(i)
}
return Float{float64(i.Value) / obj.Value}, nil
default:
return nil, NewBinaryOpError(i, other, "/")
}
@@ -78,6 +89,8 @@ func (i Integer) Mod(other Object) (Object, error) {
switch obj := other.(type) {
case Integer:
return Integer{i.Value % obj.Value}, nil
case Float:
return Float{float64(i.Value % int64(obj.Value))}, nil
default:
return nil, NewBinaryOpError(i, other, "%")
}

View File

@@ -95,3 +95,21 @@ func AssertTypes(obj Object, types ...Type) bool {
}
return false
}
func FromNativeBoolean(b bool) Boolean {
if b {
return TRUE
}
return FALSE
}
func IsTruthy(obj Object) bool {
switch obj := obj.(type) {
case Boolean:
return obj.Value
case Null:
return false
default:
return true
}
}