Skip to content

Commit 8653149

Browse files
authored
Merge pull request #258 from calcit-lang/atom-in-edn
extend edn to include atom
2 parents 7c510a2 + d497957 commit 8653149

File tree

14 files changed

+120
-87
lines changed

14 files changed

+120
-87
lines changed

Cargo.lock

Lines changed: 21 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "calcit"
3-
version = "0.9.6"
3+
version = "0.9.7"
44
authors = ["jiyinyiyong <jiyinyiyong@gmail.com>"]
55
edition = "2021"
66
license = "MIT"
@@ -15,11 +15,11 @@ exclude = ["lib/*", "calcit/*", "js-out/*", "scripts/*"]
1515
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1616

1717
[dependencies]
18-
cirru_edn = "0.6.12"
18+
cirru_edn = "0.6.13"
1919
# cirru_edn = { path = "/Users/chenyong/repo/cirru/edn.rs" }
2020
cirru_parser = "0.1.31"
2121
# cirru_parser = { path = "/Users/chenyong/repo/cirru/parser.rs" }
22-
argh = "0.1.12"
22+
argh = "0.1.13"
2323
dirs = "5.0.1"
2424
notify = "7.0.0"
2525
notify-debouncer-mini = "0.5.0"
@@ -28,12 +28,12 @@ hex = "0.4.3"
2828
rpds = "1.1.0"
2929
im_ternary_tree = "0.0.18"
3030
# im_ternary_tree = { path = "/Users/chenyong/repo/calcit-lang/ternary-tree.rs" }
31-
colored = "2.1.0"
31+
colored = "3.0.0"
3232
strum = "0.25"
3333
strum_macros = "0.25"
3434

3535
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
36-
libloading = "0.8.5"
36+
libloading = "0.8.6"
3737
ctrlc = "3.4.5"
3838

3939
[lib]

calcit/test-edn.cirru

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
test-edn-comment
1515
inside-eval:
1616
test-symbol
17+
test-atom
1718
|test-edn $ %{} :CodeEntry (:doc |)
1819
:code $ quote
1920
defn test-edn ()
@@ -126,6 +127,17 @@
126127
code $ quote $ + 1 2
127128
assert= code
128129
eval $ &data-to-code code
130+
|test-atom $ %{} :CodeEntry (:doc |)
131+
:code $ quote
132+
defn test-atom ()
133+
log-title "|Testing atom to edn"
134+
let
135+
a $ parse-cirru-edn "|atom 1"
136+
println "|Check a" a
137+
assert= true $ ref? a
138+
assert= 1 $ deref a
139+
assert= "|atom 1"
140+
trim $ format-cirru-edn a
129141

130142
:ns $ %{} :CodeEntry (:doc |)
131143
:code $ quote

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"name": "@calcit/procs",
3-
"version": "0.9.6",
3+
"version": "0.9.7",
44
"main": "./lib/calcit.procs.mjs",
55
"devDependencies": {
6-
"@types/node": "^22.8.7",
7-
"typescript": "^5.6.3"
6+
"@types/node": "^22.10.5",
7+
"typescript": "^5.7.2"
88
},
99
"scripts": {
1010
"compile": "rm -rfv lib/* && tsc",

src/builtins.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::calcit::{Calcit, CalcitErr, CalcitList, CalcitProc, CalcitScope, Calc
1717
use crate::call_stack::{using_stack, CallStackList};
1818

1919
use im_ternary_tree::TernaryTreeList;
20-
pub(crate) use refs::ValueAndListeners;
20+
pub(crate) use refs::{quick_build_atom, ValueAndListeners};
2121

2222
pub type FnType = fn(xs: Vec<Calcit>, call_stack: &CallStackList) -> Result<Calcit, CalcitErr>;
2323
pub type SyntaxType = fn(expr: &TernaryTreeList<Calcit>, scope: &CalcitScope, file_ns: &str) -> Result<Calcit, CalcitErr>;

src/builtins/refs.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -117,18 +117,21 @@ static ATOM_ID_GEN: AtomicUsize = AtomicUsize::new(0);
117117
/// proc
118118
pub fn atom(xs: &[Calcit]) -> Result<Calcit, CalcitErr> {
119119
match xs.first() {
120-
Some(value) => {
121-
let atom_idx = ATOM_ID_GEN.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
122-
let path: String = format!("atom-{atom_idx}");
120+
Some(value) => Ok(quick_build_atom(value.to_owned())),
121+
_ => CalcitErr::err_str("atom expected 1 node"),
122+
}
123+
}
123124

124-
let path_info: Arc<str> = path.into();
125-
// println!("atom {:?}", path_info);
125+
/// this is a internal helper for `atom`, not exposed to Calcit
126+
pub fn quick_build_atom(v: Calcit) -> Calcit {
127+
let atom_idx = ATOM_ID_GEN.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
128+
let path: String = format!("atom-{atom_idx}");
126129

127-
let pair_value = Arc::new(Mutex::new((value.to_owned(), HashMap::new())));
128-
Ok(Calcit::Ref(path_info, pair_value))
129-
}
130-
_ => CalcitErr::err_str("atom expected 2 nodes"),
131-
}
130+
let path_info: Arc<str> = path.into();
131+
// println!("atom {:?}", path_info);
132+
133+
let pair_value = Arc::new(Mutex::new((v, HashMap::new())));
134+
Calcit::Ref(path_info, pair_value)
132135
}
133136

134137
/// previously `deref`, but `deref` now turned into a function calling `&atom:deref`

src/data/edn.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ pub fn calcit_to_edn(x: &Calcit) -> Result<Edn, String> {
9999
MethodKind::InvokeNativeOptional => Ok(Edn::Symbol(format!(".?!{name}").into())),
100100
},
101101
Calcit::AnyRef(r) => Ok(Edn::AnyRef(r.to_owned())),
102+
Calcit::Ref(_p, pair) => {
103+
let pair = pair.lock().expect("read ref");
104+
Ok(Edn::Atom(Box::new(calcit_to_edn(&pair.0)?)))
105+
}
102106
a => Err(format!("not able to generate EDN: {a:?}")), // TODO more types to handle
103107
}
104108
}
@@ -183,6 +187,7 @@ pub fn edn_to_calcit(x: &Edn, options: &Calcit) -> Calcit {
183187
}
184188
Edn::Buffer(buf) => Calcit::Buffer(buf.to_owned()),
185189
Edn::AnyRef(r) => Calcit::AnyRef(r.to_owned()),
190+
Edn::Atom(a) => crate::builtins::quick_build_atom(edn_to_calcit(a, options)),
186191
}
187192
}
188193
/// find a record field in options

