From 0f25e98b903f6608d09ad8bfd9ca5d00fec4cec6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Wed, 10 Feb 2021 20:49:06 +0100 Subject: simplify frames --- src/compiler.rs | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/compiler.rs b/src/compiler.rs index 7f0d32d..1f3d9d8 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -132,10 +132,17 @@ struct Frame { stack: Vec, upvalues: Vec, scope: usize, - variables_below: usize, } impl Frame { + fn new() -> Self { + Self { + stack: Vec::new(), + upvalues: Vec::new(), + scope: 0, + } + } + fn find_local(&self, name: &str) -> Option { for var in self.stack.iter().rev() { if var.name == name && var.active { @@ -189,12 +196,7 @@ pub(crate) struct Compiler { macro_rules! push_frame { ($compiler:expr, $block:expr, $code:tt) => { { - $compiler.frames.push(Frame { - stack: Vec::new(), - upvalues: Vec::new(), - scope: 0, - variables_below: $compiler.frame().variables_below + $compiler.stack().len(), - }); + $compiler.frames.push(Frame::new()); // Return value stored as a variable $compiler.define_variable("", Type::Unknown, &mut $block).unwrap(); @@ -240,12 +242,7 @@ impl Compiler { tokens, current_file: PathBuf::from(current_file), - frames: vec![Frame { - stack: Vec::new(), - upvalues: Vec::new(), - scope: 0, - variables_below: 0, - }], + frames: vec![Frame::new()], panic: false, errors: vec![], -- cgit v1.2.1