diff --git a/runtime/Cargo.lock b/runtime/Cargo.lock index 454c82b..e9e80fc 100644 --- a/runtime/Cargo.lock +++ b/runtime/Cargo.lock @@ -2,6 +2,12 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "allocator-api2" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923" + [[package]] name = "bdwgc-alloc" version = "0.6.13" @@ -39,19 +45,68 @@ dependencies = [ "typewit", ] +[[package]] +name = "equivalent" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" + [[package]] name = "find-msvc-tools" version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5baebc0774151f905a1a2cc41989300b1e6fbb29aff0ceffa1064fdd3088d582" +[[package]] +name = "foldhash" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2" + +[[package]] +name = "foldhash" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77ce24cb58228fbb8aa041425bb1050850ac19177686ea6e0f41a70416f56fdb" + [[package]] name = "gyehoek" version = "0.1.0" dependencies = [ "bdwgc-alloc", "const_panic", + "internment", "libc", + "string-interner", +] + +[[package]] +name = "hashbrown" +version = "0.15.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9229cfe53dfd69f0609a49f65461bd93001ea1ef889cd5529dd176593f5338a1" +dependencies = [ + "allocator-api2", + "equivalent", + "foldhash 0.1.5", +] + +[[package]] +name = "hashbrown" +version = "0.16.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "841d1cc9bed7f9236f321df977030373f4a4163ae1a7dbfe1a51a2c1a51d9100" +dependencies = [ + "foldhash 0.2.0", +] + +[[package]] +name = "internment" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "636d4b0f6a39fd684effe2a73f5310df16a3fa7954c26d36833e98f44d1977a2" +dependencies = [ + "hashbrown 0.15.5", ] [[package]] @@ -60,14 +115,88 @@ version = "0.2.186" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "68ab91017fe16c622486840e4c83c9a37afeff978bd239b5293d61ece587de66" +[[package]] +name = "proc-macro2" +version = "1.0.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41f2619966050689382d2b44f664f4bc593e129785a36d6ee376ddf37259b924" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "serde" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e" +dependencies = [ + "serde_core", +] + +[[package]] +name = "serde_core" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "shlex" version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" +[[package]] +name = "string-interner" +version = "0.20.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad3df9b59e2eded8d825c7c4363ad339a20fb6bc0b9a4778560f518f59910b15" +dependencies = [ + "hashbrown 0.16.1", + "serde", +] + +[[package]] +name = "syn" +version = "2.0.117" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e665b8803e7b1d2a727f4023456bbbbe74da67099c585258af0ad9c5013b9b99" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + [[package]] name = "typewit" version = "1.15.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "214ca0b2191785cbc06209b9ca1861e048e39b5ba33574b3cedd58363d5bb5f6" + +[[package]] +name = "unicode-ident" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" diff --git a/runtime/Cargo.toml b/runtime/Cargo.toml index b87b264..fcdd2bb 100644 --- a/runtime/Cargo.toml +++ b/runtime/Cargo.toml @@ -13,7 +13,9 @@ bdwgc-alloc = { version = "0.6.13" , default-features = false , features = ["cmake"] } const_panic = "0.2.15" +internment = "0.8.6" libc = "0.2.186" +string-interner = "0.20.0" [patch.crates-io] bdwgc-alloc = { git = 'https://git.deertopia.net/msyds/bdwgc-rust.git' } diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index d6be78b..285646a 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -6,3 +6,4 @@ mod scm; mod primitives; mod obarray; mod capi; +mod var; diff --git a/runtime/src/obarray.rs b/runtime/src/obarray.rs index 04a8886..c247222 100644 --- a/runtime/src/obarray.rs +++ b/runtime/src/obarray.rs @@ -1,26 +1,24 @@ -use std::{collections::HashMap, hash::{BuildHasher, Hasher}}; -use crate::scm::scm_bits; -use crate::scm; +use std::{ops::DerefMut, sync::{LazyLock, Mutex, RwLock}}; -mod fnv1a; -use fnv1a::{FNV1a, SymbolHash}; +// mod fnv1a; +// use fnv1a::{FNV1a, SymbolHash}; +use string_interner::{StringInterner, symbol::SymbolU32}; -pub struct Obarray (HashMap ); - -pub const fn new () -> Obarray { - Obarray (HashMap::with_hasher (SymbolHash ())) -} - -pub fn hash (s : &str) -> u64 { - let mut a : FNV1a = Default::default (); - a.write (s.as_bytes ()); - return a.finish () -} +pub struct Obarray ( + LazyLock >> +); impl Obarray { - pub fn insert (&mut self) { - + pub const fn new () -> Obarray { + Obarray (LazyLock::new (|| RwLock::new (StringInterner::new ()))) } } -pub static symbols : Obarray = new (); +impl Obarray { + pub fn intern (&self, name : &str) -> SymbolU32 { + let mut r = symbols.0.write ().unwrap (); + r.deref_mut ().get_or_intern (name) + } +} + +pub static symbols : Obarray = Obarray::new (); diff --git a/runtime/src/scm.rs b/runtime/src/scm.rs index 31abd45..a5f7e1e 100644 --- a/runtime/src/scm.rs +++ b/runtime/src/scm.rs @@ -3,7 +3,9 @@ use std::slice; -use crate::gc; +use string_interner::Symbol as _; + +use crate::{gc, obarray}; pub type scm_bits = u64; @@ -21,6 +23,7 @@ pub enum SCM { SmallInt (i64), Cons (scm_bits, scm_bits), String (String), + Symbol (usize), Nil, False, True, @@ -147,5 +150,11 @@ pub fn make_string (s : &str) -> scm_bits { pub fn make_symbol (name : &str) -> scm_bits { - todo! () + let r = unsafe { words (tc7_symbol, 2) }; + let sym = obarray::symbols.intern (name).to_usize (); + unsafe { set_word (r, 1, sym.try_into ().unwrap ()) }; + pack_ptr (r) +} + +pub fn string_to_symbol (str : scm_bits) { } diff --git a/runtime/src/var.rs b/runtime/src/var.rs new file mode 100644 index 0000000..f4e31de --- /dev/null +++ b/runtime/src/var.rs @@ -0,0 +1,25 @@ +use std::{collections::HashMap, ops::DerefMut as _, sync::{LazyLock, RwLock}}; +use string_interner::symbol::SymbolU32; +use crate::scm::scm_bits; + +struct Vars ( + LazyLock >> +); + +impl Vars { + pub const fn new () -> Vars { + Vars (LazyLock::new (|| RwLock::new (HashMap::new ()))) + } + + pub fn lookup (&self, name : SymbolU32) -> Option { + let r = self.0.write ().unwrap (); + (*r).get (&name).map (|x| *x) + } + + pub fn define (&self, name : SymbolU32, value : scm_bits) { + let mut r = self.0.write ().unwrap (); + r.deref_mut ().insert (name, value); + } +} + +static vars : Vars = Vars::new ();