Skip to content

Commit

Permalink
Owunga
Browse files Browse the repository at this point in the history
  • Loading branch information
Quaqqer committed Jan 15, 2024
1 parent 0aace8b commit ce48dba
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
40 changes: 25 additions & 15 deletions crates/saft-bytecode/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(),
Expand Down Expand Up @@ -216,6 +222,10 @@ impl Vm {
}
Ok(())
}

pub fn get_stack(&self) -> &Vec<Value> {
&self.stack
}
}

pub enum VmItem {
Expand Down
9 changes: 9 additions & 0 deletions crates/saft/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
}
}
}
Expand Down

0 comments on commit ce48dba

Please sign in to comment.