From c1834805ddfbaa5f07a23e39c395a547125782ce Mon Sep 17 00:00:00 2001 From: Nyeogmi Date: Sat, 17 Feb 2024 15:18:59 -0800 Subject: [PATCH] Rename StackItem to Value --- src/main.rs | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/main.rs b/src/main.rs index 6ab86e5..d49a190 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,7 @@ struct VM<'p> { block: u32, ip: u32, heap: Heap<'p>, locals: Object, - stack: Vec, + stack: Vec, status: Status } @@ -16,18 +16,18 @@ enum Status { } #[derive(Clone, Copy, Debug)] -enum StackItem { +enum Value { Object(Object), Integer(u32), } -impl StackItem { +impl Value { fn object(self) -> Option { - if let StackItem::Object(t) = self { return Some(t) } + if let Value::Object(t) = self { return Some(t) } return None } fn integer(self) -> Option { - if let StackItem::Integer(u) = self { return Some(u) } + if let Value::Integer(u) = self { return Some(u) } return None } } @@ -193,20 +193,20 @@ impl<'p> VM<'p> { match instruction { &Instruction::PushNull => { - self.stack.push(StackItem::Object(NULL)) + self.stack.push(Value::Object(NULL)) } &Instruction::PushInteger(int) => { - self.stack.push(StackItem::Integer(int)) + self.stack.push(Value::Integer(int)) } &Instruction::PushNew(ty) => { let t = self.heap.object_create(ty); - self.stack.push(StackItem::Object(t)) + self.stack.push(Value::Object(t)) } Instruction::PushLocals => { - self.stack.push(StackItem::Object(self.locals)) + self.stack.push(Value::Object(self.locals)) } Instruction::PopLocals => { @@ -264,7 +264,7 @@ impl<'p> VM<'p> { } impl Op11 { - fn perform(&self, heap: &mut Heap, v0: StackItem) -> Result { + fn perform(&self, heap: &mut Heap, v0: Value) -> Result { match self { &Op11::Get(ix) => { let t0 = v0.object().ok_or("must be a object")?; @@ -276,12 +276,12 @@ impl Op11 { } impl Op21 { - fn perform(&self, heap: &mut Heap, v0: StackItem, v1: StackItem) -> Result { + fn perform(&self, heap: &mut Heap, v0: Value, v1: Value) -> Result { match self { Op21::Add => { let i0 = v0.integer().ok_or("must be an integer")?; let i1 = v1.integer().ok_or("must be an integer")?; - Ok(StackItem::Integer(i0.wrapping_add(i1))) + Ok(Value::Integer(i0.wrapping_add(i1))) } &Op21::Set(ix) => { let t0 = v0.object().ok_or("must be a object")?; @@ -329,7 +329,7 @@ struct Heap<'p> { #[derive(Debug)] enum HeapItem { Type(Type), - StackItem(StackItem), + Value(Value), } #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -357,7 +357,7 @@ impl<'p> Heap<'p> { let index = self.data.len() as u32; self.data.push(HeapItem::Type(ty)); for _ in 0..td.len { - self.data.push(HeapItem::StackItem(StackItem::Object(NULL))); + self.data.push(HeapItem::Value(Value::Object(NULL))); } Object { index } } @@ -371,28 +371,28 @@ impl<'p> Heap<'p> { } } - fn object_get_arg(&self, object: Object, ix: u32) -> StackItem { + fn object_get_arg(&self, object: Object, ix: u32) -> Value { let ty = self.object_get_type(object); let td = self.program.type_get(ty); if ix < td.len { let value = self.data.get((object.index + ix + 1) as usize).expect("should have been present"); match value { - &HeapItem::StackItem(si) => si, - _ => panic!("should have been a StackItem") + &HeapItem::Value(si) => si, + _ => panic!("should have been a Value") } } else { panic!("invalid ix: {}", ix) } } - fn object_set_arg(&mut self, object: Object, ix: u32, arg: StackItem) { + fn object_set_arg(&mut self, object: Object, ix: u32, arg: Value) { let ty = self.object_get_type(object); let td = self.program.type_get(ty); if ix < td.len { let slot = self.data.get_mut((object.index + ix + 1) as usize).expect("should have been present"); - *slot = HeapItem::StackItem(arg) + *slot = HeapItem::Value(arg) } else { panic!("invalid ix: {}", ix) }