mirror of
https://codeberg.org/scip/rpnc.git
synced 2025-12-17 12:31:04 +01:00
Compare commits
6 Commits
feature/ed
...
internal/u
| Author | SHA1 | Date | |
|---|---|---|---|
| d9a0d61efc | |||
|
|
d2db420837 | ||
|
|
b4f53d2dd6 | ||
| ec4d86f727 | |||
| 4c6caa7114 | |||
|
|
252e7eb8d9 |
48
calc.go
48
calc.go
@@ -121,19 +121,27 @@ func (c *Calc) GetCompleteCustomFuncalls() func(string) []string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for command := range c.SettingsCommands {
|
for command := range c.SettingsCommands {
|
||||||
completions = append(completions, command)
|
if len(command) > 1 {
|
||||||
|
completions = append(completions, command)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for command := range c.ShowCommands {
|
for command := range c.ShowCommands {
|
||||||
completions = append(completions, command)
|
if len(command) > 1 {
|
||||||
|
completions = append(completions, command)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for command := range c.StackCommands {
|
for command := range c.StackCommands {
|
||||||
completions = append(completions, command)
|
if len(command) > 1 {
|
||||||
|
completions = append(completions, command)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for command := range c.Commands {
|
for command := range c.Commands {
|
||||||
completions = append(completions, command)
|
if len(command) > 1 {
|
||||||
|
completions = append(completions, command)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return completions
|
return completions
|
||||||
@@ -253,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 {
|
||||||
@@ -262,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) {
|
||||||
@@ -296,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
|
||||||
}
|
}
|
||||||
@@ -499,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])
|
||||||
|
|||||||
13
command.go
13
command.go
@@ -305,6 +305,15 @@ func (c *Calc) SetCommands() {
|
|||||||
|
|
||||||
// aliases
|
// aliases
|
||||||
c.Commands["quit"] = c.Commands["exit"]
|
c.Commands["quit"] = c.Commands["exit"]
|
||||||
c.SettingsCommands["undebug"] = c.SettingsCommands["nodebug"]
|
|
||||||
c.SettingsCommands["show"] = c.SettingsCommands["showstack"]
|
c.SettingsCommands["d"] = c.SettingsCommands["debug"]
|
||||||
|
c.SettingsCommands["b"] = c.SettingsCommands["batch"]
|
||||||
|
c.SettingsCommands["s"] = c.SettingsCommands["showstack"]
|
||||||
|
|
||||||
|
c.ShowCommands["h"] = c.ShowCommands["history"]
|
||||||
|
c.ShowCommands["p"] = c.ShowCommands["dump"]
|
||||||
|
c.ShowCommands["v"] = c.ShowCommands["vars"]
|
||||||
|
|
||||||
|
c.StackCommands["c"] = c.StackCommands["clear"]
|
||||||
|
c.StackCommands["u"] = c.StackCommands["undo"]
|
||||||
}
|
}
|
||||||
|
|||||||
41
rpn.go
41
rpn.go
@@ -178,18 +178,28 @@ DESCRIPTION
|
|||||||
[no]debug toggle debug output (nodebug turns it off)
|
[no]debug toggle debug output (nodebug turns it off)
|
||||||
[no]showstack show the last 5 items of the stack (noshowtack turns it off)
|
[no]showstack show the last 5 items of the stack (noshowtack turns it off)
|
||||||
|
|
||||||
Show commands: dump display the stack contents hex show last stack item
|
Show commands:
|
||||||
in hex form (converted to int) history display calculation history vars
|
|
||||||
show list of variables
|
|
||||||
|
|
||||||
Stack manipulation commands: clear clear the whole stack shift remove
|
dump display the stack contents
|
||||||
the last element of the stack reverse reverse the stack elements swap
|
hex show last stack item in hex form (converted to int)
|
||||||
exchange the last two stack elements dup duplicate last stack item undo
|
history display calculation history
|
||||||
undo last operation edit edit the stack interactively using vi or
|
vars show list of variables
|
||||||
$EDITOR
|
|
||||||
|
|
||||||
Other commands: help|? show this message manual show manual
|
Stack manipulation commands:
|
||||||
quit|exit|c-d|c-c exit program
|
|
||||||
|
clear clear the whole stack
|
||||||
|
shift remove the last element of the stack
|
||||||
|
reverse reverse the stack elements
|
||||||
|
swap exchange the last two stack elements
|
||||||
|
dup duplicate last stack item
|
||||||
|
undo undo last operation
|
||||||
|
edit edit the stack interactively using vi or $EDITOR
|
||||||
|
|
||||||
|
Other commands:
|
||||||
|
|
||||||
|
help|? show this message
|
||||||
|
manual show manual
|
||||||
|
quit|exit|c-d|c-c exit program
|
||||||
|
|
||||||
Register variables:
|
Register variables:
|
||||||
|
|
||||||
@@ -198,6 +208,17 @@ DESCRIPTION
|
|||||||
|
|
||||||
Refer to https://pkg.go.dev/math for details about those functions.
|
Refer to https://pkg.go.dev/math for details about those functions.
|
||||||
|
|
||||||
|
There are also a number of shortcuts for some commands available:
|
||||||
|
|
||||||
|
d debug
|
||||||
|
b batch
|
||||||
|
s showstack
|
||||||
|
h history
|
||||||
|
p dump (aka print)
|
||||||
|
v vars
|
||||||
|
c clear
|
||||||
|
u undo
|
||||||
|
|
||||||
INTERACTIVE REPL
|
INTERACTIVE REPL
|
||||||
While you can use rpn in the command-line, the best experience you'll
|
While you can use rpn in the command-line, the best experience you'll
|
||||||
have is the interactive repl (read eval print loop). Just execute "rpn"
|
have is the interactive repl (read eval print loop). Just execute "rpn"
|
||||||
|
|||||||
14
rpn.pod
14
rpn.pod
@@ -186,12 +186,14 @@ Configuration Commands:
|
|||||||
[no]showstack show the last 5 items of the stack (noshowtack turns it off)
|
[no]showstack show the last 5 items of the stack (noshowtack turns it off)
|
||||||
|
|
||||||
Show commands:
|
Show commands:
|
||||||
|
|
||||||
dump display the stack contents
|
dump display the stack contents
|
||||||
hex show last stack item in hex form (converted to int)
|
hex show last stack item in hex form (converted to int)
|
||||||
history display calculation history
|
history display calculation history
|
||||||
vars show list of variables
|
vars show list of variables
|
||||||
|
|
||||||
Stack manipulation commands:
|
Stack manipulation commands:
|
||||||
|
|
||||||
clear clear the whole stack
|
clear clear the whole stack
|
||||||
shift remove the last element of the stack
|
shift remove the last element of the stack
|
||||||
reverse reverse the stack elements
|
reverse reverse the stack elements
|
||||||
@@ -201,6 +203,7 @@ Stack manipulation commands:
|
|||||||
edit edit the stack interactively using vi or $EDITOR
|
edit edit the stack interactively using vi or $EDITOR
|
||||||
|
|
||||||
Other commands:
|
Other commands:
|
||||||
|
|
||||||
help|? show this message
|
help|? show this message
|
||||||
manual show manual
|
manual show manual
|
||||||
quit|exit|c-d|c-c exit program
|
quit|exit|c-d|c-c exit program
|
||||||
@@ -213,6 +216,17 @@ Register variables:
|
|||||||
|
|
||||||
Refer to https://pkg.go.dev/math for details about those functions.
|
Refer to https://pkg.go.dev/math for details about those functions.
|
||||||
|
|
||||||
|
There are also a number of shortcuts for some commands available:
|
||||||
|
|
||||||
|
d debug
|
||||||
|
b batch
|
||||||
|
s showstack
|
||||||
|
h history
|
||||||
|
p dump (aka print)
|
||||||
|
v vars
|
||||||
|
c clear
|
||||||
|
u undo
|
||||||
|
|
||||||
=head1 INTERACTIVE REPL
|
=head1 INTERACTIVE REPL
|
||||||
|
|
||||||
While you can use rpn in the command-line, the best experience you'll
|
While you can use rpn in the command-line, the best experience you'll
|
||||||
|
|||||||
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