From 9097f4c7627afc90f2b36683bbdf337046adf277 Mon Sep 17 00:00:00 2001 From: Garrit Franke Date: Sun, 21 Feb 2021 18:32:09 +0100 Subject: [PATCH] feat: use "else" keyword instead of "default" --- docs/concepts/control-flow.md | 4 ++-- src/generator/js.rs | 2 +- src/lexer/mod.rs | 2 -- src/parser/node_type.rs | 2 +- src/parser/rules.rs | 18 +++++++++--------- tests/match_statements.sb | 2 +- 6 files changed, 14 insertions(+), 16 deletions(-) diff --git a/docs/concepts/control-flow.md b/docs/concepts/control-flow.md index 83f24a8..d427b2b 100644 --- a/docs/concepts/control-flow.md +++ b/docs/concepts/control-flow.md @@ -86,11 +86,11 @@ Working with `if` statements with multiple `else` branches can become tedious. ` 1 => println("x is 1") 2 => println("x is 2") 42 => println("The answer to the universe and everything!") - default => println("This will not be called") + else => println("This will not be called") } ``` -In this example, we check the value of `x`, and execute some code based on the value. Instead of having to type `x == 1`, `x == 2` and so on, we instead provide the value only once, and decide what to do for each case. We can optionally provide a `default` case, which will be executed if no other case was triggered. +In this example, we check the value of `x`, and execute some code based on the value. Instead of having to type `x == 1`, `x == 2` and so on, we instead provide the value only once, and decide what to do for each case. We can optionally provide a `else` case, which will be executed if no other case was triggered. You can execute multiple statements inside a single case. A common case would be to log some debug output and then return a value. diff --git a/src/generator/js.rs b/src/generator/js.rs index d8dbd55..5c37a88 100644 --- a/src/generator/js.rs +++ b/src/generator/js.rs @@ -175,7 +175,7 @@ fn generate_match(subject: Expression, arms: Vec) -> String { out_str += &format!("{}\n", &generate_statement(statement)); out_str += "break;"; } - MatchArm::Default(statement) => { + MatchArm::Else(statement) => { out_str += "default:\n"; out_str += &format!("{}\n", &generate_statement(statement)); } diff --git a/src/lexer/mod.rs b/src/lexer/mod.rs index 8788682..73b3836 100644 --- a/src/lexer/mod.rs +++ b/src/lexer/mod.rs @@ -148,7 +148,6 @@ pub enum Keyword { Struct, New, Match, - Default, Unknown, } @@ -377,7 +376,6 @@ impl Cursor<'_> { c if c == "struct" => Keyword::Struct, c if c == "new" => Keyword::New, c if c == "match" => Keyword::Match, - c if c == "default" => Keyword::Default, _ => Keyword::Unknown, } } diff --git a/src/parser/node_type.rs b/src/parser/node_type.rs index c4bb11d..7a992e2 100644 --- a/src/parser/node_type.rs +++ b/src/parser/node_type.rs @@ -145,7 +145,7 @@ impl TryFrom for Expression { #[derive(Debug, Eq, PartialEq, Clone)] pub enum MatchArm { Case(Expression, Statement), - Default(Statement), + Else(Statement), } #[derive(Debug, Eq, PartialEq, Clone)] diff --git a/src/parser/rules.rs b/src/parser/rules.rs index 95ffb53..95aeafb 100644 --- a/src/parser/rules.rs +++ b/src/parser/rules.rs @@ -465,22 +465,22 @@ impl Parser { self.match_token(TokenKind::CurlyBracesOpen)?; let mut arms: Vec = Vec::new(); - // Used to mitigate multiple default cases were defined - let mut has_default = false; + // Used to mitigate multiple else cases were defined + let mut has_else = false; loop { let next = self.peek()?; match next.kind { TokenKind::Literal(_) | TokenKind::Identifier(_) | TokenKind::Keyword(Keyword::Boolean) => arms.push(self.parse_match_arm()?), - TokenKind::Keyword(Keyword::Default) => { - if has_default { + TokenKind::Keyword(Keyword::Else) => { + if has_else { return Err(self.make_error_msg( next.pos, - "Multiple defaults are not allowed".to_string(), + "Multiple else arms are not allowed".to_string(), )); } - has_default = true; + has_else = true; arms.push(self.parse_match_arm()?); } TokenKind::CurlyBracesClose => break, @@ -495,10 +495,10 @@ impl Parser { let next = self.peek()?; match next.kind { - TokenKind::Keyword(Keyword::Default) => { - self.match_keyword(Keyword::Default)?; + TokenKind::Keyword(Keyword::Else) => { + self.match_keyword(Keyword::Else)?; self.match_token(TokenKind::ArrowRight)?; - Ok(MatchArm::Default(self.parse_statement()?)) + Ok(MatchArm::Else(self.parse_statement()?)) } _ => { let expr = self.parse_expression()?; diff --git a/tests/match_statements.sb b/tests/match_statements.sb index bfc9ba2..e4a81f4 100644 --- a/tests/match_statements.sb +++ b/tests/match_statements.sb @@ -26,7 +26,7 @@ fn test_match_with_block_statement() { println("x is 2, in case you are wondering") } 42 => println("The answer to the universe and everything!") - default => println("Default case") + else => println("Default case") } }