From 6418e05255e5621bd4348baeb863b54bc4a941f8 Mon Sep 17 00:00:00 2001 From: "M.V. Hutz" Date: Mon, 12 Jan 2026 20:53:38 -0500 Subject: [PATCH 1/3] feat: add comment support to saccharine language Add '#' comment syntax that works like Python comments. Comments can take up a whole line or appear at the end of a line. All characters after '#' until the next newline or EOF are ignored. Closes #24 --- pkg/saccharine/token/parse.go | 17 +++++++++++++++++ tests/comments.expected | 1 + tests/comments.test | 15 +++++++++++++++ 3 files changed, 33 insertions(+) create mode 100644 tests/comments.expected create mode 100644 tests/comments.test diff --git a/pkg/saccharine/token/parse.go b/pkg/saccharine/token/parse.go index ef0ad15..d0e3065 100644 --- a/pkg/saccharine/token/parse.go +++ b/pkg/saccharine/token/parse.go @@ -77,6 +77,23 @@ func getToken(i *iterator.Iterator[rune]) (*Token, error) { } case letter == ';': return NewHardBreak(index), nil + case letter == '#': + // Skip everything until the next newline or EOF + for { + if i.Done() { + break + } + r, err := i.Next() + if err != nil { + return nil, trace.Wrap(err, "error while parsing comment") + } + if r == '\n' { + // Put the newline back so it can be processed as a soft break + i.Back() + break + } + } + return nil, nil case unicode.IsSpace(letter): return nil, nil case isVariable(letter): diff --git a/tests/comments.expected b/tests/comments.expected new file mode 100644 index 0000000..2dfe6a3 --- /dev/null +++ b/tests/comments.expected @@ -0,0 +1 @@ +VALUE diff --git a/tests/comments.test b/tests/comments.test new file mode 100644 index 0000000..11d25dd --- /dev/null +++ b/tests/comments.test @@ -0,0 +1,15 @@ +# This is a full-line comment at the start +# The following defines the identity function +identity := \x.x # This is an end-of-line comment + +# Define a simple function that applies a function twice +twice := \f.\x.(f (f x)) + +# Test that comments don't interfere with expressions +result := (twice identity VALUE) # Should just return VALUE + +# Multiple comments in a row +# can appear anywhere +# without breaking the code + +result # Final comment at the end -- 2.49.1 From b588754552afdf590fedd27efe4f8056bb402d98 Mon Sep 17 00:00:00 2001 From: "M.V. Hutz" Date: Mon, 12 Jan 2026 20:58:41 -0500 Subject: [PATCH 2/3] feat: broken --- pkg/saccharine/token/parse.go | 10 ++++------ tests/comments.test | 4 +++- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/pkg/saccharine/token/parse.go b/pkg/saccharine/token/parse.go index d0e3065..3848e5e 100644 --- a/pkg/saccharine/token/parse.go +++ b/pkg/saccharine/token/parse.go @@ -78,17 +78,15 @@ func getToken(i *iterator.Iterator[rune]) (*Token, error) { case letter == ';': return NewHardBreak(index), nil case letter == '#': - // Skip everything until the next newline or EOF - for { - if i.Done() { - break - } + // Skip everything until the next newline or EOF. + for i.Done() { r, err := i.Next() if err != nil { return nil, trace.Wrap(err, "error while parsing comment") } + if r == '\n' { - // Put the newline back so it can be processed as a soft break + // Put the newline back so it can be processed as a soft break. i.Back() break } diff --git a/tests/comments.test b/tests/comments.test index 11d25dd..c033ae0 100644 --- a/tests/comments.test +++ b/tests/comments.test @@ -3,7 +3,9 @@ identity := \x.x # This is an end-of-line comment # Define a simple function that applies a function twice -twice := \f.\x.(f (f x)) +twice := \f.\x.(f + # Comments can be anywhere! +(f x)) # Test that comments don't interfere with expressions result := (twice identity VALUE) # Should just return VALUE -- 2.49.1 From 53f4081f6f66c86de2ae8ce22680165359ab2c60 Mon Sep 17 00:00:00 2001 From: "M.V. Hutz" Date: Mon, 12 Jan 2026 20:59:34 -0500 Subject: [PATCH 3/3] fix: correct loop condition in comment parsing The loop was checking 'for i.Done()' instead of 'for !i.Done()', which prevented the comment content from being consumed. This caused the tokenizer to treat comment text as code. --- pkg/saccharine/token/parse.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/saccharine/token/parse.go b/pkg/saccharine/token/parse.go index 3848e5e..b7d5033 100644 --- a/pkg/saccharine/token/parse.go +++ b/pkg/saccharine/token/parse.go @@ -79,7 +79,7 @@ func getToken(i *iterator.Iterator[rune]) (*Token, error) { return NewHardBreak(index), nil case letter == '#': // Skip everything until the next newline or EOF. - for i.Done() { + for !i.Done() { r, err := i.Next() if err != nil { return nil, trace.Wrap(err, "error while parsing comment") -- 2.49.1