Skip to content

Commit

Permalink
feat: parse return statement
Browse files Browse the repository at this point in the history
  • Loading branch information
viddrobnic committed May 30, 2024
1 parent 9c5115a commit 590f518
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
9 changes: 8 additions & 1 deletion parser/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,14 @@ impl Parser<'_> {
TokenKind::For => self.parse_for()?,
TokenKind::Break => (ast::NodeValue::Break, range.end),
TokenKind::Continue => (ast::NodeValue::Continue, range.end),
TokenKind::Return => todo!("parse return statement"),
TokenKind::Return => {
let token = self.next_token()?;
let node = self.parse_node(token, Precedence::Lowest)?;
validate_node_kind(&node, NodeKind::Expression)?;

let end = node.range.end;
(ast::NodeValue::Return(Box::new(node)), end)
}
TokenKind::Fn => self.parse_fn_literal()?,
TokenKind::Use => {
let token = self.next_token()?;
Expand Down
56 changes: 56 additions & 0 deletions parser/src/parser/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1132,6 +1132,47 @@ fn fn_literal_named() -> Result<()> {
Ok(())
}

#[test]
fn return_statement() -> Result<()> {
let program = parse("return 1 + 2")?;

assert_eq!(program.statements.len(), 1);
assert_eq!(
program.statements[0],
ast::Node {
value: ast::NodeValue::Return(Box::new(ast::Node {
value: ast::NodeValue::InfixOperator {
operator: ast::InfixOperatorKind::Add,
left: Box::new(ast::Node {
value: ast::NodeValue::IntegerLiteral(1),
range: Range {
start: Position::new(0, 7),
end: Position::new(0, 8)
}
}),
right: Box::new(ast::Node {
value: ast::NodeValue::IntegerLiteral(2),
range: Range {
start: Position::new(0, 11),
end: Position::new(0, 12)
}
})
},
range: Range {
start: Position::new(0, 7),
end: Position::new(0, 12)
}
})),
range: Range {
start: Position::new(0, 0),
end: Position::new(0, 12)
}
}
);

Ok(())
}

#[test]
fn errors() {
let tests = [
Expand Down Expand Up @@ -1175,6 +1216,19 @@ fn errors() {
},
},
),
(
"return continue",
Error {
kind: ErrorKind::InvalidNodeKind {
expected: ast::NodeKind::Expression,
got: ast::NodeKind::Statement,
},
range: Range {
start: Position::new(0, 7),
end: Position::new(0, 15),
},
},
),
];

for (input, expected) in tests {
Expand Down Expand Up @@ -1217,6 +1271,8 @@ fn precedence() -> Result<()> {
),
("// comment", ""),
("//", ""),
("return 1 + 1 * 2", "return (1 + (1 * 2))"),
("return fn(){}", "return fn() {}"),
];

for (input, expected) in tests {
Expand Down

0 comments on commit 590f518

Please sign in to comment.