diff --git a/calc.go b/calc.go index fbec70a..77dc9ff 100644 --- a/calc.go +++ b/calc.go @@ -33,6 +33,8 @@ type Calc struct { batch bool stdin bool showstack bool + intermediate bool + notdone bool // set to true as long as there are items left in the eval loop stack *Stack history []string completer readline.AutoCompleter @@ -198,7 +200,15 @@ func (c *Calc) Eval(line string) { return } - for _, item := range c.Space.Split(line, -1) { + items := c.Space.Split(line, -1) + + for pos, item := range items { + if pos+1 < len(items) { + c.notdone = true + } else { + c.notdone = false + } + num, err := strconv.ParseFloat(item, 64) if err == nil { @@ -379,11 +389,16 @@ func (c *Calc) History(format string, args ...any) { // print the result func (c *Calc) Result() float64 { - if !c.stdin { - fmt.Print("= ") - } + // we only print the result if it's either a final result or + // (if it is intermediate) if -i has been given + if c.intermediate || !c.notdone { + // only needed in repl + if !c.stdin { + fmt.Print("= ") + } - fmt.Println(c.stack.Last()[0]) + fmt.Println(c.stack.Last()[0]) + } return c.stack.Last()[0] } diff --git a/main.go b/main.go index d8d62d0..9df9488 100644 --- a/main.go +++ b/main.go @@ -30,19 +30,20 @@ import ( lua "github.com/yuin/gopher-lua" ) -const VERSION string = "2.0.4" +const VERSION string = "2.0.5" const Usage string = `This is rpn, a reverse polish notation calculator cli. Usage: rpn [-bdvh] [] Options: - -b, --batchmode enable batch mode - -d, --debug enable debug mode - -s, --stack show last 5 items of the stack (off by default) - -m, --manual show manual - -v, --version show version - -h, --help show help + -b, --batchmode enable batch mode + -d, --debug enable debug mode + -s, --stack show last 5 items of the stack (off by default) + -i --intermediate print intermediate results + -m, --manual show manual + -v, --version show version + -h, --help show help When is given, batch mode ist automatically enabled. Use this only when working with stdin. E.g.: echo "2 3 4 5" | rpn + @@ -59,7 +60,9 @@ func main() { configfile := "" flag.BoolVarP(&calc.batch, "batchmode", "b", false, "batch mode") - flag.BoolVarP(&calc.showstack, "showstack", "s", false, "show stack") + flag.BoolVarP(&calc.showstack, "show-stack", "s", false, "show stack") + flag.BoolVarP(&calc.intermediate, "showin-termediate", "i", false, + "show intermediate results") flag.BoolVarP(&enabledebug, "debug", "d", false, "debug mode") flag.BoolVarP(&showversion, "version", "v", false, "show version") flag.BoolVarP(&showhelp, "help", "h", false, "show usage") diff --git a/rpn.go b/rpn.go index 15197ff..ef4b85f 100644 --- a/rpn.go +++ b/rpn.go @@ -2,16 +2,19 @@ package main var manpage = ` NAME - rpn - Reverse Polish Notation Calculator for the commandline + rpn - Programmable command-line calculator using reverse polish notation SYNOPSIS Usage: rpn [-bdvh] [] Options: - -b, --batchmode enable batch mode - -d, --debug enable debug mode - -v, --version show version - -h, --help show help + -b, --batchmode enable batch mode + -d, --debug enable debug mode + -s, --stack show last 5 items of the stack (off by default) + -i --intermediate print intermediate results + -m, --manual show manual + -v, --version show version + -h, --help show help When is given, batch mode ist automatically enabled. Use this only when working with stdin. E.g.: echo "2 3 4 5" | rpn + @@ -193,6 +196,15 @@ INTERACTIVE REPL ctrl-r Search through history. +COMMENTS + Lines starting with "#" are being ignored as comments. You can also + append comments to rpn input, e.g.: + + # a comment + 123 # another comment + + In this case only 123 will be added to the stack. + EXTENDING RPN USING LUA 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 diff --git a/rpn.pod b/rpn.pod index 117828d..43f8a0e 100644 --- a/rpn.pod +++ b/rpn.pod @@ -1,16 +1,19 @@ =head1 NAME -rpn - Reverse Polish Notation Calculator for the commandline +rpn - Programmable command-line calculator using reverse polish notation =head1 SYNOPSIS Usage: rpn [-bdvh] [] Options: - -b, --batchmode enable batch mode - -d, --debug enable debug mode - -v, --version show version - -h, --help show help + -b, --batchmode enable batch mode + -d, --debug enable debug mode + -s, --stack show last 5 items of the stack (off by default) + -i --intermediate print intermediate results + -m, --manual show manual + -v, --version show version + -h, --help show help When is given, batch mode ist automatically enabled. Use this only when working with stdin. E.g.: echo "2 3 4 5" | rpn +