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