From adf2db436892ce4e429357e494f6ebd6a93a7d7f Mon Sep 17 00:00:00 2001 From: jiyinyiyong Date: Tue, 27 Apr 2021 00:00:48 +0800 Subject: [PATCH] fix ref type detection; alpha release --- Cargo.lock | 2 +- Cargo.toml | 2 +- calcit/snapshots/test.cirru | 12 ++++++++++ lib/calcit-data.ts | 16 ++++++------- lib/calcit.procs.ts | 46 ++++++++++++++++++------------------- lib/custom-formatter.ts | 6 ++--- lib/record-procs.ts | 2 +- package.json | 2 +- src/builtins.rs | 1 + src/codegen/emit_js.rs | 6 ++--- src/primes.rs | 2 +- 11 files changed, 55 insertions(+), 42 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 889655ed..f934683b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -81,7 +81,7 @@ dependencies = [ [[package]] name = "calcit_runner" -version = "0.3.0-a6" +version = "0.3.0-a7" dependencies = [ "cirru_edn", "cirru_parser", diff --git a/Cargo.toml b/Cargo.toml index 38f75045..5954101d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "calcit_runner" -version = "0.3.0-a6" +version = "0.3.0-a7" authors = ["jiyinyiyong "] edition = "2018" license = "MIT" diff --git a/calcit/snapshots/test.cirru b/calcit/snapshots/test.cirru index 87e29f7f..918647fa 100644 --- a/calcit/snapshots/test.cirru +++ b/calcit/snapshots/test.cirru @@ -200,6 +200,16 @@ assert= b b assert= false (&= a b) + |*ref-demo $ quote + defatom *ref-demo 0 + |test-refs $ quote + fn () + log-title "|Testing refs" + assert= 0 @*ref-demo + reset! *ref-demo 2 + assert= 2 @*ref-demo + assert= :ref (type-of *ref-demo) + |reload! $ quote defn reload! () nil @@ -235,6 +245,8 @@ test-fn-eq + test-refs + inside-nim: test-gynienic/main! diff --git a/lib/calcit-data.ts b/lib/calcit-data.ts index 3b5c4ec4..2bc89daa 100644 --- a/lib/calcit-data.ts +++ b/lib/calcit-data.ts @@ -93,7 +93,7 @@ export let tipNestedCrData = (x: CrDataValue): string => { return x.toString(); }; -export class CrDataAtom { +export class CrDataRef { value: CrDataValue; path: string; listeners: Map; @@ -104,7 +104,7 @@ export class CrDataAtom { this.listeners = new Map(); } toString(): string { - return `(&atom ${this.value.toString()})`; + return `(&ref ${this.value.toString()})`; } } @@ -498,7 +498,7 @@ export type CrDataValue = | CrDataSet | CrDataKeyword | CrDataSymbol - | CrDataAtom + | CrDataRef | CrDataFn | CrDataRecur // should not be exposed to function | CrDataRecord @@ -517,7 +517,7 @@ export let kwd = (content: string) => { } }; -export var atomsRegistry = new Map(); +export var refsRegistry = new Map(); let defaultHash_nil = valueHash("nil:"); let defaultHash_number = valueHash("number:"); @@ -527,7 +527,7 @@ let defaultHash_true = valueHash("true:"); let defaultHash_false = valueHash("false:"); let defaultHash_symbol = valueHash("symbol:"); let defaultHash_fn = valueHash("fn:"); -let defaultHash_atom = valueHash("atom:"); +let defaultHash_ref = valueHash("ref:"); let defaultHash_set = valueHash("set:"); let defaultHash_list = valueHash("list:"); let defaultHash_map = valueHash("map:"); @@ -570,8 +570,8 @@ let hashFunction = (x: CrDataValue): Hash => { (x as any).cachedHash = h; return h; } - if (x instanceof CrDataAtom) { - let h = mergeValueHash(defaultHash_atom, x.path); + if (x instanceof CrDataRef) { + let h = mergeValueHash(defaultHash_ref, x.path); x.cachedHash = h; return h; } @@ -655,7 +655,7 @@ export let toString = (x: CrDataValue, escaped: boolean): string => { if (x instanceof CrDataRecord) { return x.toString(); } - if (x instanceof CrDataAtom) { + if (x instanceof CrDataRef) { return x.toString(); } diff --git a/lib/calcit.procs.ts b/lib/calcit.procs.ts index 26c9eb98..0ec5a23b 100644 --- a/lib/calcit.procs.ts +++ b/lib/calcit.procs.ts @@ -8,11 +8,11 @@ import { CrDataKeyword, CrDataList, CrDataMap, - CrDataAtom, + CrDataRef, CrDataFn, CrDataRecur, kwd, - atomsRegistry, + refsRegistry, toString, CrDataSet, cloneSet, @@ -48,8 +48,8 @@ export let type_of = (x: any): CrDataKeyword => { if (x == null) { return kwd("nil"); } - if (x instanceof CrDataAtom) { - return kwd("atom"); + if (x instanceof CrDataRef) { + return kwd("ref"); } if (x instanceof CrDataSymbol) { return kwd("symbol"); @@ -139,19 +139,19 @@ export let _AND_list_map = (...xs: CrDataValue[]): CrDataList => { }; export let defatom = (path: string, x: CrDataValue): CrDataValue => { - let v = new CrDataAtom(x, path); - atomsRegistry.set(path, v); + let v = new CrDataRef(x, path); + refsRegistry.set(path, v); return v; }; -export let peekDefatom = (path: string): CrDataAtom => { - return atomsRegistry.get(path); +export let peekDefatom = (path: string): CrDataRef => { + return refsRegistry.get(path); }; -export let deref = (x: CrDataAtom): CrDataValue => { - let a = atomsRegistry.get(x.path); - if (!(a instanceof CrDataAtom)) { - console.warn("Can not find atom:", x); +export let deref = (x: CrDataRef): CrDataValue => { + let a = refsRegistry.get(x.path); + if (!(a instanceof CrDataRef)) { + console.warn("Can not find ref:", x); } return a.value; }; @@ -267,8 +267,8 @@ export let _AND__EQ_ = (x: CrDataValue, y: CrDataValue): boolean => { } return false; } - if (x instanceof CrDataAtom) { - if (y instanceof CrDataAtom) { + if (x instanceof CrDataRef) { + if (y instanceof CrDataRef) { return x === y; } return false; @@ -520,9 +520,9 @@ export let dissoc = function (xs: CrDataValue, k: CrDataValue) { throw new Error("Does not support `dissoc` on this type"); }; -export let reset_BANG_ = (a: CrDataAtom, v: CrDataValue): null => { - if (!(a instanceof CrDataAtom)) { - throw new Error("Expected atom for reset!"); +export let reset_BANG_ = (a: CrDataRef, v: CrDataValue): null => { + if (!(a instanceof CrDataRef)) { + throw new Error("Expected ref for reset!"); } let prev = a.value; a.value = v; @@ -532,9 +532,9 @@ export let reset_BANG_ = (a: CrDataAtom, v: CrDataValue): null => { return null; }; -export let add_watch = (a: CrDataAtom, k: CrDataKeyword, f: CrDataFn): null => { - if (!(a instanceof CrDataAtom)) { - throw new Error("Expected atom for add-watch!"); +export let add_watch = (a: CrDataRef, k: CrDataKeyword, f: CrDataFn): null => { + if (!(a instanceof CrDataRef)) { + throw new Error("Expected ref for add-watch!"); } if (!(k instanceof CrDataKeyword)) { throw new Error("Expected watcher key in keyword"); @@ -546,7 +546,7 @@ export let add_watch = (a: CrDataAtom, k: CrDataKeyword, f: CrDataFn): null => { return null; }; -export let remove_watch = (a: CrDataAtom, k: CrDataKeyword): null => { +export let remove_watch = (a: CrDataRef, k: CrDataKeyword): null => { a.listeners.delete(k); return null; }; @@ -1478,8 +1478,8 @@ export let set_QUES_ = (x: CrDataValue): boolean => { export let fn_QUES_ = (x: CrDataValue): boolean => { return typeof x === "function"; }; -export let atom_QUES_ = (x: CrDataValue): boolean => { - return x instanceof CrDataAtom; +export let ref_QUES_ = (x: CrDataValue): boolean => { + return x instanceof CrDataRef; }; export let record_QUES_ = (x: CrDataValue): boolean => { return x instanceof CrDataRecord; diff --git a/lib/custom-formatter.ts b/lib/custom-formatter.ts index e71d1498..df667aea 100644 --- a/lib/custom-formatter.ts +++ b/lib/custom-formatter.ts @@ -1,4 +1,4 @@ -import { CrDataAtom, CrDataValue, CrDataSymbol, CrDataKeyword, CrDataList, CrDataMap, CrDataRecord, CrDataSet } from "./calcit-data"; +import { CrDataRef, CrDataValue, CrDataSymbol, CrDataKeyword, CrDataList, CrDataMap, CrDataRecord, CrDataSet } from "./calcit-data"; import { toPairs } from "@calcit/ternary-tree"; declare global { @@ -60,11 +60,11 @@ export let load_console_formatter_BANG_ = () => { } return ret; } - if (obj instanceof CrDataAtom) { + if (obj instanceof CrDataRef) { return [ "div", { style: "color: hsl(280, 80%, 60%)" }, - `Atom ${obj.path}`, + `Ref ${obj.path}`, ["div", { style: "color: hsl(280, 80%, 60%)" }, ["div", { style: "margin-left: 8px;" }, embedObject(obj.value)]], ]; } diff --git a/lib/record-procs.ts b/lib/record-procs.ts index 0ab75aff..cc4f5262 100644 --- a/lib/record-procs.ts +++ b/lib/record-procs.ts @@ -5,7 +5,7 @@ import { CrDataKeyword, CrDataList, CrDataMap, - CrDataAtom, + CrDataRef, CrDataFn, CrDataRecur, kwd, diff --git a/package.json b/package.json index 3f3b3e49..f223e225 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@calcit/procs", - "version": "0.3.0-a6", + "version": "0.3.0-a7", "main": "./lib/calcit.procs.js", "devDependencies": { "@types/node": "^14.14.41", diff --git a/src/builtins.rs b/src/builtins.rs index 20dec10a..b1f707e9 100644 --- a/src/builtins.rs +++ b/src/builtins.rs @@ -298,6 +298,7 @@ pub fn handle_syntax( "macroexpand-all" => syntax::macroexpand_all(nodes, scope, file_ns, program), "try" => syntax::call_try(nodes, scope, file_ns, program), "sort" => lists::sort(nodes, scope, file_ns, program), + // "define reference" although it uses a confusing name "atom" "defatom" => refs::defatom(nodes, scope, file_ns, program), a => Err(format!("TODO syntax: {}", a)), } diff --git a/src/codegen/emit_js.rs b/src/codegen/emit_js.rs index b45c33f6..885fbc7f 100644 --- a/src/codegen/emit_js.rs +++ b/src/codegen/emit_js.rs @@ -145,7 +145,7 @@ fn is_preferred_js_proc(name: &str) -> bool { | "string?" | "fn?" | "bool?" - | "atom?" + | "ref?" | "record?" | "starts-with?" | "ends-with?" @@ -288,11 +288,11 @@ fn gen_call_code( _ if body.len() > 2 => Err(format!("defatom expected name and value, got too many: {:?}", body)), (Some(Calcit::Symbol(sym, ..)), Some(v)) => { // let _name = escape_var(sym); // TODO - let atom_path = wrap_js_str(&format!("{}/{}", ns, sym.clone())); + let ref_path = wrap_js_str(&format!("{}/{}", ns, sym.clone())); let value_code = &to_js_code(v, ns, local_defs, file_imports)?; Ok(format!( "\n({}peekDefatom({}) ?? {}defatom({}, {}))\n", - &var_prefix, &atom_path, &var_prefix, &atom_path, value_code + &var_prefix, &ref_path, &var_prefix, &ref_path, value_code )) } (_, _) => Err(format!("defatom expected name and value, got: {:?}", body)), diff --git a/src/primes.rs b/src/primes.rs index c7edecf0..e8eb1d68 100644 --- a/src/primes.rs +++ b/src/primes.rs @@ -408,7 +408,7 @@ impl PartialEq for Calcit { pub const CORE_NS: &str = "calcit.core"; pub const GENERATED_NS: &str = "calcit.gen"; -pub const CALCI_VERSION: &str = "0.3.0-a6"; +pub const CALCI_VERSION: &str = "0.3.0-a7"; impl Calcit { pub fn turn_string(&self) -> String {