mirror of
https://codeberg.org/scip/rpnc.git
synced 2025-12-17 12:31:04 +01:00
use generics for contains() and add generic exists()
This commit is contained in:
32
calc.go
32
calc.go
@@ -261,7 +261,7 @@ func (c *Calc) Eval(line string) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, ok := c.Funcalls[item]; ok {
|
if exists(c.Funcalls, item) {
|
||||||
if err := c.DoFuncall(item); err != nil {
|
if err := c.DoFuncall(item); err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
} else {
|
} else {
|
||||||
@@ -270,20 +270,18 @@ func (c *Calc) Eval(line string) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.batch {
|
if exists(c.BatchFuncalls, item) {
|
||||||
if _, ok := c.BatchFuncalls[item]; ok {
|
if !c.batch {
|
||||||
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) {
|
||||||
@@ -304,22 +302,22 @@ func (c *Calc) Eval(line string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// internal commands
|
// internal commands
|
||||||
if _, ok := c.Commands[item]; ok {
|
if exists(c.Commands, item) {
|
||||||
c.Commands[item].Func(c)
|
c.Commands[item].Func(c)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, ok := c.ShowCommands[item]; ok {
|
if exists(c.ShowCommands, item) {
|
||||||
c.ShowCommands[item].Func(c)
|
c.ShowCommands[item].Func(c)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, ok := c.StackCommands[item]; ok {
|
if exists(c.StackCommands, item) {
|
||||||
c.StackCommands[item].Func(c)
|
c.StackCommands[item].Func(c)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, ok := c.SettingsCommands[item]; ok {
|
if exists(c.SettingsCommands, item) {
|
||||||
c.SettingsCommands[item].Func(c)
|
c.SettingsCommands[item].Func(c)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -507,7 +505,7 @@ func (c *Calc) PutVar(name string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Calc) GetVar(name string) {
|
func (c *Calc) GetVar(name string) {
|
||||||
if _, ok := c.Vars[name]; ok {
|
if exists(c.Vars, name) {
|
||||||
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])
|
||||||
|
|||||||
16
util.go
16
util.go
@@ -23,16 +23,24 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// find an item in a list
|
// find an item in a list, generic variant
|
||||||
func contains(s []string, e string) bool {
|
func contains[E comparable](s []E, v E) bool {
|
||||||
for _, a := range s {
|
for _, vs := range s {
|
||||||
if a == e {
|
if v == vs {
|
||||||
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":
|
||||||
|
|||||||
Reference in New Issue
Block a user