Skip to content

Commit

Permalink
unify &call-dylib-edn ; bump 0.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
tiye committed Oct 3, 2021
1 parent a913785 commit ee2c58a
Show file tree
Hide file tree
Showing 6 changed files with 802 additions and 588 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_std"
version = "0.1.0"
version = "0.0.2"
authors = ["jiyinyiyong <[email protected]>"]
edition = "2018"

Expand Down
1,186 changes: 673 additions & 513 deletions calcit.cirru

Large diffs are not rendered by default.

68 changes: 34 additions & 34 deletions compact.cirru
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{} (:package |calcit.std)
:configs $ {} (:init-fn |calcit.std.test/main!) (:reload-fn |calcit.std.test/reload!)
:modules $ []
:version |0.0.1
:version |0.0.2
:files $ {}
|calcit.std.test.date $ {}
:ns $ quote
Expand Down Expand Up @@ -36,40 +36,40 @@
assert=
parse-json $ stringify-json data
{} (|a 1) (|b 2) (|c |k)
|try-large-json $ quote
defn try-large-json () $ slurp-cirru-edn |/Users/chen/repo/calcit-lang/apis/docs/apis.cirru
|slurp-cirru-edn $ quote
defmacro slurp-cirru-edn (file)
with-cpu-time $ stringify-json
first $ with-cpu-time
parse-cirru $ read-file file
, true
|try-large-json $ quote
defn try-large-json () $ slurp-cirru-edn |/Users/chen/repo/calcit-lang/apis/docs/apis.cirru
|calcit.std.regex $ {}
:ns $ quote
ns calcit.std.regex $ :require
calcit.std.$meta :refer $ calcit-dirname
calcit.std.util :refer $ get-dylib-ext or-current-path
:defs $ {}
|re-find-all $ quote
defn re-find-all (s pattern)
&call-dylib-edn
str (or-current-path calcit-dirname) "\"/dylibs/libcalcit_std" $ get-dylib-ext
, "\"re_find_all" s pattern
|re-matches $ quote
defn re-matches (s pattern)
&call-dylib:edn
&call-dylib-edn
str (or-current-path calcit-dirname) "\"/dylibs/libcalcit_std" $ get-dylib-ext
, "\"re_matches" s pattern
|re-find $ quote
defn re-find (s pattern)
&call-dylib:edn
str (or-current-path calcit-dirname) "\"/dylibs/libcalcit_std" $ get-dylib-ext
, "\"re_find" s pattern
|re-find-index $ quote
defn re-find-index (s pattern)
&call-dylib:edn
&call-dylib-edn
str (or-current-path calcit-dirname) "\"/dylibs/libcalcit_std" $ get-dylib-ext
, "\"re_find_index" s pattern
|re-find-all $ quote
defn re-find-all (s pattern)
&call-dylib:edn
|re-find $ quote
defn re-find (s pattern)
&call-dylib-edn
str (or-current-path calcit-dirname) "\"/dylibs/libcalcit_std" $ get-dylib-ext
, "\"re_find_all" s pattern
, "\"re_find" s pattern
|calcit.std.process $ {}
:ns $ quote
ns calcit.std.process $ :require
Expand All @@ -79,7 +79,7 @@
|execute! $ quote
defn execute! (command ? dir)
assert "\"command in list" $ and (list? command) (every? command string?)
&call-dylib:str-vec-str->tuple-str2
&call-dylib-edn
str (or-current-path calcit-dirname) "\"/dylibs/libcalcit_std" $ get-dylib-ext
, "\"execute_command" (either dir "\"./") command
|calcit.std.fs $ {}
Expand All @@ -90,22 +90,22 @@
:defs $ {}
|read-dir! $ quote
defn read-dir! (name)
&call-dylib:str->vec-str
&call-dylib-edn
str (or-current-path calcit-dirname) "\"/dylibs/libcalcit_std" $ get-dylib-ext
, "\"read_dir" name
|write-file! $ quote
defn write-file! (name content)
&call-dylib:str:str->str
&call-dylib-edn
str calcit-dirname "\"/dylibs/libcalcit_std" $ get-dylib-ext
, "\"write_file" name content
|read-file! $ quote
defn read-file! (name)
&call-dylib:str->str
&call-dylib-edn
str (or-current-path calcit-dirname) "\"/dylibs/libcalcit_std" $ get-dylib-ext
, "\"read_file" name
|path-exists? $ quote
defn path-exists? (name)
&call-dylib:str->bool
&call-dylib-edn
str (or-current-path calcit-dirname) "\"/dylibs/libcalcit_std" $ get-dylib-ext
, "\"path_exists" name
|calcit.std.test.regex $ {}
Expand All @@ -131,46 +131,46 @@
calcit.std.$meta :refer $ calcit-dirname
calcit.std.util :refer $ get-dylib-ext or-current-path
:defs $ {}
|get-time! $ quote
defn get-time! () $ &call-dylib-edn
str (or-current-path calcit-dirname) "\"/dylibs/libcalcit_std" $ get-dylib-ext
, "\"now_bang"
|parse-time $ quote
defn parse-time (time format)
&call-dylib:edn
&call-dylib-edn
str (or-current-path calcit-dirname) "\"/dylibs/libcalcit_std" $ get-dylib-ext
, "\"parse_time" time format
|format-time $ quote
defn format-time (time ? format)
&call-dylib:edn
&call-dylib-edn
str (or-current-path calcit-dirname) "\"/dylibs/libcalcit_std" $ get-dylib-ext
, "\"format_time" time format
|get-time! $ quote
defn get-time! () $ &call-dylib:edn
str (or-current-path calcit-dirname) "\"/dylibs/libcalcit_std" $ get-dylib-ext
, "\"now_bang"
|calcit.std.json $ {}
:ns $ quote
ns calcit.std.json $ :require
calcit.std.$meta :refer $ calcit-dirname
calcit.std.util :refer $ get-dylib-ext or-current-path
:defs $ {}
|parse-json $ quote
defn parse-json (s)
&call-dylib:edn
str (or-current-path calcit-dirname) "\"/dylibs/libcalcit_std" $ get-dylib-ext
, "\"parse_json" s
|stringify-json $ quote
defn stringify-json (data ? colon?)
&call-dylib:edn
&call-dylib-edn
str (or-current-path calcit-dirname) "\"/dylibs/libcalcit_std" $ get-dylib-ext
, "\"stringify_json" data colon?
|parse-json $ quote
defn parse-json (s)
&call-dylib-edn
str (or-current-path calcit-dirname) "\"/dylibs/libcalcit_std" $ get-dylib-ext
, "\"parse_json" s
|calcit.std.test $ {}
:ns $ quote
ns calcit.std.test $ :require (calcit.std.test.fs :as fs) (calcit.std.test.date :as date) (calcit.std.test.regex :as regex) (calcit.std.test.json :as json)
:defs $ {}
|run-tests $ quote
defn run-tests () (fs/main!) (json/main!) (date/main!) (regex/main!)
|main! $ quote
defn main! () $ run-tests
|reload! $ quote
defn reload! $
|run-tests $ quote
defn run-tests () (fs/main!) (json/main!) (date/main!) (regex/main!)
|calcit.std.util $ {}
:ns $ quote (ns calcit.std.util)
:defs $ {}
Expand All @@ -193,4 +193,4 @@
, 1000
println (path-exists? "\"README.md") (path-exists? "\"build.js")
println $ read-dir! "\"./"
println $ execute! ([] "\"ls")
println "\"dirs:" $ execute! ([] "\"ls")
80 changes: 57 additions & 23 deletions src/fs.rs
Original file line number Diff line number Diff line change
@@ -1,42 +1,76 @@
use cirru_edn::Edn;
use std::fs;
use std::path::Path;

