Rename StackItem to Value
This commit is contained in:
parent
e117d4a898
commit
c1834805dd
38
src/main.rs
38
src/main.rs
@ -3,7 +3,7 @@ struct VM<'p> {
|
||||
block: u32, ip: u32,
|
||||
heap: Heap<'p>,
|
||||
locals: Object,
|
||||
stack: Vec<StackItem>,
|
||||
stack: Vec<Value>,
|
||||
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<Object> {
|
||||
if let StackItem::Object(t) = self { return Some(t) }
|
||||
if let Value::Object(t) = self { return Some(t) }
|
||||
return None
|
||||
}
|
||||
|
||||
fn integer(self) -> Option<u32> {
|
||||
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<StackItem, &'static str> {
|
||||
fn perform(&self, heap: &mut Heap, v0: Value) -> Result<Value, &'static str> {
|
||||
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<StackItem, &'static str> {
|
||||
fn perform(&self, heap: &mut Heap, v0: Value, v1: Value) -> Result<Value, &'static str> {
|
||||
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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user