3 Commits

Author SHA1 Message Date
a7fa0def04 weird windows panic 2023-12-07 14:06:11 +01:00
c9815e8ba3 not supported on windows! 2023-12-07 13:55:12 +01:00
d0376a63e3 added commandline and stdin tests using testscript 2023-12-07 13:42:41 +01:00
3 changed files with 23 additions and 31 deletions

34
calc.go
View File

@@ -261,7 +261,7 @@ func (c *Calc) Eval(line string) {
continue continue
} }
if exists(c.Funcalls, item) { if _, ok := c.Funcalls[item]; ok {
if err := c.DoFuncall(item); err != nil { if err := c.DoFuncall(item); err != nil {
fmt.Println(err) fmt.Println(err)
} else { } else {
@@ -270,18 +270,20 @@ func (c *Calc) Eval(line string) {
continue continue
} }
if exists(c.BatchFuncalls, item) { if c.batch {
if !c.batch { if _, ok := c.BatchFuncalls[item]; ok {
if err := c.DoFuncall(item); err != nil {
fmt.Println(err)
} else {
c.Result()
}
continue
}
} else {
if _, ok := c.BatchFuncalls[item]; ok {
fmt.Println("only supported in batch mode") fmt.Println("only supported in batch mode")
continue continue
} }
if err := c.DoFuncall(item); err != nil {
fmt.Println(err)
} else {
c.Result()
}
continue
} }
if contains(c.LuaFunctions, item) { if contains(c.LuaFunctions, item) {
@@ -302,22 +304,22 @@ func (c *Calc) Eval(line string) {
} }
// internal commands // internal commands
if exists(c.Commands, item) { if _, ok := c.Commands[item]; ok {
c.Commands[item].Func(c) c.Commands[item].Func(c)
continue continue
} }
if exists(c.ShowCommands, item) { if _, ok := c.ShowCommands[item]; ok {
c.ShowCommands[item].Func(c) c.ShowCommands[item].Func(c)
continue continue
} }
if exists(c.StackCommands, item) { if _, ok := c.StackCommands[item]; ok {
c.StackCommands[item].Func(c) c.StackCommands[item].Func(c)
continue continue
} }
if exists(c.SettingsCommands, item) { if _, ok := c.SettingsCommands[item]; ok {
c.SettingsCommands[item].Func(c) c.SettingsCommands[item].Func(c)
continue continue
} }
@@ -505,7 +507,7 @@ func (c *Calc) PutVar(name string) {
} }
func (c *Calc) GetVar(name string) { func (c *Calc) GetVar(name string) {
if exists(c.Vars, name) { if _, ok := c.Vars[name]; ok {
c.Debug(fmt.Sprintf("retrieve %.2f from %s", c.Vars[name], name)) c.Debug(fmt.Sprintf("retrieve %.2f from %s", c.Vars[name], name))
c.stack.Backup() c.stack.Backup()
c.stack.Push(c.Vars[name]) c.stack.Push(c.Vars[name])
@@ -518,10 +520,8 @@ func sortcommands(hash Commands) []string {
keys := make([]string, 0, len(hash)) keys := make([]string, 0, len(hash))
for key := range hash { for key := range hash {
if len(key) > 1 {
keys = append(keys, key) keys = append(keys, key)
} }
}
sort.Strings(keys) sort.Strings(keys)

View File

@@ -30,7 +30,7 @@ import (
lua "github.com/yuin/gopher-lua" lua "github.com/yuin/gopher-lua"
) )
const VERSION string = "2.0.12" const VERSION string = "2.0.11"
const Usage string = `This is rpn, a reverse polish notation calculator cli. const Usage string = `This is rpn, a reverse polish notation calculator cli.

16
util.go
View File

@@ -23,24 +23,16 @@ import (
"strings" "strings"
) )
// find an item in a list, generic variant // find an item in a list
func contains[E comparable](s []E, v E) bool { func contains(s []string, e string) bool {
for _, vs := range s { for _, a := range s {
if v == vs { if a == e {
return true return true
} }
} }
return false return false
} }
// look if a key in a map exists, generic variant
func exists[K comparable, V any](m map[K]V, v K) bool {
if _, ok := m[v]; ok {
return true
}
return false
}
func const2num(name string) float64 { func const2num(name string) float64 {
switch name { switch name {
case "Pi": case "Pi":