Skip to content

Commit 82d5ef3

Browse files
committed
feat: if is also treated as an expression!
1 parent 236a9bc commit 82d5ef3

File tree

2 files changed

+49
-45
lines changed

2 files changed

+49
-45
lines changed

packages/engine/src/parser/mod.rs

Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -209,48 +209,6 @@ impl<'a> Parser<'a> {
209209
}
210210
}
211211

212-
fn if_expr(&mut self) -> ParserResult<Option<WithCursor<Expression>>> {
213-
let_expr!(condition = self.expression()?);
214-
215-
let start = self.cursor;
216-
217-
218-
let truthy_block = if self.next_if_eq(&&LexerTokenKind::LBracket).is_some() {
219-
self.parse_block()?
220-
} else if self.next_if_eq(&&LexerTokenKind::Colon).is_some() {
221-
self.parse_inline_block()?
222-
} else {
223-
return Err(ParserErrorKind::ExpectedExpression);
224-
};
225-
226-
self.next();
227-
228-
let else_condition = if self.next_if_eq(&&LexerTokenKind::Else).is_some() {
229-
let start = self.cursor;
230-
231-
Some(match self.peek().map(|t| t.kind.clone()) {
232-
Some(LexerTokenKind::LBracket) => {
233-
self.next();
234-
self.parse_block()?
235-
},
236-
Some(LexerTokenKind::Colon) => {
237-
self.next();
238-
self.parse_inline_block()?
239-
},
240-
_ => {
241-
let stmt = self.parse_if()?;
242-
WithCursor::create_with(start, self.cursor, vec![stmt])
243-
},
244-
})
245-
} else {
246-
None
247-
};
248-
249-
let if_expr = Expression::If(Box::from((condition, truthy_block, else_condition)));
250-
251-
Ok(Some(WithCursor::create_with(start, self.cursor, if_expr)))
252-
}
253-
254212
fn parse_inline_block(&mut self) -> ParserResult<WithCursor<Block>> {
255213
let Some(next) = self.peek().cloned() else {
256214
return Err(ParserErrorKind::ExpectedStatement);
@@ -514,6 +472,11 @@ impl<'a> Parser<'a> {
514472
Expression::ShellCommand(Box::from(token.as_shell_command()?.to_owned()))
515473
}
516474

475+
LexerTokenKind::If => {
476+
self.next();
477+
return self.if_expr()
478+
},
479+
517480
LexerTokenKind::EOL | LexerTokenKind::EOF => return Ok(None),
518481

519482
_ => {
@@ -529,6 +492,48 @@ impl<'a> Parser<'a> {
529492
Ok(Some(WithCursor::create_with(token.start, token.end, expr)))
530493
}
531494

495+
fn if_expr(&mut self) -> ParserResult<Option<WithCursor<Expression>>> {
496+
let_expr!(condition = self.expression()?);
497+
498+
let start = self.cursor;
499+
500+
501+
let truthy_block = if self.next_if_eq(&&LexerTokenKind::LBracket).is_some() {
502+
self.parse_block()?
503+
} else if self.next_if_eq(&&LexerTokenKind::Colon).is_some() {
504+
self.parse_inline_block()?
505+
} else {
506+
return Err(ParserErrorKind::ExpectedExpression);
507+
};
508+
509+
self.next();
510+
511+
let else_condition = if self.next_if_eq(&&LexerTokenKind::Else).is_some() {
512+
let start = self.cursor;
513+
514+
Some(match self.peek().map(|t| t.kind.clone()) {
515+
Some(LexerTokenKind::LBracket) => {
516+
self.next();
517+
self.parse_block()?
518+
},
519+
Some(LexerTokenKind::Colon) => {
520+
self.next();
521+
self.parse_inline_block()?
522+
},
523+
_ => {
524+
let stmt = self.parse_if()?;
525+
WithCursor::create_with(start, self.cursor, vec![stmt])
526+
},
527+
})
528+
} else {
529+
None
530+
};
531+
532+
let if_expr = Expression::If(Box::from((condition, truthy_block, else_condition)));
533+
534+
Ok(Some(WithCursor::create_with(start, self.cursor, if_expr)))
535+
}
536+
532537
fn expect_token(&mut self, expected: &'a LexerTokenKind) -> ParserResult<&LexerToken> {
533538
self.expect(&expected).map_err(|found| {
534539
ParserErrorKind::ExpectedToken(vec![expected.clone()], found.map(|t| t.kind.clone()))

script.tsh

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
if true: test = 5
2-
else if false: test = 10
3-
else: test = 15
1+
var test = if true: 5
2+
else: 10

0 commit comments

Comments
 (0)