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,
|
block: u32, ip: u32,
|
||||||
heap: Heap<'p>,
|
heap: Heap<'p>,
|
||||||
locals: Object,
|
locals: Object,
|
||||||
stack: Vec<StackItem>,
|
stack: Vec<Value>,
|
||||||
status: Status
|
status: Status
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -16,18 +16,18 @@ enum Status {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug)]
|
#[derive(Clone, Copy, Debug)]
|
||||||
enum StackItem {
|
enum Value {
|
||||||
Object(Object),
|
Object(Object),
|
||||||
Integer(u32),
|
Integer(u32),
|
||||||
}
|
}
|
||||||
impl StackItem {
|
impl Value {
|
||||||
fn object(self) -> Option<Object> {
|
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
|
return None
|
||||||
}
|
}
|
||||||
|
|
||||||
fn integer(self) -> Option<u32> {
|
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
|
return None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -193,20 +193,20 @@ impl<'p> VM<'p> {
|
|||||||
|
|
||||||
match instruction {
|
match instruction {
|
||||||
&Instruction::PushNull => {
|
&Instruction::PushNull => {
|
||||||
self.stack.push(StackItem::Object(NULL))
|
self.stack.push(Value::Object(NULL))
|
||||||
}
|
}
|
||||||
|
|
||||||
&Instruction::PushInteger(int) => {
|
&Instruction::PushInteger(int) => {
|
||||||
self.stack.push(StackItem::Integer(int))
|
self.stack.push(Value::Integer(int))
|
||||||
}
|
}
|
||||||
|
|
||||||
&Instruction::PushNew(ty) => {
|
&Instruction::PushNew(ty) => {
|
||||||
let t = self.heap.object_create(ty);
|
let t = self.heap.object_create(ty);
|
||||||
self.stack.push(StackItem::Object(t))
|
self.stack.push(Value::Object(t))
|
||||||
}
|
}
|
||||||
|
|
||||||
Instruction::PushLocals => {
|
Instruction::PushLocals => {
|
||||||
self.stack.push(StackItem::Object(self.locals))
|
self.stack.push(Value::Object(self.locals))
|
||||||
}
|
}
|
||||||
|
|
||||||
Instruction::PopLocals => {
|
Instruction::PopLocals => {
|
||||||
@ -264,7 +264,7 @@ impl<'p> VM<'p> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Op11 {
|
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 {
|
match self {
|
||||||
&Op11::Get(ix) => {
|
&Op11::Get(ix) => {
|
||||||
let t0 = v0.object().ok_or("must be a object")?;
|
let t0 = v0.object().ok_or("must be a object")?;
|
||||||
@ -276,12 +276,12 @@ impl Op11 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Op21 {
|
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 {
|
match self {
|
||||||
Op21::Add => {
|
Op21::Add => {
|
||||||
let i0 = v0.integer().ok_or("must be an integer")?;
|
let i0 = v0.integer().ok_or("must be an integer")?;
|
||||||
let i1 = v1.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) => {
|
&Op21::Set(ix) => {
|
||||||
let t0 = v0.object().ok_or("must be a object")?;
|
let t0 = v0.object().ok_or("must be a object")?;
|
||||||
@ -329,7 +329,7 @@ struct Heap<'p> {
|
|||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
enum HeapItem {
|
enum HeapItem {
|
||||||
Type(Type),
|
Type(Type),
|
||||||
StackItem(StackItem),
|
Value(Value),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||||
@ -357,7 +357,7 @@ impl<'p> Heap<'p> {
|
|||||||
let index = self.data.len() as u32;
|
let index = self.data.len() as u32;
|
||||||
self.data.push(HeapItem::Type(ty));
|
self.data.push(HeapItem::Type(ty));
|
||||||
for _ in 0..td.len {
|
for _ in 0..td.len {
|
||||||
self.data.push(HeapItem::StackItem(StackItem::Object(NULL)));
|
self.data.push(HeapItem::Value(Value::Object(NULL)));
|
||||||
}
|
}
|
||||||
Object { index }
|
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 ty = self.object_get_type(object);
|
||||||
let td = self.program.type_get(ty);
|
let td = self.program.type_get(ty);
|
||||||
|
|
||||||
if ix < td.len {
|
if ix < td.len {
|
||||||
let value = self.data.get((object.index + ix + 1) as usize).expect("should have been present");
|
let value = self.data.get((object.index + ix + 1) as usize).expect("should have been present");
|
||||||
match value {
|
match value {
|
||||||
&HeapItem::StackItem(si) => si,
|
&HeapItem::Value(si) => si,
|
||||||
_ => panic!("should have been a StackItem")
|
_ => panic!("should have been a Value")
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
panic!("invalid ix: {}", ix)
|
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 ty = self.object_get_type(object);
|
||||||
let td = self.program.type_get(ty);
|
let td = self.program.type_get(ty);
|
||||||
|
|
||||||
if ix < td.len {
|
if ix < td.len {
|
||||||
let slot = self.data.get_mut((object.index + ix + 1) as usize).expect("should have been present");
|
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 {
|
} else {
|
||||||
panic!("invalid ix: {}", ix)
|
panic!("invalid ix: {}", ix)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user