Skip to content

Commit

Permalink
Merge pull request #110 from calcit-lang/key-match
Browse files Browse the repository at this point in the history
improvements on key-match
  • Loading branch information
soyaine authored Oct 21, 2021
2 parents 1ffc990 + 3f847fa commit 621edee
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 29 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.5.0-a11"
version = "0.5.0-a12"
authors = ["jiyinyiyong <jiyinyiyong@gmail.com>"]
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.5.0-a11",
"version": "0.5.0-a12",
"main": "./lib/calcit.procs.js",
"devDependencies": {
"@types/node": "^16.10.5",
Expand Down
17 changes: 11 additions & 6 deletions src/bin/injection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,21 @@ pub fn callback_dylib_edn(xs: &CalcitItems) -> Result<Calcit, CalcitErr> {
};
if let Calcit::Fn(_, def_ns, _, def_scope, args, body) = callback {
let r = runner::run_fn(&im::vector![result], &def_scope, &args, &body, &def_ns);
println!("callback result: {:?}", r);
match r {
Ok(ret) => {
if ret != Calcit::Nil {
println!("[Thread] callback result: {}", ret);
}
}
Err(e) => {
println!("[Error] thread callback failed: {}", e)
}
}

track::track_task_release();
Ok(Calcit::Nil)
} else {
track::track_task_release();
return Err(CalcitErr::use_string(format!(
"expected last argument to be callback fn, got: {}",
callback
)));
unreachable!(format!("expected last argument to be callback fn, got: {}", callback));
}
});

Expand Down
50 changes: 31 additions & 19 deletions src/cirru/calcit-core.cirru
Original file line number Diff line number Diff line change
Expand Up @@ -349,32 +349,44 @@
~ $ &list:nth else 0
~@ $ &list:rest else

|key-match $ quote
|&key-match-internal $ quote
defmacro key-match (value & body)
if (&list:empty? body)
quasiquote
&let nil
echo "|[warn] key-match found no matched case, missing `_` case?" ~value
println "|[Warn] key-match found no matched case, missing `_` case?" ~value
&let
pair (&list:first body)
assert "|key-match expected pairs"
and (list? pair) (&= 2 (&list:count pair))
let[] (pattern branch) pair
if (&= pattern '_) branch
&let nil
assert "|pattern in a list" (list? pattern)
&let
k (&list:first pattern)
&let (v# (gensym 'v))
quasiquote
&let (~v# ~value)
if (&= (&list:first ~v#) ~k)
let
~ $ map-indexed (&list:rest pattern) $ defn %key-match (idx x)
[] x $ quasiquote
&list:nth ~v# (~ (inc idx))
, ~branch
key-match ~value (~@ (&list:rest body))
let
pattern $ &list:nth pair 0
branch $ &list:nth pair 1
if (list? pattern)
&let
k (&list:first pattern)
quasiquote
if (&= (&list:first ~value) ~k)
let
~ $ map-indexed (&list:rest pattern) $ defn %key-match (idx x)
[] x $ quasiquote
&list:nth ~value (~ (inc idx))
, ~branch
&key-match-internal ~value $ ~@ (&list:rest body)
if (&= pattern '_) branch
raise $ str "|unknown supported pattern: " pair

|key-match $ quote
defmacro key-match (value & body)
if (&list:empty? body)
quasiquote
println "|[Error] key-match expected some patterns and matches" ~value
if (list? value)
&let (v# (gensym 'v))
quasiquote
&let (~v# ~value)
&key-match-internal ~v# $ ~@ body
quasiquote
&key-match-internal ~value $ ~@ body

|&case $ quote
defmacro &case (item default pattern & others)
Expand Down
2 changes: 1 addition & 1 deletion ts-src/calcit.procs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// CALCIT VERSION
export const calcit_version = "0.5.0-a11";
export const calcit_version = "0.5.0-a12";

import { overwriteComparator, initTernaryTreeMap } from "@calcit/ternary-tree";
import { parse, ICirruNode } from "@cirru/parser.ts";
Expand Down

0 comments on commit 621edee

Please sign in to comment.