Fixed table indexing and construction, declarations and some instructions. Builds and passes indexing test.

This commit is contained in:
Christian Lincoln 2026-08-28 13:53:28 +01:00 committed by paladin
parent 93599ff5d7
commit 1e288b3948
5 changed files with 2984 additions and 2773 deletions

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -25,16 +25,19 @@ Notes:
type Displacement = u8;
#[cfg(feature="bighash")]
#[cfg(feature = "bighash")]
pub type Hashed = u64;
#[cfg(not(feature="bighash"))]
#[cfg(not(feature = "bighash"))]
pub type Hashed = u32;
pub trait Equivalent<T>: Sized {
fn matches(&self, other: &T) -> bool;
}
impl<T> Equivalent<T> for T where T: PartialEq {
impl<T> Equivalent<T> for T
where
T: PartialEq,
{
fn matches(&self, other: &T) -> bool {
self == other
}
@ -75,7 +78,13 @@ impl TableEntry for KeyValue {
}
}
fn is_vacant(&self) -> bool {
matches!(self,KeyValue { index: Value::Nil, .. })
matches!(
self,
KeyValue {
index: Value::Nil,
..
}
)
}
}
@ -87,7 +96,8 @@ struct Entry<E> {
}
#[derive(Clone, Debug)]
pub struct Map<E: TableEntry + Clone> { // default representing vacancy!
pub struct Map<E: TableEntry + Clone> {
// default representing vacancy!
map: Vec<Entry<E>>,
map_bounds: std::ops::Range<usize>,
map_count: usize, // number of elements in table
@ -95,14 +105,19 @@ pub struct Map<E: TableEntry + Clone> { // default representing vacancy!
impl<E: TableEntry + Clone> Map<E> {
pub(crate) fn string_cache() { // todo: this isn't a clean way, maybe make a generic Map and use a specific one for the cache and the rest of the language
}
fn exchange_table(&mut self, len: usize) {
let old = std::mem::replace(&mut self.map, vec![Entry {
entry: E::new_vacant(),
home: 0,
displacement: 0,
}; len]);
let old = std::mem::replace(
&mut self.map,
vec![
Entry {
entry: E::new_vacant(),
home: 0,
displacement: 0,
};
len
],
);
for Entry { entry, .. } in old {
if entry.is_vacant() {
self.set_table(entry)
@ -202,7 +217,11 @@ impl<E: TableEntry + Clone> Map<E> {
self.set_table(entry);
}
}
pub(crate) fn rem_table<K>(&mut self, index: K) where E: Equivalent<K>, K: Hashes {
pub(crate) fn rem_table<K>(&mut self, index: K)
where
E: Equivalent<K>,
K: Hashes,
{
if self.map.len() == 0 {
return;
}
@ -256,9 +275,12 @@ impl<E: TableEntry + Clone> Map<E> {
}
}
pub(crate) fn get_table<K>(&mut self, index: K) -> Option<&E>
where E: Equivalent<K>, K: Hashes {
where
E: Equivalent<K>,
K: Hashes,
{
if self.map.len() == 0 {
return None
return None;
}
let location = index.hashed() as usize % self.map.len();
let home = &self.map[location];
@ -377,7 +399,7 @@ impl Table {
} else {
self.table.set_table(KeyValue {
index: Value::Integer(index),
value: item
value: item,
})
}
}
@ -387,25 +409,28 @@ impl Table {
Value::Nil => Err(RunError(
"Attempt to set new index of table with key: nil".to_string(),
)),
index => Ok(self.table.set_table(KeyValue {
index,
value
})),
index => Ok(self.table.set_table(KeyValue { index, value })),
}
}
pub fn get(&mut self, index: Value) -> Result<Value, RunError> {
match index {
Value::Integer(index) => Ok(self
.array
.get(index as usize)
.get(index as usize - 1)
.unwrap_or(&Value::Nil)
.clone()),
Value::Nil => Err(RunError("Attempt to index table with key: nil".to_string())),
index => { // todo: move this into the Map
Ok(self.table.get_table(index).unwrap_or(&KeyValue {
index: Value::Nil,
value: Value::Nil,
}).value.clone())
index => {
// todo: move this into the Map
Ok(self
.table
.get_table(index)
.unwrap_or(&KeyValue {
index: Value::Nil,
value: Value::Nil,
})
.value
.clone())
}
}
}
@ -419,4 +444,4 @@ impl Table {
meta: None,
}
}
}
}

View file

@ -1 +1,2 @@
local table = {}
local table = { "hello", "friend" }
print(table[1], table[2])

1645
src/vm.rs

File diff suppressed because it is too large Load diff