ts-src/calcit-data.mts

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { CalcitSet, overwriteSetComparator } from "./js-set.mjs";
1212
import { CalcitTuple } from "./js-tuple.mjs";
1313
import { CalcitCirruQuote, cirru_deep_equal } from "./js-cirru.mjs";
1414
import { CirruWriterNode } from "@cirru/writer.ts";
15+
import { CalcitRef } from "./js-ref.mjs";
1516

1617
// we have to inject cache in a dirty way in some cases
1718
const calcit_dirty_hash_key = "_calcit_cached_hash";
@@ -100,22 +101,6 @@ export let tipNestedCalcitData = (x: CalcitValue): string => {
100101
return x.toString();
101102
};
102103

103-
export class CalcitRef {
104-
value: CalcitValue;
105-
path: string;
106-
listeners: Map<CalcitValue, CalcitFn>;
107-
cachedHash: Hash;
108-
constructor(x: CalcitValue, path: string) {
109-
this.value = x;
110-
this.path = path;
111-
this.listeners = new Map();
112-
this.cachedHash = null;
113-
}
114-
toString(): string {
115-
return `(&ref ${this.value.toString()})`;
116-
}
117-
}
118-
119104
export type CalcitFn = (...xs: CalcitValue[]) => CalcitValue;
120105

121106
export let getStringName = (x: CalcitValue): string => {

ts-src/calcit.procs.mts

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,9 @@ import { parse, ICirruNode } from "@cirru/parser.ts";
77
import { writeCirruCode } from "@cirru/writer.ts";
88

99
import { CalcitValue } from "./js-primes.mjs";
10-
import {
11-
CalcitSymbol,
12-
CalcitTag,
13-
CalcitRef,
14-
CalcitFn,
15-
CalcitRecur,
16-
newTag,
17-
refsRegistry,
18-
toString,
19-
getStringName,
20-
to_js_data,
21-
_$n__$e_,
22-
hashFunction,
23-
} from "./calcit-data.mjs";
10+
import { CalcitSymbol, CalcitTag, CalcitFn, CalcitRecur, newTag, refsRegistry, toString, getStringName, _$n__$e_, hashFunction } from "./calcit-data.mjs";
2411

12+
import { CalcitRef } from "./js-ref.mjs";
2513
import { fieldsEqual, CalcitRecord } from "./js-record.mjs";
2614

2715
export * from "./calcit-data.mjs";
@@ -148,13 +136,7 @@ export let defatom = (path: string, x: CalcitValue): CalcitValue => {
148136
return v;
149137
};
150138

151-
var atomCounter = 0;
152-
153-
export let atom = (x: CalcitValue): CalcitValue => {
154-
atomCounter = atomCounter + 1;
155-
let v = new CalcitRef(x, `atom-${atomCounter}`);
156-
return v;
157-
};
139+
export { atom } from "./js-ref.mjs";
158140

159141
export let peekDefatom = (path: string): CalcitRef => {
160142
return refsRegistry.get(path);

ts-src/custom-formatter.mts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { CalcitValue, isLiteral } from "./js-primes.mjs";
2-
import { CalcitRef, CalcitSymbol, CalcitTag } from "./calcit-data.mjs";
2+
import { CalcitSymbol, CalcitTag } from "./calcit-data.mjs";
3+
import { CalcitRef } from "./js-ref.mjs";
34

45
import { CalcitRecord } from "./js-record.mjs";
56
import { CalcitMap, CalcitSliceMap } from "./js-map.mjs";

0 commit comments

Comments
 (0)