Skip to content

Commit

Permalink
Fix/lua no funcs known (#15)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
TLINDEN authored Nov 8, 2023
1 parent 31a0ddd commit a964a99
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 38 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,15 @@ the `register()` function to register your functions to the
calculator. This function takes these parameters:

- function name
- number of arguments expected (1,2 or -1 allowed), -1 means batch
mode
- number of arguments expected (see below)
- help text

Number of expected arguments can be:

- 0: expect 1 argument but do NOT modify the stack
- 1-n: do a singular calculation
- -1: batch mode work with all numbers on the stack

Please [refer to the lua language
reference](https://www.lua.org/manual/5.4/) for more details about
LUA.
Expand Down
24 changes: 18 additions & 6 deletions calc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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:
Expand All @@ -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)
Expand All @@ -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()
}
Expand Down
38 changes: 38 additions & 0 deletions example.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
-- simple function, return the lower number of the two operands
function lower(a,b)
if a < b then
return a
else
return b
end
end

-- calculate parallel resistance. Batch function (registered with -1,
-- see below). Takes a table as parameter.
--
-- Formula: 1/( (1/R1) + (1/R2) + ...)
function parallelresistance(list)
sumres = 0

for i, value in ipairs(list) do
sumres = sumres + 1 / value
end

return 1 / sumres
end

-- converter example
function inch2centimeter(inches)
return inches * 2.54
end

function init()
-- expects 2 args
register("lower", 2, "lower")

-- expects a list of all numbers on the stack, batch mode
register("parallelresistance", -1, "parallel resistance")

-- expects 1 arg, but doesn't pop()
register("inch2centimeter", 0)
end
15 changes: 10 additions & 5 deletions interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ import (
)

type Interpreter struct {
debug bool
debug bool
script string
}

// LUA interpreter, instanciated in main()
Expand All @@ -42,8 +43,12 @@ type LuaFunction struct {
// have access to the interpreter instance
var LuaFuncs map[string]LuaFunction

func NewInterpreter(script string, debug bool) *Interpreter {
return &Interpreter{debug: debug, script: script}
}

// initialize the lua environment properly
func InitLua(config string, debug bool) *Interpreter {
func (i *Interpreter) InitLua() {
// we only load a subset of lua Open modules and don't allow
// net, system or io stuff
for _, pair := range []struct {
Expand All @@ -66,7 +71,7 @@ func InitLua(config string, debug bool) *Interpreter {
}

// load the lua config (which we expect to contain init() and math functions)
if err := L.DoFile(config); err != nil {
if err := L.DoFile(i.script); err != nil {
panic(err)
}

Expand All @@ -84,8 +89,6 @@ func InitLua(config string, debug bool) *Interpreter {
}); err != nil {
panic(err)
}

return &Interpreter{debug: debug}
}

func (i *Interpreter) Debug(msg string) {
Expand Down Expand Up @@ -113,6 +116,8 @@ func (i *Interpreter) CallLuaFunc(funcname string, items []float64) (float64, er
funcname, LuaFuncs[funcname].numargs))

switch LuaFuncs[funcname].numargs {
case 0:
fallthrough
case 1:
// 1 arg variant
if err := L.CallByParam(lua.P{
Expand Down
7 changes: 4 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
lua "github.com/yuin/gopher-lua"
)

const VERSION string = "2.0.5"
const VERSION string = "2.0.6"

const Usage string = `This is rpn, a reverse polish notation calculator cli.
Expand Down Expand Up @@ -98,8 +98,9 @@ func main() {
// our config file is interpreted as lua code, only functions can
// be defined, init() will be called by InitLua().
if _, err := os.Stat(configfile); err == nil {
I := InitLua(configfile, enabledebug)
calc.SetInt(I)
luarunner := NewInterpreter(configfile, enabledebug)
luarunner.InitLua()
calc.SetInt(luarunner)
}

if len(flag.Args()) > 1 {
Expand Down
10 changes: 7 additions & 3 deletions rpn.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,6 @@ COMMENTS
In this case only 123 will be added to the stack.
VARIABLES
You can register the last item of the stack into a variable. Variable
names must be all caps. Use the ">NAME" command to put a value into
Expand Down Expand Up @@ -245,8 +244,13 @@ EXTENDING RPN USING LUA
* function name
* number of arguments expected (1,2 or -1 allowed), -1 means batch
mode.
* number of arguments expected (see below)
Number of expected arguments can be:
- 0: expect 1 argument but do NOT modify the stack
- 1-n: do a singular calculation
- -1: batch mode work with all numbers on the stack
* help text
Expand Down
9 changes: 7 additions & 2 deletions rpn.pod
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,13 @@ function name

=item *

number of arguments expected (1,2 or -1 allowed), -1 means batch
mode.
number of arguments expected (see below)

Number of expected arguments can be:

- 0: expect 1 argument but do NOT modify the stack
- 1-n: do a singular calculation
- -1: batch mode work with all numbers on the stack

=item *

Expand Down
17 changes: 0 additions & 17 deletions test.lua

This file was deleted.

0 comments on commit a964a99

Please sign in to comment.