[fine] Evaluate is expressions, SO MANY BUG FIXES

This commit is contained in:
John Doty 2024-02-03 09:38:08 -08:00
parent b5b56b49a9
commit 198dc5bdb3
6 changed files with 215 additions and 42 deletions

View file

@ -47,6 +47,7 @@ type Result<T> = std::result::Result<T, VMErrorCode>;
#[derive(Clone, Debug)]
pub struct Object {
name: Rc<str>,
class_id: i64,
values: Box<[StackValue]>,
}
@ -68,6 +69,7 @@ pub enum StackValue {
Nothing,
Bool(bool),
Float(f64),
Int(i64),
String(Rc<str>),
Function(Rc<Function>),
ExternFunction(usize),
@ -146,6 +148,13 @@ impl Frame {
}
}
fn pop_int(&mut self) -> Result<i64> {
match self.pop_value()? {
StackValue::Int(v) => Ok(v),
v => Err(VMErrorCode::StackTypeMismatch(v, Type::I64)),
}
}
fn push_value(&mut self, v: StackValue) {
self.stack.push(v)
}
@ -178,6 +187,10 @@ impl Frame {
self.push_value(StackValue::Object(v));
}
fn push_int(&mut self, v: i64) {
self.push_value(StackValue::Int(v));
}
fn get_argument(&self, i: usize) -> Result<StackValue> {
self.args
.get(i)
@ -434,6 +447,7 @@ fn eval_one(
}
Instruction::NewObject(slots) => {
let class_id = f.pop_int()?;
let name = f.pop_string()?;
let mut values = Vec::with_capacity(slots);
for _ in 0..slots {
@ -442,6 +456,7 @@ fn eval_one(
let object = Object {
name,
class_id,
values: values.into(),
};
@ -451,6 +466,18 @@ fn eval_one(
let obj = f.pop_object()?;
f.push_value(obj.get_slot(slot)?);
}
Instruction::IsClass(id) => {
let value = f.pop_value()?;
match value {
StackValue::Object(o) => {
f.push_bool(o.class_id == id);
}
_ => f.push_bool(false),
}
}
Instruction::PushInt(v) => {
f.push_int(v);
}
}
Ok(Flow::Continue)
@ -469,16 +496,16 @@ pub fn eval(
let instructions = f.func.instructions();
let instruction = instructions[index];
// {
// eprint!("{index}: {instruction:?} [");
// for val in f.stack.iter().rev().take(3) {
// eprint!("{val:?} ");
// }
// if f.stack.len() > 3 {
// eprint!("...");
// }
// eprintln!("]");
// }
{
eprint!("{index}: {instruction:?} [");
for val in f.stack.iter().rev().take(3) {
eprint!("{val:?} ");
}
if f.stack.len() > 3 {
eprint!("...");
}
eprintln!("]");
}
index += 1;