From 7417deba0701cd1f0272f50231f2e7adc0b346e0 Mon Sep 17 00:00:00 2001 From: rezakargar Date: Mon, 27 Nov 2023 22:36:47 +0330 Subject: [PATCH 1/2] Add multiline comment support to lexical analyzer Now if lexical analyzer sees start symbol of multiline comment (/*), it tries to find ending multiline comment (*/) and ignores all code withing that block of comment --- 1_lexer.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/1_lexer.go b/1_lexer.go index f5fc8b6..4df0f74 100644 --- a/1_lexer.go +++ b/1_lexer.go @@ -46,6 +46,7 @@ package main import ( "bufio" "io" + "log" "strings" "unicode" ) @@ -255,6 +256,26 @@ func (l *lexer) lex() { l.lex() } +func (l *lexer) lexMultiLineComment() { + for { + str, err := l.reader.ReadString('*') + _ = str + if err != nil { + log.Fatal(err) + return + } + nextChar, _, err := l.reader.ReadRune() + if err != nil { + log.Fatal(err) + return + } + if nextChar == '/' { + break + } + l.reader.UnreadRune() + } +} + /* این متد وظیفه ساخت یک توکن با نوع و مقدار مورد نظر و اضافه کردن به چنل توکن ها را به عهده دارد */ @@ -382,6 +403,10 @@ func (l *lexer) lexSymbol(r rune) { l.reader.ReadLine() return } + if doubleCharSymbol == "/*" { + l.lexMultiLineComment() + return + } if t, ok := symobls[singleCharSymbol]; ok { l.reader.UnreadRune() l.emit(t, singleCharSymbol) From 5bdf323fbf16f77c540f04f181bb3546a6d5d57c Mon Sep 17 00:00:00 2001 From: rezakargar Date: Mon, 27 Nov 2023 22:40:39 +0330 Subject: [PATCH 2/2] Add multiline comment example to readme document --- docs/en_readme.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/docs/en_readme.md b/docs/en_readme.md index 0501467..c7ac380 100644 --- a/docs/en_readme.md +++ b/docs/en_readme.md @@ -25,11 +25,21 @@ $ go build ## Comments +### Single line comment: Like most of languages, you can use // to define your comments, they won't get interpreted: ```rust // This is my first program in Kahroba programming language, Let's Rock! ``` +### Multiline comment: +You can also have multiline comments in your code, codes within multiline comment block won't get interpreted: +```rust +/* + This is my first program in Kahroba programming language, + Let's Rock! +*/ +``` + ## Strings You can define string using double quotation: ```rust