Tweaking CachedString in vm.rs and constants map in compiler.rs for new Map and Set types. Does not build; in progress.

This commit is contained in:
paladin 2026-09-28 11:56:54 +01:00
parent db0ff1348c
commit f8deddcf2c
2 changed files with 310 additions and 223 deletions

View file

@ -5,12 +5,12 @@ use crate::parser::{
Statement, VarArg, Variable,
};
use crate::vm::{Code, Index, Machine, Offset, Prototype, Reference, Value};
use deimos::map::Map;
use std::cmp::PartialEq;
use std::ops::Deref;
use std::rc::Rc;
use deimos::map::{Equivalent, Hashed, Hashes, Map, TableEntry};
pub(crate) struct Traversal<'a,S> {
pub(crate) struct Traversal<'a, S> {
// A 'cursor' of where we are in assumed execution
machine: &'a mut Machine<S>,
contexts: Vec<Context>, // Stack of function prototypes being compiled
@ -20,40 +20,10 @@ pub(crate) struct Context {
// Everything known about a closure at a time
scopes: Vec<Scope>,
vararg: Option<Piece<Option<String>>>,
constants: Map<ConstantEntry>, // map some constant value to an index for not repeating constants
constants: Map<Value, Index>, // map some constant value to an index for not repeating constants
prototype: Prototype,
}
#[derive(Clone, Debug)]
struct ConstantEntry(Value, Index);
impl Equivalent<Self> for ConstantEntry {
fn matches(&self, other: &Self) -> bool {
self.0 == other.0
}
}
impl Equivalent<Value> for ConstantEntry {
fn matches(&self, other: &Value) -> bool {
self.0 == *other
}
}
impl Hashes for ConstantEntry {
fn hashed(&self) -> Hashed {
self.0.hashed()
}
}
impl TableEntry for ConstantEntry {
fn new_vacant() -> Self {
ConstantEntry(Value::Nil, 0)
}
fn is_vacant(&self) -> bool {
matches!(self.0, Value::Nil)
}
}
#[derive(Clone, Debug)]
enum BlindJump {
Break(usize),
@ -70,7 +40,7 @@ pub(crate) struct Scope {
upvalue: bool,
}
impl<S> Traversal<'_,S> {
impl<S> Traversal<'_, S> {
fn compile<T>(&mut self, it: &T) -> Result<(), LoadError>
where
T: Compile,
@ -147,14 +117,8 @@ impl<S> Traversal<'_,S> {
fn push_constant(&mut self, it: Value) -> Index {
// todo: constants need pieces too!!!
// don't use constants that already exist
if let Some(entry) = &mut self
.contexts
.last_mut()
.unwrap()
.constants
.get_table(it.clone())
{
entry.1
if let Some(entry) = self.contexts.last_mut().unwrap().constants.get(it.clone()) {
*entry
} else {
let index = {
let constants = &mut self.contexts.last_mut().unwrap().prototype.constants;
@ -163,11 +127,7 @@ impl<S> Traversal<'_,S> {
}
.try_into()
.unwrap();
self.contexts
.last_mut()
.unwrap()
.constants
.set_table(ConstantEntry(it, index));
self.contexts.last_mut().unwrap().constants.set(it, index);
index
}
}
@ -684,7 +644,11 @@ fn jump_from(here: usize, there: usize) -> Result<Offset, LoadError> {
Ok((there as i64 - here as i64) as Offset)
}
fn resolve_break<S>(traversal: &mut Traversal<S>, begin: usize, end: usize) -> Result<(), LoadError> {
fn resolve_break<S>(
traversal: &mut Traversal<S>,
begin: usize,
end: usize,
) -> Result<(), LoadError> {
for jump in std::mem::replace(&mut traversal.last_scope().jumps, Vec::new()) {
// ?
match jump {
@ -1132,7 +1096,12 @@ impl Compile for Piece<&Block> {
traversal.compile(&return_expression.inner_ref())?;
}
if !return_expressions.is_empty() {
traversal.push_code(return_expressions.first().unwrap().swap(Code::Return(return_index)));
traversal.push_code(
return_expressions
.first()
.unwrap()
.swap(Code::Return(return_index)),
);
}
}
for _ in 0..depth {