lower literal patterns

This commit is contained in:
Josh Mcguigan 2020-04-01 03:37:51 -07:00
parent 8cce752bcb
commit a9d0c9b2a4

View File

@ -417,26 +417,7 @@ fn collect_expr(&mut self, expr: ast::Expr) -> ExprId {
}
}
ast::Expr::Literal(e) => {
let lit = match e.kind() {
LiteralKind::IntNumber { suffix } => {
let known_name = suffix.and_then(|it| BuiltinInt::from_suffix(&it));
Literal::Int(Default::default(), known_name)
}
LiteralKind::FloatNumber { suffix } => {
let known_name = suffix.and_then(|it| BuiltinFloat::from_suffix(&it));
Literal::Float(Default::default(), known_name)
}
LiteralKind::ByteString => Literal::ByteString(Default::default()),
LiteralKind::String => Literal::String(Default::default()),
LiteralKind::Byte => Literal::Int(Default::default(), Some(BuiltinInt::U8)),
LiteralKind::Bool => Literal::Bool(Default::default()),
LiteralKind::Char => Literal::Char(Default::default()),
};
self.alloc_expr(Expr::Literal(lit), syntax_ptr)
}
ast::Expr::Literal(e) => self.alloc_expr(Expr::Literal(e.kind().into()), syntax_ptr),
ast::Expr::IndexExpr(e) => {
let base = self.collect_expr_opt(e.base());
let index = self.collect_expr_opt(e.index());
@ -679,10 +660,19 @@ fn collect_pat(&mut self, pat: ast::Pat) -> PatId {
suffix: suffix.into_iter().map(|p| self.collect_pat(p)).collect(),
}
}
ast::Pat::LiteralPat(lit) => {
if let Some(ast_lit) = lit.literal() {
let expr = Expr::Literal(ast_lit.kind().into());
let expr_ptr = AstPtr::new(&ast::Expr::Literal(ast_lit));
let expr_id = self.alloc_expr(expr, expr_ptr);
Pat::Lit(expr_id)
} else {
Pat::Missing
}
}
// FIXME: implement
ast::Pat::BoxPat(_) => Pat::Missing,
ast::Pat::LiteralPat(_) => Pat::Missing,
ast::Pat::RangePat(_) => Pat::Missing,
};
let ptr = AstPtr::new(&pat);
@ -741,3 +731,25 @@ fn from(ast_op: ast::BinOp) -> Self {
}
}
}
impl From<ast::LiteralKind> for Literal {
fn from(ast_lit_kind: ast::LiteralKind) -> Self {
match ast_lit_kind {
LiteralKind::IntNumber { suffix } => {
let known_name = suffix.and_then(|it| BuiltinInt::from_suffix(&it));
Literal::Int(Default::default(), known_name)
}
LiteralKind::FloatNumber { suffix } => {
let known_name = suffix.and_then(|it| BuiltinFloat::from_suffix(&it));
Literal::Float(Default::default(), known_name)
}
LiteralKind::ByteString => Literal::ByteString(Default::default()),
LiteralKind::String => Literal::String(Default::default()),
LiteralKind::Byte => Literal::Int(Default::default(), Some(BuiltinInt::U8)),
LiteralKind::Bool => Literal::Bool(Default::default()),
LiteralKind::Char => Literal::Char(Default::default()),
}
}
}