add comments support (#7)

This commit is contained in:
T.v.Dein
2023-11-07 14:18:46 +01:00
committed by GitHub
parent e963a770a7
commit 2ce8cc7a7e
5 changed files with 90 additions and 3 deletions

View File

@@ -38,6 +38,7 @@ type Calc struct {
completer readline.AutoCompleter
interpreter *Interpreter
Space *regexp.Regexp
Comment *regexp.Regexp
Constants []string
LuaFunctions []string
@@ -133,6 +134,7 @@ func NewCalc() *Calc {
)
c.Space = regexp.MustCompile(`\s+`)
c.Comment = regexp.MustCompile(`#.*`) // ignore everything after #
// pre-calculate mode switching arrays
c.Constants = strings.Split(Constants, " ")
@@ -189,7 +191,8 @@ func (c *Calc) Prompt() string {
// the actual work horse, evaluate a line of calc command[s]
func (c *Calc) Eval(line string) {
line = strings.TrimSpace(line)
// remove surrounding whitespace and comments, if any
line = strings.TrimSpace(c.Comment.ReplaceAllString(line, ""))
if line == "" {
return