Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:Champii/Rock
Browse files Browse the repository at this point in the history
  • Loading branch information
Champii committed Jun 2, 2022
2 parents 5f699c9 + 054fd32 commit 97e477a
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 3 deletions.
10 changes: 10 additions & 0 deletions src/lib/ast/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,11 +468,21 @@ impl Expression {
}
}

#[allow(dead_code)]
pub fn is_return(&self) -> bool {
matches!(&self, Expression::Return(_))
}

#[allow(dead_code)]
pub fn new_unary(unary: UnaryExpr) -> Expression {
Expression::UnaryExpr(unary)
}

#[allow(dead_code)]
pub fn new_return(expr: Expression) -> Expression {
Expression::Return(Box::new(expr))
}

#[allow(dead_code)]
pub fn new_binop(unary: UnaryExpr, operator: Operator, expr: Expression) -> Expression {
Expression::BinopExpr(unary, operator, Box::new(expr))
Expand Down
13 changes: 11 additions & 2 deletions src/lib/codegen/codegen_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,17 @@ impl<'a> CodegenContext<'a> {

builder.position_at_end(basic_block);

let stmt = body
let first_return_idx = body
.stmts
.iter()
.position(|s| s.is_return())
.unwrap_or(body.stmts.len());

let stmts = body.stmts.iter().take(first_return_idx + 1);

// FIXME: Add warning here for unreachable statements

let stmt = stmts
.map(|stmt| self.lower_stmt(stmt, builder))
.last()
.unwrap()?;
Expand Down Expand Up @@ -545,7 +553,8 @@ impl<'a> CodegenContext<'a> {
ExpressionKind::Return(expr) => {
let val = self.lower_expression(expr, builder)?;

builder.build_return(Some(&val.as_basic_value_enum()));
// This is disabled because this is handled in lower_body
// builder.build_return(Some(&val.as_basic_value_enum()));

val
}
Expand Down
15 changes: 15 additions & 0 deletions src/lib/hir/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,13 @@ impl Statement {
StatementKind::For(f) => f.get_hir_id(),
}
}

pub fn is_return(&self) -> bool {
match &*self.kind {
StatementKind::Expression(expr) => expr.is_return(),
_ => false,
}
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -516,6 +523,14 @@ impl Expression {
panic!("Not a literal");
}
}

pub fn is_return(&self) -> bool {
if let ExpressionKind::Return(_) = &*self.kind {
true
} else {
false
}
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down
5 changes: 4 additions & 1 deletion src/lib/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,10 @@ pub fn parse_assign_left_side(input: Parser) -> Res<Parser, AssignLeftSide> {

pub fn parse_expression(input: Parser) -> Res<Parser, Expression> {
alt((
map(
preceded(terminated(tag("return"), space1), parse_expression),
Expression::new_return,
),
map(
tuple((
parse_unary,
Expand All @@ -681,7 +685,6 @@ pub fn parse_expression(input: Parser) -> Res<Parser, Expression> {
map(parse_native_operator, |(op, id1, id2)| {
Expression::new_native_operator(op, id1, id2)
}),
// TODO: Return
))(input)
}

Expand Down
4 changes: 4 additions & 0 deletions src/lib/testcases/basic/early_return/main.rk
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
main =
let a = 42
return a
28
1 change: 1 addition & 0 deletions src/lib/testcases/basic/early_return/main.rk.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
42
Empty file.
4 changes: 4 additions & 0 deletions src/lib/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ fn testcases_basic_simple_struct_main() {
run("testcases/basic/simple_struct/main.rk", include_str!("testcases/basic/simple_struct/main.rk"), include_str!("testcases/basic/simple_struct/main.rk.out"), include_str!("testcases/basic/simple_struct/main.rk.stdout"));
}
#[test]
fn testcases_basic_early_return_main() {
run("testcases/basic/early_return/main.rk", include_str!("testcases/basic/early_return/main.rk"), include_str!("testcases/basic/early_return/main.rk.out"), include_str!("testcases/basic/early_return/main.rk.stdout"));
}
#[test]
fn testcases_basic_bool_false_main() {
run("testcases/basic/bool_false/main.rk", include_str!("testcases/basic/bool_false/main.rk"), include_str!("testcases/basic/bool_false/main.rk.out"), include_str!("testcases/basic/bool_false/main.rk.stdout"));
}
Expand Down

0 comments on commit 97e477a

Please sign in to comment.