re-organized command structure, added 'dup' command

This commit is contained in:
2023-11-12 11:28:12 +01:00
parent 0782b0920b
commit e78e4f84f4
2 changed files with 216 additions and 73 deletions

91
calc.go
View File

@@ -20,7 +20,6 @@ package main
import (
"errors"
"fmt"
"os"
"regexp"
"strconv"
"strings"
@@ -47,25 +46,13 @@ type Calc struct {
Funcalls Funcalls
BatchFuncalls Funcalls
Commands Commands
Vars map[string]float64
}
// help for lua functions will be added dynamically
const Help string = `Available commands:
batch toggle batch mode (nobatch turns it off)
debug toggle debug output (nodebug turns it off)
showstack toggle show 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 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 +81,7 @@ Register variables:
// commands, constants and operators, defined here to feed completion
// and our mode switch in Eval() dynamically
const (
Commands string = `dump reverse clear shift undo help history manual exit quit swap debug undebug nodebug batch nobatch showstack noshowstack 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 +94,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 +112,10 @@ func (c *Calc) GetCompleteCustomFuncalls() func(string) []string {
completions = append(completions, function)
}
for command := range c.Commands {
completions = append(completions, command)
}
return completions
}
@@ -151,6 +141,8 @@ func NewCalc() *Calc {
// pre-calculate mode switching arrays
c.Constants = strings.Split(Constants, " ")
c.SetCommands()
return &c
}
@@ -275,10 +267,19 @@ func (c *Calc) Eval(line string) {
}
// management commands
if _, ok := c.Commands[item]; ok {
c.Commands[item].Func(c)
continue
}
switch item {
case "?":
fallthrough
case "help":
fmt.Println("Available commands:")
for name, command := range c.Commands {
fmt.Printf("%-20s %s\n", name, command.Help)
}
fmt.Println(Help)
if len(LuaFuncs) > 0 {
fmt.Println("Lua functions:")
@@ -286,62 +287,6 @@ func (c *Calc) Eval(line string) {
fmt.Printf("%-20s %s\n", name, function.help)
}
}
case "dump":
c.stack.Dump()
case "debug":
c.ToggleDebug()
case "nodebug":
fallthrough
case "undebug":
c.debug = false
c.stack.debug = false
case "batch":
c.ToggleBatch()
case "nobatch":
c.batch = false
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 "showstack":
fallthrough
case "show":
c.ToggleShow()
case "noshowstack":
c.showstack = false
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")
}
default:
fmt.Println("unknown command or operator!")