From ce48dbad9b6f42fe76bd872a69af5fae38275f6d Mon Sep 17 00:00:00 2001 From: Empa Date: Mon, 15 Jan 2024 20:25:39 +0100 Subject: [PATCH] Owunga --- crates/saft-bytecode/src/vm.rs | 40 +++++++++++++++++++++------------- crates/saft/src/main.rs | 9 ++++++++ 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/crates/saft-bytecode/src/vm.rs b/crates/saft-bytecode/src/vm.rs index 295a44e..aeabf4b 100644 --- a/crates/saft-bytecode/src/vm.rs +++ b/crates/saft-bytecode/src/vm.rs @@ -150,21 +150,27 @@ impl Vm { } Op::Not => self.unary(|a| a.not(), s, "not")?, Op::Negate => self.unary(|a| a.neg(), s, "negation")?, - Op::Add => self.binop(|a, b| a.add(&b), s, "add")?, - Op::Pow => self.binop(|a, b| a.pow(&b), s, "pow")?, - Op::IDiv => self.binop(|a, b| a.idiv(&b), s, "integer division")?, - Op::Div => self.binop(|a, b| a.div(&b), s, "division")?, - Op::Mul => self.binop(|a, b| a.mul(&b), s, "multiplication")?, - Op::Sub => self.binop(|a, b| a.sub(&b), s, "subtraction")?, - Op::And => self.binop(|a, b| a.add(&b), s, "addition")?, - Op::Or => self.binop(|a, b| a.or(&b).map(Into::into), s, "or")?, - Op::Lt => self.binop(|a, b| a.lt(&b).map(Into::into), s, "less than")?, - Op::Le => self.binop(|a, b| a.le(&b).map(Into::into), s, "less or equal")?, - Op::Gt => self.binop(|a, b| a.gt(&b).map(Into::into), s, "greater than")?, - Op::Ge => self.binop(|a, b| a.ge(&b).map(Into::into), s, "greater or equal")?, - Op::Eq => self.binop(|a, b| a.eq(&b).map(Into::into), s, "equal")?, - Op::Ne => self.binop(|a, b| a.ne(&b).map(Into::into), s, "not equal")?, - Op::TrailPop(_) => todo!(), + Op::Add => self.binop(|a, b| a.add(b), s, "add")?, + Op::Pow => self.binop(|a, b| a.pow(b), s, "pow")?, + Op::IDiv => self.binop(|a, b| a.idiv(b), s, "integer division")?, + Op::Div => self.binop(|a, b| a.div(b), s, "division")?, + Op::Mul => self.binop(|a, b| a.mul(b), s, "multiplication")?, + Op::Sub => self.binop(|a, b| a.sub(b), s, "subtraction")?, + Op::And => self.binop(|a, b| a.add(b), s, "addition")?, + Op::Or => self.binop(|a, b| a.or(b).map(Into::into), s, "or")?, + Op::Lt => self.binop(|a, b| a.lt(b).map(Into::into), s, "less than")?, + Op::Le => self.binop(|a, b| a.le(b).map(Into::into), s, "less or equal")?, + Op::Gt => self.binop(|a, b| a.gt(b).map(Into::into), s, "greater than")?, + Op::Ge => self.binop(|a, b| a.ge(b).map(Into::into), s, "greater or equal")?, + Op::Eq => self.binop(|a, b| a.eq(b).map(Into::into), s, "equal")?, + Op::Ne => self.binop(|a, b| a.ne(b).map(Into::into), s, "not equal")?, + Op::TrailPop(n) => { + let v = self.stack.pop().unwrap(); + for _ in 0..*n { + self.stack.pop().unwrap(); + } + self.stack.push(v); + } Op::Call(_) => todo!(), Op::Index => todo!(), Op::Vec(_) => todo!(), @@ -216,6 +222,10 @@ impl Vm { } Ok(()) } + + pub fn get_stack(&self) -> &Vec { + &self.stack + } } pub enum VmItem { diff --git a/crates/saft/src/main.rs b/crates/saft/src/main.rs index e666bd9..2b1be10 100644 --- a/crates/saft/src/main.rs +++ b/crates/saft/src/main.rs @@ -137,12 +137,21 @@ fn interpret_stmt(s: &str) { }; let mut vm = Vm::new(vec![]); + match vm.interpret_chunk(Rc::new(chunk)) { Ok(()) => {} Err(err) => { term::emit(&mut writer.lock(), &config, &files, &err.diagnostic(id)).unwrap(); } }; + + let stack = vm.get_stack(); + if stack.is_empty() { + eprintln!( + "Stack was not zero after execution, something has gone wrong...: {:?}", + stack + ); + } } } }