Skip to content

Commit

Permalink
fix: remove decl from Ast.
Browse files Browse the repository at this point in the history
  • Loading branch information
Shylock-Hg committed Feb 15, 2025
1 parent cfc71f4 commit 1e7e32a
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 29 deletions.
10 changes: 2 additions & 8 deletions tigerc/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
13 changes: 4 additions & 9 deletions tigerc/src/escape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down
12 changes: 4 additions & 8 deletions tigerc/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Posed<Token>> {
Expand Down Expand Up @@ -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]
Expand All @@ -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]
Expand All @@ -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))),
)))
Expand Down
5 changes: 1 addition & 4 deletions tigerc/src/type_inference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,7 @@ impl TypeInference {
}

pub fn infer(&mut self, ast: &ast::Ast) -> Result<type_ast::TypeAst> {
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
Expand Down

0 comments on commit 1e7e32a

Please sign in to comment.