Skip to content

Commit

Permalink
Added support for multi-line comments
Browse files Browse the repository at this point in the history
  • Loading branch information
neeilan committed Aug 4, 2018
1 parent 4bdec85 commit 621301f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/scanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ void Scanner::scan_token() {
case '/':
if (match('/')) { // A '//' single-line comment
while (peek() != '\n' && !is_at_end()) advance();
} else if (match('*')) { // A /* multi-line comment
bool in_comment = true;
while (in_comment && !is_at_end()) {
while (!match('*') && !is_at_end()) advance();
// Matched a * - comment ends if we match a /
in_comment = !match('/');
}
} else {
add_token(SLASH);
}
Expand Down
5 changes: 5 additions & 0 deletions test/functional_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ def test_superclass_method_call_on_instance(self):
output = run_file(absolute_path('superclass_method.lox'))
self.assertEqual(expected, output)

def test_multiline_comments(self):
expected = 'Interpreter output:\nMultiline comments!'
output = run_file(absolute_path('multiline_comments.lox'))
self.assertEqual(expected, output)


class LoxppOutputErrorsTest(unittest.TestCase):

Expand Down
9 changes: 9 additions & 0 deletions test/multiline_comments.lox
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* Multi
line
comment 1 */

var x = "Multiline comments!";

/* Il$$3g^l charac%ers '\ */

print x;

0 comments on commit 621301f

Please sign in to comment.