#[no_mangle]
pub fn read_file(name: String) -> Result<String, String> {
let task = fs::read_to_string(&name);
match task {
Ok(s) => Ok(s),
Err(e) => Err(format!("Failed to read file {:?}: {}", name, e)),
pub fn read_file(args: Vec<Edn>) -> Result<Edn, String> {
if args.len() == 1 {
if let Edn::Str(name) = &args[0] {
let task = fs::read_to_string(&name);
match task {
Ok(s) => Ok(Edn::Str(s)),
Err(e) => Err(format!("Failed to read file {:?}: {}", name, e)),
}
} else {
Err(format!("read-file expected 1 filename, got {:?}", &args[0]))
}
} else {
Err(format!("read-file expected 1 argument, got {:?}", args))
}
}

#[no_mangle]
pub fn write_file(name: String, content: String) -> Result<(), String> {
let task = fs::write(name.to_owned(), content.to_owned());
match task {
Ok(()) => Ok(()),
Err(e) => Err(format!("Failed to write to file {:?}: {}", name, e)),
pub fn write_file(args: Vec<Edn>) -> Result<Edn, String> {
if args.len() == 2 {
match (&args[0], &args[1]) {
(Edn::Str(name), Edn::Str(content)) => {
let task = fs::write(name.to_owned(), content.to_owned());
match task {
Ok(()) => Ok(Edn::Nil),
Err(e) => Err(format!("Failed to write to file {:?}: {}", name, e)),
}
}
(_, _) => Err(format!("write-file expected 2 strings, got {:?}", args)),
}
} else {
Err(format!("write-file expected 2 args, got {:?}", args))
}
}

#[no_mangle]
pub fn path_exists(name: String) -> Result<bool, String> {
return Ok(Path::new(&name).exists());
pub fn path_exists(args: Vec<Edn>) -> Result<Edn, String> {
if args.len() == 1 {
if let Edn::Str(name) = &args[0] {
Ok(Edn::Bool(Path::new(&name).exists()))
} else {
Err(format!("path-exists? expected 1 filename, got {:?}", args))
}
} else {
Err(format!("path-exists? expected 1 arg, got {:?}", args))
}
}

#[no_mangle]
pub fn read_dir(name: String) -> Result<Vec<String>, String> {
let task = fs::read_dir(&name);
match task {
Ok(children) => {
let mut content = vec![];
for c in children {
content.push(format!("{}", c.unwrap().path().display()));
}
// println!("child dir: {:?}", content);
pub fn read_dir(args: Vec<Edn>) -> Result<Edn, String> {
if args.len() == 1 {
if let Edn::Str(name) = &args[0] {
let task = fs::read_dir(&name);
match task {
Ok(children) => {
let mut content: Vec<Edn> = vec![];
for c in children {
content.push(Edn::Str(format!("{}", c.unwrap().path().display())));
}
// println!("child dir: {:?}", content);

Ok(content)
Ok(Edn::List(content))
}
Err(e) => Err(format!("Failed to read dir {:?}: {}", name, e)),
}
} else {
Err(format!("read-dir expected a string, {}", &args[0]))
}
Err(e) => Err(format!("Failed to read dir {:?}: {}", name, e)),
} else {
Err(format!("read-dir expected 2 args {:?}", args))
}
}
52 changes: 36 additions & 16 deletions src/process.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,44 @@
use cirru_edn::Edn;
use std::process::Command;

#[no_mangle]
pub fn execute_command(dir: String, pieces: Vec<String>) -> Result<(String, String), String> {
let mut cmd = String::from("");
let mut args: Vec<String> = vec![];
pub fn execute_command(args: Vec<Edn>) -> Result<Edn, String> {
if args.len() == 2 {
match (&args[0], &args[1]) {
(Edn::Str(dir), Edn::List(ys)) => {
let mut cmd = String::from("");
let mut xs: Vec<String> = vec![];

for (idx, piece) in pieces.iter().enumerate() {
if idx == 0 {
cmd = piece.to_string();
} else {
args.push(piece.to_string());
}
}
for (idx, piece) in ys.iter().enumerate() {
if let Edn::Str(s) = piece {
if idx == 0 {
cmd = s.to_owned();
} else {
xs.push(s.to_owned());
}
} else {
return Err(format!(
"execute-command expected string in list, got {}",
piece
));
}
}

match Command::new(cmd).current_dir(dir).args(&args).output() {
Ok(t) => {
let content = String::from_utf8(t.stdout).unwrap();
let stderr = String::from_utf8(t.stderr).unwrap();
Ok((content, stderr))
match Command::new(cmd).current_dir(dir).args(&xs).output() {
Ok(t) => {
let content = String::from_utf8(t.stdout).unwrap();
let stderr = String::from_utf8(t.stderr).unwrap();
Ok(Edn::List(vec![Edn::Str(content), Edn::Str(stderr)]))
}
Err(e) => Err(format!("Failed to excute: {}", e)),
}
}
(_, _) => Err(format!(
"excute-command expected string and list: {:?}",
args
)),
}
Err(e) => Err(format!("Failed to excute: {}", e)),
} else {
Err(format!("excute-command expected 2 args: {:?}", args))
}
}

0 comments on commit ee2c58a

Please sign in to comment.