|
| 1 | +use crate::ast::{Expr, Program, Stmt}; |
| 2 | +use crate::semantic::analyzer::error::SemanticError; |
| 3 | +use crate::semantic::resolver::resolver::Resolver; |
| 4 | +pub type AnalzeResult<T> = Result<T, SemanticError>; |
| 5 | +pub struct Analyzer<'a> { |
| 6 | + pub name_resolver: Resolver, |
| 7 | + pub program: &'a Program, |
| 8 | + pub loop_depth: usize, |
| 9 | +} |
| 10 | +impl<'a> Analyzer<'a> { |
| 11 | + pub fn new(program: &'a Program) -> Self { |
| 12 | + Analyzer { |
| 13 | + name_resolver: Resolver::new(), |
| 14 | + program, |
| 15 | + loop_depth: 0, |
| 16 | + } |
| 17 | + } |
| 18 | + pub fn analyze(&mut self) -> AnalzeResult<()> { |
| 19 | + for func in self.program.functions.iter() { |
| 20 | + self.name_resolver.declare_function(func).map_err(|_| { |
| 21 | + SemanticError::DuplicateDeclaration { |
| 22 | + name: func.name.clone(), |
| 23 | + } |
| 24 | + })?; |
| 25 | + } |
| 26 | + |
| 27 | + for func in self.program.functions.iter() { |
| 28 | + self.name_resolver.push_scope(); |
| 29 | + for param in &func.params { |
| 30 | + self.name_resolver |
| 31 | + .declare_variable(¶m.name, ¶m.ty) |
| 32 | + .map_err(|_| SemanticError::DuplicateDeclaration { |
| 33 | + name: param.name.clone(), |
| 34 | + })?; |
| 35 | + } |
| 36 | + self.analyze_block(&Stmt::Block(func.body.clone()))?; |
| 37 | + self.name_resolver.pop_scope(); |
| 38 | + } |
| 39 | + Ok(()) |
| 40 | + } |
| 41 | + fn analyze_block(&mut self, stmt: &Stmt) -> Result<(), SemanticError> { |
| 42 | + if let Stmt::Block(block) = stmt { |
| 43 | + for s in &block.statements { |
| 44 | + self.analyze_stmt(s)?; |
| 45 | + } |
| 46 | + } else { |
| 47 | + // println!("{:?}", stmt); |
| 48 | + self.analyze_stmt(stmt)?; |
| 49 | + } |
| 50 | + Ok(()) |
| 51 | + } |
| 52 | + fn analyze_stmt(&mut self, stmt: &Stmt) -> Result<(), SemanticError> { |
| 53 | + match stmt { |
| 54 | + Stmt::ExprStmt(expr) => { |
| 55 | + if let Some(expr_opt) = expr { |
| 56 | + self.analyze_expr(expr_opt)? |
| 57 | + } |
| 58 | + } |
| 59 | + Stmt::If { |
| 60 | + cond, |
| 61 | + then_branch, |
| 62 | + else_branch, |
| 63 | + } => { |
| 64 | + self.analyze_expr(cond)?; |
| 65 | + self.analyze_block(then_branch)?; |
| 66 | + if let Some(else_branch_opt) = else_branch { |
| 67 | + self.analyze_block(else_branch_opt)?; |
| 68 | + } |
| 69 | + } |
| 70 | + Stmt::While { cond, body } => { |
| 71 | + self.loop_depth += 1; |
| 72 | + self.analyze_expr(cond)?; |
| 73 | + self.analyze_stmt(body)?; |
| 74 | + self.loop_depth -= 1; |
| 75 | + } |
| 76 | + Stmt::Return(expr) => { |
| 77 | + if let Some(expr_opt) = expr { |
| 78 | + self.analyze_expr(expr_opt)?; |
| 79 | + } |
| 80 | + } |
| 81 | + Stmt::Block(inner_stmts) => { |
| 82 | + self.analyze_block(&Stmt::Block(inner_stmts.clone()))?; |
| 83 | + } |
| 84 | + Stmt::For { |
| 85 | + init, |
| 86 | + cond, |
| 87 | + step, |
| 88 | + body, |
| 89 | + } => { |
| 90 | + self.loop_depth += 1; |
| 91 | + self.name_resolver.push_scope(); |
| 92 | + if let Some(init_stmt) = init { |
| 93 | + self.analyze_stmt(init_stmt)?; |
| 94 | + } |
| 95 | + if let Some(cond_expr) = cond { |
| 96 | + self.analyze_expr(cond_expr)?; |
| 97 | + } |
| 98 | + self.analyze_stmt(body)?; |
| 99 | + if let Some(step_expr) = step { |
| 100 | + self.analyze_expr(step_expr)?; |
| 101 | + } |
| 102 | + self.name_resolver.pop_scope(); |
| 103 | + self.loop_depth -= 1; |
| 104 | + } |
| 105 | + Stmt::Declaration { ty, declarators } => { |
| 106 | + for declarator in declarators { |
| 107 | + self.name_resolver |
| 108 | + .declare_variable(&declarator.name, ty) |
| 109 | + .map_err(|_| SemanticError::DuplicateDeclaration { |
| 110 | + name: declarator.name.clone(), |
| 111 | + })?; |
| 112 | + if let Some(init_expr) = &declarator.init { |
| 113 | + self.analyze_expr(&init_expr)?; |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + Stmt::Continue => { |
| 118 | + if self.loop_depth == 0 { |
| 119 | + return Err(SemanticError::InvalidContinue); |
| 120 | + } |
| 121 | + } |
| 122 | + Stmt::Break => { |
| 123 | + if self.loop_depth == 0 { |
| 124 | + return Err(SemanticError::InvalidBreak); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + Ok(()) |
| 129 | + } |
| 130 | + fn analyze_expr(&mut self, expr: &Expr) -> Result<(), SemanticError> { |
| 131 | + match expr { |
| 132 | + Expr::Ident(name) => { |
| 133 | + self.name_resolver |
| 134 | + .resolve_identifier(name) |
| 135 | + .map_err(|_| SemanticError::UndefinedSymbol { name: name.clone() })?; |
| 136 | + } |
| 137 | + Expr::BinaryOp { lhs, rhs, .. } => { |
| 138 | + self.analyze_expr(lhs)?; |
| 139 | + self.analyze_expr(rhs)?; |
| 140 | + } |
| 141 | + Expr::UnaryPostfixOp { lhs, .. } => { |
| 142 | + self.analyze_expr(lhs)?; |
| 143 | + } |
| 144 | + Expr::UnaryPrefixOp { rhs, .. } => { |
| 145 | + self.analyze_expr(rhs)?; |
| 146 | + } |
| 147 | + Expr::Assignment { left, right, .. } => { |
| 148 | + self.analyze_expr(left)?; |
| 149 | + self.analyze_expr(right)?; |
| 150 | + } |
| 151 | + Expr::ArrayIndex { array, index } => { |
| 152 | + self.analyze_expr(array)?; |
| 153 | + self.analyze_expr(index)?; |
| 154 | + } |
| 155 | + Expr::Call { func, args, .. } => match func.as_ref() { |
| 156 | + Expr::Ident(func_name) => { |
| 157 | + self.name_resolver |
| 158 | + .resolve_identifier(func_name) |
| 159 | + .map_err(|_| SemanticError::UndefinedSymbol { |
| 160 | + name: func_name.clone(), |
| 161 | + })?; |
| 162 | + for arg in args { |
| 163 | + self.analyze_expr(arg)?; |
| 164 | + } |
| 165 | + } |
| 166 | + _ => unreachable!(), |
| 167 | + }, |
| 168 | + Expr::InitializerList(exprs) => { |
| 169 | + for e in exprs { |
| 170 | + self.analyze_expr(e)?; |
| 171 | + } |
| 172 | + } |
| 173 | + Expr::CharLiteral(_) | Expr::IntLiteral(_) => {} |
| 174 | + } |
| 175 | + Ok(()) |
| 176 | + } |
| 177 | +} |
0 commit comments