Skip to content

Commit

Permalink
minor: remove decl from TypeAst.
Browse files Browse the repository at this point in the history
  • Loading branch information
Shylock-Hg committed Feb 16, 2025
1 parent 62e1d23 commit 52bdb9b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 56 deletions.
5 changes: 1 addition & 4 deletions tigerc/src/translate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,7 @@ impl<F: Frame + PartialEq + Eq> Translate<F> {

pub fn translate(&mut self, ty_ast: &type_ast::TypeAst) -> ir::Exp {
let level = Level::<F>::outermost();
match ty_ast {
type_ast::TypeAst::TypeDecl(decl) => unreachable!(),
type_ast::TypeAst::TypeExpr(exp) => self.translate_expr(&level, exp),
}
self.translate_expr(&level, &ty_ast.0)
}

fn translate_decl(&mut self, level: &Level<F>, decl: &type_ast::TypeDecl) -> Option<Statement> {
Expand Down
5 changes: 1 addition & 4 deletions tigerc/src/type_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ pub struct Function {
// typed AST...

#[derive(Debug, PartialEq, Eq)]
pub enum TypeAst {
TypeDecl(TypeDecl),
TypeExpr(TypeExpr),
}
pub struct TypeAst(pub TypeExpr);

use crate::ast::Value;

Expand Down
60 changes: 12 additions & 48 deletions tigerc/src/type_inference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ impl TypeInference {
}

pub fn infer(&mut self, ast: &ast::Ast) -> Result<type_ast::TypeAst> {
Ok(type_ast::TypeAst::TypeExpr(self.infer_expr(&ast.0)?))
Ok(type_ast::TypeAst(self.infer_expr(&ast.0)?))
}

// TODO maybe return void
Expand Down Expand Up @@ -786,11 +786,7 @@ mod tests {
let e = parser.parse();
let mut ti = TypeInference::new();
let te = ti.infer(&e).unwrap();
if let type_ast::TypeAst::TypeExpr(te) = te {
assert_eq!(te.ty, type_ast::Type::Nothing);
} else {
panic!("Unexpected decl.");
}
assert_eq!(te.0.ty, type_ast::Type::Nothing);
}

#[test]
Expand All @@ -803,11 +799,7 @@ mod tests {
let e = parser.parse();
let mut ti = TypeInference::new();
let te = ti.infer(&e).unwrap();
if let type_ast::TypeAst::TypeExpr(te) = te {
assert_eq!(te.ty, type_ast::Type::Nil);
} else {
panic!("Unexpected decl.");
}
assert_eq!(te.0.ty, type_ast::Type::Nil);
}

#[test]
Expand All @@ -820,11 +812,7 @@ mod tests {
let e = parser.parse();
let mut ti = TypeInference::new();
let te = ti.infer(&e).unwrap();
if let type_ast::TypeAst::TypeExpr(te) = te {
assert_eq!(te.ty, type_ast::Type::Int);
} else {
panic!("Unexpected decl.");
}
assert_eq!(te.0.ty, type_ast::Type::Int);
}

#[test]
Expand All @@ -837,11 +825,7 @@ mod tests {
let e = parser.parse();
let mut ti = TypeInference::new();
let te = ti.infer(&e).unwrap();
if let type_ast::TypeAst::TypeExpr(te) = te {
assert_eq!(te.ty, type_ast::Type::Int);
} else {
panic!("Unexpected decl.");
}
assert_eq!(te.0.ty, type_ast::Type::Int);
}

#[test]
Expand All @@ -854,11 +838,7 @@ mod tests {
let e = parser.parse();
let mut ti = TypeInference::new();
let te = ti.infer(&e).unwrap();
if let type_ast::TypeAst::TypeExpr(te) = te {
assert_eq!(te.ty, type_ast::Type::Int);
} else {
panic!("Unexpected decl.");
}
assert_eq!(te.0.ty, type_ast::Type::Int);
}

#[test]
Expand All @@ -871,11 +851,7 @@ mod tests {
let e = parser.parse();
let mut ti = TypeInference::new();
let te = ti.infer(&e).unwrap();
if let type_ast::TypeAst::TypeExpr(te) = te {
assert_eq!(te.ty, type_ast::Type::Int);
} else {
panic!("Unexpected decl.");
}
assert_eq!(te.0.ty, type_ast::Type::Int);
}

#[test]
Expand All @@ -888,11 +864,7 @@ mod tests {
let e = parser.parse();
let mut ti = TypeInference::new();
let te = ti.infer(&e).unwrap();
if let type_ast::TypeAst::TypeExpr(te) = te {
assert_eq!(te.ty, type_ast::Type::Int);
} else {
panic!("Unexpected decl.");
}
assert_eq!(te.0.ty, type_ast::Type::Int);
}

#[test]
Expand All @@ -905,11 +877,7 @@ mod tests {
let e = parser.parse();
let mut ti = TypeInference::new();
let te = ti.infer(&e).unwrap();
if let type_ast::TypeAst::TypeExpr(te) = te {
assert_eq!(te.ty, type_ast::Type::Nothing);
} else {
panic!("Unexpected decl.");
}
assert_eq!(te.0.ty, type_ast::Type::Nothing);
}

#[test]
Expand All @@ -922,11 +890,7 @@ mod tests {
let e = parser.parse();
let mut ti = TypeInference::new();
let te = ti.infer(&e).unwrap();
if let type_ast::TypeAst::TypeExpr(te) = te {
assert_eq!(te.ty, type_ast::Type::Nothing);
} else {
panic!("Unexpected decl.");
}
assert_eq!(te.0.ty, type_ast::Type::Nothing);
}

#[test]
Expand Down Expand Up @@ -968,7 +932,7 @@ mod tests {
let e = parser.parse();
let mut ti = TypeInference::new();
let te = ti.infer(&e).unwrap();
let expected = type_ast::TypeAst::TypeExpr(type_ast::TypeExpr {
let expected = type_ast::TypeAst(type_ast::TypeExpr {
ty: type_ast::Type::Nothing,
expr: type_ast::TypeExpr_::Let(type_ast::Let {
decls: vec![type_ast::TypeDecl::Type(type_ast::TyDecl {
Expand Down Expand Up @@ -1005,7 +969,7 @@ mod tests {
let e = dbg!(parser.parse());
let mut ti = TypeInference::new();
let te = ti.infer(&e).unwrap();
let expected = type_ast::TypeAst::TypeExpr(type_ast::TypeExpr {
let expected = type_ast::TypeAst(type_ast::TypeExpr {
ty: type_ast::Type::Int,
expr: type_ast::TypeExpr_::Let(type_ast::Let {
decls: vec![type_ast::TypeDecl::Func(type_ast::FuncDecl {
Expand Down

0 comments on commit 52bdb9b

Please sign in to comment.