Rename term to object
This commit is contained in:
parent
bd11d756e4
commit
e117d4a898
58
src/main.rs
58
src/main.rs
@ -2,7 +2,7 @@ struct VM<'p> {
|
|||||||
program: &'p Program,
|
program: &'p Program,
|
||||||
block: u32, ip: u32,
|
block: u32, ip: u32,
|
||||||
heap: Heap<'p>,
|
heap: Heap<'p>,
|
||||||
locals: Term,
|
locals: Object,
|
||||||
stack: Vec<StackItem>,
|
stack: Vec<StackItem>,
|
||||||
status: Status
|
status: Status
|
||||||
}
|
}
|
||||||
@ -17,12 +17,12 @@ enum Status {
|
|||||||
|
|
||||||
#[derive(Clone, Copy, Debug)]
|
#[derive(Clone, Copy, Debug)]
|
||||||
enum StackItem {
|
enum StackItem {
|
||||||
Term(Term),
|
Object(Object),
|
||||||
Integer(u32),
|
Integer(u32),
|
||||||
}
|
}
|
||||||
impl StackItem {
|
impl StackItem {
|
||||||
fn term(self) -> Option<Term> {
|
fn object(self) -> Option<Object> {
|
||||||
if let StackItem::Term(t) = self { return Some(t) }
|
if let StackItem::Object(t) = self { return Some(t) }
|
||||||
return None
|
return None
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -33,7 +33,7 @@ impl StackItem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug)]
|
#[derive(Clone, Copy, Debug)]
|
||||||
struct Term { index: u32 }
|
struct Object { index: u32 }
|
||||||
struct Program {
|
struct Program {
|
||||||
blocks: Vec<BlockData>,
|
blocks: Vec<BlockData>,
|
||||||
types: Vec<TypeData>,
|
types: Vec<TypeData>,
|
||||||
@ -49,7 +49,7 @@ enum Instruction {
|
|||||||
// push an integer to the stack
|
// push an integer to the stack
|
||||||
PushInteger(u32),
|
PushInteger(u32),
|
||||||
|
|
||||||
// push a new term to the stack with the given type
|
// push a new object to the stack with the given type
|
||||||
PushNew(Type),
|
PushNew(Type),
|
||||||
|
|
||||||
// get the locals register
|
// get the locals register
|
||||||
@ -79,7 +79,7 @@ enum Op11 { Get(u32) }
|
|||||||
enum Op21 { Add, Set(u32) }
|
enum Op21 { Add, Set(u32) }
|
||||||
|
|
||||||
const TY_NULL: Type = Type { id: 0x0 };
|
const TY_NULL: Type = Type { id: 0x0 };
|
||||||
const NULL: Term = Term { index: 0 };
|
const NULL: Object = Object { index: 0 };
|
||||||
|
|
||||||
const SYSCALL_DBG: u32 = 0x1000;
|
const SYSCALL_DBG: u32 = 0x1000;
|
||||||
|
|
||||||
@ -160,7 +160,7 @@ fn host_program(p: &Program) -> Result<(), String> {
|
|||||||
|
|
||||||
impl<'p> VM<'p> {
|
impl<'p> VM<'p> {
|
||||||
pub fn start(program: &'p Program) -> VM<'p> {
|
pub fn start(program: &'p Program) -> VM<'p> {
|
||||||
let mut heap = Heap::new(program);
|
let heap = Heap::new(program);
|
||||||
|
|
||||||
VM {
|
VM {
|
||||||
program,
|
program,
|
||||||
@ -193,7 +193,7 @@ impl<'p> VM<'p> {
|
|||||||
|
|
||||||
match instruction {
|
match instruction {
|
||||||
&Instruction::PushNull => {
|
&Instruction::PushNull => {
|
||||||
self.stack.push(StackItem::Term(NULL))
|
self.stack.push(StackItem::Object(NULL))
|
||||||
}
|
}
|
||||||
|
|
||||||
&Instruction::PushInteger(int) => {
|
&Instruction::PushInteger(int) => {
|
||||||
@ -201,16 +201,16 @@ impl<'p> VM<'p> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
&Instruction::PushNew(ty) => {
|
&Instruction::PushNew(ty) => {
|
||||||
let t = self.heap.term_create(ty);
|
let t = self.heap.object_create(ty);
|
||||||
self.stack.push(StackItem::Term(t))
|
self.stack.push(StackItem::Object(t))
|
||||||
}
|
}
|
||||||
|
|
||||||
Instruction::PushLocals => {
|
Instruction::PushLocals => {
|
||||||
self.stack.push(StackItem::Term(self.locals))
|
self.stack.push(StackItem::Object(self.locals))
|
||||||
}
|
}
|
||||||
|
|
||||||
Instruction::PopLocals => {
|
Instruction::PopLocals => {
|
||||||
let t0 = self.stack.pop().ok_or("t0 must be present")?.term().ok_or("t0 must be a term")?;
|
let t0 = self.stack.pop().ok_or("t0 must be present")?.object().ok_or("t0 must be a object")?;
|
||||||
self.locals = t0;
|
self.locals = t0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -267,8 +267,8 @@ impl Op11 {
|
|||||||
fn perform(&self, heap: &mut Heap, v0: StackItem) -> Result<StackItem, &'static str> {
|
fn perform(&self, heap: &mut Heap, v0: StackItem) -> Result<StackItem, &'static str> {
|
||||||
match self {
|
match self {
|
||||||
&Op11::Get(ix) => {
|
&Op11::Get(ix) => {
|
||||||
let t0 = v0.term().ok_or("must be a term")?;
|
let t0 = v0.object().ok_or("must be a object")?;
|
||||||
Ok(heap.term_get_arg(t0, ix))
|
Ok(heap.object_get_arg(t0, ix))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -284,8 +284,8 @@ impl Op21 {
|
|||||||
Ok(StackItem::Integer(i0.wrapping_add(i1)))
|
Ok(StackItem::Integer(i0.wrapping_add(i1)))
|
||||||
}
|
}
|
||||||
&Op21::Set(ix) => {
|
&Op21::Set(ix) => {
|
||||||
let t0 = v0.term().ok_or("must be a term")?;
|
let t0 = v0.object().ok_or("must be a object")?;
|
||||||
heap.term_set_arg(t0, ix, v1);
|
heap.object_set_arg(t0, ix, v1);
|
||||||
Ok(v0)
|
Ok(v0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -347,23 +347,23 @@ impl<'p> Heap<'p> {
|
|||||||
program: p,
|
program: p,
|
||||||
data: vec![],
|
data: vec![],
|
||||||
};
|
};
|
||||||
let null = h.term_create(TY_NULL);
|
let null = h.object_create(TY_NULL);
|
||||||
assert_eq!(NULL.index, null.index);
|
assert_eq!(NULL.index, null.index);
|
||||||
h
|
h
|
||||||
}
|
}
|
||||||
|
|
||||||
fn term_create(&mut self, ty: Type) -> Term {
|
fn object_create(&mut self, ty: Type) -> Object {
|
||||||
let td = self.program.type_get(ty);
|
let td = self.program.type_get(ty);
|
||||||
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::Term(NULL)));
|
self.data.push(HeapItem::StackItem(StackItem::Object(NULL)));
|
||||||
}
|
}
|
||||||
Term { index }
|
Object { index }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn term_get_type(&self, term: Term) -> Type {
|
fn object_get_type(&self, object: Object) -> Type {
|
||||||
let header = self.data.get(term.index as usize).expect("term should have been valid");
|
let header = self.data.get(object.index as usize).expect("object should have been valid");
|
||||||
if let &HeapItem::Type(ty) = header {
|
if let &HeapItem::Type(ty) = header {
|
||||||
ty
|
ty
|
||||||
} else {
|
} else {
|
||||||
@ -371,12 +371,12 @@ impl<'p> Heap<'p> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn term_get_arg(&self, term: Term, ix: u32) -> StackItem {
|
fn object_get_arg(&self, object: Object, ix: u32) -> StackItem {
|
||||||
let ty = self.term_get_type(term);
|
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((term.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::StackItem(si) => si,
|
||||||
_ => panic!("should have been a StackItem")
|
_ => panic!("should have been a StackItem")
|
||||||
@ -386,12 +386,12 @@ impl<'p> Heap<'p> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn term_set_arg(&mut self, term: Term, ix: u32, arg: StackItem) {
|
fn object_set_arg(&mut self, object: Object, ix: u32, arg: StackItem) {
|
||||||
let ty = self.term_get_type(term);
|
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((term.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::StackItem(arg)
|
||||||
} else {
|
} else {
|
||||||
panic!("invalid ix: {}", ix)
|
panic!("invalid ix: {}", ix)
|
||||||
|
Loading…
Reference in New Issue
Block a user