rudus/src/vm.rs

88 lines
2.3 KiB
Rust
Raw Normal View History

use crate::compiler::{Chunk, Op};
use crate::parser::Ast;
use crate::spans::Spanned;
use crate::value::Value;
use chumsky::prelude::SimpleSpan;
use num_traits::FromPrimitive;
#[derive(Debug, Clone, PartialEq)]
pub struct Panic {
pub input: &'static str,
pub src: &'static str,
pub msg: String,
pub span: SimpleSpan,
pub trace: Vec<Trace>,
pub extra: String,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Trace {
pub callee: Spanned<Ast>,
pub caller: Spanned<Ast>,
pub function: Value,
pub arguments: Value,
pub input: &'static str,
pub src: &'static str,
}
pub struct Vm<'a> {
pub stack: Vec<Value>,
pub chunk: &'a Chunk<'a>,
pub ip: usize,
pub bindings: Vec<(u8, usize)>,
}
impl<'a> Vm<'a> {
pub fn new(chunk: &'a Chunk) -> Vm<'a> {
Vm {
chunk,
stack: vec![],
ip: 0,
bindings: vec![],
}
}
pub fn push(&mut self, value: Value) {
self.stack.push(value);
}
pub fn pop(&mut self) -> Value {
self.stack.pop().unwrap()
}
pub fn interpret(&mut self) -> Result<Value, Panic> {
let byte = self.chunk.bytecode[self.ip];
let op = Op::from_u8(byte).unwrap();
use Op::*;
match op {
Return => Ok(self.stack.pop().unwrap()),
Constant => {
let const_idx = self.chunk.bytecode[self.ip + 1];
let value = self.chunk.constants[const_idx as usize].clone();
self.push(value);
self.ip += 2;
self.interpret()
}
Jump => {
let jump_len = self.chunk.bytecode[self.ip + 1];
self.ip += jump_len as usize;
self.interpret()
}
JumpIfFalse => {
let jump_len = self.chunk.bytecode[self.ip + 1];
let cond = self.stack.pop().unwrap();
match cond {
Value::Nil | Value::False => {
self.ip += jump_len as usize + 2;
self.interpret()
}
_ => {
self.ip += 2;
self.interpret()
}
}
}
}
}
}