3 Commits

Author SHA1 Message Date
T.v.Dein
e963a770a7 bump version (#6) 2023-11-06 20:14:21 +01:00
T.v.Dein
ad2d9d98d6 Doc/improve (#4)
* add interactiveness to features, add keybindings to doc
* added commands
2023-11-06 20:12:31 +01:00
T.v.Dein
bb49cb7626 Feature/add show stack (#5)
* add -s flag and show command to display the last 5 entries
2023-11-06 20:12:07 +01:00
5 changed files with 133 additions and 4 deletions

View File

@@ -15,10 +15,13 @@ Features:
- various stack manipulation commands - various stack manipulation commands
- basic math operators - basic math operators
- advanced math functions (not yet complete) - advanced math functions (not yet complete)
- provides interactive repl
- can be used on the commandline - can be used on the commandline
- can calculate data in batch mode (also from STDIN) - can calculate data in batch mode (also from STDIN)
- extensible with custom LUA functions - extensible with custom LUA functions
- provides interactive repl
- completion
- history
## Working principle ## Working principle

23
calc.go
View File

@@ -32,6 +32,7 @@ type Calc struct {
debug bool debug bool
batch bool batch bool
stdin bool stdin bool
showstack bool
stack *Stack stack *Stack
history []string history []string
completer readline.AutoCompleter completer readline.AutoCompleter
@@ -48,6 +49,7 @@ type Calc struct {
const Help string = `Available commands: const Help string = `Available commands:
batch toggle batch mode batch toggle batch mode
debug toggle debug output debug toggle debug output
show show the last 5 items of the stack
dump display the stack contents dump display the stack contents
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
@@ -81,7 +83,7 @@ median median of all values`
// commands, constants and operators, defined here to feed completion // commands, constants and operators, defined here to feed completion
// and our mode switch in Eval() dynamically // and our mode switch in Eval() dynamically
const ( const (
Commands string = `dump reverse debug undebug clear batch shift undo help history manual exit quit swap` Commands string = `dump reverse debug undebug clear batch shift undo help history manual exit quit swap show`
Constants string = `Pi Phi Sqrt2 SqrtE SqrtPi SqrtPhi Ln2 Log2E Ln10 Log10E` Constants string = `Pi Phi Sqrt2 SqrtE SqrtPi SqrtPhi Ln2 Log2E Ln10 Log10E`
) )
@@ -162,14 +164,21 @@ func (c *Calc) ToggleStdin() {
c.stdin = !c.stdin c.stdin = !c.stdin
} }
func (c *Calc) ToggleShow() {
c.showstack = !c.showstack
}
func (c *Calc) Prompt() string { func (c *Calc) Prompt() string {
p := "\033[31m»\033[0m " p := "\033[31m»\033[0m "
b := "" b := ""
if c.batch { if c.batch {
b = "->batch" b = "->batch"
} }
d := "" d := ""
v := "" v := ""
if c.debug { if c.debug {
d = "->debug" d = "->debug"
v = fmt.Sprintf("/rev%d", c.stack.rev) v = fmt.Sprintf("/rev%d", c.stack.rev)
@@ -271,6 +280,8 @@ func (c *Calc) Eval(line string) {
for _, entry := range c.history { for _, entry := range c.history {
fmt.Println(entry) fmt.Println(entry)
} }
case "show":
c.ToggleShow()
case "exit": case "exit":
fallthrough fallthrough
case "quit": case "quit":
@@ -282,6 +293,16 @@ func (c *Calc) Eval(line string) {
} }
} }
} }
if c.showstack && !c.stdin {
dots := ""
if c.stack.Len() > 5 {
dots = "... "
}
last := c.stack.Last(5)
fmt.Printf("stack: %s%s\n", dots, list2str(last))
}
} }
// Execute a math function, check if it is defined just in case // Execute a math function, check if it is defined just in case

View File

@@ -30,7 +30,7 @@ import (
lua "github.com/yuin/gopher-lua" lua "github.com/yuin/gopher-lua"
) )
const VERSION string = "2.0.2" const VERSION string = "2.0.3"
const Usage string = `This is rpn, a reverse polish notation calculator cli. const Usage string = `This is rpn, a reverse polish notation calculator cli.
@@ -39,6 +39,7 @@ Usage: rpn [-bdvh] [<operator>]
Options: Options:
-b, --batchmode enable batch mode -b, --batchmode enable batch mode
-d, --debug enable debug mode -d, --debug enable debug mode
-s, --stack show last 5 items of the stack (off by default)
-m, --manual show manual -m, --manual show manual
-v, --version show version -v, --version show version
-h, --help show help -h, --help show help
@@ -58,6 +59,7 @@ func main() {
configfile := "" configfile := ""
flag.BoolVarP(&calc.batch, "batchmode", "b", false, "batch mode") flag.BoolVarP(&calc.batch, "batchmode", "b", false, "batch mode")
flag.BoolVarP(&calc.showstack, "showstack", "s", false, "show stack")
flag.BoolVarP(&enabledebug, "debug", "d", false, "debug mode") flag.BoolVarP(&enabledebug, "debug", "d", false, "debug mode")
flag.BoolVarP(&showversion, "version", "v", false, "show version") flag.BoolVarP(&showversion, "version", "v", false, "show version")
flag.BoolVarP(&showhelp, "help", "h", false, "show usage") flag.BoolVarP(&showhelp, "help", "h", false, "show usage")

48
rpn.go
View File

@@ -111,7 +111,9 @@ DESCRIPTION
switch or debug toggle command), then the backup stack is also being switch or debug toggle command), then the backup stack is also being
displayed. displayed.
The stack can be reversed using the reverse command. The stack can be reversed using the reverse command. However, sometimes
only the last two values are in the wrong order. Use the swap command to
exchange them.
You can use the shift command to remove the last number from the stack. You can use the shift command to remove the last number from the stack.
@@ -145,8 +147,52 @@ DESCRIPTION
log10 log1p log2 logb pow round roundtoeven sin sinh tan tanh trunc y0 log10 log1p log2 logb pow round roundtoeven sin sinh tan tanh trunc y0
y1 copysign dim hypot y1 copysign dim hypot
Commands:
batch toggle batch mode
debug toggle debug output
dump display the stack contents
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
show show the last 5 items of the stack
history display calculation history
help|? show this message
quit|exit|c-d|c-c exit program
Refer to https://pkg.go.dev/math for details about those functions. Refer to https://pkg.go.dev/math for details about those functions.
INTERACTIVE REPL
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"
and you'll be there.
In interactive mode you can use TAB completion to complete commands,
operators and functions. There's also a history, which allows you to
repeat complicated calculations (as long as you've entered them in one
line).
There are also a lot of key bindings, here are the most important ones:
ctrl-c + ctrl-d
Exit interactive rpn
ctrl-z
Send rpn to the backgound.
ctrl-a
Beginning of line.
ctrl-e
End of line.
ctrl-l
Clear the screen.
ctrl-r
Search through history.
EXTENDING RPN USING LUA EXTENDING RPN USING LUA
You can use a lua script with lua functions to extend the calculator. By You can use a lua script with lua functions to extend the calculator. By
default the tool looks for "~/.rpn.lua". You can also specify a script default the tool looks for "~/.rpn.lua". You can also specify a script

57
rpn.pod
View File

@@ -154,8 +154,65 @@ Math functions:
log10 log1p log2 logb pow round roundtoeven sin sinh tan tanh trunc y0 log10 log1p log2 logb pow round roundtoeven sin sinh tan tanh trunc y0
y1 copysign dim hypot y1 copysign dim hypot
Commands:
batch toggle batch mode
debug toggle debug output
dump display the stack contents
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
show show the last 5 items of the stack
history display calculation history
help|? show this message
quit|exit|c-d|c-c exit program
Refer to https://pkg.go.dev/math for details about those functions. Refer to https://pkg.go.dev/math for details about those functions.
=head1 INTERACTIVE REPL
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
C<rpn> and you'll be there.
In interactive mode you can use TAB completion to complete commands,
operators and functions. There's also a history, which allows you to
repeat complicated calculations (as long as you've entered them in one
line).
There are also a lot of key bindings, here are the most important
ones:
=over
=item ctrl-c + ctrl-d
Exit interactive rpn
=item ctrl-z
Send rpn to the backgound.
=item ctrl-a
Beginning of line.
=item ctrl-e
End of line.
=item ctrl-l
Clear the screen.
=item ctrl-r
Search through history.
=back
=head1 EXTENDING RPN USING LUA =head1 EXTENDING RPN USING LUA
You can use a lua script with lua functions to extend the You can use a lua script with lua functions to extend the