From a46827abc5d44456f7461a73184387905d30a680 Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Sat, 6 May 2023 19:55:45 +0400 Subject: [PATCH] Add line comments --- src/lexer.c | 40 ++++++++++++++++++++++++++++++++-------- src/lexer.h | 1 + test.scm | 30 +++++++++++++++--------------- 3 files changed, 48 insertions(+), 23 deletions(-) diff --git a/src/lexer.c b/src/lexer.c index 459b32b..202ee17 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -74,7 +74,9 @@ void token_add(const Lexer lexer) { assert(lexer); - if (lexer->state != STATE_WHITESPACE) { + if (lexer->state != STATE_WHITESPACE && + lexer->state != STATE_COMMENT_LINE) + { enum TokenType token_type; assert(State_to_token_type(lexer->state, &token_type)); @@ -92,7 +94,9 @@ void Lexer_lex(const Lexer self, const char chr) switch (self->state) { case STATE_INIT: - if (chr == '(') { + if (chr == ';') { + self->state = STATE_COMMENT_LINE; + } else if (chr == '(') { self->state = STATE_ROUND_OPEN; buffer_add(self, chr); } else if (chr == ')') { @@ -131,7 +135,9 @@ void Lexer_lex(const Lexer self, const char chr) } break; case STATE_WHITESPACE: - if (chr == '(') { + if (chr == ';') { + self->state = STATE_COMMENT_LINE; + } else if (chr == '(') { token_add(self); self->state = STATE_ROUND_OPEN; buffer_add(self, chr); @@ -179,6 +185,14 @@ void Lexer_lex(const Lexer self, const char chr) assert(0); } break; + case STATE_COMMENT_LINE: + if (chr == '\n') { + token_add(self); + self->state = STATE_WHITESPACE; + } else { + buffer_add(self, chr); + } + break; case STATE_ROUND_OPEN: case STATE_ROUND_CLOSE: case STATE_SQUARE_OPEN: @@ -186,7 +200,9 @@ void Lexer_lex(const Lexer self, const char chr) case STATE_CURLY_OPEN: case STATE_CURLY_CLOSE: case STATE_QUOTE: - if (chr == '(') { + if (chr == ';') { + self->state = STATE_COMMENT_LINE; + } else if (chr == '(') { token_add(self); self->state = STATE_ROUND_OPEN; buffer_add(self, chr); @@ -245,7 +261,9 @@ void Lexer_lex(const Lexer self, const char chr) } break; case STATE_TAG: - if (chr == '(') { + if (chr == ';') { + self->state = STATE_COMMENT_LINE; + } else if (chr == '(') { token_add(self); self->state = STATE_ROUND_OPEN; buffer_add(self, chr); @@ -283,7 +301,9 @@ void Lexer_lex(const Lexer self, const char chr) } break; case STATE_IDENT: - if (chr == '(') { + if (chr == ';') { + self->state = STATE_COMMENT_LINE; + } else if (chr == '(') { token_add(self); self->state = STATE_ROUND_OPEN; buffer_add(self, chr); @@ -321,7 +341,9 @@ void Lexer_lex(const Lexer self, const char chr) } break; case STATE_NUM: - if (chr == '(') { + if (chr == ';') { + self->state = STATE_COMMENT_LINE; + } else if (chr == '(') { token_add(self); self->state = STATE_ROUND_OPEN; buffer_add(self, chr); @@ -368,7 +390,9 @@ void Lexer_lex(const Lexer self, const char chr) } break; case STATE_STRING_END: - if (chr == '(') { + if (chr == ';') { + self->state = STATE_COMMENT_LINE; + } else if (chr == '(') { self->state = STATE_ROUND_OPEN; buffer_add(self, chr); } else if (chr == ')') { diff --git a/src/lexer.h b/src/lexer.h index 9381f8a..a9be03b 100644 --- a/src/lexer.h +++ b/src/lexer.h @@ -16,6 +16,7 @@ enum Lexer_State { STATE_INIT, STATE_WHITESPACE, + STATE_COMMENT_LINE, STATE_ROUND_OPEN, STATE_ROUND_CLOSE, STATE_SQUARE_OPEN, diff --git a/test.scm b/test.scm index 1db30a0..c06ffb8 100644 --- a/test.scm +++ b/test.scm @@ -23,8 +23,8 @@ (displayln '#true) (displayln (quote #false)) (displayln '#false) - '(displayln (quote "#\n")) - '(displayln '"#\n") + ;(displayln (quote #\n)) + ;(displayln '#\n) (displayln (quote foo)) (displayln 'foo) (displayln (quote "foo")) @@ -39,8 +39,8 @@ (displayln "--- TEST: arcane/tokenize --------------------------------------") (displayln (arcane/tokenize "(")) (displayln (arcane/tokenize "#false")) - '(displayln (arcane/tokenize "''")) - '(displayln (arcane/tokenize "'qwe'")) + ;(displayln (arcane/tokenize "\"\"")) + ;(displayln (arcane/tokenize "\"qwe\"")) (displayln (arcane/tokenize "(displayln (list 1))")) (newline) (displayln "--- TEST: arcane/typeof ----------------------------------------") @@ -48,7 +48,7 @@ (displayln (arcane/typeof +)) (displayln (arcane/typeof (cons 123 456))) (displayln (arcane/typeof #false)) - '(displayln (arcane/typeof "#\n")) + ;(displayln (arcane/typeof #\n)) (displayln (arcane/typeof 'foo)) (displayln (arcane/typeof "foo")) (displayln (arcane/typeof 123)) @@ -73,7 +73,7 @@ (displayln (boolean? '())) (displayln (boolean? #true)) (displayln (boolean? #false)) - '(displayln (boolean? "#\n")) + ;(displayln (boolean? #\n)) (displayln (boolean? 'foo)) (displayln (boolean? "foo")) (displayln (boolean? 123)) @@ -84,7 +84,7 @@ (displayln (char? '())) (displayln (char? #true)) (displayln (char? #false)) - '(displayln (char? "#\n")) + ;(displayln (char? #\n)) (displayln (char? 'foo)) (displayln (char? "foo")) (displayln (char? 123)) @@ -95,7 +95,7 @@ (displayln (null? '())) (displayln (null? #true)) (displayln (null? #false)) - '(displayln (null? "#\n")) + ;(displayln (null? #\n)) (displayln (null? 'foo)) (displayln (null? "foo")) (displayln (null? 123)) @@ -106,7 +106,7 @@ (displayln (number? '())) (displayln (number? #true)) (displayln (number? #false)) - '(displayln (number? "#\n")) + ;(displayln (number? #\n)) (displayln (number? 'foo)) (displayln (number? "foo")) (displayln (number? 123)) @@ -117,7 +117,7 @@ (displayln (pair? '())) (displayln (pair? #true)) (displayln (pair? #false)) - '(displayln (pair? "#\n")) + ;(displayln (pair? #\n)) (displayln (pair? 'foo)) (displayln (pair? "foo")) (displayln (pair? 123)) @@ -128,7 +128,7 @@ (displayln (procedure? '())) (displayln (procedure? #true)) (displayln (procedure? #false)) - '(displayln (procedure? "#\n")) + ;(displayln (procedure? #\n)) (displayln (procedure? 'foo)) (displayln (procedure? "foo")) (displayln (procedure? 123)) @@ -139,7 +139,7 @@ (displayln (string? '())) (displayln (string? #true)) (displayln (string? #false)) - '(displayln (string? "#\n")) + ;(displayln (string? #\n)) (displayln (string? 'foo)) (displayln (string? "foo")) (displayln (string? 123)) @@ -150,7 +150,7 @@ (displayln (symbol? '())) (displayln (symbol? #true)) (displayln (symbol? #false)) - '(displayln (symbol? "#\n")) + ;(displayln (symbol? #\n)) (displayln (symbol? 'foo)) (displayln (symbol? "foo")) (displayln (symbol? 123)) @@ -160,7 +160,7 @@ (newline) (displayln "--- TEST: number->string ---------------------------------------") (displayln (number->string 123)) - '(displayln (number->string "-123")) + ;(displayln (number->string -123)) (displayln (number->string 123456 16)) (newline) (displayln "--- TEST: string->symbol ---------------------------------------") @@ -177,7 +177,7 @@ (displayln (not '())) (displayln (not #true)) (displayln (not #false)) - '(displayln (not "#\n")) + ;(displayln (not #\n)) (displayln (not 'foo)) (displayln (not "foo")) (displayln (not 123))