From 8b4496b3633be97f4595e434733d334b474e71b4 Mon Sep 17 00:00:00 2001 From: Sky Johnson Date: Wed, 7 May 2025 09:23:04 -0500 Subject: [PATCH] add multiline comments --- README.md | 4 ++++ scanner/scanner.go | 22 +++++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c5a19b8..fe23fac 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,9 @@ Lua-like, but with more minimalism and eventually some syntactic sugars. ``` // C style comments +/* + C style multi line comments +*/ // No local for variable definitions; the local behavior should be implicit x = "foo" y = "bar" @@ -33,6 +36,7 @@ fn add(a, b) return a + b end echo add(x, y) // Outputs: foobar +fnVar = fn(x, y, ...) /* do something */ end if expression and expression2 then res = add(1, 2) diff --git a/scanner/scanner.go b/scanner/scanner.go index 88ef921..02d2a6c 100644 --- a/scanner/scanner.go +++ b/scanner/scanner.go @@ -59,10 +59,30 @@ func (s *Scanner) NextToken() types.Token { return s.makeToken(types.STAR) case '/': if s.match('/') { - // Comment goes until end of line + // Single-line comment for s.peek() != '\n' && !s.isAtEnd() { s.advance() } + // Recursive call to get the next non-comment token + return s.NextToken() + } else if s.match('*') { + // Multiline comment + for !(s.peek() == '*' && s.peekNext() == '/') && !s.isAtEnd() { + if s.peek() == '\n' { + s.line++ + s.column = 0 + } + s.advance() + } + + if s.isAtEnd() { + return s.errorToken("Unclosed multiline comment.") + } + + // Consume the closing */ + s.advance() // * + s.advance() // / + // Recursive call to get the next non-comment token return s.NextToken() }