Skip to content
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

Merged
merged 11 commits into from
Jan 31, 2025
Merged

Conversation

hesampakdaman
Copy link
Contributor

@hesampakdaman hesampakdaman commented Jan 29, 2025

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

  • setting purpose
  • there are no extra slots

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(_) => {
Copy link
Contributor Author

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.

Copy link
Owner

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.

Copy link
Contributor Author

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.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am saying you can do this

pub struct CharTableInner<'ob> {
    parent: Option<Slot<&'ob CharTable>>,
    data: RefCell<HashMap<usize, Object<'ob>>>,
    init: Option<Slot<Object<'ob>>>,
}

Slot has a get and set method to modify it. So we only need RefCell for data.

Copy link
Contributor Author

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!(),
Copy link
Contributor Author

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.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the sym module is created as part of build.rs. you can declare a new symbol with the defsym macro and it will be added to the sym module (example).

Copy link
Contributor Author

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")),
Copy link
Contributor Author

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?

Copy link
Owner

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).

Copy link
Contributor Author

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!(),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the sym module is created as part of build.rs. you can declare a new symbol with the defsym macro and it will be added to the sym module (example).

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")),
Copy link
Owner

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(_) => {
Copy link
Owner

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.

Self(GcHeap::new(table, constant))
}

pub(super) fn display_walk(
Copy link
Owner

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is now fixed!

Copy link
Contributor Author

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.

}

#[derive(PartialEq, Eq, Trace, Debug)]
pub(crate) struct LispCharTable(GcHeap<CharTable<'static>>);
Copy link
Owner

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.

Copy link
Contributor Author

@hesampakdaman hesampakdaman Jan 30, 2025

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.

@CeleritasCelery CeleritasCelery merged commit a97f4eb into CeleritasCelery:master Jan 31, 2025
10 checks passed
@CeleritasCelery
Copy link
Owner

Thanks!

@hesampakdaman hesampakdaman deleted the master branch January 31, 2025 22:17
@hesampakdaman hesampakdaman restored the master branch January 31, 2025 22:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Sketch of first implementation of CharTable
2 participants