Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vine: allow returns with no expression #73

Merged
merged 6 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions tests/programs/no_return.vi
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

use std::io::println;

fn main(io) {
io.println("We should see this");
return;
io.println("But not this");
}
31 changes: 31 additions & 0 deletions tests/snaps/vine/no_return/compiled.iv
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

::main { ::no_return::main }

::no_return::main {
fn(n0 _)
::std::io::println = fn(ref(n0 _) fn(tup(18 tup(tup(87 tup(101 tup(32 tup(115 tup(104 tup(111 tup(117 tup(108 tup(100 tup(32 tup(115 tup(101 tup(101 tup(32 tup(116 tup(104 tup(105 tup(115 n1)))))))))))))))))) n1)) _))
}

::std::io::println {
fn(ref(n0 n3) fn(n1 _))
::std::io::print = fn(ref(n0 n2) fn(n1 _))
::std::io::print_char = fn(ref(n2 n3) fn(10 _))
}

::std::io::print {
fn(ref(n0 n1) fn(tup(n2 tup(n3 _)) _))
::std::io::print::1 = x(n0 x(n1 x(n2 n3)))
}

::std::io::print::1 { x(n0 x(n1 x(dup12(?(::std::io::print::3 ::std::io::print::2 x(n0 x(n1 x(n3 n2)))) n3) n2))) }

::std::io::print::2 {
x(n1 x(n3 x(@sub(1 n4) tup(n0 n5))))
::std::io::print_char = fn(ref(n1 n2) fn(n0 _))
::std::io::print::1 = x(n2 x(n3 x(n4 n5)))
}

::std::io::print::3 { x(n0 x(n0 _)) }

::std::io::print_char { fn(ref(@io_print_char(char io) io) fn(char _)) }

1 change: 1 addition & 0 deletions tests/snaps/vine/no_return/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
We should see this
15 changes: 15 additions & 0 deletions tests/snaps/vine/no_return/stats.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

Interactions
Total 397
Annihilate 215
Commute 0
Copy 21
Erase 44
Expand 61
Call 37
Branch 19

Memory
Heap 592 B
Allocated 8_112 B
Freed 8_112 B
1 change: 1 addition & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ fn tests(t: &mut DynTester) {
test_vi(t, "tests/programs/loop_vi_loop.vi", b"", ".txt");
test_vi(t, "tests/programs/maybe_set.vi", b"", ".txt");
test_vi(t, "tests/programs/move_it_move_it.vi", b"", ".txt");
test_vi(t, "tests/programs/no_return.vi", b"", ".txt");
test_vi(t, "tests/programs/option_party.vi", b"", ".txt");
test_vi(t, "tests/programs/pretty_div.vi", b"", ".txt");
test_vi(t, "tests/programs/so_random.vi", b"", ".txt");
Expand Down
2 changes: 1 addition & 1 deletion vine/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ pub enum ExprKind {
#[class(value)]
Fn(Vec<Pat>, B<Expr>),
#[class(value)]
Return(B<Expr>),
Return(Option<B<Expr>>),
#[class(value)]
Break,
#[class(value)]
Expand Down
2 changes: 1 addition & 1 deletion vine/src/compile/build_stages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ impl Compiler<'_> {
ExprKind::Loop(body) => self.lower_loop(body),
ExprKind::While(cond, body) => self.lower_while(cond, body),
ExprKind::If(cond, then, els) => self.lower_if(cond, then, els),
ExprKind::Return(r) => self.lower_return(r),
ExprKind::Return(r) => self.lower_return(r.as_deref()),
ExprKind::Break => self.lower_break(),
ExprKind::Continue => self.lower_continue(),

Expand Down
9 changes: 6 additions & 3 deletions vine/src/compile/build_stages/control_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ impl Compiler<'_> {
func.1
}

pub(super) fn lower_return(&mut self, r: &Expr) -> Port {
let r = self.lower_expr_value(r);
pub(super) fn lower_return(&mut self, r: Option<&Expr>) -> Port {
let ret = self.return_target.unwrap();
self.set_local_to(ret.0, r);
if let Some(r) = r {
let r = self.lower_expr_value(r);
self.set_local_to(ret.0, r);
}

self.diverge(ret.1);
Port::Erase
}
Expand Down
6 changes: 5 additions & 1 deletion vine/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,11 @@ impl<'ctx, 'src> VineParser<'ctx, 'src> {

fn _parse_expr_prefix(&mut self, span: usize) -> Parse<'src, ExprKind> {
if self.eat(Token::Return)? {
return Ok(ExprKind::Return(Box::new(self.parse_expr_bp(BP::ControlFlow)?)));
if self.check(Token::Semi) {
return Ok(ExprKind::Return(None));
}

return Ok(ExprKind::Return(Some(Box::new(self.parse_expr_bp(BP::ControlFlow)?))));
}
if self.eat(Token::Break)? {
return Ok(ExprKind::Break);
Expand Down
3 changes: 2 additions & 1 deletion vine/src/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,15 @@ pub trait VisitMut<'a> {
| ExprKind::Error(_)
| ExprKind::CopyLocal(_)
| ExprKind::MoveLocal(_)
| ExprKind::Return(None)
| ExprKind::SetLocal(_) => {}
ExprKind::Ref(a)
| ExprKind::Deref(a)
| ExprKind::Move(a)
| ExprKind::Field(a, _)
| ExprKind::Neg(a)
| ExprKind::Not(a)
| ExprKind::Return(a)
| ExprKind::Return(Some(a))
| ExprKind::Inverse(a)
| ExprKind::Copy(a)
| ExprKind::Set(a)
Expand Down
Loading