From 069c2775165b2bd9dd9d0f59f8f2ae65d8469d36 Mon Sep 17 00:00:00 2001 From: sofia Date: Mon, 21 Jul 2025 19:32:40 +0300 Subject: [PATCH] Update String to be *str --- reid/lib/std.reid | 4 ++-- reid/src/ast/mod.rs | 2 +- reid/src/ast/parse.rs | 2 +- reid/src/ast/process.rs | 2 +- reid/src/codegen.rs | 18 +++--------------- reid/src/mir/fmt.rs | 2 +- reid/src/mir/implement.rs | 8 ++++---- reid/src/mir/mod.rs | 4 ++-- reid/src/mir/typecheck.rs | 5 ++++- 9 files changed, 19 insertions(+), 28 deletions(-) diff --git a/reid/lib/std.reid b/reid/lib/std.reid index 08ab852..4cf7f15 100644 --- a/reid/lib/std.reid +++ b/reid/lib/std.reid @@ -1,5 +1,5 @@ -extern fn puts(message: string) -> i32; +extern fn puts(message: *str) -> i32; struct DivT { quotient: i32, @@ -8,7 +8,7 @@ struct DivT { extern fn div(numerator: i32, denominator: i32) -> DivT; -pub fn print(message: string) { +pub fn print(message: *str) { puts(message); } diff --git a/reid/src/ast/mod.rs b/reid/src/ast/mod.rs index d78a134..153c5ec 100644 --- a/reid/src/ast/mod.rs +++ b/reid/src/ast/mod.rs @@ -31,7 +31,7 @@ pub enum TypeKind { F128, F80, F128PPC, - String, + Str, Array(Box, u64), Custom(String), Borrow(Box, bool), diff --git a/reid/src/ast/parse.rs b/reid/src/ast/parse.rs index ba8b908..7fc170b 100644 --- a/reid/src/ast/parse.rs +++ b/reid/src/ast/parse.rs @@ -60,7 +60,7 @@ impl Parse for Type { "f80" => TypeKind::F80, "f128" => TypeKind::F128, "f128ppc" => TypeKind::F128PPC, - "string" => TypeKind::String, + "str" => TypeKind::Str, _ => TypeKind::Custom(ident), } } else { diff --git a/reid/src/ast/process.rs b/reid/src/ast/process.rs index 7b18b57..23e1c41 100644 --- a/reid/src/ast/process.rs +++ b/reid/src/ast/process.rs @@ -307,7 +307,7 @@ impl From for mir::TypeKind { ast::TypeKind::Array(type_kind, length) => { mir::TypeKind::Array(Box::new(mir::TypeKind::from(*type_kind.clone())), *length) } - ast::TypeKind::String => mir::TypeKind::StringPtr, + ast::TypeKind::Str => mir::TypeKind::Str, ast::TypeKind::Custom(name) => mir::TypeKind::CustomType(name.clone()), ast::TypeKind::Borrow(type_kind, mutable) => { mir::TypeKind::Borrow(Box::new(mir::TypeKind::from(*type_kind.clone())), *mutable) diff --git a/reid/src/codegen.rs b/reid/src/codegen.rs index ccd17c5..367ecb0 100644 --- a/reid/src/codegen.rs +++ b/reid/src/codegen.rs @@ -247,7 +247,7 @@ impl mir::Module { insert_debug!(&TypeKind::I64); insert_debug!(&TypeKind::I128); insert_debug!(&TypeKind::Void); - insert_debug!(&TypeKind::StringPtr); + insert_debug!(&TypeKind::Str); for typedef in &self.typedefs { let type_value = match &typedef.kind { @@ -1206,7 +1206,7 @@ impl TypeKind { TypeKind::F128 => Type::F128, TypeKind::F80 => Type::F80, TypeKind::F128PPC => Type::F128PPC, - TypeKind::StringPtr => Type::Ptr(Box::new(Type::I8)), + TypeKind::Str => Type::I8, TypeKind::Array(elem_t, len) => { Type::Array(Box::new(elem_t.get_type(type_vals, typedefs)), *len) } @@ -1257,18 +1257,6 @@ impl TypeKind { let name = format!("{}", self); let data = match self { - TypeKind::StringPtr => DebugTypeData::Pointer(DebugPointerType { - name, - pointee: TypeKind::I8.get_debug_type_hard( - scope, - debug_info, - debug_types, - type_values, - types, - tokens, - ), - size_bits: self.size_of(), - }), TypeKind::CodegenPtr(inner) | TypeKind::UserPtr(inner) | TypeKind::Borrow(inner, _) => { DebugTypeData::Pointer(DebugPointerType { name, @@ -1358,7 +1346,7 @@ impl TypeKind { | TypeKind::F128 | TypeKind::F128PPC => DwarfEncoding::Float, TypeKind::Void => DwarfEncoding::Address, - TypeKind::StringPtr => DwarfEncoding::Address, + TypeKind::Str => DwarfEncoding::UnsignedChar, TypeKind::Array(_, _) => DwarfEncoding::Address, TypeKind::CustomType(_) => DwarfEncoding::Address, _ => panic!("tried fetching debug-type for non-supported type!"), diff --git a/reid/src/mir/fmt.rs b/reid/src/mir/fmt.rs index 7352f9a..6e00663 100644 --- a/reid/src/mir/fmt.rs +++ b/reid/src/mir/fmt.rs @@ -335,7 +335,7 @@ impl Display for TypeKind { TypeKind::U64 => write!(f, "u64"), TypeKind::U128 => write!(f, "u128"), TypeKind::Void => write!(f, "void"), - TypeKind::StringPtr => write!(f, "string"), + TypeKind::Str => write!(f, "str"), TypeKind::Array(type_kind, len) => { f.write_char('[')?; Display::fmt(type_kind, f)?; diff --git a/reid/src/mir/implement.rs b/reid/src/mir/implement.rs index c13ccc4..aab7ba1 100644 --- a/reid/src/mir/implement.rs +++ b/reid/src/mir/implement.rs @@ -43,7 +43,7 @@ impl TypeKind { TypeKind::U64 => false, TypeKind::U128 => false, TypeKind::Void => false, - TypeKind::StringPtr => false, + TypeKind::Str => false, TypeKind::Array(_, _) => false, TypeKind::CustomType(_) => false, TypeKind::CodegenPtr(_) => false, @@ -74,7 +74,7 @@ impl TypeKind { TypeKind::U64 => false, TypeKind::U128 => false, TypeKind::Void => false, - TypeKind::StringPtr => false, + TypeKind::Str => false, TypeKind::Array(_, _) => false, TypeKind::CustomType(_) => false, TypeKind::CodegenPtr(_) => false, @@ -105,7 +105,7 @@ impl TypeKind { TypeKind::I128 => 128, TypeKind::U128 => 128, TypeKind::Void => 0, - TypeKind::StringPtr => 32, + TypeKind::Str => 8, TypeKind::Array(type_kind, len) => type_kind.size_of() * len, TypeKind::CustomType(_) => 32, TypeKind::CodegenPtr(_) => 64, @@ -136,7 +136,7 @@ impl TypeKind { TypeKind::I128 => 128, TypeKind::U128 => 128, TypeKind::Void => 0, - TypeKind::StringPtr => 32, + TypeKind::Str => 8, TypeKind::Array(type_kind, _) => type_kind.alignment(), TypeKind::CustomType(_) => 32, TypeKind::CodegenPtr(_) => 64, diff --git a/reid/src/mir/mod.rs b/reid/src/mir/mod.rs index af21d79..18da67a 100644 --- a/reid/src/mir/mod.rs +++ b/reid/src/mir/mod.rs @@ -98,7 +98,7 @@ pub enum TypeKind { F128, F80, F128PPC, - StringPtr, + Str, Array(Box, u64), CustomType(String), Borrow(Box, bool), @@ -185,7 +185,7 @@ impl Literal { Literal::U64(_) => TypeKind::U64, Literal::U128(_) => TypeKind::U128, Literal::Bool(_) => TypeKind::Bool, - Literal::String(_) => TypeKind::StringPtr, + Literal::String(_) => TypeKind::UserPtr(Box::new(TypeKind::Str)), Literal::Vague(VagueLiteral::Number(_)) => TypeKind::Vague(VagueType::Integer), Literal::Vague(VagueLiteral::Decimal(_)) => TypeKind::Vague(VagueType::Decimal), Literal::F16(_) => TypeKind::F16, diff --git a/reid/src/mir/typecheck.rs b/reid/src/mir/typecheck.rs index 8fb5ebf..630e904 100644 --- a/reid/src/mir/typecheck.rs +++ b/reid/src/mir/typecheck.rs @@ -742,7 +742,10 @@ impl Literal { (L::F128(_), TypeKind::F128) => self, (L::F128PPC(_), TypeKind::F128PPC) => self, (L::Bool(_), TypeKind::Bool) => self, - (L::String(_), TypeKind::StringPtr) => self, + (L::String(_), TypeKind::UserPtr(ptr)) => match *ptr.clone() { + TypeKind::Str => self, + _ => Err(ErrorKind::LiteralIncompatible(self, hint.clone()))?, + }, // TODO make sure that v is actually able to fit in the // requested type (L::Vague(VagueL::Number(v)), TypeKind::I8) => L::I8(v as i8),