-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
A first impl of CharTable #106
A first impl of CharTable #106
Conversation
src/data.rs
Outdated
@@ -335,6 +335,9 @@ pub(crate) fn aset<'ob>( | |||
Err(anyhow!("index {idx} is out of bounds. Length was {len}")) | |||
} | |||
} | |||
ObjectType::CharTable(_) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not too sure how to get a mutable view of LispCharTable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for now, you can just use a RefCell
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean that we should have something like
#[derive(PartialEq, Eq, Trace, Debug)]
pub(crate) struct CharTable(GcHeap<RefCell<CharTableInner<'static>>>);
In this case I get into issues with Trace
not being implemented for RefCell. Of course we could add this
impl<T: Trace> Trace for RefCell<T> {
fn trace(&self, state: &mut GcState) {
self.borrow().trace(state);
}
}
I committed this. Let me know what you think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed!
src/data.rs
Outdated
@@ -392,6 +399,7 @@ fn type_of(object: Object) -> Object { | |||
ObjectType::String(_) | ObjectType::ByteString(_) => sym::STRING.into(), | |||
ObjectType::SubrFn(_) => sym::SUBR.into(), | |||
ObjectType::Buffer(_) => sym::BUFFER.into(), | |||
ObjectType::CharTable(_) => todo!(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried changing the sym
module, but it doesn't quite work as I was pointed to a file outside of the project.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it! This should now be fixed.
src/data.rs
Outdated
@@ -374,6 +377,10 @@ pub(crate) fn aref<'ob>(array: Object<'ob>, idx: usize, cx: &'ob Context) -> Res | |||
Some(x) => Ok(x), | |||
None => Err(anyhow!("index {idx} is out of bounds")), | |||
}, | |||
ObjectType::CharTable(chartab) => match chartab.get(&idx) { | |||
Some(x) => Ok(x), | |||
None => Err(anyhow!("index {idx} is out of bounds")), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will actually never happen in the current implementation since we currently return NIL. Maybe we don't want that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can return NIL
on the None
branch (example).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed it so that in chartab.get(..)
we return None
instead of NIL
. When we match against None
here, we now return NIL
instead.
src/data.rs
Outdated
@@ -392,6 +399,7 @@ fn type_of(object: Object) -> Object { | |||
ObjectType::String(_) | ObjectType::ByteString(_) => sym::STRING.into(), | |||
ObjectType::SubrFn(_) => sym::SUBR.into(), | |||
ObjectType::Buffer(_) => sym::BUFFER.into(), | |||
ObjectType::CharTable(_) => todo!(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
src/data.rs
Outdated
@@ -374,6 +377,10 @@ pub(crate) fn aref<'ob>(array: Object<'ob>, idx: usize, cx: &'ob Context) -> Res | |||
Some(x) => Ok(x), | |||
None => Err(anyhow!("index {idx} is out of bounds")), | |||
}, | |||
ObjectType::CharTable(chartab) => match chartab.get(&idx) { | |||
Some(x) => Ok(x), | |||
None => Err(anyhow!("index {idx} is out of bounds")), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can return NIL
on the None
branch (example).
src/data.rs
Outdated
@@ -335,6 +335,9 @@ pub(crate) fn aset<'ob>( | |||
Err(anyhow!("index {idx} is out of bounds. Length was {len}")) | |||
} | |||
} | |||
ObjectType::CharTable(_) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for now, you can just use a RefCell
.
src/core/object/chartab.rs
Outdated
Self(GcHeap::new(table, constant)) | ||
} | ||
|
||
pub(super) fn display_walk( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
display_walk
is used when we have recursive data structures like lists or vectors. For something like a char-table which is not recursive, we can just Implement Display
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is now fixed!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just noticed that we want these sorted. I just do that in the display function itself.
src/core/object/chartab.rs
Outdated
} | ||
|
||
#[derive(PartialEq, Eq, Trace, Debug)] | ||
pub(crate) struct LispCharTable(GcHeap<CharTable<'static>>); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I generally only use the Lisp
prefix for thing that already have a rust analog (i.e. String
, Vec
, HashMap
, etc). For this we can change LispCharTable
to CharTable
and CharTable
to something like CharTableData
or CharTableInner
. We can even make the inner struct private and only expose methods on the top level struct if we want.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Renamed LispCharTable
to CharTable
. I opted for the inner struct to CharTableInner
since we have that same name pattern in other places.
I tried making the CharTableInner
private. However, the IntoObject
trait is implemented in tagged.rs
hinders me from doing so.
Thanks! |
Fixes #105
This is a first iteration of char table to get things tarted. It is obviously not what we want in the end, especially since this implementation uses
std::collections::HashMap
under the hood. There's also functionality still missing, such as