Skip to content

Commit

Permalink
add invoke; fix duplication evaluation of thunks; bump 0.3.20
Browse files Browse the repository at this point in the history
  • Loading branch information
tiye committed May 25, 2021
1 parent d2d4fe1 commit 012c600
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 10 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.19"
version = "0.3.20"
authors = ["jiyinyiyong <jiyinyiyong@gmail.com>"]
edition = "2018"
license = "MIT"
Expand Down
21 changes: 21 additions & 0 deletions calcit/snapshots/test.cirru
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,25 @@
assert= 2 @*ref-demo
assert= :ref (type-of *ref-demo)

|%Num $ quote
defrecord %Num :inc :show
|Num $ quote
def Num $ %{} %Num
:inc $ fn (x) $ [] Num (&+ x 1)
:show $ fn (x) $ str x

|test-invoke $ quote
fn ()
log-title "|Testing invoke"

let
a $ [] Num 0
assert=
[] Num 2
-> a (invoke :inc) (invoke :inc)
assert= |1
-> a (invoke :inc) (invoke :show)

|reload! $ quote
defn reload! () nil

Expand Down Expand Up @@ -257,6 +276,8 @@
test-fn-eq

test-refs

test-invoke

inside-eval:
test-gynienic/main!
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.19",
"version": "0.3.20",
"main": "./lib/calcit.procs.js",
"devDependencies": {
"@types/node": "^15.0.1",
Expand Down
21 changes: 16 additions & 5 deletions src/bin/bundle_calcit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use walkdir::WalkDir;

pub fn main() -> io::Result<()> {
let cli_matches = parse_cli();
let verbose = cli_matches.is_present("verbose");
let base_dir = Path::new(cli_matches.value_of("src").unwrap());
let out_path = Path::new(cli_matches.value_of("out").unwrap());
let out_file = match out_path.extension() {
Expand All @@ -22,8 +23,9 @@ pub fn main() -> io::Result<()> {
}
None => out_path.join("compact.cirru").to_path_buf(),
};

println!("reading from {}", base_dir.display());
if verbose {
println!("reading from {}", base_dir.display());
}

let mut dict: HashMap<Edn, Edn> = HashMap::new();
let mut a: Vec<String> = vec![];
Expand Down Expand Up @@ -77,7 +79,7 @@ pub fn main() -> io::Result<()> {
if let Cirru::List(ys) = line {
match (ys.get(0), ys.get(1)) {
(Some(Cirru::Leaf(x0)), Some(Cirru::Leaf(x1))) => {
if x0 == "def" || x0 == "defn" || x0 == "defmacro" || x0 == "defatom" {
if x0 == "def" || x0 == "defn" || x0 == "defmacro" || x0 == "defatom" || x0 == "defrecord" {
defs.insert(Edn::Str(x1.to_owned()), Edn::Quote(line.to_owned()));
} else {
return Err(io_err(format!("invalid def op: {}", x0)));
Expand All @@ -100,7 +102,9 @@ pub fn main() -> io::Result<()> {
} else {
return Err(io_err(format!("file should be expressions, molformed, {}", file_data)));
}
// println!("bunding {}", entry.path().display());
if verbose {
println!("bunding {}", entry.path().display());
}
a.push(entry.path().to_str().unwrap().to_string());
}
}
Expand All @@ -111,7 +115,7 @@ pub fn main() -> io::Result<()> {
// println!("data {}", Edn::Map(dict));

write(&out_file, cirru_edn::format(&Edn::Map(dict.clone()), true).unwrap())?;
println!("\nfile created. you can run it with:\n\ncr -1 {}\n", out_file.display());
println!("file created at {}", out_file.display());

Ok(())
}
Expand Down Expand Up @@ -139,6 +143,13 @@ fn parse_cli<'a>() -> clap::ArgMatches<'a> {
.long("out")
.takes_value(true),
)
.arg(
clap::Arg::with_name("verbose")
.help("verbose mode")
.short("v")
.long("verbose")
.takes_value(false),
)
.get_matches()
}

Expand Down
1 change: 1 addition & 0 deletions src/builtins/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ pub fn turn_string(xs: &CalcitItems) -> Result<Calcit, String> {
Some(Calcit::Str(s)) => Ok(Calcit::Str(s.clone())),
Some(Calcit::Keyword(s)) => Ok(Calcit::Str(s.clone())),
Some(Calcit::Symbol(s, ..)) => Ok(Calcit::Str(s.clone())),
Some(Calcit::Number(n)) => Ok(Calcit::Str(n.to_string())),
Some(a) => Err(format!("turn-string cannot turn this to string: {}", a)),
None => Err(String::from("turn-string expected 1 argument, got nothing")),
}
Expand Down
12 changes: 12 additions & 0 deletions src/cirru/calcit-core.cirru
Original file line number Diff line number Diff line change
Expand Up @@ -1115,3 +1115,15 @@

|/= $ quote
defn /= (a b) (not= a b)

|invoke $ quote
defn invoke (pair name & params)
assert "|method! applies on a pair, leading a record"
and (list? pair) (= 2 (count pair)) (record? (first pair))
assert "|method by string or keyword"
or (string? name) (keyword? name) (symbol? name)
let
proto $ nth pair 0
f $ &get proto name
assert "|expected function" (fn? f)
f (nth pair 1) & params
9 changes: 7 additions & 2 deletions src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,15 @@ pub fn evaluate_expr(
Calcit::Symbol(s, ..) if s == "&" => Ok(expr.clone()),
Calcit::Symbol(s, ns, resolved) => match resolved {
Some(ResolvedDef(r_ns, r_def, _import_rule)) => {
let v = evaluate_symbol(&r_def, scope, &r_ns, program_code)?;
let v = evaluate_symbol(r_def, scope, r_ns, program_code)?;
match v {
// extra check to make sure thunks extracted
Calcit::Thunk(code) => evaluate_expr(&code, scope, file_ns, program_code),
Calcit::Thunk(code) => {
let evaled_v = evaluate_expr(&code, scope, file_ns, program_code)?;
// and write back to program state to fix duplicated evalution
program::write_evaled_def(r_ns, r_def, evaled_v.to_owned())?;
Ok(evaled_v)
}
_ => Ok(v),
}
}
Expand Down

0 comments on commit 012c600

Please sign in to comment.