Fix std Strings

This commit is contained in:
Sofia 2025-07-22 19:50:54 +03:00
parent 3d73c52cb4
commit bbdfae081d
5 changed files with 46 additions and 11 deletions

View File

@ -36,15 +36,27 @@ pub fn new_string() -> String {
pub fn add_char(string: &mut String, c: char) {
if ((*string).length + 1) >= (*string).max_length {
let new = allocate((*string).max_length + 4) as *char;
copy_bits((*string).inner, new, 0, (*string).max_length);
free((*string).inner as *u8);
(*string).max_length = (*string).max_length + 4;
(*string).inner = allocate((*string).max_length) as *char;
(*string).inner = new;
}
(*string).inner[(*string).length] = c;
(((*string).inner) as *u8)[((*string).length + 1)] = 0;
(*string).length = (*string).length + 1;
}
fn copy_bits(from: *char, to: *char, pos: u64, max: u64) -> u8 {
if (pos >= max) {
return 0;
}
to[pos] = from[pos];
return copy_bits(from, to, pos + 1, max);
}
pub fn free_string(string: &mut String) {
free((*string).inner as *u8);
}

View File

@ -150,7 +150,7 @@ pub fn perform_all_passes<'map>(
#[cfg(debug_assertions)]
println!("{:-^100}", "TYPE INFERRER OUTPUT");
#[cfg(debug_assertions)]
dbg!(&refs);
println!("{}", &refs);
#[cfg(debug_assertions)]
println!("{}", &context);
#[cfg(debug_assertions)]

View File

@ -2,7 +2,27 @@ use std::fmt::{Debug, Display, Write};
use crate::pad_adapter::PadAdapter;
use super::*;
use super::{
typerefs::{TypeRef, TypeRefs},
*,
};
impl Display for TypeRefs {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for (i, typeref) in self.type_refs.borrow().iter().enumerate() {
let idx = *typeref.borrow();
writeln!(
f,
"{:<3} = {:<3} = {:?} = {}",
i,
unsafe { *self.recurse_type_ref(idx).borrow() },
self.retrieve_type(idx),
TypeKind::Vague(VagueType::TypeRef(idx)).resolve_ref(self)
)?;
}
Ok(())
}
}
impl Display for Context {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {

View File

@ -13,8 +13,8 @@ use super::{
pass::{Pass, PassResult, PassState},
typecheck::ErrorKind,
typerefs::{ScopeTypeRefs, TypeRef, TypeRefs},
Block, ExprKind, Expression, FunctionDefinition, FunctionDefinitionKind, IfExpression, Module,
ReturnKind, StmtKind, CustomTypeKey,
Block, CustomTypeKey, ExprKind, Expression, FunctionDefinition, FunctionDefinitionKind,
IfExpression, Module, ReturnKind, StmtKind,
TypeKind::*,
VagueType::*,
};
@ -183,8 +183,8 @@ impl Expression {
type_refs
.binop(op, &mut lhs_ref, &mut rhs_ref)
.ok_or(ErrorKind::TypesIncompatible(
lhs_ref.as_type(),
rhs_ref.as_type(),
lhs_ref.resolve_deep().unwrap(),
rhs_ref.resolve_deep().unwrap(),
))
}
ExprKind::FunctionCall(function_call) => {

View File

@ -9,7 +9,10 @@ use crate::mir::VagueType;
use super::{typecheck::ErrorKind, BinaryOperator, TypeKind};
#[derive(Clone)]
pub struct TypeRef<'scope>(TypeIdRef, &'scope ScopeTypeRefs<'scope>);
pub struct TypeRef<'scope>(
pub(super) TypeIdRef,
pub(super) &'scope ScopeTypeRefs<'scope>,
);
impl<'scope> TypeRef<'scope> {
/// Resolve current type in a weak manner, not resolving any Arrays or
@ -54,9 +57,9 @@ type TypeIdRef = Rc<RefCell<usize>>;
#[derive(Debug, Default)]
pub struct TypeRefs {
/// Simple list of types that variables can refrence
hints: RefCell<Vec<TypeKind>>,
pub(super) hints: RefCell<Vec<TypeKind>>,
/// Indirect ID-references, referring to hints-vec
type_refs: RefCell<Vec<TypeIdRef>>,
pub(super) type_refs: RefCell<Vec<TypeIdRef>>,
}
impl TypeRefs {
@ -89,7 +92,7 @@ impl TypeRefs {
}
}
unsafe fn recurse_type_ref(&self, mut idx: usize) -> TypeIdRef {
pub(super) unsafe fn recurse_type_ref(&self, mut idx: usize) -> TypeIdRef {
let refs = self.type_refs.borrow();
let mut inner_idx = refs.get_unchecked(idx);
let mut seen = HashSet::new();