diff options
| author | Gustav Sörnäs <gustav@sornas.net> | 2021-02-05 20:54:20 +0100 |
|---|---|---|
| committer | Gustav Sörnäs <gustav@sornas.net> | 2021-02-05 20:54:20 +0100 |
| commit | 826eb43254ddf8baadcd2ec1dacce594690526db (patch) | |
| tree | 1e9f09449d8eb675f6d747fc5e123393841cdaf1 /src/vm.rs | |
| parent | 636bd9f149a1df344933e1eaac39cc91ecdca706 (diff) | |
| download | sylt-826eb43254ddf8baadcd2ec1dacce594690526db.tar.gz | |
document public things and change some visibility and rename some variables and
good commit yes thank
Diffstat (limited to 'src/vm.rs')
| -rw-r--r-- | src/vm.rs | 60 |
1 files changed, 26 insertions, 34 deletions
@@ -9,7 +9,7 @@ use owo_colors::OwoColorize; use crate::{Blob, Block, Op, Prog, UpValue, Value, op}; use crate::error::{Error, ErrorKind}; use crate::RustFunction; -pub use crate::Type; +use crate::Type; macro_rules! error { ( $thing:expr, $kind:expr) => { @@ -59,8 +59,8 @@ pub struct VM { blobs: Vec<Rc<Blob>>, - print_blocks: bool, - print_ops: bool, + pub print_blocks: bool, + pub print_ops: bool, extern_functions: Vec<RustFunction>, @@ -69,12 +69,15 @@ pub struct VM { #[derive(Eq, PartialEq)] pub enum OpResult { Yield, - Continue, Done, + + // Will never be returned. + #[doc(hidden)] + Continue, } impl VM { - pub fn new() -> Self { + pub(crate) fn new() -> Self { Self { upvalues: HashMap::new(), @@ -88,16 +91,6 @@ impl VM { } } - pub fn print_blocks(mut self, b: bool) -> Self { - self.print_blocks = b; - self - } - - pub fn print_ops(mut self, b: bool) -> Self { - self.print_ops = b; - self - } - fn drop_upvalue(&mut self, slot: usize, value: Value) { if let Entry::Occupied(entry) = self.upvalues.entry(slot) { entry.get().borrow_mut().close(value); @@ -210,7 +203,7 @@ impl VM { let value = match value { Value::Function(_, block) => { let mut ups = Vec::new(); - for (slot, is_up, _) in block.borrow().ups.iter() { + for (slot, is_up, _) in block.borrow().upvalues.iter() { let up = if *is_up { if let Value::Function(local_ups, _) = &self.stack[offset] { Rc::clone(&local_ups[*slot]) @@ -253,7 +246,7 @@ impl VM { Op::Get(field) => { let inst = self.pop(); if let Value::BlobInstance(ty, values) = inst { - let slot = self.blobs[ty].name_to_field.get(&field).unwrap().0; + let slot = self.blobs[ty].fields.get(&field).unwrap().0; self.push(values.borrow()[slot].clone()); } else { error!(self, ErrorKind::RuntimeTypeError(Op::Get(field.clone()), vec![inst])); @@ -263,7 +256,7 @@ impl VM { Op::Set(field) => { let (inst, value) = self.poppop(); if let Value::BlobInstance(ty, values) = inst { - let slot = self.blobs[ty].name_to_field.get(&field).unwrap().0; + let slot = self.blobs[ty].fields.get(&field).unwrap().0; values.borrow_mut()[slot] = value; } else { error!(self, ErrorKind::RuntimeTypeError(Op::Get(field.clone()), vec![inst])); @@ -350,7 +343,7 @@ impl VM { Value::Blob(blob_id) => { let blob = &self.blobs[blob_id]; - let mut values = Vec::with_capacity(blob.name_to_field.len()); + let mut values = Vec::with_capacity(blob.fields.len()); for _ in 0..values.capacity() { values.push(Value::Nil); } @@ -417,7 +410,7 @@ impl VM { Ok(OpResult::Continue) } - pub fn print_stack(&self) { + fn print_stack(&self) { let start = self.frame().stack_offset; print!(" {:3} [", start); for (i, s) in self.stack.iter().skip(start).enumerate() { @@ -434,7 +427,7 @@ impl VM { self.frame().block.borrow().ops[self.frame().ip]); } - pub fn init(&mut self, prog: &Prog) { + pub(crate) fn init(&mut self, prog: &Prog) { let block = Rc::clone(&prog.blocks[0]); self.blobs = prog.blobs.clone(); self.extern_functions = prog.functions.clone(); @@ -451,7 +444,6 @@ impl VM { } pub fn run(&mut self) -> Result<OpResult, Error> { - if self.print_blocks { println!("\n [[{}]]\n", "RUNNING".red()); self.frame().block.borrow().debug_print(); @@ -483,7 +475,7 @@ impl VM { self.push(Value::Function(Vec::new(), block.clone())); let mut types = Vec::new(); - for (slot, is_up, ty) in block.borrow().ups.iter() { + for (slot, is_up, ty) in block.borrow().upvalues.iter() { if *is_up { types.push(ty.clone()); } else { @@ -492,11 +484,11 @@ impl VM { } let mut block_mut = block.borrow_mut(); - for (i, (_, is_up, ty)) in block_mut.ups.iter_mut().enumerate() { + for (i, (_, is_up, ty)) in block_mut.upvalues.iter_mut().enumerate() { if *is_up { continue; } let suggestion = &types[i]; - if ty.is_unkown() { + if matches!(ty, Type::Unknown) { *ty = suggestion.clone(); } else { if ty != suggestion { @@ -517,7 +509,7 @@ impl VM { Op::Get(field) => { let inst = self.pop(); if let Value::BlobInstance(ty, _) = inst { - let value = Value::from(&self.blobs[ty].name_to_field.get(&field).unwrap().1); + let value = Value::from(&self.blobs[ty].fields.get(&field).unwrap().1); self.push(value); } else { self.push(Value::Nil); @@ -530,7 +522,7 @@ impl VM { let inst = self.pop(); if let Value::BlobInstance(ty, _) = inst { - let ty = &self.blobs[ty].name_to_field.get(&field).unwrap().1; + let ty = &self.blobs[ty].fields.get(&field).unwrap().1; if ty != &Type::from(&value) { error!(self, ErrorKind::RuntimeTypeError(Op::Set(field.clone()), vec![inst])); } @@ -544,12 +536,12 @@ impl VM { } Op::ReadUpvalue(slot) => { - let value = Value::from(&self.frame().block.borrow().ups[slot].2); + let value = Value::from(&self.frame().block.borrow().upvalues[slot].2); self.push(value); } Op::AssignUpvalue(slot) => { - let var = self.frame().block.borrow().ups[slot].2.clone(); + let var = self.frame().block.borrow().upvalues[slot].2.clone(); let up = self.pop().into(); if var != up { error!(self, ErrorKind::TypeError(op, vec![var, up]), @@ -575,8 +567,8 @@ impl VM { Op::Define(ref ty) => { let top_type = self.stack.last().unwrap().into(); match (ty, top_type) { - (Type::UnknownType, top_type) - if top_type != Type::UnknownType => {} + (Type::Unknown, top_type) + if top_type != Type::Unknown => {} (a, b) if a != &b => { error!(self, ErrorKind::TypeError( @@ -595,12 +587,12 @@ impl VM { Value::Blob(blob_id) => { let blob = &self.blobs[blob_id]; - let mut values = Vec::with_capacity(blob.name_to_field.len()); + let mut values = Vec::with_capacity(blob.fields.len()); for _ in 0..values.capacity() { values.push(Value::Nil); } - for (slot, ty) in blob.name_to_field.values() { + for (slot, ty) in blob.fields.values() { values[*slot] = ty.into(); } @@ -710,7 +702,7 @@ impl VM { errors } - pub fn typecheck(&mut self, prog: &Prog) -> Result<(), Vec<Error>> { + pub(crate) fn typecheck(&mut self, prog: &Prog) -> Result<(), Vec<Error>> { let mut errors = Vec::new(); self.blobs = prog.blobs.clone(); |
