From fa1e37aba8fb4892dbbce366570afc7ba93cd8e2 Mon Sep 17 00:00:00 2001 From: jiyinyiyong Date: Mon, 29 Nov 2021 23:40:47 +0800 Subject: [PATCH 1/3] drop unnecessary impl for Measured --- src/primes.rs | 11 ----------- src/primes/finger_list.rs | 11 ----------- 2 files changed, 22 deletions(-) diff --git a/src/primes.rs b/src/primes.rs index 9c2ce8f7..7c0cee15 100644 --- a/src/primes.rs +++ b/src/primes.rs @@ -13,9 +13,6 @@ use std::sync::Arc; use crate::primes::finger_list::FingerList; use cirru_edn::EdnKwd; -use fingertrees::measure::Measured; -use fingertrees::monoid::Sum; - static ID_GEN: AtomicUsize = AtomicUsize::new(0); pub use syntax_name::CalcitSyntax; @@ -504,14 +501,6 @@ impl PartialEq for Calcit { } } -impl Measured for Calcit { - type Measure = Sum; - - fn measure(&self) -> Self::Measure { - Sum(1) - } -} - pub const CORE_NS: &str = "calcit.core"; pub const BUILTIN_CLASSES_ENTRY: &str = "&init-builtin-classes!"; pub const GENERATED_NS: &str = "calcit.gen"; diff --git a/src/primes/finger_list.rs b/src/primes/finger_list.rs index 7fdca9c9..bc502cc0 100644 --- a/src/primes/finger_list.rs +++ b/src/primes/finger_list.rs @@ -100,17 +100,6 @@ where } } -impl Measured for FingerList -where - T: Debug + Clone, -{ - type Measure = Sum; - - fn measure(&self) -> Self::Measure { - Sum(1) - } -} - impl<'a, T> Index for FingerList where T: Clone + Eq + PartialEq + Debug + Ord + PartialOrd + Hash, From e8481b53d8ed690eb4ee3b9348954d8ec312656f Mon Sep 17 00:00:00 2001 From: jiyinyiyong Date: Tue, 30 Nov 2021 00:06:41 +0800 Subject: [PATCH 2/3] reduce usages of Arc --- src/builtins/lists.rs | 44 +++++++++++++++++++--------------------- src/builtins/maps.rs | 10 ++++----- src/builtins/meta.rs | 2 +- src/builtins/sets.rs | 3 +-- src/builtins/strings.rs | 5 ++--- src/builtins/syntax.rs | 10 ++++----- src/codegen/emit_js.rs | 4 ++-- src/codegen/gen_ir.rs | 2 +- src/data/cirru.rs | 12 +++++------ src/data/edn.rs | 2 +- src/primes.rs | 10 ++++----- src/runner.rs | 10 ++++----- src/runner/preprocess.rs | 32 ++++++++++++++--------------- 13 files changed, 71 insertions(+), 75 deletions(-) diff --git a/src/builtins/lists.rs b/src/builtins/lists.rs index 5d991df7..622d980f 100644 --- a/src/builtins/lists.rs +++ b/src/builtins/lists.rs @@ -1,7 +1,5 @@ use core::cmp::Ordering; -use std::sync::Arc; - use crate::primes::{Calcit, CalcitErr, CalcitItems, CrListWrap}; use crate::util::number::f64_to_usize; @@ -11,7 +9,7 @@ use crate::primes::finger_list::{FingerList, Size}; use crate::runner; pub fn new_list(xs: &CalcitItems) -> Result { - Ok(Calcit::List(Arc::new(xs.to_owned()))) + Ok(Calcit::List(Box::new(xs.to_owned()))) } pub fn count(xs: &CalcitItems) -> Result { @@ -56,7 +54,7 @@ pub fn slice(xs: &CalcitItems) -> Result { }; let from_idx: usize = unsafe { from.to_int_unchecked() }; - Ok(Calcit::List(Arc::new(ys.slice(from_idx, to_idx)?))) + Ok(Calcit::List(Box::new(ys.slice(from_idx, to_idx)?))) } (a, b) => CalcitErr::err_str(format!("slice expected list and indexes: {} {}", a, b)), } @@ -67,14 +65,14 @@ pub fn append(xs: &CalcitItems) -> Result { return CalcitErr::err_str(format!("append expected 2 arguments, got: {}", CrListWrap(xs.to_owned()))); } match &xs[0] { - Calcit::List(ys) => Ok(Calcit::List(Arc::new(ys.push(xs[1].to_owned())))), + Calcit::List(ys) => Ok(Calcit::List(Box::new(ys.push(xs[1].to_owned())))), a => CalcitErr::err_str(format!("append expected a list: {}", a)), } } pub fn prepend(xs: &CalcitItems) -> Result { match (xs.get(0), xs.get(1)) { - (Some(Calcit::List(ys)), Some(a)) => Ok(Calcit::List(Arc::new(ys.unshift(a.to_owned())))), + (Some(Calcit::List(ys)), Some(a)) => Ok(Calcit::List(Box::new(ys.unshift(a.to_owned())))), (Some(a), _) => CalcitErr::err_str(format!("prepend expected list, got: {}", a)), (None, _) => CalcitErr::err_str("prepend expected 2 arguments, got nothing"), } @@ -89,7 +87,7 @@ pub fn rest(xs: &CalcitItems) -> Result { if ys.is_empty() { Ok(Calcit::Nil) } else { - Ok(Calcit::List(Arc::new(ys.rest()?))) + Ok(Calcit::List(Box::new(ys.rest()?))) } } a => CalcitErr::err_str(format!("list:rest expected a list, got: {}", a)), @@ -106,7 +104,7 @@ pub fn butlast(xs: &CalcitItems) -> Result { if ys.is_empty() { Ok(Calcit::Nil) } else { - Ok(Calcit::List(Arc::new(ys.butlast()?))) + Ok(Calcit::List(Box::new(ys.butlast()?))) } } a => CalcitErr::err_str(format!("butlast expected a list, got: {}", a)), @@ -124,7 +122,7 @@ pub fn concat(xs: &CalcitItems) -> Result { return CalcitErr::err_str(format!("concat expects list arguments, got: {}", x)); } } - Ok(Calcit::List(Arc::new(ys))) + Ok(Calcit::List(Box::new(ys))) } pub fn range(xs: &CalcitItems) -> Result { @@ -144,7 +142,7 @@ pub fn range(xs: &CalcitItems) -> Result { }; if (bound - base).abs() < f64::EPSILON { - return Ok(Calcit::List(Arc::new(FingerList::new_empty()))); + return Ok(Calcit::List(Box::new(FingerList::new_empty()))); } if step == 0.0 || (bound > base && step < 0.0) || (bound < base && step > 0.0) { @@ -164,7 +162,7 @@ pub fn range(xs: &CalcitItems) -> Result { i += step; } } - Ok(Calcit::List(Arc::new(ys))) + Ok(Calcit::List(Box::new(ys))) } pub fn reverse(xs: &CalcitItems) -> Result { @@ -173,7 +171,7 @@ pub fn reverse(xs: &CalcitItems) -> Result { } match &xs[0] { Calcit::Nil => Ok(Calcit::Nil), - Calcit::List(ys) => Ok(Calcit::List(Arc::new(ys.reverse()))), + Calcit::List(ys) => Ok(Calcit::List(Box::new(ys.reverse()))), a => CalcitErr::err_str(format!("butlast expected a list, got: {}", a)), } } @@ -234,7 +232,7 @@ pub fn foldl(xs: &CalcitItems, call_stack: &CallStackList) -> Result Result Result Result { let mut xs2: Vec<&Calcit> = xs.into_iter().collect::>(); @@ -522,7 +520,7 @@ pub fn sort(xs: &CalcitItems, call_stack: &CallStackList) -> Result Err(CalcitErr::use_msg_stack( format!("sort expected list and function, got: {} {}", a, b), @@ -562,7 +560,7 @@ pub fn assoc_before(xs: &CalcitItems) -> Result { (Calcit::List(zs), Calcit::Number(n)) => match f64_to_usize(*n) { Ok(idx) => { // let ys = insert(zs, idx, xs[2].to_owned()); - Ok(Calcit::List(Arc::new(zs.assoc_before(idx, xs[2].to_owned())?))) + Ok(Calcit::List(Box::new(zs.assoc_before(idx, xs[2].to_owned())?))) } Err(e) => CalcitErr::err_str(format!("assoc-before expect usize, {}", e)), }, @@ -578,7 +576,7 @@ pub fn assoc_after(xs: &CalcitItems) -> Result { (Calcit::List(zs), Calcit::Number(n)) => match f64_to_usize(*n) { Ok(idx) => { // let ys = insert(zs, idx + 1, xs[2].to_owned()); - Ok(Calcit::List(Arc::new(zs.assoc_after(idx, xs[2].to_owned())?))) + Ok(Calcit::List(Box::new(zs.assoc_after(idx, xs[2].to_owned())?))) } Err(e) => CalcitErr::err_str(format!("assoc-after expect usize, {}", e)), }, @@ -628,9 +626,9 @@ pub fn assoc(xs: &CalcitItems) -> Result { let mut ys = (**zs).to_owned(); // ys[idx] = xs[2].to_owned(); ys = ys.assoc(idx, xs[2].to_owned())?; - Ok(Calcit::List(Arc::new(ys))) + Ok(Calcit::List(Box::new(ys))) } else { - Ok(Calcit::List(Arc::new(xs.to_owned()))) + Ok(Calcit::List(Box::new(xs.to_owned()))) } } Err(e) => CalcitErr::err_str(e), @@ -642,7 +640,7 @@ pub fn assoc(xs: &CalcitItems) -> Result { pub fn dissoc(xs: &CalcitItems) -> Result { match (xs.get(0), xs.get(1)) { (Some(Calcit::List(xs)), Some(Calcit::Number(n))) => match f64_to_usize(*n) { - Ok(at) => Ok(Calcit::List(Arc::new(xs.dissoc(at)?))), + Ok(at) => Ok(Calcit::List(Box::new(xs.dissoc(at)?))), Err(e) => CalcitErr::err_str(format!("dissoc expected number, {}", e)), }, (Some(a), ..) => CalcitErr::err_str(format!("list dissoc expected a list, got: {}", a)), @@ -678,7 +676,7 @@ pub fn distinct(xs: &CalcitItems) -> Result { zs = zs.push(y.to_owned()); } } - Ok(Calcit::List(Arc::new(zs))) + Ok(Calcit::List(Box::new(zs))) } a => CalcitErr::err_str(format!("&list:distinct expected a list, got {}", a)), } diff --git a/src/builtins/maps.rs b/src/builtins/maps.rs index 75c67161..9e794c07 100644 --- a/src/builtins/maps.rs +++ b/src/builtins/maps.rs @@ -102,7 +102,7 @@ pub fn to_pairs(xs: &CalcitItems) -> Result { Some(Calcit::Map(ys)) => { let mut zs: rpds::HashTrieSetSync = rpds::HashTrieSet::new_sync(); for (k, v) in ys { - zs.insert_mut(Calcit::List(Arc::new( + zs.insert_mut(Calcit::List(Box::new( FingerList::new_empty().push(k.to_owned()).push(v.to_owned()), ))); } @@ -111,7 +111,7 @@ pub fn to_pairs(xs: &CalcitItems) -> Result { Some(Calcit::Record(_name, fields, values)) => { let mut zs: rpds::HashTrieSetSync = rpds::HashTrieSet::new_sync(); for idx in 0..fields.len() { - zs.insert_mut(Calcit::List(Arc::new( + zs.insert_mut(Calcit::List(Box::new( FingerList::new_empty() .push(Calcit::Keyword(fields[idx].to_owned())) .push(values[idx].to_owned()), @@ -147,9 +147,9 @@ pub fn to_list(xs: &CalcitItems) -> Result { let mut ys: FingerList = FingerList::new_empty(); for (k, v) in m { let zs: FingerList = FingerList::from(&[Size(k.to_owned()), Size(v.to_owned())]); - ys = ys.push(Calcit::List(Arc::new(zs))); + ys = ys.push(Calcit::List(Box::new(zs))); } - Ok(Calcit::List(Arc::new(ys))) + Ok(Calcit::List(Box::new(ys))) } Some(a) => CalcitErr::err_str(format!("&map:to-list expected a map, got: {}", a)), None => CalcitErr::err_str("&map:to-list expected a map, got nothing"), @@ -200,7 +200,7 @@ pub fn first(xs: &CalcitItems) -> Result { match xs.get(0) { Some(Calcit::Map(ys)) => match ys.iter().next() { // TODO order may not be stable enough - Some((k, v)) => Ok(Calcit::List(Arc::new(FingerList::from(&[Size(k.to_owned()), Size(v.to_owned())])))), + Some((k, v)) => Ok(Calcit::List(Box::new(FingerList::from(&[Size(k.to_owned()), Size(v.to_owned())])))), None => Ok(Calcit::Nil), }, Some(a) => CalcitErr::err_str(format!("map:first expected a map, got: {}", a)), diff --git a/src/builtins/meta.rs b/src/builtins/meta.rs index 387d7bf3..46e9b1e3 100644 --- a/src/builtins/meta.rs +++ b/src/builtins/meta.rs @@ -49,7 +49,7 @@ pub fn type_of(xs: &CalcitItems) -> Result { } pub fn recur(xs: &CalcitItems) -> Result { - Ok(Calcit::Recur(Arc::new(xs.to_owned()))) + Ok(Calcit::Recur(Box::new(xs.to_owned()))) } pub fn format_to_lisp(xs: &CalcitItems) -> Result { diff --git a/src/builtins/sets.rs b/src/builtins/sets.rs index 33407538..9c8dd386 100644 --- a/src/builtins/sets.rs +++ b/src/builtins/sets.rs @@ -1,6 +1,5 @@ use crate::primes::finger_list::FingerList; use crate::primes::{Calcit, CalcitErr, CalcitItems}; -use std::sync::Arc; pub fn new_set(xs: &CalcitItems) -> Result { let mut ys = rpds::HashTrieSet::new_sync(); @@ -85,7 +84,7 @@ pub fn set_to_list(xs: &CalcitItems) -> Result { for x in xs { ys = ys.push(x.to_owned()); } - Ok(Calcit::List(Arc::new(ys))) + Ok(Calcit::List(Box::new(ys))) } Some(a) => CalcitErr::err_str(format!("&set:to-list expected a set: {}", a)), None => CalcitErr::err_str("&set:to-list expected 1 argument, got none"), diff --git a/src/builtins/strings.rs b/src/builtins/strings.rs index c4f65210..1a79057c 100644 --- a/src/builtins/strings.rs +++ b/src/builtins/strings.rs @@ -1,6 +1,5 @@ use std::char; use std::cmp::Ordering; -use std::sync::Arc; use crate::primes; use crate::primes::finger_list::FingerList; @@ -68,7 +67,7 @@ pub fn split(xs: &CalcitItems) -> Result { ys = ys.push(Calcit::Str(p.to_owned().into())); } } - Ok(Calcit::List(Arc::new(ys))) + Ok(Calcit::List(Box::new(ys))) } (Some(a), Some(b)) => CalcitErr::err_str(format!("split expected 2 strings, got: {} {}", a, b)), (_, _) => CalcitErr::err_str("split expected 2 arguments, got nothing"), @@ -104,7 +103,7 @@ pub fn split_lines(xs: &CalcitItems) -> Result { for line in lines { ys = ys.push(Calcit::Str(line.to_owned().into())); } - Ok(Calcit::List(Arc::new(ys))) + Ok(Calcit::List(Box::new(ys))) } Some(a) => CalcitErr::err_str(format!("split-lines expected 1 string, got: {}", a)), _ => CalcitErr::err_str("split-lines expected 1 argument, got nothing"), diff --git a/src/builtins/syntax.rs b/src/builtins/syntax.rs index 45274f2d..260c4580 100644 --- a/src/builtins/syntax.rs +++ b/src/builtins/syntax.rs @@ -39,12 +39,12 @@ pub fn defmacro(expr: &CalcitItems, _scope: &CalcitScope, def_ns: Arc) -> R (Some(a), Some(b)) => CalcitErr::err_str(format!("invalid structure for defmacro: {} {}", a, b)), _ => CalcitErr::err_str(format!( "invalid structure for defmacro: {}", - Calcit::List(Arc::new(expr.to_owned())) + Calcit::List(Box::new(expr.to_owned())) )), } } -pub fn get_raw_args(args: &CalcitItems) -> Result>>, String> { +pub fn get_raw_args(args: &CalcitItems) -> Result>>, String> { let mut xs: Vec> = vec![]; for item in args { if let Calcit::Symbol { sym, .. } = item { @@ -53,7 +53,7 @@ pub fn get_raw_args(args: &CalcitItems) -> Result>>, String> { return Err(format!("Unexpected argument: {}", item)); } } - Ok(Arc::new(xs)) + Ok(Box::new(xs)) } pub fn quote(expr: &CalcitItems, _scope: &CalcitScope, _file_ns: Arc) -> Result { @@ -115,7 +115,7 @@ pub fn syntax_let(expr: &CalcitItems, scope: &CalcitScope, file_ns: Arc, ca #[derive(Clone, PartialEq, Debug)] enum SpanResult { Single(Calcit), - Range(Arc), + Range(Box), } pub fn quasiquote(expr: &CalcitItems, scope: &CalcitScope, file_ns: Arc, call_stack: &CallStackList) -> Result { @@ -162,7 +162,7 @@ fn replace_code(c: &Calcit, scope: &CalcitScope, file_ns: Arc, call_stack: } } } - Ok(SpanResult::Single(Calcit::List(Arc::new(ret)))) + Ok(SpanResult::Single(Calcit::List(Box::new(ret)))) } }, _ => Ok(SpanResult::Single(c.to_owned())), diff --git a/src/codegen/emit_js.rs b/src/codegen/emit_js.rs index 73d4eb28..7cb7fa4f 100644 --- a/src/codegen/emit_js.rs +++ b/src/codegen/emit_js.rs @@ -388,7 +388,7 @@ fn gen_call_code( } Calcit::Symbol { sym: s, .. } | Calcit::Proc(s) => { match &**s { - ";" => Ok(format!("(/* {} */ null)", Calcit::List(Arc::new(body)))), + ";" => Ok(format!("(/* {} */ null)", Calcit::List(Box::new(body)))), "raise" => { // not core syntax, but treat as macro for better debugging experience @@ -940,7 +940,7 @@ fn uses_recur(xs: &Calcit) -> bool { fn gen_js_func( name: &str, - args: Arc>>, + args: Box>>, raw_body: &CalcitItems, ns: &str, exported: bool, diff --git a/src/codegen/gen_ir.rs b/src/codegen/gen_ir.rs index 8a3d3112..1e54163a 100644 --- a/src/codegen/gen_ir.rs +++ b/src/codegen/gen_ir.rs @@ -214,7 +214,7 @@ fn dump_items_code(xs: &CalcitItems) -> Edn { Edn::List(ys) } -fn dump_args_code(xs: &Arc>>) -> Edn { +fn dump_args_code(xs: &Box>>) -> Edn { let mut ys: Vec = Vec::with_capacity(xs.len()); for x in &**xs { ys.push(Edn::sym(&*x.to_owned())); diff --git a/src/data/cirru.rs b/src/data/cirru.rs index e1dfd35a..20e1c8b1 100644 --- a/src/data/cirru.rs +++ b/src/data/cirru.rs @@ -45,7 +45,7 @@ pub fn code_to_calcit(xs: &Cirru, ns: Arc, def: Arc) -> Result Ok(Calcit::Number(n as f64)), Err(e) => Err(format!("failed to parse hex: {} => {:?}", s, e)), }, - '\'' if s.len() > 1 => Ok(Calcit::List(Arc::new(FingerList::new2( + '\'' if s.len() > 1 => Ok(Calcit::List(Box::new(FingerList::new2( Calcit::Symbol { sym: String::from("quote").into(), ns: ns.to_owned(), @@ -60,7 +60,7 @@ pub fn code_to_calcit(xs: &Cirru, ns: Arc, def: Arc) -> Result 2 => Ok(Calcit::List(Arc::new(FingerList::new2( + '~' if s.starts_with("~@") && s.chars().count() > 2 => Ok(Calcit::List(Box::new(FingerList::new2( Calcit::Symbol { sym: String::from("~@").into(), ns: ns.to_owned(), @@ -74,7 +74,7 @@ pub fn code_to_calcit(xs: &Cirru, ns: Arc, def: Arc) -> Result 1 && !s.starts_with("~@") => Ok(Calcit::List(Arc::new(FingerList::new2( + '~' if s.chars().count() > 1 && !s.starts_with("~@") => Ok(Calcit::List(Box::new(FingerList::new2( Calcit::Symbol { sym: String::from("~").into(), ns: ns.to_owned(), @@ -88,7 +88,7 @@ pub fn code_to_calcit(xs: &Cirru, ns: Arc, def: Arc) -> Result Ok(Calcit::List(Arc::new(FingerList::new2( + '@' => Ok(Calcit::List(Box::new(FingerList::new2( Calcit::Symbol { sym: String::from("deref").into(), ns: ns.to_owned(), @@ -129,7 +129,7 @@ pub fn code_to_calcit(xs: &Cirru, ns: Arc, def: Arc) -> Result return Err(e), } } - Ok(Calcit::List(Arc::new(FingerList::from(&zs)))) + Ok(Calcit::List(Box::new(FingerList::from(&zs)))) } } } @@ -143,7 +143,7 @@ pub fn cirru_to_calcit(xs: &Cirru) -> Calcit { for y in ys { zs.push(Size(cirru_to_calcit(y))); } - Calcit::List(Arc::new(FingerList::from(&zs))) + Calcit::List(Box::new(FingerList::from(&zs))) } } } diff --git a/src/data/edn.rs b/src/data/edn.rs index 13fbbd1b..fc092030 100644 --- a/src/data/edn.rs +++ b/src/data/edn.rs @@ -104,7 +104,7 @@ pub fn edn_to_calcit(x: &Edn) -> Calcit { for x in xs { ys = ys.push(edn_to_calcit(x)) } - Calcit::List(Arc::new(ys)) + Calcit::List(Box::new(ys)) } Edn::Set(xs) => { let mut ys: rpds::HashTrieSetSync = rpds::HashTrieSet::new_sync(); diff --git a/src/primes.rs b/src/primes.rs index 7c0cee15..a7c4a4c8 100644 --- a/src/primes.rs +++ b/src/primes.rs @@ -67,8 +67,8 @@ pub enum Calcit { /// to be used by FFIs Buffer(Vec), /// not for data, but for recursion - Recur(Arc), - List(Arc), + Recur(Box), + List(Box), Set(rpds::HashTrieSetSync), Map(rpds::HashTrieMapSync), Record(EdnKwd, Arc>, Arc>), // usize of keyword id @@ -77,7 +77,7 @@ pub enum Calcit { name: Arc, // name def_ns: Arc, // ns id: Arc, // an id - args: Arc>>, // args + args: Box>>, // args body: Arc, // body }, Fn { @@ -85,7 +85,7 @@ pub enum Calcit { def_ns: Arc, // ns id: Arc, // an id scope: Arc, - args: Arc>>, // args + args: Box>>, // args body: Arc, // body }, Syntax(CalcitSyntax, Arc), // name, ns... notice that `ns` is a meta info @@ -235,7 +235,7 @@ fn buffer_bit_hex(n: u8) -> String { /// special types wraps vector of calcit data for displaying impl fmt::Display for CrListWrap { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.write_str(&format_to_lisp(&Calcit::List(Arc::new(self.0.to_owned())))) // TODO performance + f.write_str(&format_to_lisp(&Calcit::List(Box::new(self.0.to_owned())))) // TODO performance } } diff --git a/src/runner.rs b/src/runner.rs index a97c392d..e2a56c1e 100644 --- a/src/runner.rs +++ b/src/runner.rs @@ -118,7 +118,7 @@ pub fn evaluate_expr(expr: &Calcit, scope: &CalcitScope, file_ns: Arc, call ); // TODO moving to preprocess - let mut current_values = Arc::new(rest_nodes.to_owned()); + let mut current_values = Box::new(rest_nodes.to_owned()); // println!("eval macro: {} {}", x, expr.lisp_str())); // println!("macro... {} {}", x, CrListWrap(current_values.to_owned())); @@ -285,12 +285,12 @@ fn eval_symbol_from_program(sym: &str, ns: &str, call_stack: &CallStackList) -> pub fn run_fn( values: &CalcitItems, scope: &CalcitScope, - args: Arc>>, + args: Box>>, body: &CalcitItems, file_ns: Arc, call_stack: &CallStackList, ) -> Result { - let mut current_values = Arc::new(values.to_owned()); + let mut current_values = Box::new(values.to_owned()); loop { let body_scope = bind_args(args.to_owned(), ¤t_values, scope, call_stack)?; let v = evaluate_lines(body, &body_scope, file_ns.to_owned(), call_stack)?; @@ -306,7 +306,7 @@ pub fn run_fn( /// create new scope by writing new args /// notice that `&` is a mark for spreading, `?` for optional arguments pub fn bind_args( - args: Arc>>, + args: Box>>, values: &CalcitItems, base_scope: &CalcitScope, call_stack: &CallStackList, @@ -352,7 +352,7 @@ pub fn bind_args( while let Some(v) = values_pop_front() { chunk = chunk.push(v.to_owned()); } - scope.insert_mut(sym.to_owned(), Calcit::List(Arc::new(chunk))); + scope.insert_mut(sym.to_owned(), Calcit::List(Box::new(chunk))); if !is_args_empty() { return Err(CalcitErr::use_msg_stack( format!("extra args `{:?}` after spreading in `{:?}`", collected_args, args,), diff --git a/src/runner/preprocess.rs b/src/runner/preprocess.rs index 6f9a9b6b..9445ebe3 100644 --- a/src/runner/preprocess.rs +++ b/src/runner/preprocess.rs @@ -327,7 +327,7 @@ fn process_list_call( match (&head_form, &head_evaled) { (Calcit::Keyword(..), _) => { if args.len() == 1 { - let code = Calcit::List(Arc::new(FingerList::from(&[ + let code = Calcit::List(Box::new(FingerList::from(&[ Size(Calcit::Symbol { sym: String::from("get").into(), ns: String::from(primes::CORE_NS).into(), @@ -356,12 +356,12 @@ fn process_list_call( .. }), ) => { - let mut current_values = Arc::new(args.to_owned()); + let mut current_values = Box::new(args.to_owned()); // println!("eval macro: {}", primes::CrListWrap(xs.to_owned())); // println!("macro... {} {}", x, CrListWrap(current_values.to_owned())); - let code = Calcit::List(Arc::new(xs.to_owned())); + let code = Calcit::List(Box::new(xs.to_owned())); let next_stack = extend_call_stack(call_stack, def_ns.to_owned(), name.to_owned(), StackKind::Macro, code, &args); loop { @@ -447,7 +447,7 @@ fn process_list_call( let (form, _v) = preprocess_expr(a, scope_defs, file_ns.to_owned(), check_warnings, call_stack)?; ys.push(Size(form)); } - Ok((Calcit::List(Arc::new(FingerList::from(&ys))), None)) + Ok((Calcit::List(Box::new(FingerList::from(&ys))), None)) } (_, _) => { let mut ys = Vec::with_capacity(args.len() + 1); @@ -456,14 +456,14 @@ fn process_list_call( let (form, _v) = preprocess_expr(a, scope_defs, file_ns.to_owned(), check_warnings, call_stack)?; ys.push(Size(form)); } - Ok((Calcit::List(Arc::new(FingerList::from(&ys))), None)) + Ok((Calcit::List(Box::new(FingerList::from(&ys))), None)) } } } // detects arguments of top-level functions when possible fn check_fn_args( - defined_args: &Arc>>, + defined_args: &Box>>, params: &CalcitItems, file_ns: Arc, f_name: Arc, @@ -556,7 +556,7 @@ pub fn preprocess_each_items( let (form, _v) = preprocess_expr(a, scope_defs, file_ns.to_owned(), check_warnings, call_stack)?; xs = xs.push(form); } - Ok(Calcit::List(Arc::new(xs))) + Ok(Calcit::List(Box::new(xs))) } pub fn preprocess_defn( @@ -614,7 +614,7 @@ pub fn preprocess_defn( } } } - xs = xs.push(Calcit::List(Arc::new(zs))); + xs = xs.push(Calcit::List(Box::new(zs))); for (idx, a) in args.into_iter().enumerate() { if idx >= 2 { @@ -622,7 +622,7 @@ pub fn preprocess_defn( xs = xs.push(form); } } - Ok(Calcit::List(Arc::new(xs))) + Ok(Calcit::List(Box::new(xs))) } (Some(a), Some(b)) => Err(CalcitErr::use_msg_stack( format!("defn/defmacro expected name and args: {} {}", a, b), @@ -666,7 +666,7 @@ pub fn preprocess_call_let( check_symbol(sym, args, check_warnings); body_defs.insert(sym.to_owned()); let (form, _v) = preprocess_expr(a, &body_defs, file_ns.to_owned(), check_warnings, call_stack)?; - Calcit::List(Arc::new(FingerList::from(&[Size(ys[0].to_owned()), Size(form)]))) + Calcit::List(Box::new(FingerList::from(&[Size(ys[0].to_owned()), Size(form)]))) } (a, b) => { return Err(CalcitErr::use_msg_stack( @@ -701,7 +701,7 @@ pub fn preprocess_call_let( xs = xs.push(form); } } - Ok(Calcit::List(Arc::new(xs))) + Ok(Calcit::List(Box::new(xs))) } pub fn preprocess_quote( @@ -715,7 +715,7 @@ pub fn preprocess_quote( for a in args { xs = xs.push(a.to_owned()); } - Ok(Calcit::List(Arc::new(xs))) + Ok(Calcit::List(Box::new(xs))) } pub fn preprocess_defatom( @@ -733,7 +733,7 @@ pub fn preprocess_defatom( let (form, _v) = preprocess_expr(a, scope_defs, file_ns.to_owned(), check_warnings, call_stack)?; xs = xs.push(form.to_owned()); } - Ok(Calcit::List(Arc::new(xs))) + Ok(Calcit::List(Box::new(xs))) } /// need to handle experssions inside unquote snippets @@ -756,7 +756,7 @@ pub fn preprocess_quasiquote( call_stack, )?); } - Ok(Calcit::List(Arc::new(xs))) + Ok(Calcit::List(Box::new(xs))) } pub fn preprocess_quasiquote_internal( @@ -775,14 +775,14 @@ pub fn preprocess_quasiquote_internal( let (form, _) = preprocess_expr(y, scope_defs, file_ns.to_owned(), check_warnings, call_stack)?; xs = xs.push(form.to_owned()); } - Ok(Calcit::List(Arc::new(xs))) + Ok(Calcit::List(Box::new(xs))) } _ => { let mut xs: CalcitItems = FingerList::new_empty(); for y in &**ys { xs = xs.push(preprocess_quasiquote_internal(y, scope_defs, file_ns.to_owned(), check_warnings, call_stack)?.to_owned()); } - Ok(Calcit::List(Arc::new(xs))) + Ok(Calcit::List(Box::new(xs))) } }, _ => Ok(x.to_owned()), From 7f474dc74df93dfcdbaaa9306ca6dd9439be0e5d Mon Sep 17 00:00:00 2001 From: jiyinyiyong Date: Tue, 30 Nov 2021 00:15:29 +0800 Subject: [PATCH 3/3] handle warnings related to Box Vec --- src/bin/injection/mod.rs | 13 +++---------- src/builtins/lists.rs | 16 ++++++++-------- src/builtins/meta.rs | 2 +- src/builtins/refs.rs | 2 +- src/builtins/syntax.rs | 12 ++++++------ src/codegen/emit_js.rs | 15 +++------------ src/codegen/gen_ir.rs | 2 +- src/lib.rs | 2 +- src/primes.rs | 14 +++++++------- src/runner.rs | 10 +++++----- src/runner/preprocess.rs | 4 ++-- 11 files changed, 38 insertions(+), 54 deletions(-) diff --git a/src/bin/injection/mod.rs b/src/bin/injection/mod.rs index 2b294ed1..cfd2cb71 100644 --- a/src/bin/injection/mod.rs +++ b/src/bin/injection/mod.rs @@ -141,7 +141,7 @@ pub fn call_dylib_edn_fn(xs: &CalcitItems, call_stack: &CallStackList) -> Result for p in ps { real_args = real_args.push(edn_to_calcit(&p)); } - let r = runner::run_fn(&real_args, scope, args.to_owned(), body, def_ns.to_owned(), &copied_stack); + let r = runner::run_fn(&real_args, scope, args, body, def_ns.to_owned(), &copied_stack); match r { Ok(ret) => calcit_to_edn(&ret), Err(e) => { @@ -229,7 +229,7 @@ pub fn blocking_dylib_edn_fn(xs: &CalcitItems, call_stack: &CallStackList) -> Re for p in ps { real_args = real_args.push(edn_to_calcit(&p)); } - let r = runner::run_fn(&real_args, scope, args.to_owned(), body, def_ns.to_owned(), &copied_stack.clone()); + let r = runner::run_fn(&real_args, scope, args, body, def_ns.to_owned(), &copied_stack.clone()); match r { Ok(ret) => calcit_to_edn(&ret), Err(e) => { @@ -267,14 +267,7 @@ pub fn on_ctrl_c(xs: &CalcitItems, call_stack: &CallStackList) -> Result Result { for x in &**xs { let values = FingerList::from(&[Size(ret), Size(x.to_owned())]); - ret = runner::run_fn(&values, scope, args.to_owned(), body, def_ns.to_owned(), call_stack)?; + ret = runner::run_fn(&values, scope, args, body, def_ns.to_owned(), call_stack)?; } Ok(ret) } @@ -211,7 +211,7 @@ pub fn foldl(xs: &CalcitItems, call_stack: &CallStackList) -> Result { for x in xs { let values = FingerList::from(&[Size(ret), Size(x.to_owned())]); - ret = runner::run_fn(&values, scope, args.to_owned(), body, def_ns.to_owned(), call_stack)?; + ret = runner::run_fn(&values, scope, args, body, def_ns.to_owned(), call_stack)?; } Ok(ret) } @@ -234,7 +234,7 @@ pub fn foldl(xs: &CalcitItems, call_stack: &CallStackList) -> Result Result match &*x0 { Calcit::Bool(b) => { @@ -320,7 +320,7 @@ pub fn foldl_shortcut(xs: &CalcitItems, call_stack: &CallStackList) -> Result match &*x0 { Calcit::Bool(b) => { @@ -360,7 +360,7 @@ pub fn foldl_shortcut(xs: &CalcitItems, call_stack: &CallStackList) -> Result match &*x0 { Calcit::Bool(b) => { @@ -420,7 +420,7 @@ pub fn foldr_shortcut(xs: &CalcitItems, call_stack: &CallStackList) -> Result match &*x0 { Calcit::Bool(b) => { @@ -474,7 +474,7 @@ pub fn sort(xs: &CalcitItems, call_stack: &CallStackList) -> Result = xs.into_iter().collect::>(); xs2.sort_by(|a, b| -> Ordering { let values = FingerList::from(&[Size((*a).to_owned()), Size((*b).to_owned())]); - let v = runner::run_fn(&values, scope, args.to_owned(), body, def_ns.to_owned(), call_stack); + let v = runner::run_fn(&values, scope, args, body, def_ns.to_owned(), call_stack); match v { Ok(Calcit::Number(x)) if x < 0.0 => Ordering::Less, Ok(Calcit::Number(x)) if x == 0.0 => Ordering::Equal, diff --git a/src/builtins/meta.rs b/src/builtins/meta.rs index 46e9b1e3..9cd02859 100644 --- a/src/builtins/meta.rs +++ b/src/builtins/meta.rs @@ -301,7 +301,7 @@ pub fn invoke_method(name: &str, invoke_args: &CalcitItems, call_stack: &CallSta // dirty copy... Calcit::Fn { def_ns, scope, args, body, .. - } => runner::run_fn(&method_args, scope, args.to_owned(), body, def_ns.to_owned(), call_stack), + } => runner::run_fn(&method_args, scope, args, body, def_ns.to_owned(), call_stack), Calcit::Proc(proc) => builtins::handle_proc(proc, &method_args, call_stack), Calcit::Syntax(syn, _ns) => Err(CalcitErr::use_msg_stack( format!("cannot get syntax here since instance is always evaluated, got: {}", syn), diff --git a/src/builtins/refs.rs b/src/builtins/refs.rs index 2438feda..021e6eda 100644 --- a/src/builtins/refs.rs +++ b/src/builtins/refs.rs @@ -36,7 +36,7 @@ fn modify_ref(path: Arc, v: Calcit, call_stack: &CallStackList) -> Result<( def_ns, scope, args, body, .. } => { let values = FingerList::from(&[Size(v.to_owned()), Size(prev.to_owned())]); - runner::run_fn(&values, scope, args.to_owned(), body, def_ns.to_owned(), call_stack)?; + runner::run_fn(&values, scope, args, body, def_ns.to_owned(), call_stack)?; } a => { return Err(CalcitErr::use_msg_stack( diff --git a/src/builtins/syntax.rs b/src/builtins/syntax.rs index 260c4580..ec68035c 100644 --- a/src/builtins/syntax.rs +++ b/src/builtins/syntax.rs @@ -44,7 +44,7 @@ pub fn defmacro(expr: &CalcitItems, _scope: &CalcitScope, def_ns: Arc) -> R } } -pub fn get_raw_args(args: &CalcitItems) -> Result>>, String> { +pub fn get_raw_args(args: &CalcitItems) -> Result>, String> { let mut xs: Vec> = vec![]; for item in args { if let Calcit::Symbol { sym, .. } = item { @@ -53,7 +53,7 @@ pub fn get_raw_args(args: &CalcitItems) -> Result>>, String> { return Err(format!("Unexpected argument: {}", item)); } } - Ok(Box::new(xs)) + Ok(xs) } pub fn quote(expr: &CalcitItems, _scope: &CalcitScope, _file_ns: Arc) -> Result { @@ -206,7 +206,7 @@ pub fn macroexpand( // println!("macro: {:?} ... {:?}", args, rest_nodes); // keep expanding until return value is not a recur loop { - let body_scope = runner::bind_args(args.to_owned(), &rest_nodes, scope, call_stack)?; + let body_scope = runner::bind_args(&args, &rest_nodes, scope, call_stack)?; let v = runner::evaluate_lines(&body, &body_scope, def_ns.to_owned(), call_stack)?; match v { Calcit::Recur(rest_code) => { @@ -243,7 +243,7 @@ pub fn macroexpand_1( let v = runner::evaluate_expr(&xs[0], scope, file_ns.to_owned(), call_stack)?; match v { Calcit::Macro { def_ns, args, body, .. } => { - let body_scope = runner::bind_args(args.to_owned(), &xs.skip(1)?, scope, call_stack)?; + let body_scope = runner::bind_args(&args, &xs.skip(1)?, scope, call_stack)?; runner::evaluate_lines(&body, &body_scope, def_ns, call_stack) } _ => Ok(quoted_code), @@ -279,7 +279,7 @@ pub fn macroexpand_all( // println!("macro: {:?} ... {:?}", args, rest_nodes); // keep expanding until return value is not a recur loop { - let body_scope = runner::bind_args(args.to_owned(), &rest_nodes, scope, call_stack)?; + let body_scope = runner::bind_args(&args, &rest_nodes, scope, call_stack)?; let v = runner::evaluate_lines(&body, &body_scope, def_ns.to_owned(), call_stack)?; match v { Calcit::Recur(rest_code) => { @@ -336,7 +336,7 @@ pub fn call_try(expr: &CalcitItems, scope: &CalcitScope, file_ns: Arc, call def_ns, scope, args, body, .. } => { let values = FingerList::from(&[Size(err_data)]); - runner::run_fn(&values, &scope, args.to_owned(), &body, def_ns, call_stack) + runner::run_fn(&values, &scope, &args, &body, def_ns, call_stack) } Calcit::Proc(proc) => builtins::handle_proc(&proc, &FingerList::from(&[Size(err_data)]), call_stack), a => CalcitErr::err_str(format!("try expected a function handler, got: {}", a)), diff --git a/src/codegen/emit_js.rs b/src/codegen/emit_js.rs index 7cb7fa4f..2cfe687a 100644 --- a/src/codegen/emit_js.rs +++ b/src/codegen/emit_js.rs @@ -343,7 +343,7 @@ fn gen_call_code( (Some(Calcit::Symbol { sym, .. }), Some(Calcit::List(ys))) => { let func_body = body.skip(2)?; gen_stack::push_call_stack(ns, sym, StackKind::Codegen, xs.to_owned(), &FingerList::new_empty()); - let ret = gen_js_func(sym, get_raw_args(ys)?, &func_body, ns, false, local_defs, file_imports, keywords); + let ret = gen_js_func(sym, &get_raw_args(ys)?, &func_body, ns, false, local_defs, file_imports, keywords); gen_stack::pop_call_stack(); match ret { Ok(code) => Ok(format!("{}{}", return_code, code)), @@ -940,7 +940,7 @@ fn uses_recur(xs: &Calcit) -> bool { fn gen_js_func( name: &str, - args: Box>>, + args: &Vec>, raw_body: &CalcitItems, ns: &str, exported: bool, @@ -1239,16 +1239,7 @@ pub fn emit_js(entry_ns: &str, emit_path: &str) -> Result<(), String> { .. } => { gen_stack::push_call_stack(def_ns, name, StackKind::Codegen, f.to_owned(), &FingerList::new_empty()); - defs_code.push_str(&gen_js_func( - &def, - args.to_owned(), - code, - &ns, - true, - &def_names, - &file_imports, - &keywords, - )?); + defs_code.push_str(&gen_js_func(&def, args, code, &ns, true, &def_names, &file_imports, &keywords)?); gen_stack::pop_call_stack(); } Calcit::Thunk(code, _) => { diff --git a/src/codegen/gen_ir.rs b/src/codegen/gen_ir.rs index 1e54163a..c1e011cc 100644 --- a/src/codegen/gen_ir.rs +++ b/src/codegen/gen_ir.rs @@ -214,7 +214,7 @@ fn dump_items_code(xs: &CalcitItems) -> Edn { Edn::List(ys) } -fn dump_args_code(xs: &Box>>) -> Edn { +fn dump_args_code(xs: &Vec>) -> Edn { let mut ys: Vec = Vec::with_capacity(xs.len()); for x in &**xs { ys.push(Edn::sym(&*x.to_owned())); diff --git a/src/lib.rs b/src/lib.rs index 5987af17..e50e042d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -73,7 +73,7 @@ pub fn run_program(init_ns: Arc, init_def: Arc, params: CalcitItems) - Calcit::Fn { def_ns, scope, args, body, .. } => { - let result = runner::run_fn(¶ms, &scope, args.to_owned(), &body, def_ns, &rpds::List::new_sync()); + let result = runner::run_fn(¶ms, &scope, &args, &body, def_ns, &rpds::List::new_sync()); match result { Ok(v) => Ok(v), Err(failure) => { diff --git a/src/primes.rs b/src/primes.rs index a7c4a4c8..7fefddab 100644 --- a/src/primes.rs +++ b/src/primes.rs @@ -74,19 +74,19 @@ pub enum Calcit { Record(EdnKwd, Arc>, Arc>), // usize of keyword id Proc(Arc), Macro { - name: Arc, // name - def_ns: Arc, // ns - id: Arc, // an id - args: Box>>, // args - body: Arc, // body + name: Arc, // name + def_ns: Arc, // ns + id: Arc, // an id + args: Vec>, // args + body: Arc, // body }, Fn { name: Arc, // name def_ns: Arc, // ns id: Arc, // an id scope: Arc, - args: Box>>, // args - body: Arc, // body + args: Vec>, // args + body: Arc, // body }, Syntax(CalcitSyntax, Arc), // name, ns... notice that `ns` is a meta info } diff --git a/src/runner.rs b/src/runner.rs index e2a56c1e..ba60b76b 100644 --- a/src/runner.rs +++ b/src/runner.rs @@ -107,7 +107,7 @@ pub fn evaluate_expr(expr: &Calcit, scope: &CalcitScope, file_ns: Arc, call &values, ); - run_fn(&values, def_scope, args.to_owned(), body, def_ns.to_owned(), &next_stack) + run_fn(&values, def_scope, args, body, def_ns.to_owned(), &next_stack) } Calcit::Macro { name, def_ns, args, body, .. @@ -133,7 +133,7 @@ pub fn evaluate_expr(expr: &Calcit, scope: &CalcitScope, file_ns: Arc, call Ok(loop { // need to handle recursion - let body_scope = bind_args(args.to_owned(), ¤t_values, &rpds::HashTrieMap::new_sync(), call_stack)?; + let body_scope = bind_args(args, ¤t_values, &rpds::HashTrieMap::new_sync(), call_stack)?; let code = evaluate_lines(body, &body_scope, def_ns.to_owned(), &next_stack)?; match code { Calcit::Recur(ys) => { @@ -285,14 +285,14 @@ fn eval_symbol_from_program(sym: &str, ns: &str, call_stack: &CallStackList) -> pub fn run_fn( values: &CalcitItems, scope: &CalcitScope, - args: Box>>, + args: &Vec>, body: &CalcitItems, file_ns: Arc, call_stack: &CallStackList, ) -> Result { let mut current_values = Box::new(values.to_owned()); loop { - let body_scope = bind_args(args.to_owned(), ¤t_values, scope, call_stack)?; + let body_scope = bind_args(args, ¤t_values, scope, call_stack)?; let v = evaluate_lines(body, &body_scope, file_ns.to_owned(), call_stack)?; match v { Calcit::Recur(xs) => { @@ -306,7 +306,7 @@ pub fn run_fn( /// create new scope by writing new args /// notice that `&` is a mark for spreading, `?` for optional arguments pub fn bind_args( - args: Box>>, + args: &Vec>, values: &CalcitItems, base_scope: &CalcitScope, call_stack: &CallStackList, diff --git a/src/runner/preprocess.rs b/src/runner/preprocess.rs index 9445ebe3..93a7e669 100644 --- a/src/runner/preprocess.rs +++ b/src/runner/preprocess.rs @@ -367,7 +367,7 @@ fn process_list_call( loop { // need to handle recursion // println!("evaling line: {:?}", body); - let body_scope = runner::bind_args(def_args.to_owned(), ¤t_values, &rpds::HashTrieMap::new_sync(), &next_stack)?; + let body_scope = runner::bind_args(def_args, ¤t_values, &rpds::HashTrieMap::new_sync(), &next_stack)?; let code = runner::evaluate_lines(body, &body_scope, def_ns.to_owned(), &next_stack)?; match code { Calcit::Recur(ys) => { @@ -463,7 +463,7 @@ fn process_list_call( // detects arguments of top-level functions when possible fn check_fn_args( - defined_args: &Box>>, + defined_args: &Vec>, params: &CalcitItems, file_ns: Arc, f_name: Arc,