Files
rpnc/funcs.go

177 lines
4.2 KiB
Go
Raw Normal View History

2023-11-04 19:40:53 +01:00
/*
Copyright © 2023 Thomas von Dein
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"errors"
"fmt"
"math"
"strings"
)
2023-11-04 19:55:45 +01:00
type R struct {
Res float64
Err error
}
type Numbers []float64
type Function func(Numbers) R
2023-11-04 19:40:53 +01:00
// every function we are able to call must be of type Funcall, which
2023-11-04 19:55:45 +01:00
// needs to specify how many numbers it expects and the actual go
2023-11-04 19:40:53 +01:00
// function to be executed.
//
// The function has to take a float slice as argument and return a
// float and an error object. The float slice is guaranteed to have
// the expected number of arguments.
//
// However, Lua functions are handled differently, see interpreter.go.
type Funcall struct {
Expectargs int // -1 means batch only mode, you'll get the whole stack as arg
Func Function
}
// will hold all hard coded functions and operators
type Funcalls map[string]*Funcall
// convenience function, create a new Funcall object, if expectargs
// was not specified, 2 is assumed.
func NewFuncall(function Function, expectargs ...int) *Funcall {
expect := 2
if len(expectargs) > 0 {
expect = expectargs[0]
}
return &Funcall{
Expectargs: expect,
Func: function,
}
}
2023-11-04 19:55:45 +01:00
// Convenience function, create new result
func NewR(n float64, e error) R {
return R{Res: n, Err: e}
}
2023-11-04 19:40:53 +01:00
// the actual functions, called once during initialization.
func DefineFunctions() Funcalls {
f := map[string]*Funcall{
// simple operators, they all expect 2 args
"+": NewFuncall(
2023-11-04 19:55:45 +01:00
func(arg Numbers) R {
return NewR(arg[0]+arg[1], nil)
2023-11-04 19:40:53 +01:00
},
),
"-": NewFuncall(
2023-11-04 19:55:45 +01:00
func(arg Numbers) R {
return NewR(arg[0]-arg[1], nil)
2023-11-04 19:40:53 +01:00
},
),
"x": NewFuncall(
2023-11-04 19:55:45 +01:00
func(arg Numbers) R {
return NewR(arg[0]*arg[1], nil)
2023-11-04 19:40:53 +01:00
},
),
"/": NewFuncall(
2023-11-04 19:55:45 +01:00
func(arg Numbers) R {
2023-11-04 19:40:53 +01:00
if arg[1] == 0 {
2023-11-04 19:55:45 +01:00
return NewR(0, errors.New("division by null"))
2023-11-04 19:40:53 +01:00
}
2023-11-04 19:55:45 +01:00
return NewR(arg[0]/arg[1], nil)
2023-11-04 19:40:53 +01:00
},
),
"^": NewFuncall(
2023-11-04 19:55:45 +01:00
func(arg Numbers) R {
return NewR(math.Pow(arg[0], arg[1]), nil)
2023-11-04 19:40:53 +01:00
},
),
}
// aliases
f["*"] = f["x"]
return f
}
2023-11-04 19:55:45 +01:00
func list2str(list Numbers) string {
2023-11-04 19:40:53 +01:00
return strings.Trim(strings.Join(strings.Fields(fmt.Sprint(list)), " "), "[]")
}
// we need to add a history entry for each operation
2023-11-04 19:55:45 +01:00
func (c *Calc) SetHistory(op string, args Numbers) {
2023-11-04 19:40:53 +01:00
c.History("%s %s", list2str(args))
}
// Execute a math function, check if it is defined just in case
//
// FIXME: add a loop over DoFuncall() for non-batch-only functions
// like + or *
2023-11-04 19:55:45 +01:00
//
// FIXME: use R{} as well? or even everywhere, while we're at it?
2023-11-04 19:40:53 +01:00
func (c *Calc) DoFuncall(funcname string) (float64, error) {
if function, ok := c.Functions[funcname]; ok {
2023-11-04 19:55:45 +01:00
args := Numbers{}
2023-11-04 19:40:53 +01:00
batch := false
if function.Expectargs == -1 {
// batch mode, but always < stack len, so check first
args = c.stack.All()
batch = true
} else {
// this is way better behavior than just using 0 in place of
// non-existing stack items
if c.stack.Len() < function.Expectargs {
return -1, errors.New("stack doesn't provide enough arguments")
}
args = c.stack.Last(function.Expectargs)
}
// the actual lambda call, so to say. We provide a slice of
// the requested size, fetched from the stack (but not popped
// yet!)
2023-11-04 19:55:45 +01:00
R := function.Func(args)
2023-11-04 19:40:53 +01:00
2023-11-04 19:55:45 +01:00
if R.Err != nil {
2023-11-04 19:40:53 +01:00
// leave the stack untouched in case of any error
2023-11-04 19:55:45 +01:00
return R.Res, R.Err
2023-11-04 19:40:53 +01:00
}
if batch {
// get rid of stack
c.stack.Clear()
} else {
// remove operands
c.stack.Shift(function.Expectargs)
}
// save result
2023-11-04 19:55:45 +01:00
c.stack.Push(R.Res)
2023-11-04 19:40:53 +01:00
// thanks a lot
c.SetHistory(funcname, args)
2023-11-04 19:55:45 +01:00
return R.Res, nil
2023-11-04 19:40:53 +01:00
}
// should not happen, if it does: programmer fault!
return -1, errors.New("heck, no such function")
}