mirror of
https://codeberg.org/scip/rpnc.git
synced 2025-12-17 04:21:01 +01:00
added swap stack command, bump version (#2)
This commit is contained in:
25
calc.go
25
calc.go
@@ -51,6 +51,8 @@ debug toggle debug output
|
|||||||
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
|
||||||
|
reverse reverse the stack elements
|
||||||
|
swap exchange the last two elements
|
||||||
history display calculation history
|
history display calculation history
|
||||||
help|? show this message
|
help|? show this message
|
||||||
quit|exit|c-d|c-c exit program
|
quit|exit|c-d|c-c exit program
|
||||||
@@ -79,7 +81,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`
|
Commands string = `dump reverse debug undebug clear batch shift undo help history manual exit quit swap`
|
||||||
Constants string = `Pi Phi Sqrt2 SqrtE SqrtPi SqrtPhi Ln2 Log2E Ln10 Log10E`
|
Constants string = `Pi Phi Sqrt2 SqrtE SqrtPi SqrtPhi Ln2 Log2E Ln10 Log10E`
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -191,20 +193,6 @@ func (c *Calc) Eval(line string) {
|
|||||||
c.stack.Backup()
|
c.stack.Backup()
|
||||||
c.stack.Push(num)
|
c.stack.Push(num)
|
||||||
} else {
|
} else {
|
||||||
/*
|
|
||||||
if contains(c.MathFunctions, item) {
|
|
||||||
// go builtin math function, if implemented
|
|
||||||
c.mathfunc(item)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if contains(c.BatchFunctions, item) {
|
|
||||||
// math functions only supported in batch mode like max or mean
|
|
||||||
c.batchfunc(item)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
if contains(c.Constants, item) {
|
if contains(c.Constants, item) {
|
||||||
// put the constant onto the stack
|
// put the constant onto the stack
|
||||||
c.stack.Backup()
|
c.stack.Backup()
|
||||||
@@ -270,6 +258,13 @@ func (c *Calc) Eval(line string) {
|
|||||||
case "reverse":
|
case "reverse":
|
||||||
c.stack.Backup()
|
c.stack.Backup()
|
||||||
c.stack.Reverse()
|
c.stack.Reverse()
|
||||||
|
case "swap":
|
||||||
|
if c.stack.Len() < 2 {
|
||||||
|
fmt.Println("stack too small, can't swap")
|
||||||
|
} else {
|
||||||
|
c.stack.Backup()
|
||||||
|
c.stack.Swap()
|
||||||
|
}
|
||||||
case "undo":
|
case "undo":
|
||||||
c.stack.Restore()
|
c.stack.Restore()
|
||||||
case "history":
|
case "history":
|
||||||
|
|||||||
2
main.go
2
main.go
@@ -30,7 +30,7 @@ import (
|
|||||||
lua "github.com/yuin/gopher-lua"
|
lua "github.com/yuin/gopher-lua"
|
||||||
)
|
)
|
||||||
|
|
||||||
const VERSION string = "2.0.1"
|
const VERSION string = "2.0.2"
|
||||||
|
|
||||||
const Usage string = `This is rpn, a reverse polish notation calculator cli.
|
const Usage string = `This is rpn, a reverse polish notation calculator cli.
|
||||||
|
|
||||||
|
|||||||
4
rpn.pod
4
rpn.pod
@@ -116,7 +116,9 @@ You can use B<dump> to display the stack. If debugging
|
|||||||
is enabled (C<-d> switch or B<debug> toggle command), then the backup
|
is enabled (C<-d> switch or B<debug> toggle command), then the backup
|
||||||
stack is also being displayed.
|
stack is also being displayed.
|
||||||
|
|
||||||
The stack can be reversed using the B<reverse> command.
|
The stack can be reversed using the B<reverse> command. However,
|
||||||
|
sometimes only the last two values are in the wrong order. Use the
|
||||||
|
B<swap> command to exchange them.
|
||||||
|
|
||||||
You can use the B<shift> command to remove the last number from the
|
You can use the B<shift> command to remove the last number from the
|
||||||
stack.
|
stack.
|
||||||
|
|||||||
20
stack.go
20
stack.go
@@ -115,6 +115,26 @@ func (s *Stack) Shift(num ...int) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Stack) Swap() {
|
||||||
|
s.mutex.Lock()
|
||||||
|
defer s.mutex.Unlock()
|
||||||
|
|
||||||
|
if s.linklist.Len() < 2 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
a := s.linklist.Back()
|
||||||
|
s.linklist.Remove(a)
|
||||||
|
|
||||||
|
b := s.linklist.Back()
|
||||||
|
s.linklist.Remove(b)
|
||||||
|
|
||||||
|
s.Debug(fmt.Sprintf("swapping %.2f with %.2f", b.Value, a.Value))
|
||||||
|
|
||||||
|
s.linklist.PushBack(a.Value)
|
||||||
|
s.linklist.PushBack(b.Value)
|
||||||
|
}
|
||||||
|
|
||||||
// Return the last num items from the stack w/o modifying it.
|
// Return the last num items from the stack w/o modifying it.
|
||||||
func (s *Stack) Last(num ...int) []float64 {
|
func (s *Stack) Last(num ...int) []float64 {
|
||||||
items := []float64{}
|
items := []float64{}
|
||||||
|
|||||||
Reference in New Issue
Block a user