added lua interpreter support for custom math functions

This commit is contained in:
2023-10-31 19:02:40 +01:00
parent 4ace2b4385
commit e10faf2204
8 changed files with 185 additions and 34 deletions

View File

@@ -6,6 +6,7 @@ import (
"github.com/chzyer/readline"
flag "github.com/spf13/pflag"
lua "github.com/yuin/gopher-lua"
)
const VERSION string = "0.0.1"
@@ -23,8 +24,7 @@ Options:
When <operator> is given, batch mode ist automatically enabled. Use
this only when working with stdin. E.g.: echo "2 3 4 5" | rpn +
Copyright (c) 2023 T.v.Dein
`
Copyright (c) 2023 T.v.Dein`
func main() {
calc := NewCalc()
@@ -32,11 +32,14 @@ func main() {
showversion := false
showhelp := false
enabledebug := false
configfile := ""
flag.BoolVarP(&calc.batch, "batchmode", "b", false, "batch mode")
flag.BoolVarP(&enabledebug, "debug", "d", false, "debug mode")
flag.BoolVarP(&showversion, "version", "v", false, "show version")
flag.BoolVarP(&showhelp, "help", "h", false, "show usage")
flag.StringVarP(&configfile, "config", "c", os.Getenv("HOME")+"/.rpn.lua", "config file (lua format)")
flag.Parse()
if showversion {
@@ -53,6 +56,18 @@ func main() {
calc.ToggleDebug()
}
if _, err := os.Stat(configfile); err == nil {
L = lua.NewState()
defer L.Close()
if err := L.DoFile(configfile); err != nil {
panic(err)
}
InitLua(L)
calc.L = L
}
rl, err := readline.NewEx(&readline.Config{
Prompt: "\033[31m»\033[0m ",
HistoryFile: os.Getenv("HOME") + "/.rpn-history",