mirror of
https://github.com/chaisql/chai.git
synced 2025-10-08 17:10:08 +08:00

This replaces the Value struct by an interface to allow us to override some values behavior in the future. It also introduces a new package types, which contains type definitions, comparison, and arithmetics. Concerning encoding, Genji now only uses on type of encoding for values. This simplifies indexing logic as well as table access in general.
33 lines
683 B
Go
33 lines
683 B
Go
package functions
|
|
|
|
import (
|
|
"math"
|
|
|
|
"github.com/genjidb/genji/internal/stringutil"
|
|
"github.com/genjidb/genji/types"
|
|
)
|
|
|
|
// MathFunctions returns all math package functions.
|
|
func MathFunctions() Definitions {
|
|
return mathFunctions
|
|
}
|
|
|
|
var mathFunctions = Definitions{
|
|
"floor": floorFunc,
|
|
}
|
|
|
|
var floorFunc = &ScalarDefinition{
|
|
name: "floor",
|
|
arity: 1,
|
|
callFn: func(args ...types.Value) (types.Value, error) {
|
|
switch args[0].Type() {
|
|
case types.DoubleValue:
|
|
return types.NewDoubleValue(math.Floor(args[0].V().(float64))), nil
|
|
case types.IntegerValue:
|
|
return args[0], nil
|
|
default:
|
|
return nil, stringutil.Errorf("floor(arg1) expects arg1 to be a number")
|
|
}
|
|
},
|
|
}
|