From 742c16dccef17a57494fe5846818cfc3324bbd45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Tue, 16 Feb 2021 23:45:45 +0100 Subject: add tests for unusued variables --- src/vm.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'src/vm.rs') diff --git a/src/vm.rs b/src/vm.rs index f5c1cf0..70cb5e4 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -831,15 +831,18 @@ mod tests { test_string!(uncallable_type, " f := fn i: int { i() - }", + } + f", [ErrorKind::TypeError(_, _)]); test_string!(wrong_params, " - f : fn -> int = fn a: int -> int {}", + f : fn -> int = fn a: int -> int {} + f", [ErrorKind::TypeError(_, _), ErrorKind::TypeError(_, _)]); test_string!(wrong_ret, " - f : fn -> int = fn {}", + f : fn -> int = fn {} + f", [ErrorKind::TypeError(_, _)]); } } -- cgit v1.2.1 From b205748bde51c551468a8dc89123f85b67c660dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Wed, 17 Feb 2021 21:01:12 +0100 Subject: solve edge case for constants --- src/vm.rs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) (limited to 'src/vm.rs') diff --git a/src/vm.rs b/src/vm.rs index f5c1cf0..20125be 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -557,6 +557,13 @@ impl VM { Value::Function(_, block) => { self.push(Value::Function(Vec::new(), block.clone())); + if block.borrow().constant && !block.borrow().linked { + error!(self, + ErrorKind::InvalidProgram, + format!("Calling function '{}' before all captured variables are declared.", + block.borrow().name)); + } + let mut types = Vec::new(); for (slot, is_up, ty) in block.borrow().upvalues.iter() { if *is_up { @@ -667,10 +674,10 @@ impl VM { } Op::Link(slot) => { - println!("{:?}", self.constants); - println!("{:?} - {}", self.constant(slot), slot); match self.constant(slot).clone() { - Value::Function(_, _) => {} + Value::Function(_, block) => { + block.borrow_mut().linked = true; + } value => { error!(self, ErrorKind::TypeError(op, vec![Type::from(&value)]), @@ -733,7 +740,7 @@ impl VM { } _ => { error!(self, - ErrorKind::TypeError(op, vec![Type::from(&self.stack[new_base])]), + ErrorKind::InvalidProgram, format!("Tried to call non-function {:?}", self.stack[new_base])); } } @@ -773,7 +780,7 @@ impl VM { }); if self.print_blocks { - println!("\n [[{}]]\n", "TYPECHECK".purple()); + println!("\n [[{} - {}]]\n", "TYPECHECKING".purple(), self.frame().block.borrow().name); self.frame().block.borrow().debug_print(); } @@ -832,7 +839,7 @@ mod tests { f := fn i: int { i() }", - [ErrorKind::TypeError(_, _)]); + [ErrorKind::InvalidProgram]); test_string!(wrong_params, " f : fn -> int = fn a: int -> int {}", -- cgit v1.2.1 From 090dd8c52e4ae60742fe8bad7b74e18bb808ba0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Wed, 17 Feb 2021 21:15:54 +0100 Subject: use enums instead of 2 bools --- src/vm.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/vm.rs') diff --git a/src/vm.rs b/src/vm.rs index 20125be..5e7810d 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -557,7 +557,7 @@ impl VM { Value::Function(_, block) => { self.push(Value::Function(Vec::new(), block.clone())); - if block.borrow().constant && !block.borrow().linked { + if block.borrow().needs_linking() { error!(self, ErrorKind::InvalidProgram, format!("Calling function '{}' before all captured variables are declared.", @@ -676,7 +676,7 @@ impl VM { Op::Link(slot) => { match self.constant(slot).clone() { Value::Function(_, block) => { - block.borrow_mut().linked = true; + block.borrow_mut().link(); } value => { error!(self, -- cgit v1.2.1 From 9c8089bc3ec2a8e810682e8d1a1cab30096a157b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Fri, 19 Feb 2021 18:18:25 +0100 Subject: fix expected errors in tests --- src/vm.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/vm.rs') diff --git a/src/vm.rs b/src/vm.rs index a637e90..2d45c30 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -836,7 +836,7 @@ mod tests { i() } f", - [ErrorKind::ValueError(Op::Call(0), _)]); + [ErrorKind::InvalidProgram]); test_string!(invalid_assign, "a := 1\na = 0.1\na", [ErrorKind::TypeMismatch(Type::Int, Type::Float)]); -- cgit v1.2.1 From 930401a3ee2e45e449b1927cabee32c2c7bb705e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Fri, 19 Feb 2021 18:21:33 +0100 Subject: remove unused import --- src/vm.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/vm.rs') diff --git a/src/vm.rs b/src/vm.rs index 2d45c30..f5e5c58 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -829,7 +829,7 @@ impl VM { mod tests { mod typing { use crate::error::ErrorKind; - use crate::{test_string, Op, Type}; + use crate::{test_string, Type}; test_string!(uncallable_type, " f := fn i: int { -- cgit v1.2.1