add multiline comments

This commit is contained in:
Sky Johnson 2025-05-07 09:23:04 -05:00
parent b24c0d3abd
commit 8b4496b363
2 changed files with 25 additions and 1 deletions

View File

@ -26,6 +26,9 @@ Lua-like, but with more minimalism and eventually some syntactic sugars.
``` ```
// C style comments // C style comments
/*
C style multi line comments
*/
// No local for variable definitions; the local behavior should be implicit // No local for variable definitions; the local behavior should be implicit
x = "foo" x = "foo"
y = "bar" y = "bar"
@ -33,6 +36,7 @@ fn add(a, b)
return a + b return a + b
end end
echo add(x, y) // Outputs: foobar echo add(x, y) // Outputs: foobar
fnVar = fn(x, y, ...) /* do something */ end
if expression and expression2 then if expression and expression2 then
res = add(1, 2) res = add(1, 2)

View File

@ -59,10 +59,30 @@ func (s *Scanner) NextToken() types.Token {
return s.makeToken(types.STAR) return s.makeToken(types.STAR)
case '/': case '/':
if s.match('/') { if s.match('/') {
// Comment goes until end of line // Single-line comment
for s.peek() != '\n' && !s.isAtEnd() { for s.peek() != '\n' && !s.isAtEnd() {
s.advance() 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 // Recursive call to get the next non-comment token
return s.NextToken() return s.NextToken()
} }