Skip to content

Commit

Permalink
fix ref type detection; alpha release
Browse files Browse the repository at this point in the history
  • Loading branch information
tiye committed Apr 26, 2021
1 parent 7a8930d commit def2acf
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 43 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.0-a6"
version = "0.3.0-a7"
authors = ["jiyinyiyong <jiyinyiyong@gmail.com>"]
edition = "2018"
license = "MIT"
Expand Down
12 changes: 12 additions & 0 deletions calcit/snapshots/test.cirru
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -235,6 +245,8 @@

test-fn-eq

test-refs

inside-nim:
test-gynienic/main!

Expand Down
16 changes: 8 additions & 8 deletions lib/calcit-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<CrDataValue, CrDataFn>;
Expand All @@ -104,7 +104,7 @@ export class CrDataAtom {
this.listeners = new Map();
}
toString(): string {
return `(&atom ${this.value.toString()})`;
return `(&ref ${this.value.toString()})`;
}
}

Expand Down Expand Up @@ -498,7 +498,7 @@ export type CrDataValue =
| CrDataSet
| CrDataKeyword
| CrDataSymbol
| CrDataAtom
| CrDataRef
| CrDataFn
| CrDataRecur // should not be exposed to function
| CrDataRecord
Expand All @@ -517,7 +517,7 @@ export let kwd = (content: string) => {
}
};

export var atomsRegistry = new Map<string, CrDataAtom>();
export var refsRegistry = new Map<string, CrDataRef>();

let defaultHash_nil = valueHash("nil:");
let defaultHash_number = valueHash("number:");
Expand All @@ -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:");
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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();
}

Expand Down
46 changes: 23 additions & 23 deletions lib/calcit.procs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import {
CrDataKeyword,
CrDataList,
CrDataMap,
CrDataAtom,
CrDataRef,
CrDataFn,
CrDataRecur,
kwd,
atomsRegistry,
refsRegistry,
toString,
CrDataSet,
cloneSet,
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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;
};
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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");
Expand All @@ -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;
};
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions lib/custom-formatter.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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)]],
];
}
Expand Down
4 changes: 2 additions & 2 deletions lib/record-procs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import {
CrDataKeyword,
CrDataList,
CrDataMap,
CrDataAtom,
CrDataRef,
CrDataFn,
CrDataRecur,
kwd,
atomsRegistry,
refsRegistry,
toString,
CrDataSet,
cloneSet,
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.0-a6",
"version": "0.3.0-a7",
"main": "./lib/calcit.procs.js",
"devDependencies": {
"@types/node": "^14.14.41",
Expand Down
1 change: 1 addition & 0 deletions src/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
}
Expand Down
6 changes: 3 additions & 3 deletions src/codegen/emit_js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ fn is_preferred_js_proc(name: &str) -> bool {
| "string?"
| "fn?"
| "bool?"
| "atom?"
| "ref?"
| "record?"
| "starts-with?"
| "ends-with?"
Expand Down Expand Up @@ -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)),
Expand Down
2 changes: 1 addition & 1 deletion src/primes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit def2acf

Please sign in to comment.