Skip to content

Commit

Permalink
fix preprocessing inside quasiquote; bump 0.3.13
Browse files Browse the repository at this point in the history
  • Loading branch information
tiye committed May 12, 2021
1 parent f3d55f3 commit ce3694e
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "calcit_runner"
version = "0.3.13-a1"
version = "0.3.13"
authors = ["jiyinyiyong <[email protected]>"]
edition = "2018"
license = "MIT"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/cirru/calcit-core.cirru
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
50 changes: 49 additions & 1 deletion src/runner/preprocess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<String>,
file_ns: &str,
program_code: &program::ProgramCodeData,
) -> Result<Calcit, String> {
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<String>,
file_ns: &str,
program_code: &program::ProgramCodeData,
) -> Result<Calcit, String> {
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()),
}
}

0 comments on commit ce3694e

Please sign in to comment.