From d04d46c9057a5ca89e543172a2c2846333b64c7e Mon Sep 17 00:00:00 2001 From: imxyy_soope_ Date: Sat, 10 May 2025 20:13:00 +0800 Subject: [PATCH] chore: fmt --- src/bytecode.rs | 20 +++++--------------- src/compile.rs | 8 +++----- src/ir.rs | 21 +++++++++++---------- src/ty/internal/func.rs | 22 ++++++++++++++++------ src/ty/internal/mod.rs | 16 ++++++---------- src/vm/stack.rs | 7 ++++--- src/vm/vm.rs | 8 ++++---- 7 files changed, 49 insertions(+), 53 deletions(-) diff --git a/src/bytecode.rs b/src/bytecode.rs index 83b58ed..69cdd66 100644 --- a/src/bytecode.rs +++ b/src/bytecode.rs @@ -4,17 +4,7 @@ use crate::ty::internal::{Const, Func}; type Slice = Box<[T]>; -pub type ThunkIdx = usize; -pub type FuncIdx = usize; pub type OpCodes = Slice; -pub type Consts = Slice; -pub type Thunks = Slice; -pub type Funcs = Slice; - -#[derive(Debug, Clone)] -pub struct Thunk { - pub opcodes: OpCodes, -} #[derive(Debug, Clone)] pub enum OpCode { @@ -23,9 +13,9 @@ pub enum OpCode { /// load a dynamic var onto stack LookUp { sym: EcoString }, /// load a thunk lazily onto stack - LoadThunk { idx: ThunkIdx }, + LoadThunk { idx: usize }, /// load a thunk onto stack and force its value - LoadValue { idx: ThunkIdx }, + LoadValue { idx: usize }, /// force TOS to value ForceValue, @@ -33,7 +23,7 @@ pub enum OpCode { /// Example: __add 1 2 => [ LookUp("__add") Const(1) Const(2) Call(2) ] Call { arity: usize }, /// make a function - Func { idx: ThunkIdx }, + Func { idx: usize }, /// consume 1 element, assert TOS is true Assert, @@ -107,6 +97,6 @@ pub enum UnOp { #[derive(Debug)] pub struct Program { pub top_level: OpCodes, - pub thunks: Thunks, - pub funcs: Box<[Func]> + pub thunks: Slice, + pub funcs: Slice, } diff --git a/src/compile.rs b/src/compile.rs index f25a581..8dff552 100644 --- a/src/compile.rs +++ b/src/compile.rs @@ -13,9 +13,7 @@ pub fn compile(downgraded: ir::Downgraded) -> Program { thunks: downgraded .thunks .into_iter() - .map(|thunk| Thunk { - opcodes: Compiler::new().compile(thunk), - }) + .map(|thunk| Compiler::new().compile(thunk)) .collect(), funcs: downgraded .funcs @@ -23,9 +21,9 @@ pub fn compile(downgraded: ir::Downgraded) -> Program { .map(|func| Func { env: None, param: func.param.into(), - opcodes: Compiler::new().compile(*func.body) + opcodes: Compiler::new().compile(*func.body), }) - .collect() + .collect(), } } diff --git a/src/ir.rs b/src/ir.rs index 9267e95..5504dea 100644 --- a/src/ir.rs +++ b/src/ir.rs @@ -3,11 +3,10 @@ use std::collections::HashMap; use ecow::EcoString; use rnix::ast::{self, Expr}; -use crate::bytecode::{Consts, ThunkIdx, FuncIdx}; use crate::compile::*; +use crate::error::*; #[cfg(feature = "jit")] use crate::jit::*; -use crate::error::*; use crate::ty::internal as i; pub fn downgrade(expr: Expr) -> Result { @@ -17,7 +16,7 @@ pub fn downgrade(expr: Expr) -> Result { top_level: ir, consts: ctx.consts.into(), thunks: ctx.thunks.into(), - funcs: ctx.funcs.into() + funcs: ctx.funcs.into(), }) } @@ -115,7 +114,7 @@ ir! { UnOp => { rhs: Box, kind: UnOpKind }, Select => { expr: Box, attrpath: Vec, default: Option> }, If => { cond: Box, consq: Box, alter: Box }, - LoadFunc => { idx: FuncIdx }, + LoadFunc => { idx: usize }, Call => { func: Box, args: Vec }, Let => { attrs: Attrs, expr: Box }, @@ -125,7 +124,7 @@ ir! { Const => { value: i::Const }, Var => { sym: EcoString }, #[derive(Copy)] - Thunk => { idx: ThunkIdx }, + Thunk => { idx: usize }, Path => { expr: Box }, } @@ -141,9 +140,9 @@ pub struct DowngradeContext { pub struct Downgraded { pub top_level: Ir, - pub consts: Consts, + pub consts: Box<[i::Const]>, pub thunks: Box<[Ir]>, - pub funcs: Box<[Func]> + pub funcs: Box<[Func]>, } impl DowngradeContext { @@ -174,9 +173,11 @@ impl Attrs { .get_mut(&ident) .unwrap() .downcast_mut() - .ok_or_else(|| Error::DowngradeError(format!( - r#""{ident}" already exsists in this set"# - ))) + .ok_or_else(|| { + Error::DowngradeError(format!( + r#""{ident}" already exsists in this set"# + )) + }) .and_then(|attrs: &mut Attrs| attrs._insert(path, name, value)) } else { let mut attrs = Attrs { diff --git a/src/ty/internal/func.rs b/src/ty/internal/func.rs index 96bd2e2..bc4feee 100644 --- a/src/ty/internal/func.rs +++ b/src/ty/internal/func.rs @@ -2,17 +2,17 @@ use ecow::EcoString; use itertools::Itertools; use rpds::HashTrieMap; -use crate::bytecode::{OpCodes, ThunkIdx}; +use crate::bytecode::OpCodes; use crate::error::Result; +use crate::ir; use crate::ty::internal::Value; use crate::vm::{CapturedEnv, VM}; -use crate::ir; #[derive(Debug, Clone)] pub enum Param { Ident(EcoString), Formals { - formals: Vec<(EcoString, Option)>, + formals: Vec<(EcoString, Option)>, ellipsis: bool, alias: Option, }, @@ -22,8 +22,18 @@ impl From for Param { fn from(value: ir::Param) -> Self { match value { ir::Param::Ident(ident) => Param::Ident(ident), - ir::Param::Formals { formals, ellipsis, alias } => - Param::Formals { formals: formals.into_iter().map(|(sym, default)| (sym, default.map(|default| default.idx))).collect(), ellipsis, alias } + ir::Param::Formals { + formals, + ellipsis, + alias, + } => Param::Formals { + formals: formals + .into_iter() + .map(|(sym, default)| (sym, default.map(|default| default.idx))) + .collect(), + ellipsis, + alias, + }, } } } @@ -32,7 +42,7 @@ impl From for Param { pub struct Func { pub env: Option, pub param: Param, - pub opcodes: OpCodes + pub opcodes: OpCodes, } impl Func { diff --git a/src/ty/internal/mod.rs b/src/ty/internal/mod.rs index 14595e9..6666be6 100644 --- a/src/ty/internal/mod.rs +++ b/src/ty/internal/mod.rs @@ -362,18 +362,14 @@ impl Value { pub fn select(&mut self, sym: Symbol) -> &mut Self { if let Value::AttrSet(attrs) = self { - let val = attrs - .select(sym.clone()) - .unwrap_or_else(|| Value::Catchable(c::Catchable::new(Some(format!( - "{sym:?} not found" - ))))); + let val = attrs.select(sym.clone()).unwrap_or_else(|| { + Value::Catchable(c::Catchable::new(Some(format!("{sym:?} not found")))) + }); *self = val; } else if let Value::RecAttrSet(attrs) = self { - let val = attrs - .select(sym.clone()) - .unwrap_or_else(|| Value::Catchable(c::Catchable::new(Some(format!( - "{sym:?} not found" - ))))); + let val = attrs.select(sym.clone()).unwrap_or_else(|| { + Value::Catchable(c::Catchable::new(Some(format!("{sym:?} not found")))) + }); *self = val; } else { *self = Value::Catchable(Catchable::new(Some(format!( diff --git a/src/vm/stack.rs b/src/vm/stack.rs index dd038fb..5887cdf 100644 --- a/src/vm/stack.rs +++ b/src/vm/stack.rs @@ -32,9 +32,10 @@ impl Stack { pub fn push(&mut self, item: Value) -> Result<()> { self.items .get_mut(self.top) - .map_or_else(|| Err(Error::EvalError("stack overflow".to_string())), |ok| { - Ok(ok) - })? + .map_or_else( + || Err(Error::EvalError("stack overflow".to_string())), + |ok| Ok(ok), + )? .write(item); self.top += 1; Ok(()) diff --git a/src/vm/vm.rs b/src/vm/vm.rs index 8e98d3c..e16d21e 100644 --- a/src/vm/vm.rs +++ b/src/vm/vm.rs @@ -1,7 +1,7 @@ use std::sync::Arc; use crate::builtins::env; -use crate::bytecode::{self, BinOp, Funcs, OpCode, OpCodes, Program, Thunks, UnOp}; +use crate::bytecode::{BinOp, OpCode, OpCodes, Program, UnOp}; use crate::error::*; use crate::ty::common::Symbol; use crate::ty::internal::*; @@ -17,14 +17,14 @@ pub fn run(prog: Program) -> Result { pub struct VM { thunks: Box<[Thunk]>, - funcs: Funcs, + funcs: Box<[Func]>, } impl VM { - fn new(thunks: Thunks, funcs: Funcs) -> Self { + fn new(thunks: Box<[OpCodes]>, funcs: Box<[Func]>) -> Self { let thunks = thunks .into_iter() - .map(|bytecode::Thunk { opcodes }| Thunk::new(opcodes)) + .map(|opcodes| Thunk::new(opcodes)) .collect(); VM { thunks, funcs } }