From ce3694e21a7e311da57abf009fd593978a60137c Mon Sep 17 00:00:00 2001 From: jiyinyiyong Date: Thu, 13 May 2021 01:55:23 +0800 Subject: [PATCH] fix preprocessing inside quasiquote; bump 0.3.13 --- Cargo.lock | 2 +- Cargo.toml | 2 +- package.json | 2 +- src/cirru/calcit-core.cirru | 2 +- src/runner/preprocess.rs | 50 ++++++++++++++++++++++++++++++++++++- 5 files changed, 53 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fd1cfc8d..8e1b4aa5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -81,7 +81,7 @@ dependencies = [ [[package]] name = "calcit_runner" -version = "0.3.13-a1" +version = "0.3.13" dependencies = [ "chrono", "cirru_edn", diff --git a/Cargo.toml b/Cargo.toml index a2d7f832..05e39ab1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "calcit_runner" -version = "0.3.13-a1" +version = "0.3.13" authors = ["jiyinyiyong "] edition = "2018" license = "MIT" diff --git a/package.json b/package.json index b9ae576f..f44a364d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@calcit/procs", - "version": "0.3.13-a1", + "version": "0.3.13", "main": "./lib/calcit.procs.js", "devDependencies": { "@types/node": "^15.0.1", diff --git a/src/cirru/calcit-core.cirru b/src/cirru/calcit-core.cirru index eca6922f..0c64a8c7 100644 --- a/src/cirru/calcit-core.cirru +++ b/src/cirru/calcit-core.cirru @@ -316,7 +316,7 @@ &let (~v# ~value) if (&= (first ~v#) ~k) let - ~ $ map-indexed (rest pattern) $ defn args% (idx x) + ~ $ map-indexed (rest pattern) $ fn (idx x) [] x $ quasiquote nth ~v# (~ (inc idx)) , ~branch diff --git a/src/runner/preprocess.rs b/src/runner/preprocess.rs index f09a74df..10f7aeb2 100644 --- a/src/runner/preprocess.rs +++ b/src/runner/preprocess.rs @@ -229,7 +229,10 @@ fn process_list_call( } } (Calcit::Syntax(name, name_ns), _) => match name.as_str() { - "quote-replace" | "quasiquote" => Ok((Calcit::List(xs.clone()), None)), + "quote-replace" | "quasiquote" => Ok(( + preprocess_quasiquote(&name, &name_ns, args, scope_defs, file_ns, program_code)?, + None, + )), "defn" | "defmacro" => Ok(( preprocess_defn(&name, &name_ns, args, scope_defs, file_ns, program_code)?, None, @@ -400,3 +403,48 @@ pub fn preprocess_defatom( } Ok(Calcit::List(xs)) } + +/// need to handle experssions inside unquote snippets +pub fn preprocess_quasiquote( + head: &str, + head_ns: &str, + args: &CalcitItems, + scope_defs: &HashSet, + file_ns: &str, + program_code: &program::ProgramCodeData, +) -> Result { + let mut xs: CalcitItems = im::vector![Calcit::Syntax(head.to_owned(), head_ns.to_owned())]; + for a in args { + xs.push_back(preprocess_quasiquote_internal(a, scope_defs, file_ns, program_code)?); + } + Ok(Calcit::List(xs)) +} + +pub fn preprocess_quasiquote_internal( + x: &Calcit, + scope_defs: &HashSet, + file_ns: &str, + program_code: &program::ProgramCodeData, +) -> Result { + match x { + Calcit::List(ys) if ys.is_empty() => Ok(x.to_owned()), + Calcit::List(ys) => match &ys[0] { + Calcit::Symbol(s, _, _) if s == "~" || s == "~@" => { + let mut xs: CalcitItems = im::vector![]; + for y in ys { + let (form, _) = preprocess_expr(y, scope_defs, file_ns, program_code)?; + xs.push_back(form.to_owned()); + } + Ok(Calcit::List(xs)) + } + _ => { + let mut xs: CalcitItems = im::vector![]; + for y in ys { + xs.push_back(preprocess_quasiquote_internal(y, scope_defs, file_ns, program_code)?.to_owned()); + } + Ok(Calcit::List(xs)) + } + }, + _ => Ok(x.to_owned()), + } +}