From bbdfae081debfef2018cca3f9b56e3e6fb4cd574 Mon Sep 17 00:00:00 2001 From: sofia Date: Tue, 22 Jul 2025 19:50:54 +0300 Subject: [PATCH] Fix std Strings --- reid/lib/std.reid | 14 +++++++++++++- reid/src/lib.rs | 2 +- reid/src/mir/fmt.rs | 22 +++++++++++++++++++++- reid/src/mir/typeinference.rs | 8 ++++---- reid/src/mir/typerefs.rs | 11 +++++++---- 5 files changed, 46 insertions(+), 11 deletions(-) diff --git a/reid/lib/std.reid b/reid/lib/std.reid index c0e1c9a..b8991a6 100644 --- a/reid/lib/std.reid +++ b/reid/lib/std.reid @@ -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); } \ No newline at end of file diff --git a/reid/src/lib.rs b/reid/src/lib.rs index cd4897f..61903ad 100644 --- a/reid/src/lib.rs +++ b/reid/src/lib.rs @@ -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)] diff --git a/reid/src/mir/fmt.rs b/reid/src/mir/fmt.rs index 6e1452d..dbe054f 100644 --- a/reid/src/mir/fmt.rs +++ b/reid/src/mir/fmt.rs @@ -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 { diff --git a/reid/src/mir/typeinference.rs b/reid/src/mir/typeinference.rs index 8574da3..c109879 100644 --- a/reid/src/mir/typeinference.rs +++ b/reid/src/mir/typeinference.rs @@ -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) => { diff --git a/reid/src/mir/typerefs.rs b/reid/src/mir/typerefs.rs index 2e6586d..2be4ad7 100644 --- a/reid/src/mir/typerefs.rs +++ b/reid/src/mir/typerefs.rs @@ -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>; #[derive(Debug, Default)] pub struct TypeRefs { /// Simple list of types that variables can refrence - hints: RefCell>, + pub(super) hints: RefCell>, /// Indirect ID-references, referring to hints-vec - type_refs: RefCell>, + pub(super) type_refs: RefCell>, } 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();