SymTab

Struct SymTab 

Source
pub struct SymTab { /* private fields */ }
Expand description

A simple symbol table that maps string names to integer values.

Each inserted name receives a stable index corresponding to its insertion order. Re-inserting the same name returns the existing index.

Implementations§

Source§

impl SymTab

Source

pub fn new() -> Self

Creates a new, empty symbol table.

Source

pub fn len(&self) -> usize

Returns the number of entries currently stored in the symbol table.

Each entry corresponds to a unique symbol (e.g., variable or identifier) that has been interned.

§Example
let mut symtab = SymTab::new();
assert_eq!(symtab.len(), 0);
symtab.intern("foo");
assert_eq!(symtab.len(), 1);
symtab.intern("baz");
assert_eq!(symtab.len(), 2);
symtab.intern("foo");
assert_eq!(symtab.len(), 2);
Source

pub fn intern(&mut self, name: impl AsRef<str>) -> usize

Inserts the given name if it doesn’t exist and returns its index.

If the name already exists, this returns the existing index without modifying the stored value.

§Examples
let mut st = SymTab::new();
let i = st.intern("a");
assert_eq!(i, 0);
assert_eq!(st.intern("a"), 0); // existing index
Source

pub fn set(&mut self, index: usize, new_value: i64) -> Result<(), SymTabError>

Updates the value at the given index.

Returns Err if the index is out of bounds.

§Examples
let mut st = SymTab::new();
let i = st.intern("x");
st.set(i, 123).unwrap();
assert_eq!(st.get(i).unwrap(), 123);
Source

pub fn get(&self, index: usize) -> Result<i64, SymTabError>

Returns the value stored at the given index.

Returns Err if the index is out of bounds.

Trait Implementations§

Source§

impl Debug for SymTab

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for SymTab

§

impl RefUnwindSafe for SymTab

§

impl Send for SymTab

§

impl Sync for SymTab

§

impl Unpin for SymTab

§

impl UnwindSafe for SymTab

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.