diff --git a/README.md b/README.md index fde6954..6bc30a6 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,17 @@ const x int = 9; ``` --- +### Comments + +Comments in One start with a # and end with another # or a newline + +``` +#this is a comment +let a #this is also a comment# int = 9; +``` + +--- + ### Scopes Like in most languages, variables in One are only limited to their scope. @@ -62,7 +73,7 @@ let x int = 3 let y real = 5.5; print(y); } -print("y is now undefined again here"); +#y is now undefined again here { let y real = 0.2; print(y); @@ -223,7 +234,7 @@ The 'main' function must always return `int` and does not accept any arguments. ``` fn main() int { - print("my code here"); + #my code here } ``` diff --git a/lexer.v b/lexer.v index a8133f8..8f81a84 100644 --- a/lexer.v +++ b/lexer.v @@ -116,8 +116,8 @@ fn toktype_from_kw(kw string) TokenType { fn is_delimiter(c u8, is_inside_number bool) bool { valid_chars := match is_inside_number { - true {" +-*/,;:%<>()[]{}=\n\""} - false {". +-*/,;:%<>()[]{}=\n\""} + true {" #+-*/,;:%<>()[]{}=\n\""} + false {". #+-*/,;:%<>()[]{}=\n\""} } return valid_chars.contains(c.ascii_str()) } @@ -150,8 +150,16 @@ fn lex(input string) ?[]Token { mut tokens := []Token{} mut is_inside_number := false mut is_inside_string := false + mut is_inside_comment := false for (right < input.len && left <= right) { + for is_inside_comment { + right++ + if ['#', '\n'].contains(input[right].ascii_str()) { + is_inside_comment = false + } + left = right + } for is_inside_string { right++ if input[right].ascii_str() == '\"' { @@ -174,6 +182,7 @@ fn lex(input string) ?[]Token { if is_delimiter(input[right], is_inside_number) && left == right { if !input[right].is_space() { if input[right].ascii_str() == '\"' {is_inside_string = true; continue} + if input[right].ascii_str() == '#' {is_inside_comment = true; continue} mut tok_str := input[right].ascii_str() if right + 1 < input.len { combined := input.substr(right, right + 2) diff --git a/one b/one index e817f6f..711b1ec 100755 Binary files a/one and b/one differ