mirror of
https://codeberg.org/scip/rpnc.git
synced 2025-12-17 12:31:04 +01:00
save more typing with handy types
This commit is contained in:
57
funcs.go
57
funcs.go
@@ -24,8 +24,17 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type R struct {
|
||||||
|
Res float64
|
||||||
|
Err error
|
||||||
|
}
|
||||||
|
|
||||||
|
type Numbers []float64
|
||||||
|
|
||||||
|
type Function func(Numbers) R
|
||||||
|
|
||||||
// every function we are able to call must be of type Funcall, which
|
// every function we are able to call must be of type Funcall, which
|
||||||
// needs tp specify how many numbers it expects and the actual go
|
// needs to specify how many numbers it expects and the actual go
|
||||||
// function to be executed.
|
// function to be executed.
|
||||||
//
|
//
|
||||||
// The function has to take a float slice as argument and return a
|
// The function has to take a float slice as argument and return a
|
||||||
@@ -33,7 +42,6 @@ import (
|
|||||||
// the expected number of arguments.
|
// the expected number of arguments.
|
||||||
//
|
//
|
||||||
// However, Lua functions are handled differently, see interpreter.go.
|
// However, Lua functions are handled differently, see interpreter.go.
|
||||||
type Function func([]float64) (float64, error)
|
|
||||||
type Funcall struct {
|
type Funcall struct {
|
||||||
Expectargs int // -1 means batch only mode, you'll get the whole stack as arg
|
Expectargs int // -1 means batch only mode, you'll get the whole stack as arg
|
||||||
Func Function
|
Func Function
|
||||||
@@ -57,37 +65,42 @@ func NewFuncall(function Function, expectargs ...int) *Funcall {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Convenience function, create new result
|
||||||
|
func NewR(n float64, e error) R {
|
||||||
|
return R{Res: n, Err: e}
|
||||||
|
}
|
||||||
|
|
||||||
// the actual functions, called once during initialization.
|
// the actual functions, called once during initialization.
|
||||||
func DefineFunctions() Funcalls {
|
func DefineFunctions() Funcalls {
|
||||||
f := map[string]*Funcall{
|
f := map[string]*Funcall{
|
||||||
// simple operators, they all expect 2 args
|
// simple operators, they all expect 2 args
|
||||||
"+": NewFuncall(
|
"+": NewFuncall(
|
||||||
func(arg []float64) (float64, error) {
|
func(arg Numbers) R {
|
||||||
return arg[0] + arg[1], nil
|
return NewR(arg[0]+arg[1], nil)
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
"-": NewFuncall(
|
"-": NewFuncall(
|
||||||
func(arg []float64) (float64, error) {
|
func(arg Numbers) R {
|
||||||
return arg[0] - arg[1], nil
|
return NewR(arg[0]-arg[1], nil)
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
"x": NewFuncall(
|
"x": NewFuncall(
|
||||||
func(arg []float64) (float64, error) {
|
func(arg Numbers) R {
|
||||||
return arg[0] * arg[1], nil
|
return NewR(arg[0]*arg[1], nil)
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
"/": NewFuncall(
|
"/": NewFuncall(
|
||||||
func(arg []float64) (float64, error) {
|
func(arg Numbers) R {
|
||||||
if arg[1] == 0 {
|
if arg[1] == 0 {
|
||||||
return 0, errors.New("division by null")
|
return NewR(0, errors.New("division by null"))
|
||||||
}
|
}
|
||||||
|
|
||||||
return arg[0] / arg[1], nil
|
return NewR(arg[0]/arg[1], nil)
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
"^": NewFuncall(
|
"^": NewFuncall(
|
||||||
func(arg []float64) (float64, error) {
|
func(arg Numbers) R {
|
||||||
return math.Pow(arg[0], arg[1]), nil
|
return NewR(math.Pow(arg[0], arg[1]), nil)
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
@@ -98,12 +111,12 @@ func DefineFunctions() Funcalls {
|
|||||||
return f
|
return f
|
||||||
}
|
}
|
||||||
|
|
||||||
func list2str(list []float64) string {
|
func list2str(list Numbers) string {
|
||||||
return strings.Trim(strings.Join(strings.Fields(fmt.Sprint(list)), " "), "[]")
|
return strings.Trim(strings.Join(strings.Fields(fmt.Sprint(list)), " "), "[]")
|
||||||
}
|
}
|
||||||
|
|
||||||
// we need to add a history entry for each operation
|
// we need to add a history entry for each operation
|
||||||
func (c *Calc) SetHistory(op string, args []float64) {
|
func (c *Calc) SetHistory(op string, args Numbers) {
|
||||||
c.History("%s %s", list2str(args))
|
c.History("%s %s", list2str(args))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -111,9 +124,11 @@ func (c *Calc) SetHistory(op string, args []float64) {
|
|||||||
//
|
//
|
||||||
// FIXME: add a loop over DoFuncall() for non-batch-only functions
|
// FIXME: add a loop over DoFuncall() for non-batch-only functions
|
||||||
// like + or *
|
// like + or *
|
||||||
|
//
|
||||||
|
// FIXME: use R{} as well? or even everywhere, while we're at it?
|
||||||
func (c *Calc) DoFuncall(funcname string) (float64, error) {
|
func (c *Calc) DoFuncall(funcname string) (float64, error) {
|
||||||
if function, ok := c.Functions[funcname]; ok {
|
if function, ok := c.Functions[funcname]; ok {
|
||||||
args := []float64{}
|
args := Numbers{}
|
||||||
batch := false
|
batch := false
|
||||||
|
|
||||||
if function.Expectargs == -1 {
|
if function.Expectargs == -1 {
|
||||||
@@ -133,11 +148,11 @@ func (c *Calc) DoFuncall(funcname string) (float64, error) {
|
|||||||
// the actual lambda call, so to say. We provide a slice of
|
// the actual lambda call, so to say. We provide a slice of
|
||||||
// the requested size, fetched from the stack (but not popped
|
// the requested size, fetched from the stack (but not popped
|
||||||
// yet!)
|
// yet!)
|
||||||
res, err := function.Func(args)
|
R := function.Func(args)
|
||||||
|
|
||||||
if err != nil {
|
if R.Err != nil {
|
||||||
// leave the stack untouched in case of any error
|
// leave the stack untouched in case of any error
|
||||||
return res, err
|
return R.Res, R.Err
|
||||||
}
|
}
|
||||||
|
|
||||||
if batch {
|
if batch {
|
||||||
@@ -149,11 +164,11 @@ func (c *Calc) DoFuncall(funcname string) (float64, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// save result
|
// save result
|
||||||
c.stack.Push(res)
|
c.stack.Push(R.Res)
|
||||||
|
|
||||||
// thanks a lot
|
// thanks a lot
|
||||||
c.SetHistory(funcname, args)
|
c.SetHistory(funcname, args)
|
||||||
return res, nil
|
return R.Res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// should not happen, if it does: programmer fault!
|
// should not happen, if it does: programmer fault!
|
||||||
|
|||||||
Reference in New Issue
Block a user