From 10372d3b1a7c9ecab89aee1ce9a9145bcb6c18e8 Mon Sep 17 00:00:00 2001 From: stevenhuyn <18359644+stevenhuyn@users.noreply.github.com> Date: Wed, 23 Oct 2024 17:00:32 -0700 Subject: [PATCH 1/6] Add null_return example --- vine/examples/null_return.vi | 26 ++++++++++++++++++++++++++ vine/src/ast.rs | 2 +- vine/src/compile/build_stages.rs | 7 ++++++- vine/src/parser.rs | 6 +++++- vine/src/visit.rs | 3 ++- 5 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 vine/examples/null_return.vi diff --git a/vine/examples/null_return.vi b/vine/examples/null_return.vi new file mode 100644 index 00000000..7bdfb9b1 --- /dev/null +++ b/vine/examples/null_return.vi @@ -0,0 +1,26 @@ + +use std::io::println; + +fn main(io) { + io.println("Hello, world!"); + io.println(something()); + let u = unit(); + let v = something(); + io.println(something()); + + if u == v { + io.println("woah"); + } else { + io.println(something()); + } + + io.println(something()); +} + +fn unit() { + return; +} + +fn something() { + return "yo"; +} diff --git a/vine/src/ast.rs b/vine/src/ast.rs index 5f3f0b72..1e4d98d7 100644 --- a/vine/src/ast.rs +++ b/vine/src/ast.rs @@ -153,7 +153,7 @@ pub enum ExprKind { #[class(value)] Fn(Vec, B), #[class(value)] - Return(B), + Return(Option>), #[class(value)] Break, #[class(value)] diff --git a/vine/src/compile/build_stages.rs b/vine/src/compile/build_stages.rs index 4643ad17..5d42bfb3 100644 --- a/vine/src/compile/build_stages.rs +++ b/vine/src/compile/build_stages.rs @@ -132,7 +132,12 @@ 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(None) => { + let default = Expr { span: Span::NONE, kind: ExprKind::Tuple(vec![]) }; + eprintln!("{:?}", default); + self.lower_return(&default) + } + ExprKind::Return(Some(r)) => self.lower_return(r), ExprKind::Break => self.lower_break(), ExprKind::Continue => self.lower_continue(), diff --git a/vine/src/parser.rs b/vine/src/parser.rs index a529fa74..f19c6986 100644 --- a/vine/src/parser.rs +++ b/vine/src/parser.rs @@ -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.eat(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); diff --git a/vine/src/visit.rs b/vine/src/visit.rs index f566781f..c89e31a9 100644 --- a/vine/src/visit.rs +++ b/vine/src/visit.rs @@ -50,6 +50,7 @@ pub trait VisitMut<'a> { | ExprKind::Error(_) | ExprKind::CopyLocal(_) | ExprKind::MoveLocal(_) + | ExprKind::Return(None) | ExprKind::SetLocal(_) => {} ExprKind::Ref(a) | ExprKind::Deref(a) @@ -57,7 +58,7 @@ pub trait VisitMut<'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) From c5f61cbddddff9ec167e422cfe6b613c3f2d1821 Mon Sep 17 00:00:00 2001 From: stevenhuyn <18359644+stevenhuyn@users.noreply.github.com> Date: Wed, 23 Oct 2024 17:12:11 -0700 Subject: [PATCH 2/6] Add empty return test program --- tests/programs/no_return.vi | 8 +++++++ tests/snaps/vine/no_return/compiled.iv | 31 ++++++++++++++++++++++++++ tests/snaps/vine/no_return/output.txt | 1 + tests/snaps/vine/no_return/stats.txt | 15 +++++++++++++ tests/tests.rs | 1 + vine/src/compile/build_stages.rs | 1 - 6 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 tests/programs/no_return.vi create mode 100644 tests/snaps/vine/no_return/compiled.iv create mode 100644 tests/snaps/vine/no_return/output.txt create mode 100644 tests/snaps/vine/no_return/stats.txt diff --git a/tests/programs/no_return.vi b/tests/programs/no_return.vi new file mode 100644 index 00000000..3a739e27 --- /dev/null +++ b/tests/programs/no_return.vi @@ -0,0 +1,8 @@ + +use std::io::println; + +fn main(io) { + io.println("We should see this"); + return; + io.println("But not this"); +} diff --git a/tests/snaps/vine/no_return/compiled.iv b/tests/snaps/vine/no_return/compiled.iv new file mode 100644 index 00000000..4f2dca1e --- /dev/null +++ b/tests/snaps/vine/no_return/compiled.iv @@ -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 _)) } + diff --git a/tests/snaps/vine/no_return/output.txt b/tests/snaps/vine/no_return/output.txt new file mode 100644 index 00000000..701608cb --- /dev/null +++ b/tests/snaps/vine/no_return/output.txt @@ -0,0 +1 @@ +We should see this diff --git a/tests/snaps/vine/no_return/stats.txt b/tests/snaps/vine/no_return/stats.txt new file mode 100644 index 00000000..a05918fe --- /dev/null +++ b/tests/snaps/vine/no_return/stats.txt @@ -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 diff --git a/tests/tests.rs b/tests/tests.rs index 9a0c09c4..686cbf72 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -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"); diff --git a/vine/src/compile/build_stages.rs b/vine/src/compile/build_stages.rs index 5d42bfb3..9f0863a3 100644 --- a/vine/src/compile/build_stages.rs +++ b/vine/src/compile/build_stages.rs @@ -134,7 +134,6 @@ impl Compiler<'_> { ExprKind::If(cond, then, els) => self.lower_if(cond, then, els), ExprKind::Return(None) => { let default = Expr { span: Span::NONE, kind: ExprKind::Tuple(vec![]) }; - eprintln!("{:?}", default); self.lower_return(&default) } ExprKind::Return(Some(r)) => self.lower_return(r), From f586d9524d646fd0d051224b7b1b7e654af02d05 Mon Sep 17 00:00:00 2001 From: stevenhuyn <18359644+stevenhuyn@users.noreply.github.com> Date: Wed, 23 Oct 2024 17:14:12 -0700 Subject: [PATCH 3/6] Call default value, unit --- vine/src/compile/build_stages.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vine/src/compile/build_stages.rs b/vine/src/compile/build_stages.rs index 9f0863a3..8f62e403 100644 --- a/vine/src/compile/build_stages.rs +++ b/vine/src/compile/build_stages.rs @@ -133,8 +133,8 @@ impl Compiler<'_> { ExprKind::While(cond, body) => self.lower_while(cond, body), ExprKind::If(cond, then, els) => self.lower_if(cond, then, els), ExprKind::Return(None) => { - let default = Expr { span: Span::NONE, kind: ExprKind::Tuple(vec![]) }; - self.lower_return(&default) + let unit = Expr { span: Span::NONE, kind: ExprKind::Tuple(vec![]) }; + self.lower_return(&unit) } ExprKind::Return(Some(r)) => self.lower_return(r), ExprKind::Break => self.lower_break(), From 762ac27f57ac5d2be1cc516877383df0f65c3ba9 Mon Sep 17 00:00:00 2001 From: stevenhuyn <18359644+stevenhuyn@users.noreply.github.com> Date: Wed, 23 Oct 2024 17:16:04 -0700 Subject: [PATCH 4/6] Remove old example --- vine/examples/null_return.vi | 26 -------------------------- 1 file changed, 26 deletions(-) delete mode 100644 vine/examples/null_return.vi diff --git a/vine/examples/null_return.vi b/vine/examples/null_return.vi deleted file mode 100644 index 7bdfb9b1..00000000 --- a/vine/examples/null_return.vi +++ /dev/null @@ -1,26 +0,0 @@ - -use std::io::println; - -fn main(io) { - io.println("Hello, world!"); - io.println(something()); - let u = unit(); - let v = something(); - io.println(something()); - - if u == v { - io.println("woah"); - } else { - io.println(something()); - } - - io.println(something()); -} - -fn unit() { - return; -} - -fn something() { - return "yo"; -} From 8d55c1b370651aad08d42c6282da7c7cedf11ed7 Mon Sep 17 00:00:00 2001 From: Steven <18359644+stevenhuyn@users.noreply.github.com> Date: Wed, 23 Oct 2024 17:32:59 -0700 Subject: [PATCH 5/6] Update vine/src/parser.rs Co-authored-by: T6 --- vine/src/parser.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vine/src/parser.rs b/vine/src/parser.rs index f19c6986..9cba396b 100644 --- a/vine/src/parser.rs +++ b/vine/src/parser.rs @@ -248,7 +248,7 @@ impl<'ctx, 'src> VineParser<'ctx, 'src> { fn _parse_expr_prefix(&mut self, span: usize) -> Parse<'src, ExprKind> { if self.eat(Token::Return)? { - if self.eat(Token::Semi)? { + if self.check(Token::Semi) { return Ok(ExprKind::Return(None)); } From 27306addcc3ea6a85dd9e4457f00321667573f53 Mon Sep 17 00:00:00 2001 From: stevenhuyn <18359644+stevenhuyn@users.noreply.github.com> Date: Wed, 23 Oct 2024 17:34:42 -0700 Subject: [PATCH 6/6] Make lower_return take a Option<&Expr> --- vine/src/compile/build_stages.rs | 6 +----- vine/src/compile/build_stages/control_flow.rs | 9 ++++++--- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/vine/src/compile/build_stages.rs b/vine/src/compile/build_stages.rs index 8f62e403..6d6d7fde 100644 --- a/vine/src/compile/build_stages.rs +++ b/vine/src/compile/build_stages.rs @@ -132,11 +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(None) => { - let unit = Expr { span: Span::NONE, kind: ExprKind::Tuple(vec![]) }; - self.lower_return(&unit) - } - ExprKind::Return(Some(r)) => self.lower_return(r), + ExprKind::Return(r) => self.lower_return(r.as_deref()), ExprKind::Break => self.lower_break(), ExprKind::Continue => self.lower_continue(), diff --git a/vine/src/compile/build_stages/control_flow.rs b/vine/src/compile/build_stages/control_flow.rs index 3850458a..6b344066 100644 --- a/vine/src/compile/build_stages/control_flow.rs +++ b/vine/src/compile/build_stages/control_flow.rs @@ -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 }