optimizations
Some checks failed
Build / build (push) Successful in 9m47s
Publish Image / publish (push) Failing after 49s
Test / build (push) Failing after 6m19s

This commit is contained in:
Chuck Smith
2024-04-02 14:32:03 -04:00
parent 07fd82b261
commit 88e3330856
18 changed files with 510 additions and 31 deletions

111
internal/object/float.go Normal file
View File

@@ -0,0 +1,111 @@
package object
import (
"fmt"
"math"
)
// Float is the floating-point number type used to represent float literals and holds
// an internal float64 value
type Float struct {
Value float64
}
func (f Float) Bool() bool {
return f.Value != 0
}
func (f Float) Add(other Object) (Object, error) {
switch obj := other.(type) {
case Float:
return Float{f.Value + obj.Value}, nil
default:
return nil, NewBinaryOpError(f, other, "+")
}
}
func (f Float) Sub(other Object) (Object, error) {
switch obj := other.(type) {
case Float:
return Float{f.Value - obj.Value}, nil
default:
return nil, NewBinaryOpError(f, other, "-")
}
}
func (f Float) Mul(other Object) (Object, error) {
switch obj := other.(type) {
case Float:
return Float{f.Value * obj.Value}, nil
case String:
return obj.Mul(f)
case *Array:
return obj.Mul(f)
default:
return nil, NewBinaryOpError(f, other, "*")
}
}
func (f Float) Div(other Object) (Object, error) {
switch obj := other.(type) {
case Float:
if obj.Value == 0 {
return nil, NewDivisionByZeroError(f)
}
return Float{f.Value / obj.Value}, nil
default:
return nil, NewBinaryOpError(f, other, "/")
}
}
func (f Float) Mod(other Object) (Object, error) {
switch obj := other.(type) {
case Float:
return Float{math.Mod(f.Value, obj.Value)}, nil
default:
return nil, NewBinaryOpError(f, other, "%")
}
}
func (f Float) LogicalNot() Object {
return Boolean{!f.Bool()}
}
func (f Float) Negate() Object {
return Float{-f.Value}
}
func (f Float) Compare(other Object) int {
switch obj := other.(type) {
case Float:
if f.Value < obj.Value {
return -1
}
if f.Value > obj.Value {
return 1
}
return 0
default:
return -1
}
}
func (f Float) String() string {
return f.Inspect()
}
// Copy implements the Copyable interface
func (i Float) Copy() Object {
return Float{Value: i.Value}
}
// Hash implements the Hasher interface
func (i Float) Hash() HashKey {
return HashKey{Type: i.Type(), Value: uint64(i.Value)}
}
// Type returns the type of the object
func (i 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) }

View File

@@ -11,6 +11,7 @@ type Type int
const (
NullType = iota
IntegerType
FloatType
StringType
BooleanType
ReturnType
@@ -30,6 +31,8 @@ func (t Type) String() string {
return "null"
case IntegerType:
return "int"
case FloatType:
return "float"
case StringType:
return "str"
case BooleanType: