From d53c560f5dcf390b22283967a1fe5908dae85fdc Mon Sep 17 00:00:00 2001 From: yukirij Date: Fri, 20 Dec 2024 14:37:34 -0800 Subject: [PATCH] Progress update. --- Cargo.toml | 3 ++ LICENSE.txt | 2 ++ README.md | 1 + src/bin/schema.rs | 16 +++++++++ src/implement/any.rs | 5 ++- src/implement/boolean.rs | 14 ++++++++ src/implement/mod.rs | 1 + src/implement/natural.rs | 8 +++++ src/implement/null.rs | 3 ++ src/implement/optional.rs | 27 +++++++++++++++ src/implement/record.rs | 0 src/implement/schema.rs | 42 +++++++++++++++++++++++ src/implement/sequence.rs | 8 +++++ src/lib.rs | 3 ++ src/prelude/mod.rs | 4 +-- src/runtime/mod.rs | 23 ++++++++++++- src/test/any.rs | 30 ++++++++++++++++ src/test/mod.rs | 1 + src/util/container/dynlist.rs | 64 +++++++++++++++++++++++++++++++++++ src/util/container/mod.rs | 1 + src/util/mod.rs | 1 + 21 files changed, 253 insertions(+), 4 deletions(-) create mode 100644 LICENSE.txt create mode 100644 README.md create mode 100644 src/bin/schema.rs create mode 100644 src/implement/record.rs create mode 100644 src/implement/schema.rs create mode 100644 src/test/any.rs create mode 100644 src/test/mod.rs create mode 100644 src/util/container/dynlist.rs create mode 100644 src/util/container/mod.rs diff --git a/Cargo.toml b/Cargo.toml index d889142..7e69103 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,3 +7,6 @@ edition = "2021" num = "0.4.3" pack = { git = "https://git.tsukiyo.org/Utility/pack" } +dict = { git = "https://git.tsukiyo.org/Utility/dictionary" } +pool = { git = "https://git.tsukiyo.org/Utility/pool" } +sparse = { git = "https://git.tsukiyo.org/Utility/sparse" } diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..9ca6797 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,2 @@ +Project: Suzu +https://ykr.info/license/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..732a102 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# Suzu Data System diff --git a/src/bin/schema.rs b/src/bin/schema.rs new file mode 100644 index 0000000..931123c --- /dev/null +++ b/src/bin/schema.rs @@ -0,0 +1,16 @@ +struct Member { + name:Option, + typeid:usize, +} + +struct Schema { + capacity:usize, + length:usize, + data:*mut usize, +} + +struct Record { + +} + +fn main() { } diff --git a/src/implement/any.rs b/src/implement/any.rs index 31caf0f..2bc01d0 100644 --- a/src/implement/any.rs +++ b/src/implement/any.rs @@ -13,13 +13,16 @@ impl Any { } impl Szun for Any { fn typeid() -> usize { 0 } + + fn encode(&self) -> Vec { Vec::new() } + fn decode(&mut self, _:&Vec, _:&mut usize) -> Result<(),()> { Err(()) } } impl internal::SzunInternal for Any { fn to_ref(&self) -> Ref { self.refer } fn from_ref(refer:Ref) -> Self { Self { refer } } } impl Clone for Any { - fn clone(&self) -> Self { Self::from_ref(clone(self.to_ref(), None)) } + fn clone(&self) -> Self { println!("tid {}", self.to_ref().typeid()); Self::from_ref(clone(self.to_ref(), None)) } } impl Drop for Any { fn drop(&mut self) { drop(self.to_ref()); } diff --git a/src/implement/boolean.rs b/src/implement/boolean.rs index 4f55fb0..35a1045 100644 --- a/src/implement/boolean.rs +++ b/src/implement/boolean.rs @@ -34,6 +34,20 @@ impl Boolean { } impl Szun for Boolean { fn typeid() -> usize { Runtime::type_from(types::ANY, &[types::BOOLEAN]) } + + fn encode(&self) -> Vec { + vec![self.get() as u8] + } + + fn decode(&mut self, bytes:&Vec, index:&mut usize) -> Result<(),()> { + if bytes.len() - *index >= 1 { + self.set(bytes[*index] != 0); + *index += 1; + Ok(()) + } else { + Err(()) + } + } } impl SzunInternal for Boolean { fn to_ref(&self) -> Ref { self.refer } diff --git a/src/implement/mod.rs b/src/implement/mod.rs index 561a46a..fcbe665 100644 --- a/src/implement/mod.rs +++ b/src/implement/mod.rs @@ -7,3 +7,4 @@ pub mod block; pub use block::Block; pub mod sequence; pub use sequence::Sequence; pub mod optional; pub use optional::Optional; pub mod array; pub use array::Array; +pub mod schema; pub use schema::Schema; diff --git a/src/implement/natural.rs b/src/implement/natural.rs index c8cb662..6e0fe03 100644 --- a/src/implement/natural.rs +++ b/src/implement/natural.rs @@ -35,6 +35,14 @@ impl Natural { } impl Szun for Natural { fn typeid() -> usize { Runtime::type_from(types::ANY, &[types::NATURAL]) } + + fn encode(&self) -> Vec { + Vec::new() + } + + fn decode(&mut self, bytes:&Vec, index:&mut usize) -> Result<(),()> { + Err(()) + } } impl SzunInternal for Natural { fn to_ref(&self) -> Ref { self.refer } diff --git a/src/implement/null.rs b/src/implement/null.rs index 4d4e57c..4914df4 100644 --- a/src/implement/null.rs +++ b/src/implement/null.rs @@ -8,6 +8,9 @@ impl Null { } impl Szun for Null { fn typeid() -> usize { Runtime::type_from(types::ANY, &[types::BOOLEAN]) } + + fn encode(&self) -> Vec { Vec::new() } + fn decode(&mut self, _:&Vec, _:&mut usize) -> Result<(),()> { Err(()) } } impl SzunInternal for Null { fn to_ref(&self) -> Ref { Ref::null() } diff --git a/src/implement/optional.rs b/src/implement/optional.rs index 2ba868e..482a6c7 100644 --- a/src/implement/optional.rs +++ b/src/implement/optional.rs @@ -94,6 +94,33 @@ impl Optional { } impl Szun for Optional { fn typeid() -> usize { Runtime::type_from(T::typeid(), &[types::OPTIONAL]) } + + fn encode(&self) -> Vec { + if let Some(value) = self.get() { + [ + vec![0x01], + encode(value.to_ref()), + ].concat() + } else { + vec![0x00] + } + } + + fn decode(&mut self, bytes:&Vec, index:&mut usize) -> Result<(),()> { + if bytes.len() - *index >= 1 { + let some = bytes[*index]; + *index += 1; + + if some != 0 { + self.set_default(); + decode(self.to_ref().inner(), bytes, index) + } else { + Ok(()) + } + } else { + Err(()) + } + } } impl SzunInternal for Optional { fn to_ref(&self) -> Ref { self.refer } diff --git a/src/implement/record.rs b/src/implement/record.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/implement/schema.rs b/src/implement/schema.rs new file mode 100644 index 0000000..1595a68 --- /dev/null +++ b/src/implement/schema.rs @@ -0,0 +1,42 @@ +use crate::{prelude::{*, internal::*}, Runtime, Any, types}; + +pub(crate) struct SchemaData { + +} + +pub struct Schema { + refer:Ref, +} +impl Schema { + pub fn new() -> Self { + Self { + refer:Runtime::acquire(Self::typeid()), + } + } + + pub fn add(&mut self) -> Result<(),()> + { + Err(()) + } + + pub fn add_named(&mut self, name:&str) -> Result<(),()> + { + Err(()) + } +} +impl Szun for Schema { + fn typeid() -> usize { Runtime::type_from(types::ANY, &[types::SCHEMA]) } + + fn encode(&self) -> Vec { + Vec::new() + } + + fn decode(&mut self, bytes:&Vec, index:&mut usize) -> Result<(),()> { + Err(()) + } +} +impl SzunInternal for Schema { + fn to_ref(&self) -> Ref { self.refer } + fn from_ref(refer:Ref) -> Self { Self { refer } } +} +crate::impl_szun!(Schema); diff --git a/src/implement/sequence.rs b/src/implement/sequence.rs index 98c2eb3..950904c 100644 --- a/src/implement/sequence.rs +++ b/src/implement/sequence.rs @@ -101,6 +101,14 @@ impl Sequence { } impl Szun for Sequence { fn typeid() -> usize { Runtime::type_from(types::ANY, &[types::SEQUENCE]) } + + fn encode(&self) -> Vec { + Vec::new() + } + + fn decode(&mut self, bytes:&Vec, index:&mut usize) -> Result<(),()> { + Err(()) + } } impl internal::SzunInternal for Sequence { fn to_ref(&self) -> Ref { self.refer } diff --git a/src/lib.rs b/src/lib.rs index 5c4e32f..468f43c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,3 +5,6 @@ pub mod prelude; pub use prelude::*; mod types; mod runtime; use runtime::Runtime; mod implement; pub use implement::*; + +#[cfg(test)] +mod test; diff --git a/src/prelude/mod.rs b/src/prelude/mod.rs index b4da354..b1def38 100644 --- a/src/prelude/mod.rs +++ b/src/prelude/mod.rs @@ -18,8 +18,8 @@ pub trait Szun : internal::SzunInternal + Clone + TryFrom + Into + Dro || Runtime::type_in(self_type) == 0 } - fn encode(&self) -> Vec { encode(self.to_ref()) } - fn decode(&mut self, bytes:&Vec, index:&mut usize) -> Result<(),()> { decode(self.to_ref(), bytes, index) } + fn encode(&self) -> Vec; + fn decode(&mut self, bytes:&Vec, index:&mut usize) -> Result<(),()>; fn tag(&self) -> Vec { diff --git a/src/runtime/mod.rs b/src/runtime/mod.rs index a8b9239..744eae6 100644 --- a/src/runtime/mod.rs +++ b/src/runtime/mod.rs @@ -1,10 +1,29 @@ use crate::{prelude::*, types}; +use sparse::Sparse; +use pool::Pool; mod typetree; use typetree::TypeTree; +enum Binding { + Schema { + length:usize, + size:usize, + align:usize, + next:usize, + }, + Member { + term:usize, + typeid:usize, + offset:usize, + next:usize, + }, +} + struct RuntimeData { - terms:Vec, + terms:dict::Dictionary, types:TypeTree, + bindings:Pool, + bindings_ext:Sparse, } impl RuntimeData { const fn init() -> Self @@ -12,6 +31,8 @@ impl RuntimeData { Self { terms:Vec::new(), types:TypeTree::new(), + bindings:Pool::new(), + bindings_ext:Sparse::new(), } } diff --git a/src/test/any.rs b/src/test/any.rs new file mode 100644 index 0000000..28d44f1 --- /dev/null +++ b/src/test/any.rs @@ -0,0 +1,30 @@ +use crate::*; + +#[test] +fn any_from_natural() +{ + const EXPECT :u32 = 5025; + + let any :Any = Natural::new().with(EXPECT).into(); + + let mut value = 0; + if let Ok(nat) = Natural::try_from(any) { + value = nat.get(); + } + assert_eq!(EXPECT, value); +} + +#[test] +fn any_clone_natural() +{ + const EXPECT :u32 = 5025; + + let any :Any = Natural::new().with(EXPECT).into(); + let any = any.clone(); + + let mut value = 0; + if let Ok(nat) = Natural::try_from(any) { + value = nat.get(); + } + assert_eq!(EXPECT, value); +} diff --git a/src/test/mod.rs b/src/test/mod.rs new file mode 100644 index 0000000..bd4c243 --- /dev/null +++ b/src/test/mod.rs @@ -0,0 +1 @@ +mod any; diff --git a/src/util/container/dynlist.rs b/src/util/container/dynlist.rs new file mode 100644 index 0000000..964a163 --- /dev/null +++ b/src/util/container/dynlist.rs @@ -0,0 +1,64 @@ +#[derive(Clone, Copy)] +pub(crate) struct DynListData { + capacity:usize, + length:usize, + data:*mut u8, +} + +#[derive(Clone, Copy)] +pub struct DynList { + size:usize, + align:usize, + data:DynListData, +} +impl DynList { + pub fn from_parts(ptr:*const DynListData, size:usize, align:usize) -> Self { + Self { + size, + align, + data:unsafe {*ptr}, + } + } + + pub fn reserve(&mut self) -> Result<*mut u8,()> + { + if self.length == self.capacity { + self.resize(self.capacity * 2)?; + } + self.length += 1; + cell(self.length - 1) + } + + pub fn cell(&self, index:usize) -> Result<*mut u8,()> + { + if index < self.length { + Ok(unsafe {self.data.byte_add(index * self.size)}) + } else { + Err(()) + } + } + + pub fn resize(&mut self, capacity:usize) -> Result<(),()> + { + use std::alloc::{alloc_zeroed, Layout}; + + let ptr = self.data; + + if capacity > 0 { + if let Ok(layout) = Layout::from_size_align(size, types::align(typeid)) { + let address :*const u8; + unsafe { + address = alloc_zeroed(layout) as _; + } + Ref::new(typeid, address) + } else { + Ref::null() + } + } else { + Ref::null() + } + + self.capacity = capacity; + Ok(()) + } +} diff --git a/src/util/container/mod.rs b/src/util/container/mod.rs new file mode 100644 index 0000000..0a52305 --- /dev/null +++ b/src/util/container/mod.rs @@ -0,0 +1 @@ +mod dynlist; pub use dynlist::RawList; diff --git a/src/util/mod.rs b/src/util/mod.rs index eda363d..4f2f16c 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -1 +1,2 @@ pub mod macros; +mod container; pub use container::*;