feat: refactor

This commit is contained in:
2025-05-03 16:22:05 +08:00
parent 43cfb9be62
commit f78c516d17
19 changed files with 361 additions and 345 deletions

View File

@@ -1,13 +1,12 @@
use anyhow::{anyhow, Result};
use anyhow::Result;
use rpds::{HashTrieMap, HashTrieMapSync, Vector};
use crate::bytecode::{self, *};
use crate::slice::*;
use crate::value::{self, Value};
use crate::builtins::env;
use crate::bytecode::{self, *};
use crate::value::{Const, Value};
use super::env::Env;
use super::stack::{Stack, STACK_SIZE};
use super::stack::{STACK_SIZE, Stack};
use super::value::{self as vmValue, *};
use super::vmthunk::*;
@@ -17,7 +16,7 @@ pub fn run(prog: Program) -> Result<Value> {
}
pub struct VM {
thunks: Slice<VmThunk>,
thunks: Box<[VmThunk]>,
}
impl VM {
@@ -26,9 +25,7 @@ impl VM {
.into_iter()
.map(|bytecode::Thunk { opcodes }| VmThunk::new(opcodes))
.collect();
VM {
thunks,
}
VM { thunks }
}
pub fn get_thunk_value(&self, idx: usize, env: &mut Env) -> Result<VmValue> {
@@ -77,7 +74,7 @@ impl VM {
}
}
OpCode::Call { arity } => {
let mut args = Vec::with_capacity(arity);
let mut args = Vec::with_capacity(arity);
for _ in 0..arity {
args.insert(0, stack.pop()?);
}
@@ -139,9 +136,10 @@ impl VM {
.select_with_default(Symbol::new(sym), default.clone());
}
OpCode::SelectOrEmpty { sym } => {
stack
.tos_mut()?
.select_with_default(Symbol::new(sym), VmValue::AttrSet(AttrSet::new(HashTrieMapSync::new_sync())));
stack.tos_mut()?.select_with_default(
Symbol::new(sym),
VmValue::AttrSet(AttrSet::new(HashTrieMapSync::new_sync())),
);
}
OpCode::SelectDynamic => {
let mut val = stack.pop().unwrap();
@@ -160,7 +158,10 @@ impl VM {
let mut val = stack.pop().unwrap();
val.coerce_to_string();
let sym = val.unwrap_const().unwrap_string().into();
stack.tos_mut()?.select_with_default(sym, VmValue::AttrSet(AttrSet::new(HashTrieMapSync::new_sync())));
stack.tos_mut()?.select_with_default(
sym,
VmValue::AttrSet(AttrSet::new(HashTrieMapSync::new_sync())),
);
}
OpCode::HasAttr { sym } => {
stack.tos_mut()?.has_attr(Symbol::new(sym));
@@ -185,4 +186,3 @@ impl VM {
Ok(0)
}
}