Skip to content

Commit

Permalink
adding some cases and fixed several edge cases; bump 0.3.23
Browse files Browse the repository at this point in the history
  • Loading branch information
tiye committed May 29, 2021
1 parent f0640e2 commit 722dfa5
Show file tree
Hide file tree
Showing 14 changed files with 143 additions and 65 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.22"
version = "0.3.23"
authors = ["jiyinyiyong <jiyinyiyong@gmail.com>"]
edition = "2018"
license = "MIT"
Expand Down
14 changes: 14 additions & 0 deletions calcit/snapshots/test-list.cirru
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,18 @@
log-title "|Testing alias"
assert= (' 1 2 3) ([] 1 2 3)

|test-methods $ quote
fn ()
log-title "|Testing list methods"
assert= 3
.count $ [] 1 2 3
assert=
[] 4 5 6
.map ([] 1 2 3) $ fn (x) (+ x 3)
assert=
[] 2 3 4
.map ([] 1 2 3) .inc

|main! $ quote
defn main! ()

Expand Down Expand Up @@ -326,6 +338,8 @@
test-doseq
test-let[]

test-methods

do true

:proc $ quote ()
Expand Down
14 changes: 14 additions & 0 deletions calcit/snapshots/test-map.cirru
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,19 @@
unselect-keys ({} (:a 1) (:b 2) (:c 3)) ([] :c :d)
{} (:a 1) (:b 2)

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

assert= 2
.count $ {} (:a 1) (:b 2)
assert=
{} (:a 11)
.map-kv ({} (:a 1)) $ fn (k v)
[] k (+ v 10)

|main! $ quote

defn main! ()

log-title "|Testing maps"
Expand All @@ -193,6 +205,8 @@

test-select

test-methods

do true

:proc $ quote ()
Expand Down
9 changes: 9 additions & 0 deletions calcit/snapshots/test-math.cirru
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@
assert= true (integer? 1)
assert= false (integer? 1.1)

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

assert= 1 $ .floor 1.1
assert= 16 $ .pow 2 4

|main! $ quote
defn main! ()
log-title "|Testing numbers"
Expand All @@ -89,6 +96,8 @@

test-integer

test-methods

do true

:proc $ quote ()
Expand Down
9 changes: 9 additions & 0 deletions calcit/snapshots/test-record.cirru
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,19 @@
|Cat $ quote
defrecord Cat :name :color

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

assert= |Cat
.get-name Cat

|main! $ quote
defn main! ()
test-record

test-methods

do true

:proc $ quote ()
Expand Down
7 changes: 7 additions & 0 deletions calcit/snapshots/test-set.cirru
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,18 @@
assert= (#{} 1 2 3) (#{} 1 2 (+ 1 2))
assert-detect not $ = (#{} 1 2 3) (#{} 2 3 4)

|test-methods $ quote
fn ()
assert= 3
.count $ #{} 1 2 3

|main! $ quote
defn main! ()
log-title "|Testing set"
test-set

test-methods

do true

:proc $ quote ()
Expand Down
5 changes: 4 additions & 1 deletion calcit/snapshots/test-string.cirru
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,12 @@

|test-methods $ quote
defn test-methods ()
log-title "|Testing methods"
log-title "|Testing string methods"

assert= 3 (.count |abc)
assert=
[] |a |c
.split |abc |b

|main! $ quote
defn main! ()
Expand Down
16 changes: 8 additions & 8 deletions calcit/snapshots/test.cirru
Original file line number Diff line number Diff line change
Expand Up @@ -227,20 +227,20 @@
defrecord %Num :inc :show
|Num $ quote
def Num $ %{} %Num
:inc $ fn (x) $ [] Num (&+ x 1)
:inc $ fn (x) $ :: Num (&+ x 1)
:show $ fn (x) $ str x

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

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

|test-tuple $ quote
fn ()
Expand Down Expand Up @@ -286,7 +286,7 @@

test-refs

test-invoke
test-method

test-tuple

Expand Down
76 changes: 43 additions & 33 deletions lib/calcit.procs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,13 @@ export let _AND__EQ_ = (x: CrDataValue, y: CrDataValue): boolean => {
}
if (x instanceof CrDataRef) {
if (y instanceof CrDataRef) {
return x === y;
return x.path === y.path;
}
return false;
}
if (x instanceof CrDataTuple) {
if (y instanceof CrDataTuple) {
return _AND__EQ_(x.fst, y.fst) && _AND__EQ_(x.snd, y.snd);
}
return false;
}
Expand Down Expand Up @@ -1758,39 +1764,43 @@ export let register_calcit_builtin_classes = (options: typeof calcit_builtin_cla
Object.assign(calcit_builtin_classes, options);
};

export let invoke_method = (p: string, obj: CrDataValue, ...args: CrDataValue[]) => {
let klass: CrDataRecord;
let rawValue = obj;
if (obj instanceof CrDataTuple) {
if (obj.fst instanceof CrDataRecord) {
klass = obj.fst;
rawValue = obj.snd;
export let invoke_method =
(p: string) =>
(obj: CrDataValue, ...args: CrDataValue[]) => {
let klass: CrDataRecord;
let rawValue = obj;
if (obj instanceof CrDataTuple) {
if (obj.fst instanceof CrDataRecord) {
klass = obj.fst;
rawValue = obj.snd;
} else {
throw new Error("Method invoking expected a record as class");
}
} else if (typeof obj === "number") {
klass = calcit_builtin_classes.number;
} else if (typeof obj === "string") {
klass = calcit_builtin_classes.string;
} else if (obj instanceof CrDataSet) {
klass = calcit_builtin_classes.set;
} else if (obj instanceof CrDataList) {
klass = calcit_builtin_classes.list;
} else if (obj instanceof CrDataRecord) {
klass = calcit_builtin_classes.record;
} else if (obj instanceof CrDataMap) {
klass = calcit_builtin_classes.map;
} else {
throw new Error("Method invoking expected a record as class");
}
} else if (typeof obj === "number") {
klass = calcit_builtin_classes.number;
} else if (typeof obj === "string") {
klass = calcit_builtin_classes.string;
} else if (obj instanceof CrDataSet) {
klass = calcit_builtin_classes.set;
} else if (obj instanceof CrDataList) {
klass = calcit_builtin_classes.list;
} else if (obj instanceof CrDataRecord) {
klass = calcit_builtin_classes.record;
} else {
return (obj as any)[p](...args); // trying to call JavaScript method
}
if (klass == null) {
throw new Error("Cannot find class for this object for invoking");
}
let method = klass.get(p);
if (typeof method === "function") {
return method(rawValue, ...args);
} else {
throw new Error("Method for invoking is not a function");
}
};
return (obj as any)[p](...args); // trying to call JavaScript method
}
if (klass == null) {
throw new Error("Cannot find class for this object for invoking");
}
let method = klass.get(p);
if (typeof method === "function") {
return method(rawValue, ...args);
} else {
throw new Error("Method for invoking is not a function");
}
};

// special procs have to be defined manually
export let reduce = foldl;
Expand Down
4 changes: 2 additions & 2 deletions lib/record-procs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ export let _AND__PCT__MAP_ = (proto: CrDataValue, ...xs: Array<CrDataValue>): Cr
}
};

export let get_record_name = (x: CrDataRecord): CrDataSymbol => {
export let get_record_name = (x: CrDataRecord): string => {
if (x instanceof CrDataRecord) {
return new CrDataSymbol(x.name);
return x.name;
} else {
throw new Error("Expected a record");
}
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.22",
"version": "0.3.23",
"main": "./lib/calcit.procs.js",
"devDependencies": {
"@types/node": "^15.0.1",
Expand Down
1 change: 1 addition & 0 deletions src/cirru/calcit-core.cirru
Original file line number Diff line number Diff line change
Expand Up @@ -1170,6 +1170,7 @@
|&core-set-class $ quote
defrecord! &core-set-class
:add coll-append
:count count
:difference difference
:exclude exclude
:empty empty
Expand Down
47 changes: 29 additions & 18 deletions src/codegen/emit_js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,22 @@ fn to_js_code(
// println!("gen proc {} under {}", s, ns,);
// let resolved = Some(ResolvedDef(String::from(primes::CORE_NS), s.to_owned()));
// gen_symbol_code(s, primes::CORE_NS, &resolved, ns, xs, local_defs)
Ok(format!("{}{}", proc_prefix, escape_var(s)))

if s.starts_with('.') {
if s.starts_with(".-") || s.starts_with(".!") {
Err(format!("invalid js method {} at this position", s))
} else {
// `.method` being used as a parameter
let name = s.strip_prefix('.').unwrap();
Ok(format!(
"{}invoke_method({})",
var_prefix,
escape_cirru_str(&name), // TODO need confirm
))
}
} else {
Ok(format!("{}{}", proc_prefix, escape_var(s)))
}
}
Calcit::Syntax(s, ..) => {
let resolved = Some(ResolvedDef(String::from(primes::CORE_NS), s.to_owned(), None));
Expand Down Expand Up @@ -406,28 +421,24 @@ fn gen_call_code(
None => Err(format!("expected 1 object, got {}", xs)),
}
} else {
Err(format!("invalid member accessor {}", s))
Err(format!("invalid static member accessor {}", s))
}
}
_ if s.starts_with('.') => {
let name = s.strip_prefix('.').unwrap();
if matches_js_var(name) {
match body.get(0) {
Some(obj) => {
let args = body.skip(1);
let args_code = gen_args_code(&args, ns, local_defs, file_imports)?;
Ok(format!(
"{}invoke_method({},{},{})",
var_prefix,
escape_cirru_str(&name), // TODO need confirm
to_js_code(&obj, ns, local_defs, file_imports)?,
args_code
))
}
None => Err(format!("expected 1 object, got {}", xs)),
match body.get(0) {
Some(obj) => {
let args = body.skip(1);
let args_code = gen_args_code(&args, ns, local_defs, file_imports)?;
Ok(format!(
"{}invoke_method({})({},{})",
var_prefix,
escape_cirru_str(&name), // TODO need confirm
to_js_code(&obj, ns, local_defs, file_imports)?,
args_code
))
}
} else {
Err(format!("invalid member accessor {}", s))
None => Err(format!("expected 1 object, got {}", xs)),
}
}
_ => {
Expand Down

0 comments on commit 722dfa5

Please sign in to comment.