From f2e4b3eff76da43df9e5f2005488d4020508ea54 Mon Sep 17 00:00:00 2001 From: sofia Date: Wed, 16 Jul 2025 18:57:42 +0300 Subject: [PATCH] Do some cleanup --- libtest.sh | 2 ++ reid-llvm-lib/src/compile.rs | 2 +- reid/src/ast/process.rs | 10 +++++-- reid/src/codegen.rs | 4 +-- reid/src/mir/display.rs | 10 +++++-- reid/src/mir/mod.rs | 58 ++++-------------------------------- reid/src/mir/pass.rs | 4 --- reid/src/mir/typecheck.rs | 12 ++++---- reid/src/mir/types.rs | 10 +++++-- 9 files changed, 41 insertions(+), 71 deletions(-) diff --git a/libtest.sh b/libtest.sh index 2ef698f..775bf55 100755 --- a/libtest.sh +++ b/libtest.sh @@ -14,6 +14,8 @@ BINARY="$(echo $1 | cut -d'.' -f1)"".out" +echo $1 + make clean SRC=$1 ; make SRC=$1 && echo "" $BINARY ; echo "Return value: ""$?" diff --git a/reid-llvm-lib/src/compile.rs b/reid-llvm-lib/src/compile.rs index 7520c86..c41aee4 100644 --- a/reid-llvm-lib/src/compile.rs +++ b/reid-llvm-lib/src/compile.rs @@ -21,7 +21,7 @@ use llvm_sys::{ }; use crate::{ - CustomTypeKind, NamedStruct, + CustomTypeKind, builder::{TypeHolder, TypeValue}, util::{ErrorMessageHolder, MemoryBufferHolder, from_cstring, into_cstring}, }; diff --git a/reid/src/ast/process.rs b/reid/src/ast/process.rs index 1620a77..4a9d24f 100644 --- a/reid/src/ast/process.rs +++ b/reid/src/ast/process.rs @@ -2,7 +2,7 @@ use std::path::PathBuf; use crate::{ ast::{self}, - mir::{self, NamedVariableRef, StmtKind, StructType}, + mir::{self, NamedVariableRef, StmtKind, StructField, StructType}, }; impl mir::Context { @@ -71,7 +71,13 @@ impl ast::Module { mir::TypeDefinitionKind::Struct(StructType( struct_definition_fields .iter() - .map(|s| (s.name.clone(), s.ty.clone().into())) + .map(|s| { + StructField( + s.name.clone(), + s.ty.clone().into(), + s.range.into(), + ) + }) .collect(), )) } diff --git a/reid/src/codegen.rs b/reid/src/codegen.rs index 5041664..82093aa 100644 --- a/reid/src/codegen.rs +++ b/reid/src/codegen.rs @@ -8,7 +8,7 @@ use reid_lib::{ }; use crate::mir::{ - self, types::ReturnType, IndexedVariableReference, NamedVariableRef, StructType, + self, types::ReturnType, IndexedVariableReference, NamedVariableRef, StructField, StructType, TypeDefinitionKind, TypeKind, }; @@ -65,7 +65,7 @@ impl mir::Module { // TODO: Reorder custom-type definitions such that // inner types get evaluated first. Otherwise this // will cause a panic! - .map(|(_, t)| t.get_type(&type_values)) + .map(|StructField(_, t, _)| t.get_type(&type_values)) .collect(), ))) } diff --git a/reid/src/mir/display.rs b/reid/src/mir/display.rs index a9003f7..912caf3 100644 --- a/reid/src/mir/display.rs +++ b/reid/src/mir/display.rs @@ -55,8 +55,8 @@ impl Display for TypeDefinitionKind { writeln!(f)?; let mut state = Default::default(); let mut inner_f = PadAdapter::wrap(f, &mut state); - for (field_name, field_ty) in &items.0 { - writeln!(inner_f, "{}: {:?},", field_name, field_ty)?; + for field in &items.0 { + writeln!(inner_f, "{},", field)?; } f.write_char('}') } @@ -64,6 +64,12 @@ impl Display for TypeDefinitionKind { } } +impl Display for StructField { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + writeln!(f, "{}: {:?},", self.0, self.1) + } +} + impl Display for FunctionDefinition { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!( diff --git a/reid/src/mir/mod.rs b/reid/src/mir/mod.rs index 3853f9f..4514dca 100644 --- a/reid/src/mir/mod.rs +++ b/reid/src/mir/mod.rs @@ -82,20 +82,21 @@ pub enum VagueType { } #[derive(Clone, Debug)] -pub struct StructType(pub Vec<(String, TypeKind)>); +pub struct StructType(pub Vec); + +#[derive(Clone, Debug)] +pub struct StructField(pub String, pub TypeKind, pub Metadata); impl StructType { pub fn find_index(&self, name: &String) -> Option { self.0 .iter() .enumerate() - .find(|(_, (n, _))| n == name) + .find(|(_, StructField(n, _, _))| n == name) .map(|(i, _)| i as u32) } } -pub type TypedefMap = HashMap; - impl TypeKind { pub fn known(&self) -> Result { if let TypeKind::Vague(vague) = self { @@ -106,55 +107,6 @@ impl TypeKind { } } -impl TypeKind { - pub fn signed(&self, typedefs: &TypedefMap) -> bool { - match self { - TypeKind::Void => false, - TypeKind::Vague(_) => false, - TypeKind::Bool => false, - TypeKind::I8 => false, - TypeKind::I16 => false, - TypeKind::I32 => false, - TypeKind::I64 => false, - TypeKind::I128 => false, - TypeKind::U8 => false, - TypeKind::U16 => false, - TypeKind::U32 => false, - TypeKind::U64 => false, - TypeKind::U128 => false, - TypeKind::StringPtr => false, - TypeKind::Array(_, _) => false, - TypeKind::CustomType(name) => match typedefs.get(name).unwrap() { - TypeDefinitionKind::Struct(_) => false, - }, - } - } - - pub fn is_maths(&self, typedefs: &TypedefMap) -> bool { - use TypeKind::*; - match &self { - I8 => true, - I16 => true, - I32 => true, - I64 => true, - I128 => true, - U8 => true, - U16 => true, - U32 => true, - U64 => true, - U128 => true, - Bool => true, - Vague(_) => false, - Void => false, - StringPtr => false, - Array(_, _) => false, - TypeKind::CustomType(name) => match typedefs.get(name).unwrap() { - TypeDefinitionKind::Struct(_) => false, - }, - } - } -} - #[derive(Debug, Clone)] pub enum Literal { I8(i8), diff --git a/reid/src/mir/pass.rs b/reid/src/mir/pass.rs index 4ccd916..cc5eae3 100644 --- a/reid/src/mir/pass.rs +++ b/reid/src/mir/pass.rs @@ -135,10 +135,6 @@ impl Scope { return_type_hint: self.return_type_hint.clone(), } } - - pub fn get_typedefs(&self) -> &TypedefMap { - &self.types.0 - } } pub struct PassState<'st, 'sc, TError: STDError + Clone> { diff --git a/reid/src/mir/typecheck.rs b/reid/src/mir/typecheck.rs index 16257d8..57da88b 100644 --- a/reid/src/mir/typecheck.rs +++ b/reid/src/mir/typecheck.rs @@ -71,7 +71,7 @@ fn check_typedefs_for_recursion<'a, 'b>( ) { match &typedef.kind { TypeDefinitionKind::Struct(StructType(fields)) => { - for field_ty in fields.iter().map(|(_, ty)| ty) { + for field_ty in fields.iter().map(|StructField(_, ty, _)| ty) { if let TypeKind::CustomType(name) = field_ty { if seen.contains(name) { state.ok::<_, Infallible>( @@ -103,11 +103,11 @@ impl<'t> Pass for TypeCheck<'t> { match kind { TypeDefinitionKind::Struct(StructType(fields)) => { let mut fieldmap = HashMap::new(); - for (name, field_ty) in fields { + for StructField(name, field_ty, field_meta) in fields { if let Some(_) = fieldmap.insert(name, field_ty) { state.ok::<_, Infallible>( Err(ErrorKind::DuplicateStructField(name.clone())), - meta.clone(), + field_meta.clone(), ); } } @@ -649,8 +649,10 @@ impl IndexedVariableReference { if let Some(kind) = types.get(type_name) { match &kind { TypeDefinitionKind::Struct(struct_type) => { - if let Some((_, field_ty)) = - struct_type.0.iter().find(|(n, _)| n == field_name) + if let Some(StructField(_, field_ty, _)) = struct_type + .0 + .iter() + .find(|StructField(n, _, _)| n == field_name) { Ok(Some(ScopeVariable { ty: field_ty.clone(), diff --git a/reid/src/mir/types.rs b/reid/src/mir/types.rs index 6a2ab96..1701fb4 100644 --- a/reid/src/mir/types.rs +++ b/reid/src/mir/types.rs @@ -29,11 +29,17 @@ impl TypeKind { impl StructType { pub fn get_field_ty(&self, name: &String) -> Option<&TypeKind> { - self.0.iter().find(|(n, _)| n == name).map(|(_, ty)| ty) + self.0 + .iter() + .find(|StructField(n, _, _)| n == name) + .map(|StructField(_, ty, _)| ty) } pub fn get_field_ty_mut(&mut self, name: &String) -> Option<&mut TypeKind> { - self.0.iter_mut().find(|(n, _)| n == name).map(|(_, ty)| ty) + self.0 + .iter_mut() + .find(|StructField(n, _, _)| n == name) + .map(|StructField(_, ty, _)| ty) } }