From 1e7e32aff264702d74c607877aa18a58b47fb656 Mon Sep 17 00:00:00 2001 From: Shylock Hg Date: Sat, 15 Feb 2025 11:21:06 +0800 Subject: [PATCH] fix: remove decl from Ast. --- tigerc/src/ast.rs | 10 ++-------- tigerc/src/escape.rs | 13 ++++--------- tigerc/src/parser.rs | 12 ++++-------- tigerc/src/type_inference.rs | 5 +---- 4 files changed, 11 insertions(+), 29 deletions(-) diff --git a/tigerc/src/ast.rs b/tigerc/src/ast.rs index 476122e..ad5478f 100644 --- a/tigerc/src/ast.rs +++ b/tigerc/src/ast.rs @@ -5,17 +5,11 @@ use std::rc::Rc; use crate::ident_pool::Symbol; #[derive(Debug, PartialEq, Eq)] -pub enum Ast { - Decl(Decl), - Expr(Expr), -} +pub struct Ast(pub Expr); impl Display for Ast { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - Ast::Decl(decl) => write!(f, "{}", decl), - Ast::Expr(expr) => write!(f, "{}", expr), - } + write!(f, "{}", self.0) } } diff --git a/tigerc/src/escape.rs b/tigerc/src/escape.rs index 7da9cc8..531ca74 100644 --- a/tigerc/src/escape.rs +++ b/tigerc/src/escape.rs @@ -16,12 +16,7 @@ impl Escape { // mark escape parameters/local variables pub fn escape(&mut self, ast: &mut ast::Ast) { - match ast { - ast::Ast::Expr(expr) => { - self.escape_expr(expr, 0, 0, &None); - } - _ => unreachable!(), - } + self.escape_expr(&mut ast.0, 0, 0, &None); } // depth of nested function @@ -185,7 +180,7 @@ mod tests { let it = tokenize(doc); let mut parser = Parser::new(Box::new(it)); let mut ast = parser.parse(); - let expected = ast::Ast::Expr(ast::Expr::Let(ast::Let { + let expected = ast::Ast(ast::Expr::Let(ast::Let { decls: vec![ ast::Decl::Type(ast::TypeDecl { type_name: ident_pool::symbol("r1"), @@ -239,7 +234,7 @@ mod tests { let mut ast = parser.parse(); Escape::new().escape(&mut ast); let escape = match ast { - ast::Ast::Expr(ast::Expr::Let(ast::Let { decls, .. })) => { + ast::Ast(ast::Expr::Let(ast::Let { decls, .. })) => { let decl = decls.get(1).unwrap(); match decl { ast::Decl::Var(v) => *v.escape.borrow(), @@ -268,7 +263,7 @@ mod tests { let mut ast = parser.parse(); Escape::new().escape(&mut ast); let escape = match ast { - ast::Ast::Expr(ast::Expr::Let(ast::Let { decls, .. })) => { + ast::Ast(ast::Expr::Let(ast::Let { decls, .. })) => { let decl = decls.get(2).unwrap(); match decl { ast::Decl::Func(f) => *f.args[0].escape.borrow(), diff --git a/tigerc/src/parser.rs b/tigerc/src/parser.rs index c0aab48..8ba4be3 100644 --- a/tigerc/src/parser.rs +++ b/tigerc/src/parser.rs @@ -65,11 +65,7 @@ impl<'a> Parser<'a> { } pub fn parse(&mut self) -> ast::Ast { - if let Ok(decl) = self.parse_decl() { - ast::Ast::Decl(decl) - } else { - ast::Ast::Expr(self.parse_expr()) - } + ast::Ast(self.parse_expr()) } fn bump(&mut self) -> Option> { @@ -635,7 +631,7 @@ mod tests { let it = tokenize(doc); let mut parser = Parser::new(Box::new(it)); let ast = parser.parse(); - assert_eq!(ast, ast::Ast::Expr(ast::Expr::Literal(ast::Value::Nil))); + assert_eq!(ast, ast::Ast(ast::Expr::Literal(ast::Value::Nil))); } #[test] @@ -646,7 +642,7 @@ mod tests { let it = tokenize(doc); let mut parser = Parser::new(Box::new(it)); let ast = parser.parse(); - assert_eq!(ast, ast::Ast::Expr(ast::Expr::Literal(ast::Value::Nothing))); + assert_eq!(ast, ast::Ast(ast::Expr::Literal(ast::Value::Nothing))); } #[test] @@ -659,7 +655,7 @@ mod tests { let ast = parser.parse(); assert_eq!( ast, - ast::Ast::Expr(ast::Expr::Binary(ast::Binary::Ne( + ast::Ast(ast::Expr::Binary(ast::Binary::Ne( Box::new(ast::Expr::Literal(ast::Value::Int(1))), Box::new(ast::Expr::Literal(ast::Value::Int(2))), ))) diff --git a/tigerc/src/type_inference.rs b/tigerc/src/type_inference.rs index 0b2e10a..7c1c172 100644 --- a/tigerc/src/type_inference.rs +++ b/tigerc/src/type_inference.rs @@ -193,10 +193,7 @@ impl TypeInference { } pub fn infer(&mut self, ast: &ast::Ast) -> Result { - match ast { - ast::Ast::Decl(decl) => Ok(type_ast::TypeAst::TypeDecl(self.infer_decl(decl)?)), - ast::Ast::Expr(expr) => Ok(type_ast::TypeAst::TypeExpr(self.infer_expr(expr)?)), - } + Ok(type_ast::TypeAst::TypeExpr(self.infer_expr(&ast.0)?)) } // TODO maybe return void