1
0

Change VM's final value to last modified register

This commit is contained in:
Jeff 2024-09-22 12:57:58 -04:00
parent 31bb7eaffc
commit 573e5ae470
2 changed files with 6 additions and 2 deletions

View File

@ -16,6 +16,7 @@ pub fn run(source: &str) -> Result<Option<Value>, DustError> {
pub struct Vm { pub struct Vm {
chunk: Chunk, chunk: Chunk,
ip: usize, ip: usize,
last_modified: u8,
register_stack: Vec<Option<Value>>, register_stack: Vec<Option<Value>>,
} }
@ -26,6 +27,7 @@ impl Vm {
Self { Self {
chunk, chunk,
ip: 0, ip: 0,
last_modified: 0,
register_stack: Vec::new(), register_stack: Vec::new(),
} }
} }
@ -303,7 +305,7 @@ impl Vm {
} }
} }
let final_value = self.pop(Span(0, 0))?; let final_value = self.take(self.last_modified, Span(0, 0))?;
Ok(Some(final_value)) Ok(Some(final_value))
} }
@ -312,6 +314,8 @@ impl Vm {
if self.register_stack.len() == Self::STACK_LIMIT { if self.register_stack.len() == Self::STACK_LIMIT {
Err(VmError::StackOverflow { position }) Err(VmError::StackOverflow { position })
} else { } else {
self.last_modified = index;
let index = index as usize; let index = index as usize;
while index >= self.register_stack.len() { while index >= self.register_stack.len() {

View File

@ -4,7 +4,7 @@ use dust_lang::*;
fn long_math() { fn long_math() {
assert_eq!( assert_eq!(
run("1 + 2 * 3 - 4 / 2"), run("1 + 2 * 3 - 4 / 2"),
Ok(Some(Value::integer(2).into_reference())) Ok(Some(Value::integer(5).into_reference()))
); );
} }