Removed self.advance() code duplication in lexer.

This commit is contained in:
PaddiM8 2020-06-04 20:09:43 +02:00
parent cbc0b340a1
commit 3a8e939ac0
2 changed files with 28 additions and 60 deletions

View File

@ -70,63 +70,31 @@ impl<'a> Lexer<'a> {
}
}
if c.is_digit(10) {
return self.next_number_literal();
}
if c.is_alphabetic() {
return self.next_identifier();
}
let token = match c {
'+' => {
self.advance();
build(TokenKind::Plus, "")
}
'-' => {
self.advance();
build(TokenKind::Minus, "")
}
'*' => {
self.advance();
build(TokenKind::Star, "")
}
'/' => {
self.advance();
build(TokenKind::Slash, "")
}
'^' => {
self.advance();
build(TokenKind::Power, "")
}
'(' => {
self.advance();
build(TokenKind::OpenParenthesis, "")
}
')' => {
self.advance();
build(TokenKind::ClosedParenthesis, "")
}
'|' => {
self.advance();
build(TokenKind::Pipe, "")
}
'=' => {
self.advance();
build(TokenKind::Equals, "")
}
'!' => {
self.advance();
build(TokenKind::Exclamation, "")
}
',' => {
self.advance();
build(TokenKind::Comma, "")
}
_ => {
if c.is_digit(10) {
self.next_number_literal()
} else if c.is_alphabetic() {
self.next_identifier()
} else {
self.advance();
build(TokenKind::Unknown, "")
}
}
'+' => build(TokenKind::Plus, ""),
'-' => build(TokenKind::Minus, ""),
'*' => build(TokenKind::Star, ""),
'/' => build(TokenKind::Slash, ""),
'^' => build(TokenKind::Power, ""),
'(' => build(TokenKind::OpenParenthesis, ""),
')' => build(TokenKind::ClosedParenthesis, ""),
'|' => build(TokenKind::Pipe, ""),
'=' => build(TokenKind::Equals, ""),
'!' => build(TokenKind::Exclamation, ""),
',' => build(TokenKind::Comma, ""),
_ => build(TokenKind::Unknown, ""),
};
self.advance();
token
}

12
kalk_cli/Cargo.lock generated
View File

@ -128,12 +128,6 @@ dependencies = [
"winapi",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "kalk"
version = "0.1.0"
@ -151,6 +145,12 @@ dependencies = [
"rustyline",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.71"