Skip to content

Commit

Permalink
Adding scanning support for val
Browse files Browse the repository at this point in the history
The idea is to make val a constant value, while var will be mutable
  • Loading branch information
patbuc committed Jul 12, 2024
1 parent 17480fa commit 1eca85e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/compiler/parser/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ lazy_static! {
(TokenType::Super, ParseRule::new(None, None, Precedence::None)),
(TokenType::This, ParseRule::new(None, None, Precedence::None)),
(TokenType::True, ParseRule::new(Some(Parser::literal), None, Precedence::None)),
(TokenType::Val, ParseRule::new(None, None, Precedence::None)),
(TokenType::Var, ParseRule::new(None, None, Precedence::None)),
(TokenType::While, ParseRule::new(None, None, Precedence::None)),
(TokenType::Error, ParseRule::new(None, None, Precedence::None)),
Expand Down
11 changes: 10 additions & 1 deletion src/compiler/scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,16 @@ impl Scanner {
'p' => self.check_keyword(1, 4, "rint", TokenType::Print),
'r' => self.check_keyword(1, 5, "eturn", TokenType::Return),
's' => self.check_keyword(1, 4, "uper", TokenType::Super),
'v' => self.check_keyword(1, 2, "ar", TokenType::Var),
'v' => {
if self.current - self.start > 1 && self.source[self.start + 1] == 'a' {
return match self.source[self.start + 2] {
'l' => TokenType::Val,
'r' => TokenType::Var,
_ => TokenType::Identifier,
};
}
TokenType::Identifier
}
'w' => self.check_keyword(1, 4, "hile", TokenType::While),
'f' => {
if self.current - self.start > 1 {
Expand Down
1 change: 1 addition & 0 deletions src/compiler/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub(in crate::compiler) enum TokenType {
Super,
This,
True,
Val,
Var,
While,

Expand Down

0 comments on commit 1eca85e

Please sign in to comment.