boolean-type from comparisons

This commit is contained in:
uan
2026-02-04 18:06:51 +01:00
parent 6525fa711d
commit fc79c23b77
4 changed files with 42 additions and 16 deletions

24
lexer.v
View File

@@ -22,6 +22,10 @@ enum TokenType as u8 {
equals
less
greater
eq_eq
greater_eq
less_eq
not_eq
lparen
rparen
lsqparen
@@ -52,7 +56,7 @@ fn str_from_toktype(type TokenType) string {
.kw_else {'else'}
.kw_for {'for'}
.kw_break {'break'}
.kw_fn {'break'}
.kw_fn {'fn'}
.kw_return {'return'}
.identifier {'identifier'}
.eof {'EOF'}
@@ -74,6 +78,10 @@ fn str_from_toktype(type TokenType) string {
.comma {'comma'}
.semicolon {'semicolon'}
.colon {'colon'}
.eq_eq {'eq eq'}
.greater_eq {'greater eq'}
.less_eq {'less eq'}
.not_eq {'not eq'}
}
}
@@ -96,6 +104,10 @@ fn toktype_from_delimiter(delimiter string) TokenType {
',' {.comma}
';' {.semicolon}
':' {.colon}
'==' {.eq_eq}
'>=' {.greater_eq}
'<=' {.less_eq}
'!=' {.not_eq}
else {.unknown}
}
}
@@ -160,7 +172,15 @@ fn lex(input string) ?[]Token {
}
if is_delimiter(input[right], is_inside_number) && left == right {
if !input[right].is_space() {
tokens << Token{toktype_from_delimiter(input[right].ascii_str()), input[right].ascii_str()}
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++
}
}
tokens << Token{toktype_from_delimiter(tok_str), tok_str}
}
right++
left = right