Fix/lua no funcs known (#15)

* lua fixes:

- fix lua function calling, didn't work in the last
releases (regression)
- add lua funcs which don't modify the stack (for converters etc)
- added better lua examples
This commit is contained in:
T.v.Dein
2023-11-08 19:03:37 +01:00
committed by GitHub
parent 31a0ddd547
commit a964a99f3d
8 changed files with 91 additions and 38 deletions

24
calc.go
View File

@@ -151,16 +151,16 @@ func NewCalc() *Calc {
// pre-calculate mode switching arrays
c.Constants = strings.Split(Constants, " ")
for name := range LuaFuncs {
c.LuaFunctions = append(c.LuaFunctions, name)
}
return &c
}
// setup the interpreter, called from main()
// setup the interpreter, called from main(), import lua functions
func (c *Calc) SetInt(I *Interpreter) {
c.interpreter = I
for name := range LuaFuncs {
c.LuaFunctions = append(c.LuaFunctions, name)
}
}
func (c *Calc) ToggleDebug() {
@@ -448,6 +448,8 @@ func (c *Calc) luafunc(funcname string) {
var err error
switch c.interpreter.FuncNumArgs(funcname) {
case 0:
fallthrough
case 1:
x, err = c.interpreter.CallLuaFunc(funcname, c.stack.Last())
case 2:
@@ -465,7 +467,15 @@ func (c *Calc) luafunc(funcname string) {
c.stack.Backup()
dopush := true
switch c.interpreter.FuncNumArgs(funcname) {
case 0:
a := c.stack.Last()
if len(a) == 1 {
c.History("%s(%f) = %f", funcname, a, x)
}
dopush = false
case 1:
a := c.stack.Pop()
c.History("%s(%f) = %f", funcname, a, x)
@@ -478,7 +488,9 @@ func (c *Calc) luafunc(funcname string) {
c.History("%s(*) = %f", funcname, x)
}
c.stack.Push(x)
if dopush {
c.stack.Push(x)
}
c.Result()
}