Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add comments support #7

Merged
merged 2 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ test:
go test -v ./...

singletest:
@echo "Call like this: ''make singletest TEST=TestPrepareColumns MOD=lib"
go test -run $(TEST) github.com/tlinden/rpn/$(MOD)
@echo "Call like this: ''make singletest TEST=TestPrepareColumns"
go test -run $(TEST)

cover-report:
go test ./... -cover -coverprofile=coverage.out
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Features:
- provides interactive repl
- completion
- history
- comments (comment character is `#`)


## Working principle
Expand Down
5 changes: 4 additions & 1 deletion calc.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type Calc struct {
completer readline.AutoCompleter
interpreter *Interpreter
Space *regexp.Regexp
Comment *regexp.Regexp
Constants []string
LuaFunctions []string

Expand Down Expand Up @@ -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, " ")
Expand Down Expand Up @@ -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
Expand Down
73 changes: 73 additions & 0 deletions calc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,79 @@ import (
"testing"
)

func TestCommentsAndWhitespace(t *testing.T) {
calc := NewCalc()

var tests = []struct {
name string
cmd []string
exp float64 // last element of the stack
}{
{
name: "whitespace prefix",
cmd: []string{" 5"},
exp: 5.0,
},
{
name: "whitespace postfix",
cmd: []string{"5 "},
exp: 5.0,
},
{
name: "whitespace both",
cmd: []string{" 5 "},
exp: 5.0,
},
{
name: "comment line w/ spaces",
cmd: []string{"5", " # 19"},
exp: 5.0,
},
{
name: "comment line w/o spaces",
cmd: []string{"5", `#19`},
exp: 5.0,
},
{
name: "inline comment w/ spaces",
cmd: []string{"5 # 19"},
exp: 5.0,
},
{
name: "inline comment w/o spaces",
cmd: []string{"5#19"},
exp: 5.0,
},
}

for _, tt := range tests {
testname := fmt.Sprintf("%s .(expect %.2f)",
tt.name, tt.exp)

t.Run(testname, func(t *testing.T) {
for _, line := range tt.cmd {
calc.Eval(line)
}
got := calc.stack.Last()

if len(got) > 0 {
if got[0] != tt.exp {
t.Errorf("parsing failed:\n+++ got: %f\n--- want: %f",
got, tt.exp)
}
}

if calc.stack.Len() != 1 {
t.Errorf("invalid stack size:\n+++ got: %d\n--- want: 1",
calc.stack.Len())
}

})

calc.stack.Clear()
}
}

func TestCalc(t *testing.T) {
calc := NewCalc()

Expand Down
10 changes: 10 additions & 0 deletions rpn.pod
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,16 @@ Search through history.

=back

=head1 COMMENTS

Lines starting with C<#> 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.

=head1 EXTENDING RPN USING LUA

You can use a lua script with lua functions to extend the
Expand Down