string ancora un po' rustiche

This commit is contained in:
uan
2026-02-05 22:30:50 +01:00
parent 078a300c1f
commit 1581ca4928
3 changed files with 35 additions and 11 deletions

32
lexer.v
View File

@@ -17,6 +17,7 @@ enum TokenType as u8 {
integer
real
boolean
string
identifier
plus
minus
@@ -95,7 +96,7 @@ fn toktype_from_kw(kw string) TokenType {
return match kw {
'let' {.kw_let}
'const' {.kw_const}
'void', 'real', 'bool', 'int' {.type}
'void', 'real', 'bool', 'int', 'string'{.type}
'if' {.kw_if}
'else' {.kw_else}
'for' {.kw_for}
@@ -111,8 +112,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())
}
@@ -124,7 +125,7 @@ fn is_real(str string) bool {
fn is_keyword(str string) bool {
return [
"void", "int", "real", "bool", "if", "else", "for", "break", "fn", "return", "let", "const", "true", "false", "print", "class"
"void", "int", "real", "bool", "string", "if", "else", "for", "break", "fn", "return", "let", "const", "true", "false", "print", "class"
].contains(str)
}
@@ -144,8 +145,18 @@ fn lex(input string) ?[]Token {
mut line := 1
mut tokens := []Token{}
mut is_inside_number := false
mut is_inside_string := false
for (right < input.len && left <= right) {
for is_inside_string {
right++
if input[right].ascii_str() == '\"' {
is_inside_string = false
right++
tokens << Token{.string, input.substr(left+1, right-1)}
left = right
}
}
is_inside_number = input[left].ascii_str().is_int()
if input[right] == `\n` {
line++
@@ -158,14 +169,15 @@ 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}
mut tok_str := input[right].ascii_str()
if right + 1 < input.len {
combined := input.substr(right, right + 2)
if combined in ['==', '>=', '<=', '!=', '+=', '-=', '*=', '/=', '++', '--'] {
tok_str = combined
right++
}
if right + 1 < input.len {
combined := input.substr(right, right + 2)
if combined in ['==', '>=', '<=', '!=', '+=', '-=', '*=', '/=', '++', '--'] {
tok_str = combined
right++
}
}
tokens << Token{toktype_from_delimiter(tok_str), tok_str}
}
right++