return register now an 8-member array

This commit is contained in:
Scott Richmond 2025-05-26 09:16:47 -04:00
parent 1e1593298d
commit db7eb5965d
4 changed files with 33 additions and 12 deletions

View File

@ -870,9 +870,10 @@ impl Compiler {
self.leave_loop(); self.leave_loop();
} }
Recur(_) => { Recur(args) => {
// algo // algo
// visit each member of the arguments // visit each member of the arguments
// then store those in the return register // then store those in the return register
// then pop back to loop stack root // then pop back to loop stack root
// then jump to loop start // then jump to loop start

View File

@ -74,9 +74,7 @@ pub fn run(src: &'static str) {
pub fn main() { pub fn main() {
env::set_var("RUST_BACKTRACE", "1"); env::set_var("RUST_BACKTRACE", "1");
let src = " let src = "
repeat 4.8 { 123
true
}
"; ";
run(src); run(src);
} }

View File

@ -25,6 +25,7 @@ impl LFn {
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub enum Value { pub enum Value {
Nothing,
Nil, Nil,
True, True,
False, False,
@ -44,6 +45,7 @@ impl std::fmt::Display for Value {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
use Value::*; use Value::*;
match self { match self {
Nothing => write!(f, "_"),
Nil => write!(f, "nil"), Nil => write!(f, "nil"),
True => write!(f, "true"), True => write!(f, "true"),
False => write!(f, "false"), False => write!(f, "false"),
@ -130,6 +132,7 @@ impl Value {
pub fn type_of(&self) -> &'static str { pub fn type_of(&self) -> &'static str {
use Value::*; use Value::*;
match self { match self {
Nothing => unreachable!(),
Nil => "nil", Nil => "nil",
True => "bool", True => "bool",
False => "bool", False => "bool",

View File

@ -34,7 +34,7 @@ pub struct Vm<'a> {
pub stack: Vec<Value>, pub stack: Vec<Value>,
pub chunk: &'a Chunk, pub chunk: &'a Chunk,
pub ip: usize, pub ip: usize,
pub return_register: Value, pub return_register: [Value; 8],
pub matches: bool, pub matches: bool,
pub match_depth: u8, pub match_depth: u8,
pub result: Option<Result<Value, Panic>>, pub result: Option<Result<Value, Panic>>,
@ -46,7 +46,16 @@ impl<'a> Vm<'a> {
chunk, chunk,
stack: vec![], stack: vec![],
ip: 0, ip: 0,
return_register: Value::Nil, return_register: [
Value::Nothing,
Value::Nothing,
Value::Nothing,
Value::Nothing,
Value::Nothing,
Value::Nothing,
Value::Nothing,
Value::Nothing,
],
matches: false, matches: false,
match_depth: 0, match_depth: 0,
result: None, result: None,
@ -72,7 +81,13 @@ impl<'a> Vm<'a> {
.map(|val| val.to_string()) .map(|val| val.to_string())
.collect::<Vec<_>>() .collect::<Vec<_>>()
.join("|"); .join("|");
println!("{:04}: [{inner}] {}", self.ip, self.return_register); let register = self
.return_register
.iter()
.map(|val| val.to_string())
.collect::<Vec<_>>()
.join(",");
println!("{:04}: [{inner}] ({register})", self.ip);
} }
fn print_debug(&self) { fn print_debug(&self) {
@ -174,14 +189,18 @@ impl<'a> Vm<'a> {
self.ip += 2; self.ip += 2;
} }
Store => { Store => {
self.return_register = self.pop(); self.return_register[0] = self.pop();
self.push(Value::Nil); self.push(Value::Nothing);
self.ip += 1; self.ip += 1;
} }
Load => { Load => {
let mut value = Value::Nil; let mut i = 0;
swap(&mut self.return_register, &mut value); while i < 8 && self.return_register[i] != Value::Nothing {
let mut value = Value::Nothing;
swap(&mut self.return_register[i], &mut value);
self.push(value); self.push(value);
i += 1;
}
self.ip += 1; self.ip += 1;
} }
ResetMatch => { ResetMatch => {