Skip to content

Commit

Permalink
Merge pull request #134 from calcit-lang/pad-left-right
Browse files Browse the repository at this point in the history
new APIs pad-left , pad-right ; allow nil in merge
  • Loading branch information
soyaine authored Nov 17, 2021
2 parents 79ce654 + e2e0ccc commit 90fea1f
Show file tree
Hide file tree
Showing 12 changed files with 132 additions and 41 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.7"
version = "0.5.8"
authors = ["jiyinyiyong <jiyinyiyong@gmail.com>"]
edition = "2018"
license = "MIT"
Expand Down
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
### Calcit Scripting Language

> Lisp compiling to JavaScript ES Modules. (Similar to ClojureScript, but in very different syntax.)
> Lisp compiling to JavaScript ES Modules. Runs in Rust(Similar to ClojureScript, but in very different syntax.).
- Home http://calcit-lang.org/
- API Doc(heavily influenced by ClojureScript) http://apis.calcit-lang.org/
Expand All @@ -27,17 +27,7 @@ For Ubuntu 20.04, try binaries from http://bin.calcit-lang.org/linux/ , which ar

### Usage

Run:

```bash
cr compact.cirru --1 # run only once

cr compact.cirru # watch mode enabled by default

cr compact.cirru --init-fn='app.main/main!' # specifying init-fn
```

Inline evaling:
Snippets evaling:

```bash
cr -e 'range 100'
Expand All @@ -56,6 +46,16 @@ println "|a demo"
'
```

