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

Configure Feed

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

"type check" arrays a bit

+46 -19
+2 -2
frontend/src/ast.rs
··· 52 52 53 53 #[derive(Debug, PartialEq, Eq, Clone)] 54 54 pub enum AllocKind { 55 - DynArray(Box<Type>), 56 - Array(Box<Type>, usize), 55 + DynArray(Box<TypeKind>), 56 + Array(Box<TypeKind>, usize), 57 57 Tuple(Vec<TypeKind>), 58 58 } 59 59
+5 -10
frontend/src/ast_visitor.rs
··· 16 16 mir::Ty::Tuple(tys) 17 17 } 18 18 19 - AllocKind::DynArray(ty) => mir::Ty::DynArray(Box::new(ast_type_to_mir_type(&ty.node))), 20 - AllocKind::Array(ty, len) => { 21 - mir::Ty::Array(Box::new(ast_type_to_mir_type(&ty.node)), *len) 22 - } 19 + AllocKind::DynArray(ty) => mir::Ty::DynArray(Box::new(ast_type_to_mir_type(&ty))), 20 + AllocKind::Array(ty, len) => mir::Ty::Array(Box::new(ast_type_to_mir_type(&ty)), *len), 23 21 }, 24 22 _ => todo!(), 25 23 } ··· 201 199 202 200 match kind { 203 201 AllocKind::Array(ty, _) => { 204 - let ty = ast_type_to_mir_type(&ty.node); 202 + let ty = ast_type_to_mir_type(&ty); 205 203 mir::RValue::Alloc(mir::AllocKind::Array(ty), ops) 206 204 } 207 205 AllocKind::Tuple(tys) => { 208 - let tys = tys 209 - .into_iter() 210 - .map(|t| ast_type_to_mir_type(&t)) 211 - .collect(); 206 + let tys = tys.into_iter().map(|t| ast_type_to_mir_type(&t)).collect(); 212 207 213 208 mir::RValue::Alloc(mir::AllocKind::Tuple(tys), ops) 214 209 } 215 210 216 211 AllocKind::DynArray(ty) => { 217 - let ty = ast_type_to_mir_type(&ty.node); 212 + let ty = ast_type_to_mir_type(&ty); 218 213 mir::RValue::Alloc(mir::AllocKind::DynArray(ty), ops) 219 214 } 220 215 }
+2 -2
frontend/src/mir/visualize.rs
··· 23 23 rhs.visualize(indent); 24 24 } 25 25 RValue::Alloc(kind, ops) => { 26 - print!("alloc {} {{", kind); 26 + print!("alloc({}) {{", kind); 27 27 for (i, op) in ops.iter().enumerate() { 28 28 op.visualize(indent); 29 29 if i != ops.len() - 1 { 30 30 print!(", "); 31 31 } 32 32 } 33 - println!("}}"); 33 + print!("}}"); 34 34 } 35 35 } 36 36 }
+2 -2
frontend/src/parse.rs
··· 260 260 let next_tok = tokens.pop_front().ok_or_else(ParseError::eof)?; 261 261 262 262 let alloc_kind = match next_tok.node { 263 - Token::RBracket => AllocKind::DynArray(Type::synthetic(TypeKind::Unit).into()), 263 + Token::RBracket => AllocKind::DynArray(TypeKind::Int.into()), 264 264 Token::Int(i) => { 265 265 expect_next(tokens, Token::RBracket)?; 266 - AllocKind::Array(Type::synthetic(TypeKind::Int).into(), i as usize) 266 + AllocKind::Array(TypeKind::Int.into(), i as usize) 267 267 } 268 268 _ => { 269 269 return Err(ParseError::new(
+33 -2
frontend/src/typecheck.rs
··· 191 191 Ok(()) 192 192 } 193 193 ExprKind::Call(c) => type_check_call(c, ctx), 194 - ExprKind::Allocation { elements, .. } => { 195 - for e in elements { 194 + ExprKind::Allocation { elements, kind, .. } => { 195 + for e in &mut *elements { 196 196 e.typecheck(ctx)?; 197 + } 198 + 199 + match kind { 200 + AllocKind::Tuple(tys) => { 201 + let mut types = Vec::new(); 202 + for elem in elements { 203 + types.push(elem.resolve_type(ctx).node); 204 + } 205 + 206 + if !tys.is_empty() && tys.len() != types.len() && *tys != types { 207 + panic!("expected tuple types to be equal"); 208 + } 209 + 210 + *tys = types; 211 + } 212 + AllocKind::Array(ty_kind, size) => { 213 + assert_eq!(*size, elements.len()); 214 + 215 + assert!( 216 + elements 217 + .iter() 218 + .all(|e| e.resolve_type(ctx).node == **ty_kind) 219 + ); 220 + } 221 + AllocKind::DynArray(ty_kind) => { 222 + assert!( 223 + elements 224 + .iter() 225 + .all(|e| e.resolve_type(ctx).node == **ty_kind) 226 + ); 227 + } 197 228 } 198 229 Ok(()) 199 230 }
+2 -1
tests/working_subset.nm
··· 2 2 untyped_int := 1 3 3 typed_int : int = 2 4 4 array := [2]{ 1, 2 } 5 - // dyn_array := [] { 1, 2 } 5 + dyn_array := [] { 1, 2 } 6 + tuple := (1, 2) 6 7 7 8 println(untyped_int) 8 9 }