mirror of
https://codeberg.org/scip/rpnc.git
synced 2025-12-17 12:31:04 +01:00
25
calc.go
25
calc.go
@@ -33,6 +33,8 @@ type Calc struct {
|
|||||||
batch bool
|
batch bool
|
||||||
stdin bool
|
stdin bool
|
||||||
showstack bool
|
showstack bool
|
||||||
|
intermediate bool
|
||||||
|
notdone bool // set to true as long as there are items left in the eval loop
|
||||||
stack *Stack
|
stack *Stack
|
||||||
history []string
|
history []string
|
||||||
completer readline.AutoCompleter
|
completer readline.AutoCompleter
|
||||||
@@ -198,7 +200,15 @@ func (c *Calc) Eval(line string) {
|
|||||||
return
|
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)
|
num, err := strconv.ParseFloat(item, 64)
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
@@ -379,11 +389,16 @@ func (c *Calc) History(format string, args ...any) {
|
|||||||
|
|
||||||
// print the result
|
// print the result
|
||||||
func (c *Calc) Result() float64 {
|
func (c *Calc) Result() float64 {
|
||||||
if !c.stdin {
|
// we only print the result if it's either a final result or
|
||||||
fmt.Print("= ")
|
// (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]
|
return c.stack.Last()[0]
|
||||||
}
|
}
|
||||||
|
|||||||
19
main.go
19
main.go
@@ -30,19 +30,20 @@ import (
|
|||||||
lua "github.com/yuin/gopher-lua"
|
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.
|
const Usage string = `This is rpn, a reverse polish notation calculator cli.
|
||||||
|
|
||||||
Usage: rpn [-bdvh] [<operator>]
|
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)
|
-s, --stack show last 5 items of the stack (off by default)
|
||||||
-m, --manual show manual
|
-i --intermediate print intermediate results
|
||||||
-v, --version show version
|
-m, --manual show manual
|
||||||
-h, --help show help
|
-v, --version show version
|
||||||
|
-h, --help show help
|
||||||
|
|
||||||
When <operator> is given, batch mode ist automatically enabled. Use
|
When <operator> is given, batch mode ist automatically enabled. Use
|
||||||
this only when working with stdin. E.g.: echo "2 3 4 5" | rpn +
|
this only when working with stdin. E.g.: echo "2 3 4 5" | rpn +
|
||||||
@@ -59,7 +60,9 @@ 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(&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(&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")
|
||||||
|
|||||||
22
rpn.go
22
rpn.go
@@ -2,16 +2,19 @@ package main
|
|||||||
|
|
||||||
var manpage = `
|
var manpage = `
|
||||||
NAME
|
NAME
|
||||||
rpn - Reverse Polish Notation Calculator for the commandline
|
rpn - Programmable command-line calculator using reverse polish notation
|
||||||
|
|
||||||
SYNOPSIS
|
SYNOPSIS
|
||||||
Usage: rpn [-bdvh] [<operator>]
|
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
|
||||||
-v, --version show version
|
-s, --stack show last 5 items of the stack (off by default)
|
||||||
-h, --help show help
|
-i --intermediate print intermediate results
|
||||||
|
-m, --manual show manual
|
||||||
|
-v, --version show version
|
||||||
|
-h, --help show help
|
||||||
|
|
||||||
When <operator> is given, batch mode ist automatically enabled. Use
|
When <operator> is given, batch mode ist automatically enabled. Use
|
||||||
this only when working with stdin. E.g.: echo "2 3 4 5" | rpn +
|
this only when working with stdin. E.g.: echo "2 3 4 5" | rpn +
|
||||||
@@ -193,6 +196,15 @@ INTERACTIVE REPL
|
|||||||
ctrl-r
|
ctrl-r
|
||||||
Search through history.
|
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
|
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
|
||||||
|
|||||||
13
rpn.pod
13
rpn.pod
@@ -1,16 +1,19 @@
|
|||||||
=head1 NAME
|
=head1 NAME
|
||||||
|
|
||||||
rpn - Reverse Polish Notation Calculator for the commandline
|
rpn - Programmable command-line calculator using reverse polish notation
|
||||||
|
|
||||||
=head1 SYNOPSIS
|
=head1 SYNOPSIS
|
||||||
|
|
||||||
Usage: rpn [-bdvh] [<operator>]
|
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
|
||||||
-v, --version show version
|
-s, --stack show last 5 items of the stack (off by default)
|
||||||
-h, --help show help
|
-i --intermediate print intermediate results
|
||||||
|
-m, --manual show manual
|
||||||
|
-v, --version show version
|
||||||
|
-h, --help show help
|
||||||
|
|
||||||
When <operator> is given, batch mode ist automatically enabled. Use
|
When <operator> is given, batch mode ist automatically enabled. Use
|
||||||
this only when working with stdin. E.g.: echo "2 3 4 5" | rpn +
|
this only when working with stdin. E.g.: echo "2 3 4 5" | rpn +
|
||||||
|
|||||||
Reference in New Issue
Block a user