Run with a [compact.cirru](https://github.com/calcit-lang/lilac/blob/master/compact.cirru):

```bash
cr compact.cirru --1 # run only once

cr compact.cirru # watch mode enabled by default

cr compact.cirru --init-fn='app.main/main!' # specifying init-fn
```

Emitting code:

```bash
Expand All @@ -68,7 +68,7 @@ cr compact.cirru --emit-ir # compiles intermediate representation into program-i
### Calcit Editor & Bundler

Install [Calcit Editor](https://github.com/calcit-lang/editor) and run `ct` to launch editor server,
which writes `compact.cirru` and `.compact-inc.cirru` on saving. Try launching example by clong [Calcit Workflow](https://github.com/calcit-lang/calcit-workflow).
which writes `compact.cirru` and `.compact-inc.cirru` on saving. Try launching example by cloning [Calcit Workflow](https://github.com/calcit-lang/calcit-workflow).

Read more in [Minimal Calcit](https://github.com/calcit-lang/minimal-calcit/blob/main/README.md) to learn how to code Calcit with a plain text editor.

Expand Down
4 changes: 4 additions & 0 deletions calcit/test-map.cirru
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@
{} (:a false) (:b false) (:c true) (:d true)
{} (:a false) (:b false) (:c true) (:d true)

assert=
merge ({} (:a 1)) nil
{} (:a 1)

|test-pairs $ quote
fn ()

Expand Down
5 changes: 5 additions & 0 deletions calcit/test-string.cirru
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,11 @@
assert= 1 $ .find-index |abc |b
assert= "|\"a \\\"\"" $ .escape "|a \""

assert= |00000a $ .pad-left |a 6 |0
assert= |a00000 $ .pad-right |a 6 |0
assert= |12312a $ .pad-left |a 6 |123
assert= |a12312 $ .pad-right |a 6 |123

|main! $ quote
defn main! ()
log-title "|Testing str"
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.7",
"version": "0.5.8",
"main": "./lib/calcit.procs.js",
"devDependencies": {
"@types/node": "^16.11.6",
Expand Down
3 changes: 2 additions & 1 deletion src/bin/injection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ pub fn call_dylib_edn_fn(xs: &CalcitItems, call_stack: &CallStackList) -> Result
Ok(ret) => edn_to_calcit(&ret),
Err(e) => {
track::track_task_release();
let _ = display_stack(&format!("failed to call request: {}", e), &copied_stack_1);
// let _ = display_stack(&format!("failed to call request: {}", e), &copied_stack_1);
println!("failure inside ffi thread: {}", e);
return CalcitErr::err_str(e);
}
};
Expand Down
4 changes: 4 additions & 0 deletions src/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ pub fn is_proc_name(s: &str) -> bool {
| "&str:nth"
| "&str:first"
| "&str:rest"
| "&str:pad-left"
| "&str:pad-right"
// lists
| "[]"
| "'" // used as an alias for `[]`, experimental
Expand Down Expand Up @@ -290,6 +292,8 @@ fn handle_proc_internal(name: &str, args: &CalcitItems, call_stack: &CallStackLi
"&str:nth" => strings::nth(args),
"&str:first" => strings::first(args),
"&str:rest" => strings::rest(args),
"&str:pad-left" => strings::pad_left(args),
"&str:pad-right" => strings::pad_right(args),
// lists
"[]" => lists::new_list(args),
"'" => lists::new_list(args), // alias
Expand Down
50 changes: 27 additions & 23 deletions src/builtins/maps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,33 +61,37 @@ pub fn get(xs: &CalcitItems) -> Result<Calcit, CalcitErr> {
}

pub fn call_merge(xs: &CalcitItems) -> Result<Calcit, CalcitErr> {
match (xs.get(0), xs.get(1)) {
(Some(Calcit::Map(xs)), Some(Calcit::Map(ys))) => {
let mut zs: rpds::HashTrieMapSync<Calcit, Calcit> = xs.to_owned();
for (k, v) in ys {
zs.insert_mut(k.to_owned(), v.to_owned());
if xs.len() == 2 {
match (&xs[0], &xs[1]) {
(Calcit::Map(xs), Calcit::Nil) => Ok(Calcit::Map(xs.to_owned())),
(Calcit::Map(xs), Calcit::Map(ys)) => {
let mut zs: rpds::HashTrieMapSync<Calcit, Calcit> = xs.to_owned();
for (k, v) in ys {
zs.insert_mut(k.to_owned(), v.to_owned());
}
Ok(Calcit::Map(zs))
}
Ok(Calcit::Map(zs))
}
(Some(Calcit::Record(name, fields, values)), Some(Calcit::Map(ys))) => {
let mut new_values = (**values).to_owned();
for (k, v) in ys {
match k {
Calcit::Str(s) | Calcit::Symbol { sym: s, .. } => match find_in_fields(fields, &EdnKwd::from(s)) {
Some(pos) => new_values[pos] = v.to_owned(),
None => return CalcitErr::err_str(format!("invalid field `{}` for {:?}", s, fields)),
},
Calcit::Keyword(s) => match find_in_fields(fields, s) {
Some(pos) => new_values[pos] = v.to_owned(),
None => return CalcitErr::err_str(format!("invalid field `{}` for {:?}", s, fields)),
},
a => return CalcitErr::err_str(format!("invalid field key: {}", a)),
(Calcit::Record(name, fields, values), Calcit::Map(ys)) => {
let mut new_values = (**values).to_owned();
for (k, v) in ys {
match k {
Calcit::Str(s) | Calcit::Symbol { sym: s, .. } => match find_in_fields(fields, &EdnKwd::from(s)) {
Some(pos) => new_values[pos] = v.to_owned(),
None => return CalcitErr::err_str(format!("invalid field `{}` for {:?}", s, fields)),
},
Calcit::Keyword(s) => match find_in_fields(fields, s) {
Some(pos) => new_values[pos] = v.to_owned(),
None => return CalcitErr::err_str(format!("invalid field `{}` for {:?}", s, fields)),
},
a => return CalcitErr::err_str(format!("invalid field key: {}", a)),
}
}
Ok(Calcit::Record(name.to_owned(), fields.to_owned(), Arc::new(new_values)))
}
Ok(Calcit::Record(name.to_owned(), fields.to_owned(), Arc::new(new_values)))
(a, b) => CalcitErr::err_str(format!("expected 2 maps, got: {} {}", a, b)),
}
(Some(a), Some(b)) => CalcitErr::err_str(format!("expected 2 maps, got: {} {}", a, b)),
(_, _) => CalcitErr::err_str(format!("expected 2 arguments, got: {:?}", xs)),
} else {
CalcitErr::err_str(format!("expected 2 arguments, got: {:?}", xs))
}
}

Expand Down
63 changes: 63 additions & 0 deletions src/builtins/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,3 +320,66 @@ pub fn rest(xs: &CalcitItems) -> Result<Calcit, CalcitErr> {
None => CalcitErr::err_str("str:rest expected 1 argument"),
}
}

pub fn pad_left(xs: &CalcitItems) -> Result<Calcit, CalcitErr> {
if xs.len() == 3 {
match (&xs[0], &xs[1], &xs[2]) {
(Calcit::Str(s), Calcit::Number(n), Calcit::Str(pattern)) => {
let size = n.floor() as usize;
if pattern.is_empty() {
return CalcitErr::err_str("&str:pad-left expected non-empty pattern");
}
if s.len() >= size {
Ok(xs[0].to_owned())
} else {
let mut buffer = String::with_capacity(size);
let pad_size = size - s.len();
'write: loop {
for c in pattern.chars() {
buffer.push(c);
if buffer.len() >= pad_size {
break 'write;
}
}
}
buffer.push_str(s);
Ok(Calcit::Str(buffer.into()))
}
}
(a, b, c) => CalcitErr::err_str(format!("&str:pad-left expected string, number, string, got: {} {} {}", a, b, c)),
}
} else {
CalcitErr::err_str(format!("&str:pad-left expected 3 arguments, {:?}", xs))
}
}

pub fn pad_right(xs: &CalcitItems) -> Result<Calcit, CalcitErr> {
if xs.len() == 3 {
match (&xs[0], &xs[1], &xs[2]) {
(Calcit::Str(s), Calcit::Number(n), Calcit::Str(pattern)) => {
let size = n.floor() as usize;
if pattern.is_empty() {
return CalcitErr::err_str("&str:pad-right expected non-empty pattern");
}
if s.len() >= size {
Ok(xs[0].to_owned())
} else {
let mut buffer = String::with_capacity(size);
buffer.push_str(s);
'write: loop {
for c in pattern.chars() {
buffer.push(c);
if buffer.len() >= size {
break 'write;
}
}
}
Ok(Calcit::Str(buffer.into()))
}
}
(a, b, c) => CalcitErr::err_str(format!("&str:pad-right expected string, number, string, got: {} {} {}", a, b, c)),
}
} else {
CalcitErr::err_str(format!("&str:pad-right expected 3 arguments, {:?}", xs))
}
}
2 changes: 2 additions & 0 deletions src/cirru/calcit-core.cirru
Original file line number Diff line number Diff line change
Expand Up @@ -1351,6 +1351,8 @@
:nth &str:nth
:first &str:first
:rest &str:rest
:pad-left &str:pad-left
:pad-right &str:pad-right
:find-index &str:find-index
:get-char-code get-char-code
:escape &str:escape
Expand Down
10 changes: 9 additions & 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.7";
export const calcit_version = "0.5.8";

import { overwriteComparator, initTernaryTreeMap } from "@calcit/ternary-tree";
import { parse, ICirruNode } from "@cirru/parser.ts";
Expand Down Expand Up @@ -1423,6 +1423,14 @@ export let _$n_list_$o_distinct = (xs: CalcitList): CalcitSliceList => {
return new CalcitSliceList(result);
};

export let _$n_str_$o_pad_left = (s: string, size: number, pattern: string): string => {
return s.padStart(size, pattern);
};

export let _$n_str_$o_pad_right = (s: string, size: number, pattern: string): string => {
return s.padEnd(size, pattern);
};

export let _$n_get_os = (): CalcitKeyword => {
return kwd("js-engine");
};
Expand Down

0 comments on commit 90fea1f

Please sign in to comment.