region-based memory management in a c-like form
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

worry about tuple types a bit

+54 -17
+1 -1
frontend/src/ast.rs
··· 54 54 pub enum AllocKind { 55 55 DynArray(Box<Type>), 56 56 Array(Box<Type>, usize), 57 - Tuple, 57 + Tuple(Vec<TypeKind>), 58 58 } 59 59 60 60 #[derive(Debug, PartialEq, Eq, Clone, Copy)]
+18 -4
frontend/src/ast_visitor.rs
··· 10 10 TypeKind::Unit => mir::Ty::Unit, 11 11 TypeKind::Alloc(kind, _) => match kind { 12 12 // TODO: resolve tuple tys 13 - AllocKind::Tuple => mir::Ty::Tuple(vec![]), 13 + AllocKind::Tuple(tys) => { 14 + let tys = tys.iter().map(|t| ast_type_to_mir_type(&t)).collect(); 15 + 16 + mir::Ty::Tuple(tys) 17 + } 18 + 14 19 AllocKind::DynArray(ty) => mir::Ty::DynArray(Box::new(ast_type_to_mir_type(&ty.node))), 15 20 AllocKind::Array(ty, len) => { 16 21 mir::Ty::Array(Box::new(ast_type_to_mir_type(&ty.node)), *len) ··· 199 204 let ty = ast_type_to_mir_type(&ty.node); 200 205 mir::RValue::Alloc(mir::AllocKind::Array(ty), ops) 201 206 } 202 - AllocKind::Tuple => mir::RValue::Alloc(mir::AllocKind::Tuple, ops), 203 - AllocKind::DynArray(_ty) => { 204 - todo!() 207 + AllocKind::Tuple(tys) => { 208 + let tys = tys 209 + .into_iter() 210 + .map(|t| ast_type_to_mir_type(&t)) 211 + .collect(); 212 + 213 + mir::RValue::Alloc(mir::AllocKind::Tuple(tys), ops) 214 + } 215 + 216 + AllocKind::DynArray(ty) => { 217 + let ty = ast_type_to_mir_type(&ty.node); 218 + mir::RValue::Alloc(mir::AllocKind::DynArray(ty), ops) 205 219 } 206 220 } 207 221 }
+12 -2
frontend/src/mir/mod.rs
··· 54 54 #[derive(Debug, PartialEq, Eq, Clone)] 55 55 pub enum AllocKind { 56 56 Array(Ty), 57 - Tuple, 57 + DynArray(Ty), 58 + Tuple(Vec<Ty>), 58 59 } 59 60 60 61 impl Display for AllocKind { 61 62 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 62 63 match self { 63 64 AllocKind::Array(ty) => write!(f, "array<{}>", ty), 64 - AllocKind::Tuple => write!(f, "tuple"), 65 + AllocKind::Tuple(tys) => { 66 + let ty_str = tys 67 + .iter() 68 + .map(|t| t.to_string()) 69 + .collect::<Vec<String>>() 70 + .join(" * "); 71 + 72 + write!(f, "{}", ty_str) 73 + } 74 + AllocKind::DynArray(ty) => write!(f, "dyn_array<{}>", ty), 65 75 } 66 76 } 67 77 }
+5 -9
frontend/src/parse.rs
··· 326 326 327 327 Ok(Spanned::new( 328 328 ExprKind::Allocation { 329 - kind: AllocKind::Tuple, 329 + // filled in during typechecking 330 + kind: AllocKind::Tuple(vec![]), 330 331 elements: exprs, 331 332 region: None, 332 333 }, ··· 729 730 730 731 #[cfg(test)] 731 732 mod tests { 732 - use std::clone; 733 - 734 733 use super::*; 735 - use crate::{ 736 - ast::{Type, UserDefinedType}, 737 - ctx::Symbol, 738 - lex, 739 - }; 734 + use crate::{ast::UserDefinedType, ctx::Symbol, lex}; 740 735 741 736 fn tokenify(s: &str) -> (Ctx, VecDeque<SpannedToken>) { 742 737 let mut ctx = Ctx::new(); ··· 1067 1062 let ExprKind::Allocation { kind, elements, .. } = &expr.node else { 1068 1063 panic!("expected allocation") 1069 1064 }; 1070 - assert!(matches!(kind, AllocKind::Tuple)); 1065 + // types have not been resolved since we did not provide them 1066 + assert_eq!(*kind, AllocKind::Tuple(vec![])); 1071 1067 assert_eq!(elements.len(), 3); 1072 1068 } 1073 1069 }
+18 -1
frontend/src/typecheck.rs
··· 106 106 // TODO: implement region handling 107 107 // regions should be resolved by the time we get here 108 108 let region_handle = region.unwrap_or(Region::Local); 109 - Type::synthetic(TypeKind::Alloc(kind.clone(), region_handle)) 109 + let kind = match kind { 110 + AllocKind::Tuple(tys) => { 111 + let mut types = Vec::new(); 112 + for elem in elements { 113 + types.push(elem.resolve_type(ctx).node); 114 + } 115 + 116 + if tys.len() != 0 && tys.len() != types.len() && *tys != types { 117 + panic!("expected tuple types to be equal"); 118 + } 119 + 120 + // here we resolve the tuple types if they were not provided by a type hint 121 + AllocKind::Tuple(types) 122 + } 123 + _ => kind.clone(), 124 + }; 125 + 126 + Type::synthetic(TypeKind::Alloc(kind, region_handle)) 110 127 } 111 128 } 112 129 }