mirror of
https://codeberg.org/scip/rpnc.git
synced 2025-12-17 12:31:04 +01:00
Compare commits
13 Commits
v2.0.6
...
feature/co
| Author | SHA1 | Date | |
|---|---|---|---|
| 1379d62c9e | |||
| 2fb1c7e7ad | |||
| 8c6095ea7a | |||
| 0f2ca114a4 | |||
| e78e4f84f4 | |||
|
|
0782b0920b | ||
| 40c4cf0e45 | |||
|
|
b13fbc63e3 | ||
| bacbfcc517 | |||
| b91e024569 | |||
| a6f8a0fdbe | |||
| 7b656c492a | |||
| 7d0443ce4b |
177
calc.go
177
calc.go
@@ -20,8 +20,8 @@ package main
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
@@ -48,24 +48,17 @@ type Calc struct {
|
||||
Funcalls Funcalls
|
||||
BatchFuncalls Funcalls
|
||||
|
||||
// different kinds of commands, displays nicer in help output
|
||||
StackCommands Commands
|
||||
SettingsCommands Commands
|
||||
ShowCommands Commands
|
||||
Commands Commands
|
||||
|
||||
Vars map[string]float64
|
||||
}
|
||||
|
||||
// help for lua functions will be added dynamically
|
||||
const Help string = `Available commands:
|
||||
batch toggle batch mode
|
||||
debug toggle debug output
|
||||
show show the last 5 items of the stack
|
||||
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 elements
|
||||
vars show list of variables
|
||||
history display calculation history
|
||||
help|? show this message
|
||||
quit|exit|c-d|c-c exit program
|
||||
|
||||
const Help string = `
|
||||
Operators:
|
||||
basic operators: + - x * / ^ (* is an alias of x)
|
||||
|
||||
@@ -94,7 +87,7 @@ Register variables:
|
||||
// commands, constants and operators, defined here to feed completion
|
||||
// and our mode switch in Eval() dynamically
|
||||
const (
|
||||
Commands string = `dump reverse debug undebug clear batch shift undo help history manual exit quit swap show vars`
|
||||
//Commands string = `dump reverse clear shift undo help history manual exit quit swap debug undebug nodebug batch nobatch showstack noshowstack vars`
|
||||
Constants string = `Pi Phi Sqrt2 SqrtE SqrtPi SqrtPhi Ln2 Log2E Ln10 Log10E`
|
||||
)
|
||||
|
||||
@@ -107,7 +100,6 @@ func GetCompleteCustomFunctions() func(string) []string {
|
||||
completions = append(completions, luafunc)
|
||||
}
|
||||
|
||||
completions = append(completions, strings.Split(Commands, " ")...)
|
||||
completions = append(completions, strings.Split(Constants, " ")...)
|
||||
|
||||
return completions
|
||||
@@ -126,6 +118,22 @@ func (c *Calc) GetCompleteCustomFuncalls() func(string) []string {
|
||||
completions = append(completions, function)
|
||||
}
|
||||
|
||||
for command := range c.SettingsCommands {
|
||||
completions = append(completions, command)
|
||||
}
|
||||
|
||||
for command := range c.ShowCommands {
|
||||
completions = append(completions, command)
|
||||
}
|
||||
|
||||
for command := range c.StackCommands {
|
||||
completions = append(completions, command)
|
||||
}
|
||||
|
||||
for command := range c.Commands {
|
||||
completions = append(completions, command)
|
||||
}
|
||||
|
||||
return completions
|
||||
}
|
||||
|
||||
@@ -151,6 +159,8 @@ func NewCalc() *Calc {
|
||||
// pre-calculate mode switching arrays
|
||||
c.Constants = strings.Split(Constants, " ")
|
||||
|
||||
c.SetCommands()
|
||||
|
||||
return &c
|
||||
}
|
||||
|
||||
@@ -259,7 +269,7 @@ func (c *Calc) Eval(line string) {
|
||||
|
||||
if contains(c.LuaFunctions, item) {
|
||||
// user provided custom lua functions
|
||||
c.luafunc(item)
|
||||
c.EvalLuaFunction(item)
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -274,65 +284,32 @@ func (c *Calc) Eval(line string) {
|
||||
continue
|
||||
}
|
||||
|
||||
// management commands
|
||||
// internal commands
|
||||
if _, ok := c.Commands[item]; ok {
|
||||
c.Commands[item].Func(c)
|
||||
continue
|
||||
}
|
||||
|
||||
if _, ok := c.ShowCommands[item]; ok {
|
||||
c.ShowCommands[item].Func(c)
|
||||
continue
|
||||
}
|
||||
|
||||
if _, ok := c.StackCommands[item]; ok {
|
||||
c.StackCommands[item].Func(c)
|
||||
continue
|
||||
}
|
||||
|
||||
if _, ok := c.SettingsCommands[item]; ok {
|
||||
c.SettingsCommands[item].Func(c)
|
||||
continue
|
||||
}
|
||||
|
||||
switch item {
|
||||
case "?":
|
||||
fallthrough
|
||||
case "help":
|
||||
fmt.Println(Help)
|
||||
if len(LuaFuncs) > 0 {
|
||||
fmt.Println("Lua functions:")
|
||||
for name, function := range LuaFuncs {
|
||||
fmt.Printf("%-20s %s\n", name, function.help)
|
||||
}
|
||||
}
|
||||
case "dump":
|
||||
c.stack.Dump()
|
||||
case "debug":
|
||||
c.ToggleDebug()
|
||||
case "undebug":
|
||||
c.debug = false
|
||||
case "batch":
|
||||
c.ToggleBatch()
|
||||
case "clear":
|
||||
c.stack.Backup()
|
||||
c.stack.Clear()
|
||||
case "shift":
|
||||
c.stack.Backup()
|
||||
c.stack.Shift()
|
||||
case "reverse":
|
||||
c.stack.Backup()
|
||||
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":
|
||||
c.stack.Restore()
|
||||
case "history":
|
||||
for _, entry := range c.history {
|
||||
fmt.Println(entry)
|
||||
}
|
||||
case "show":
|
||||
c.ToggleShow()
|
||||
case "exit":
|
||||
fallthrough
|
||||
case "quit":
|
||||
os.Exit(0)
|
||||
case "manual":
|
||||
man()
|
||||
case "vars":
|
||||
if len(c.Vars) > 0 {
|
||||
fmt.Printf("%-20s %s\n", "VARIABLE", "VALUE")
|
||||
for k, v := range c.Vars {
|
||||
fmt.Printf("%-20s -> %.2f\n", k, v)
|
||||
}
|
||||
} else {
|
||||
fmt.Println("no vars registered")
|
||||
}
|
||||
c.PrintHelp()
|
||||
|
||||
default:
|
||||
fmt.Println("unknown command or operator!")
|
||||
@@ -393,6 +370,10 @@ func (c *Calc) DoFuncall(funcname string) error {
|
||||
return R.Err
|
||||
}
|
||||
|
||||
// don't forget to backup!
|
||||
c.stack.Backup()
|
||||
|
||||
// "pop"
|
||||
if batch {
|
||||
// get rid of stack
|
||||
c.stack.Clear()
|
||||
@@ -442,7 +423,7 @@ func (c *Calc) Debug(msg string) {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Calc) luafunc(funcname string) {
|
||||
func (c *Calc) EvalLuaFunction(funcname string) {
|
||||
// called from calc loop
|
||||
var x float64
|
||||
var err error
|
||||
@@ -515,3 +496,51 @@ func (c *Calc) GetVar(name string) {
|
||||
fmt.Println("variable doesn't exist")
|
||||
}
|
||||
}
|
||||
|
||||
func sortcommands(hash Commands) []string {
|
||||
keys := make([]string, 0, len(hash))
|
||||
|
||||
for key := range hash {
|
||||
keys = append(keys, key)
|
||||
}
|
||||
|
||||
sort.Strings(keys)
|
||||
|
||||
return keys
|
||||
}
|
||||
|
||||
func (c *Calc) PrintHelp() {
|
||||
fmt.Println("Available configuration commands:")
|
||||
for _, name := range sortcommands(c.SettingsCommands) {
|
||||
fmt.Printf("%-20s %s\n", name, c.SettingsCommands[name].Help)
|
||||
}
|
||||
fmt.Println()
|
||||
|
||||
fmt.Println("Available show commands:")
|
||||
for _, name := range sortcommands(c.ShowCommands) {
|
||||
fmt.Printf("%-20s %s\n", name, c.ShowCommands[name].Help)
|
||||
}
|
||||
fmt.Println()
|
||||
|
||||
fmt.Println("Available stack manipulation commands:")
|
||||
for _, name := range sortcommands(c.StackCommands) {
|
||||
fmt.Printf("%-20s %s\n", name, c.StackCommands[name].Help)
|
||||
}
|
||||
fmt.Println()
|
||||
|
||||
fmt.Println("Other commands:")
|
||||
for _, name := range sortcommands(c.Commands) {
|
||||
fmt.Printf("%-20s %s\n", name, c.Commands[name].Help)
|
||||
}
|
||||
fmt.Println()
|
||||
|
||||
fmt.Println(Help)
|
||||
|
||||
// append lua functions, if any
|
||||
if len(LuaFuncs) > 0 {
|
||||
fmt.Println("Lua functions:")
|
||||
for name, function := range LuaFuncs {
|
||||
fmt.Printf("%-20s %s\n", name, function.help)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
99
calc_test.go
99
calc_test.go
@@ -20,6 +20,8 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
lua "github.com/yuin/gopher-lua"
|
||||
)
|
||||
|
||||
func TestCommentsAndWhitespace(t *testing.T) {
|
||||
@@ -104,6 +106,7 @@ func TestCalc(t *testing.T) {
|
||||
exp float64
|
||||
batch bool
|
||||
}{
|
||||
// ops
|
||||
{
|
||||
name: "plus",
|
||||
cmd: `15 15 +`,
|
||||
@@ -144,6 +147,8 @@ func TestCalc(t *testing.T) {
|
||||
cmd: `400 20 %+`,
|
||||
exp: 480,
|
||||
},
|
||||
|
||||
// math tests
|
||||
{
|
||||
name: "mod",
|
||||
cmd: `9 2 mod`,
|
||||
@@ -164,6 +169,20 @@ func TestCalc(t *testing.T) {
|
||||
cmd: `6 4 dim`,
|
||||
exp: 2,
|
||||
},
|
||||
|
||||
// constants tests
|
||||
{
|
||||
name: "pitimes2",
|
||||
cmd: `Pi 2 *`,
|
||||
exp: 6.283185307179586,
|
||||
},
|
||||
{
|
||||
name: "pi+sqrt2",
|
||||
cmd: `Pi Sqrt2 +`,
|
||||
exp: 4.555806215962888,
|
||||
},
|
||||
|
||||
// batch tests
|
||||
{
|
||||
name: "batch-sum",
|
||||
cmd: `2 2 2 2 sum`,
|
||||
@@ -194,6 +213,34 @@ func TestCalc(t *testing.T) {
|
||||
exp: 5,
|
||||
batch: true,
|
||||
},
|
||||
|
||||
// stack tests
|
||||
{
|
||||
name: "use-vars",
|
||||
cmd: `10 >TEN clear 5 <TEN *`,
|
||||
exp: 50,
|
||||
},
|
||||
{
|
||||
name: "reverse",
|
||||
cmd: `100 500 reverse -`,
|
||||
exp: 400,
|
||||
},
|
||||
{
|
||||
name: "swap",
|
||||
cmd: `2 16 swap /`,
|
||||
exp: 8,
|
||||
},
|
||||
{
|
||||
name: "clear batch",
|
||||
cmd: "1 1 1 1 1 clear 1 1 sum",
|
||||
exp: 2,
|
||||
batch: true,
|
||||
},
|
||||
{
|
||||
name: "undo",
|
||||
cmd: `4 4 + undo *`,
|
||||
exp: 16,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
@@ -212,3 +259,55 @@ func TestCalc(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCalcLua(t *testing.T) {
|
||||
var tests = []struct {
|
||||
function string
|
||||
stack []float64
|
||||
exp float64
|
||||
}{
|
||||
{
|
||||
function: "lower",
|
||||
stack: []float64{5, 6},
|
||||
exp: 5.0,
|
||||
},
|
||||
{
|
||||
function: "parallelresistance",
|
||||
stack: []float64{100, 200, 300},
|
||||
exp: 54.54545454545455,
|
||||
},
|
||||
}
|
||||
|
||||
calc := NewCalc()
|
||||
L = lua.NewState(lua.Options{SkipOpenLibs: true})
|
||||
defer L.Close()
|
||||
|
||||
luarunner := NewInterpreter("example.lua", false)
|
||||
luarunner.InitLua()
|
||||
calc.SetInt(luarunner)
|
||||
|
||||
for _, tt := range tests {
|
||||
testname := fmt.Sprintf("lua-%s", tt.function)
|
||||
|
||||
t.Run(testname, func(t *testing.T) {
|
||||
calc.stack.Clear()
|
||||
for _, item := range tt.stack {
|
||||
calc.stack.Push(item)
|
||||
}
|
||||
|
||||
calc.EvalLuaFunction(tt.function)
|
||||
|
||||
got := calc.stack.Last()
|
||||
|
||||
if calc.stack.Len() != 1 {
|
||||
t.Errorf("invalid stack size:\n+++ got: %d\n--- want: 1",
|
||||
calc.stack.Len())
|
||||
}
|
||||
|
||||
if got[0] != tt.exp {
|
||||
t.Errorf("lua function %s failed:\n+++ got: %f\n--- want: %f",
|
||||
tt.function, got, tt.exp)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
201
command.go
Normal file
201
command.go
Normal file
@@ -0,0 +1,201 @@
|
||||
/*
|
||||
Copyright © 2023 Thomas von Dein
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
type CommandFunction func(*Calc)
|
||||
|
||||
type Command struct {
|
||||
Help string
|
||||
Func CommandFunction
|
||||
}
|
||||
|
||||
type Commands map[string]*Command
|
||||
|
||||
func NewCommand(help string, function CommandFunction) *Command {
|
||||
return &Command{
|
||||
Help: help,
|
||||
Func: function,
|
||||
}
|
||||
}
|
||||
|
||||
// define all management (that is: non calculation) commands
|
||||
func (c *Calc) SetCommands() {
|
||||
c.SettingsCommands = Commands{
|
||||
// Toggles
|
||||
"debug": NewCommand(
|
||||
"toggle debugging",
|
||||
func(c *Calc) {
|
||||
c.ToggleDebug()
|
||||
},
|
||||
),
|
||||
|
||||
"nodebug": NewCommand(
|
||||
"disable debugging",
|
||||
func(c *Calc) {
|
||||
c.debug = false
|
||||
c.stack.debug = false
|
||||
},
|
||||
),
|
||||
|
||||
"batch": NewCommand(
|
||||
"toggle batch mode",
|
||||
func(c *Calc) {
|
||||
c.ToggleBatch()
|
||||
},
|
||||
),
|
||||
|
||||
"nobatch": NewCommand(
|
||||
"disable batch mode",
|
||||
func(c *Calc) {
|
||||
c.batch = false
|
||||
},
|
||||
),
|
||||
|
||||
"showstack": NewCommand(
|
||||
"toggle show last 5 items of the stack",
|
||||
func(c *Calc) {
|
||||
c.ToggleShow()
|
||||
},
|
||||
),
|
||||
|
||||
"noshowstack": NewCommand(
|
||||
"disable display of the stack",
|
||||
func(c *Calc) {
|
||||
c.showstack = false
|
||||
},
|
||||
),
|
||||
}
|
||||
|
||||
c.ShowCommands = Commands{
|
||||
// Display commands
|
||||
"dump": NewCommand(
|
||||
"display the stack contents",
|
||||
func(c *Calc) {
|
||||
c.stack.Dump()
|
||||
},
|
||||
),
|
||||
|
||||
"history": NewCommand(
|
||||
"display calculation history",
|
||||
func(c *Calc) {
|
||||
for _, entry := range c.history {
|
||||
fmt.Println(entry)
|
||||
}
|
||||
},
|
||||
),
|
||||
|
||||
"vars": NewCommand(
|
||||
"show list of variables",
|
||||
func(c *Calc) {
|
||||
if len(c.Vars) > 0 {
|
||||
fmt.Printf("%-20s %s\n", "VARIABLE", "VALUE")
|
||||
for k, v := range c.Vars {
|
||||
fmt.Printf("%-20s -> %.2f\n", k, v)
|
||||
}
|
||||
} else {
|
||||
fmt.Println("no vars registered")
|
||||
}
|
||||
},
|
||||
),
|
||||
}
|
||||
|
||||
c.StackCommands = Commands{
|
||||
"clear": NewCommand(
|
||||
"clear the whole stack",
|
||||
func(c *Calc) {
|
||||
c.stack.Backup()
|
||||
c.stack.Clear()
|
||||
},
|
||||
),
|
||||
|
||||
"shift": NewCommand(
|
||||
"remove the last element of the stack",
|
||||
func(c *Calc) {
|
||||
c.stack.Backup()
|
||||
c.stack.Shift()
|
||||
},
|
||||
),
|
||||
|
||||
"reverse": NewCommand(
|
||||
"reverse the stack elements",
|
||||
func(c *Calc) {
|
||||
c.stack.Backup()
|
||||
c.stack.Reverse()
|
||||
},
|
||||
),
|
||||
|
||||
"swap": NewCommand(
|
||||
"exchange the last two elements",
|
||||
func(c *Calc) {
|
||||
if c.stack.Len() < 2 {
|
||||
fmt.Println("stack too small, can't swap")
|
||||
} else {
|
||||
c.stack.Backup()
|
||||
c.stack.Swap()
|
||||
}
|
||||
},
|
||||
),
|
||||
|
||||
"undo": NewCommand(
|
||||
"undo last operation",
|
||||
func(c *Calc) {
|
||||
c.stack.Restore()
|
||||
},
|
||||
),
|
||||
|
||||
"dup": NewCommand(
|
||||
"duplicate last stack item",
|
||||
func(c *Calc) {
|
||||
item := c.stack.Last()
|
||||
if len(item) == 1 {
|
||||
c.stack.Backup()
|
||||
c.stack.Push(item[0])
|
||||
} else {
|
||||
fmt.Println("stack empty")
|
||||
}
|
||||
},
|
||||
),
|
||||
}
|
||||
|
||||
// general commands
|
||||
c.Commands = Commands{
|
||||
"exit": NewCommand(
|
||||
"exit program",
|
||||
func(c *Calc) {
|
||||
os.Exit(0)
|
||||
},
|
||||
),
|
||||
|
||||
"manual": NewCommand(
|
||||
"show manual",
|
||||
func(c *Calc) {
|
||||
man()
|
||||
},
|
||||
),
|
||||
}
|
||||
|
||||
// aliases
|
||||
c.Commands["quit"] = c.Commands["exit"]
|
||||
c.SettingsCommands["undebug"] = c.SettingsCommands["nodebug"]
|
||||
c.SettingsCommands["show"] = c.SettingsCommands["showstack"]
|
||||
}
|
||||
2
main.go
2
main.go
@@ -30,7 +30,7 @@ import (
|
||||
lua "github.com/yuin/gopher-lua"
|
||||
)
|
||||
|
||||
const VERSION string = "2.0.6"
|
||||
const VERSION string = "2.0.9"
|
||||
|
||||
const Usage string = `This is rpn, a reverse polish notation calculator cli.
|
||||
|
||||
|
||||
7
rpn.go
7
rpn.go
@@ -152,14 +152,15 @@ DESCRIPTION
|
||||
|
||||
Commands:
|
||||
|
||||
batch toggle batch mode
|
||||
debug toggle debug output
|
||||
[no]batch toggle batch mode (nobatch 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)
|
||||
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
|
||||
dup duplicate last stack item
|
||||
history display calculation history
|
||||
help|? show this message
|
||||
quit|exit|c-d|c-c exit program
|
||||
|
||||
7
rpn.pod
7
rpn.pod
@@ -159,14 +159,15 @@ Math functions:
|
||||
|
||||
Commands:
|
||||
|
||||
batch toggle batch mode
|
||||
debug toggle debug output
|
||||
[no]batch toggle batch mode (nobatch 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)
|
||||
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
|
||||
dup duplicate last stack item
|
||||
history display calculation history
|
||||
help|? show this message
|
||||
quit|exit|c-d|c-c exit program
|
||||
|
||||
31
stack.go
31
stack.go
@@ -195,14 +195,24 @@ func (s *Stack) Backup() {
|
||||
// make a backup, because the elements in list.List{} are pointers
|
||||
// and lead to unexpected results. The methid here works reliably
|
||||
// at least.
|
||||
s.mutex.Lock()
|
||||
defer s.mutex.Unlock()
|
||||
|
||||
s.Debug(fmt.Sprintf("backing up %d items from rev %d",
|
||||
s.linklist.Len(), s.rev))
|
||||
|
||||
s.backup = list.List{}
|
||||
for e := s.linklist.Front(); e != nil; e = e.Next() {
|
||||
s.backup.PushBack(e.Value)
|
||||
s.backup.PushBack(e.Value.(float64))
|
||||
}
|
||||
|
||||
s.backuprev = s.rev
|
||||
}
|
||||
|
||||
func (s *Stack) Restore() {
|
||||
s.mutex.Lock()
|
||||
defer s.mutex.Unlock()
|
||||
|
||||
if s.rev == 0 {
|
||||
fmt.Println("error: stack is empty.")
|
||||
return
|
||||
@@ -211,15 +221,26 @@ func (s *Stack) Restore() {
|
||||
s.Debug(fmt.Sprintf("restoring stack to revision %d", s.backuprev))
|
||||
|
||||
s.rev = s.backuprev
|
||||
s.linklist = s.backup
|
||||
|
||||
s.linklist = list.List{}
|
||||
for e := s.backup.Front(); e != nil; e = e.Next() {
|
||||
s.linklist.PushBack(e.Value.(float64))
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Stack) Reverse() {
|
||||
newstack := list.List{}
|
||||
s.mutex.Lock()
|
||||
defer s.mutex.Unlock()
|
||||
|
||||
items := []float64{}
|
||||
|
||||
for e := s.linklist.Front(); e != nil; e = e.Next() {
|
||||
newstack.PushFront(e.Value)
|
||||
tail := s.linklist.Back()
|
||||
items = append(items, tail.Value.(float64))
|
||||
s.linklist.Remove(tail)
|
||||
}
|
||||
|
||||
s.linklist = newstack
|
||||
for i := len(items) - 1; i >= 0; i-- {
|
||||
s.linklist.PushFront(items[i])
|
||||
}
|
||||
}
|
||||
|
||||
9
util.go
9
util.go
@@ -33,6 +33,15 @@ func contains(s []string, e string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func exists(m map[string]interface{}, item string) bool {
|
||||
// FIXME: try to use this for all cases
|
||||
if _, ok := m[item]; ok {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func const2num(name string) float64 {
|
||||
switch name {
|
||||
case "Pi":
|
||||
|
||||
Reference in New Issue
Block a user