Do some cleanup

This commit is contained in:
Sofia 2025-07-16 18:57:42 +03:00
parent 233ddb60f7
commit f2e4b3eff7
9 changed files with 41 additions and 71 deletions

View File

@ -14,6 +14,8 @@
BINARY="$(echo $1 | cut -d'.' -f1)"".out" BINARY="$(echo $1 | cut -d'.' -f1)"".out"
echo $1
make clean SRC=$1 ; make SRC=$1 && echo "" make clean SRC=$1 ; make SRC=$1 && echo ""
$BINARY ; echo "Return value: ""$?" $BINARY ; echo "Return value: ""$?"

View File

@ -21,7 +21,7 @@ use llvm_sys::{
}; };
use crate::{ use crate::{
CustomTypeKind, NamedStruct, CustomTypeKind,
builder::{TypeHolder, TypeValue}, builder::{TypeHolder, TypeValue},
util::{ErrorMessageHolder, MemoryBufferHolder, from_cstring, into_cstring}, util::{ErrorMessageHolder, MemoryBufferHolder, from_cstring, into_cstring},
}; };

View File

@ -2,7 +2,7 @@ use std::path::PathBuf;
use crate::{ use crate::{
ast::{self}, ast::{self},
mir::{self, NamedVariableRef, StmtKind, StructType}, mir::{self, NamedVariableRef, StmtKind, StructField, StructType},
}; };
impl mir::Context { impl mir::Context {
@ -71,7 +71,13 @@ impl ast::Module {
mir::TypeDefinitionKind::Struct(StructType( mir::TypeDefinitionKind::Struct(StructType(
struct_definition_fields struct_definition_fields
.iter() .iter()
.map(|s| (s.name.clone(), s.ty.clone().into())) .map(|s| {
StructField(
s.name.clone(),
s.ty.clone().into(),
s.range.into(),
)
})
.collect(), .collect(),
)) ))
} }

View File

@ -8,7 +8,7 @@ use reid_lib::{
}; };
use crate::mir::{ use crate::mir::{
self, types::ReturnType, IndexedVariableReference, NamedVariableRef, StructType, self, types::ReturnType, IndexedVariableReference, NamedVariableRef, StructField, StructType,
TypeDefinitionKind, TypeKind, TypeDefinitionKind, TypeKind,
}; };
@ -65,7 +65,7 @@ impl mir::Module {
// TODO: Reorder custom-type definitions such that // TODO: Reorder custom-type definitions such that
// inner types get evaluated first. Otherwise this // inner types get evaluated first. Otherwise this
// will cause a panic! // will cause a panic!
.map(|(_, t)| t.get_type(&type_values)) .map(|StructField(_, t, _)| t.get_type(&type_values))
.collect(), .collect(),
))) )))
} }

View File

@ -55,8 +55,8 @@ impl Display for TypeDefinitionKind {
writeln!(f)?; writeln!(f)?;
let mut state = Default::default(); let mut state = Default::default();
let mut inner_f = PadAdapter::wrap(f, &mut state); let mut inner_f = PadAdapter::wrap(f, &mut state);
for (field_name, field_ty) in &items.0 { for field in &items.0 {
writeln!(inner_f, "{}: {:?},", field_name, field_ty)?; writeln!(inner_f, "{},", field)?;
} }
f.write_char('}') 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 { impl Display for FunctionDefinition {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!( write!(

View File

@ -82,20 +82,21 @@ pub enum VagueType {
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct StructType(pub Vec<(String, TypeKind)>); pub struct StructType(pub Vec<StructField>);
#[derive(Clone, Debug)]
pub struct StructField(pub String, pub TypeKind, pub Metadata);
impl StructType { impl StructType {
pub fn find_index(&self, name: &String) -> Option<u32> { pub fn find_index(&self, name: &String) -> Option<u32> {
self.0 self.0
.iter() .iter()
.enumerate() .enumerate()
.find(|(_, (n, _))| n == name) .find(|(_, StructField(n, _, _))| n == name)
.map(|(i, _)| i as u32) .map(|(i, _)| i as u32)
} }
} }
pub type TypedefMap = HashMap<String, TypeDefinitionKind>;
impl TypeKind { impl TypeKind {
pub fn known(&self) -> Result<TypeKind, VagueType> { pub fn known(&self) -> Result<TypeKind, VagueType> {
if let TypeKind::Vague(vague) = self { 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)] #[derive(Debug, Clone)]
pub enum Literal { pub enum Literal {
I8(i8), I8(i8),

View File

@ -135,10 +135,6 @@ impl Scope {
return_type_hint: self.return_type_hint.clone(), 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> { pub struct PassState<'st, 'sc, TError: STDError + Clone> {

View File

@ -71,7 +71,7 @@ fn check_typedefs_for_recursion<'a, 'b>(
) { ) {
match &typedef.kind { match &typedef.kind {
TypeDefinitionKind::Struct(StructType(fields)) => { 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 let TypeKind::CustomType(name) = field_ty {
if seen.contains(name) { if seen.contains(name) {
state.ok::<_, Infallible>( state.ok::<_, Infallible>(
@ -103,11 +103,11 @@ impl<'t> Pass for TypeCheck<'t> {
match kind { match kind {
TypeDefinitionKind::Struct(StructType(fields)) => { TypeDefinitionKind::Struct(StructType(fields)) => {
let mut fieldmap = HashMap::new(); 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) { if let Some(_) = fieldmap.insert(name, field_ty) {
state.ok::<_, Infallible>( state.ok::<_, Infallible>(
Err(ErrorKind::DuplicateStructField(name.clone())), Err(ErrorKind::DuplicateStructField(name.clone())),
meta.clone(), field_meta.clone(),
); );
} }
} }
@ -649,8 +649,10 @@ impl IndexedVariableReference {
if let Some(kind) = types.get(type_name) { if let Some(kind) = types.get(type_name) {
match &kind { match &kind {
TypeDefinitionKind::Struct(struct_type) => { TypeDefinitionKind::Struct(struct_type) => {
if let Some((_, field_ty)) = if let Some(StructField(_, field_ty, _)) = struct_type
struct_type.0.iter().find(|(n, _)| n == field_name) .0
.iter()
.find(|StructField(n, _, _)| n == field_name)
{ {
Ok(Some(ScopeVariable { Ok(Some(ScopeVariable {
ty: field_ty.clone(), ty: field_ty.clone(),

View File

@ -29,11 +29,17 @@ impl TypeKind {
impl StructType { impl StructType {
pub fn get_field_ty(&self, name: &String) -> Option<&TypeKind> { 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> { 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)
} }
} }