use std::collections::HashMap; use crate::{common::loc::LocBox, parser::ast::{expr::Block, statement::{Statement, TypeAlias}, Ast, Program}}; pub mod predefined; pub fn validate_code(prog: &mut Program) -> anyhow::Result<()> { let Block(items) = prog.ast.clone(); predefined::load_builtin(prog); collect_types(prog, &items); //dbg!(&prog.types); //dbg!(&prog.structs); //dbg!(&prog.enums); //dbg!(&prog.member_functions); //dbg!(&prog.functions); for item in items { match item { Ast::Statement(stat) => { match stat.inner() { Statement::Fn(func) => {} Statement::Let { name, typ, val } => {} Statement::ConstVar { name, typ, val } => {} Statement::StaticVar { name, typ, val } => {} Statement::Enum(enm) => {} Statement::Struct(strct) => {} Statement::TypeAlias(alias) => {} } } Ast::Expr(_) => unreachable!() } } Ok(()) } fn collect_types(prog: &mut Program, items: &Vec) { for item in items { match item { Ast::Statement(stat) => { match stat.inner() { Statement::Fn(func)=> { if let Some(struct_name) = &func.struct_name { if let Some(v) = prog.member_functions.get_mut(&struct_name) { v.insert(func.name.clone(), LocBox::new(stat.loc(), func.clone())); } else { let mut v = HashMap::new(); v.insert(func.name.clone(), LocBox::new(stat.loc(), func.clone())); prog.member_functions.insert(struct_name.clone(), v); } } else { prog.functions.insert(func.name.clone(), LocBox::new(stat.loc(), func.clone())); } } Statement::Enum(enm) => { prog.enums.insert(enm.name.clone(), LocBox::new(stat.loc(), enm.clone())); } Statement::Struct(strct) => { prog.structs.insert(strct.name.clone(), LocBox::new(stat.loc(), strct.clone())); } Statement::TypeAlias(alias) => { let typ = alias.clone().typ.inner().clone(); prog.types.insert(alias.name.clone(), predefined::TypeType::Normal(LocBox::new(stat.loc(), typ))); } Statement::Let { .. } | Statement::ConstVar { .. } | Statement::StaticVar { .. } => (), } } Ast::Expr(_) => unreachable!() } } }