Browse Source

Allow newlines in conditionals

David Peter 2 years ago
parent
commit
862141c546
3 changed files with 12 additions and 4 deletions
  1. 4 2
      book/src/example-numbat_syntax.md
  2. 4 2
      examples/numbat_syntax.nbt
  3. 4 0
      numbat/src/parser.rs

+ 4 - 2
book/src/example-numbat_syntax.md

@@ -91,8 +91,10 @@ unit thing                       # New base unit with automatically generated
 
 # 8. Conditionals
 
-fn step(x: Scalar) -> Scalar =   # Numbat has 'if <cond> then <expr> else <expr>'
-  if x < 0 then 0 else 1         # condition expressions
+fn step(x: Scalar) -> Scalar =   # The construct 'if <cond> then <expr> else <expr>'
+  if x < 0                       # is an expression, not a statement. It can span
+    then 0                       # multiple lines.
+    else 1
 
 # 9. Procedures
 

+ 4 - 2
examples/numbat_syntax.nbt

@@ -86,8 +86,10 @@ unit thing                       # New base unit with automatically generated
 
 # 8. Conditionals
 
-fn step(x: Scalar) -> Scalar =   # Numbat has 'if <cond> then <expr> else <expr>'
-  if x < 0 then 0 else 1         # condition expressions
+fn step(x: Scalar) -> Scalar =   # The construct 'if <cond> then <expr> else <expr>'
+  if x < 0                       # is an expression, not a statement. It can span
+    then 0                       # multiple lines.
+    else 1
 
 # 9. Procedures
 

+ 4 - 0
numbat/src/parser.rs

@@ -651,6 +651,8 @@ impl<'a> Parser<'a> {
             let span_if = self.last().unwrap().span;
             let condition_expr = self.conversion()?;
 
+            self.match_exact(TokenKind::Newline);
+
             if self.match_exact(TokenKind::Then).is_none() {
                 return Err(ParseError::new(
                     ParseErrorKind::ExpectedThen,
@@ -660,6 +662,8 @@ impl<'a> Parser<'a> {
 
             let then_expr = self.condition()?;
 
+            self.match_exact(TokenKind::Newline);
+
             if self.match_exact(TokenKind::Else).is_none() {
                 return Err(ParseError::new(
                     ParseErrorKind::ExpectedElse,