-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
232 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,197 @@ | ||
use fe_parser2::ast; | ||
|
||
use crate::hir_def::expr::*; | ||
use crate::{ | ||
hir_def::{expr::*, IdentId, LitKind, MaybeInvalid, PathId, Stmt}, | ||
span::HirOriginKind, | ||
}; | ||
|
||
use super::body::BodyCtxt; | ||
|
||
impl Expr { | ||
pub(super) fn push_to_body(ctxt: &mut BodyCtxt<'_>, ast: ast::Expr) -> ExprId { | ||
todo!() | ||
let expr = match ast.kind() { | ||
ast::ExprKind::Lit(lit) => { | ||
if let Some(lit) = lit.lit() { | ||
let lit = LitKind::from_ast(ctxt.db, lit); | ||
Self::Lit(lit) | ||
} else { | ||
return ctxt.push_invalid_expr(HirOriginKind::raw(&ast)); | ||
} | ||
} | ||
|
||
ast::ExprKind::Block(block) => { | ||
let mut stmts = vec![]; | ||
for stmt in block.stmts() { | ||
let stmt = Stmt::push_to_body(ctxt, stmt); | ||
stmts.push(stmt); | ||
} | ||
Self::Block(stmts) | ||
} | ||
|
||
ast::ExprKind::Bin(bin) => { | ||
let lhs = Self::push_to_body_opt(ctxt, bin.lhs()); | ||
let rhs = Self::push_to_body_opt(ctxt, bin.rhs()); | ||
let op = bin.op().map(|op| BinOp::from_ast(op)).into(); | ||
Self::Bin(lhs, rhs, op) | ||
} | ||
|
||
ast::ExprKind::Un(un) => { | ||
let expr = Self::push_to_body_opt(ctxt, un.expr()); | ||
let op = un.op().map(|op| UnOp::from_ast(op)).into(); | ||
Self::Un(expr, op) | ||
} | ||
|
||
ast::ExprKind::Call(call) => { | ||
let callee = Self::push_to_body_opt(ctxt, call.callee()); | ||
let args = call | ||
.args() | ||
.map(|args| { | ||
args.into_iter() | ||
.map(|arg| CallArg::from_ast(ctxt, arg)) | ||
.collect() | ||
}) | ||
.unwrap_or_default(); | ||
Self::Call(callee, args) | ||
} | ||
|
||
ast::ExprKind::MethodCall(method_call) => { | ||
let receiver = Self::push_to_body_opt(ctxt, method_call.receiver()); | ||
let method_name = IdentId::maybe_from_token(ctxt.db, method_call.method_name()); | ||
let args = method_call | ||
.args() | ||
.map(|args| { | ||
args.into_iter() | ||
.map(|arg| CallArg::from_ast(ctxt, arg)) | ||
.collect() | ||
}) | ||
.unwrap_or_default(); | ||
Self::MethodCall(receiver, method_name, args) | ||
} | ||
|
||
ast::ExprKind::Path(path) => { | ||
let path = PathId::maybe_from_ast(ctxt.db, path.path()); | ||
Self::Path(path) | ||
} | ||
|
||
ast::ExprKind::RecordInit(record_init) => { | ||
// let path = PathId::maybe_from_ast(ctxt.db, record_init.path()); | ||
todo!() | ||
} | ||
|
||
ast::ExprKind::Field(FieldExpr) => { | ||
todo!() | ||
} | ||
|
||
ast::ExprKind::Index(IndexExpr) => { | ||
todo!() | ||
} | ||
|
||
ast::ExprKind::Tuple(TupleExpr) => { | ||
todo!() | ||
} | ||
|
||
ast::ExprKind::Array(ArrayExpr) => { | ||
todo!() | ||
} | ||
|
||
ast::ExprKind::ArrayRep(ArrayRepExpr) => { | ||
todo!() | ||
} | ||
|
||
ast::ExprKind::If(IfExpr) => { | ||
todo!() | ||
} | ||
|
||
ast::ExprKind::Match(MatchExpr) => { | ||
todo!() | ||
} | ||
|
||
ast::ExprKind::Paren(ParenExpr) => { | ||
todo!() | ||
} | ||
}; | ||
|
||
ctxt.push_expr(expr, HirOriginKind::raw(&ast)) | ||
} | ||
|
||
pub(super) fn push_to_body_opt(ctxt: &mut BodyCtxt<'_>, ast: Option<ast::Expr>) -> ExprId { | ||
todo!() | ||
if let Some(ast) = ast { | ||
Expr::push_to_body(ctxt, ast) | ||
} else { | ||
ctxt.push_missing_expr() | ||
} | ||
} | ||
} | ||
|
||
impl BinOp { | ||
pub fn from_ast(&self, ast: ast::BinOp) -> Self { | ||
pub(super) fn from_ast(ast: ast::BinOp) -> Self { | ||
match ast { | ||
ast::BinOp::Arith(arith) => ArithBinOp::from_ast(arith).into(), | ||
_ => { | ||
todo!() | ||
} | ||
ast::BinOp::Comp(arith) => CompBinOp::from_ast(arith).into(), | ||
ast::BinOp::Logical(arith) => LogicalBinOp::from_ast(arith).into(), | ||
} | ||
} | ||
} | ||
|
||
impl ArithBinOp { | ||
pub(super) fn from_ast(ast: ast::ArithBinOp) -> Self { | ||
match ast { | ||
ast::ArithBinOp::Add(_) => ArithBinOp::Add, | ||
ast::ArithBinOp::Sub(_) => ArithBinOp::Sub, | ||
ast::ArithBinOp::Mul(_) => ArithBinOp::Mul, | ||
ast::ArithBinOp::Div(_) => ArithBinOp::Div, | ||
ast::ArithBinOp::Mod(_) => ArithBinOp::Mod, | ||
ast::ArithBinOp::Pow(_) => ArithBinOp::Pow, | ||
ast::ArithBinOp::LShift(_) => ArithBinOp::LShift, | ||
ast::ArithBinOp::RShift(_) => ArithBinOp::RShift, | ||
ast::ArithBinOp::BitAnd(_) => ArithBinOp::BitAnd, | ||
ast::ArithBinOp::BitOr(_) => ArithBinOp::BitOr, | ||
ast::ArithBinOp::BitXor(_) => ArithBinOp::BitXor, | ||
ast::ArithBinOp::Add(_) => Self::Add, | ||
ast::ArithBinOp::Sub(_) => Self::Sub, | ||
ast::ArithBinOp::Mul(_) => Self::Mul, | ||
ast::ArithBinOp::Div(_) => Self::Div, | ||
ast::ArithBinOp::Mod(_) => Self::Mod, | ||
ast::ArithBinOp::Pow(_) => Self::Pow, | ||
ast::ArithBinOp::LShift(_) => Self::LShift, | ||
ast::ArithBinOp::RShift(_) => Self::RShift, | ||
ast::ArithBinOp::BitAnd(_) => Self::BitAnd, | ||
ast::ArithBinOp::BitOr(_) => Self::BitOr, | ||
ast::ArithBinOp::BitXor(_) => Self::BitXor, | ||
} | ||
} | ||
} | ||
|
||
impl CompBinOp { | ||
pub(super) fn from_ast(ast: ast::CompBinOp) -> Self { | ||
match ast { | ||
ast::CompBinOp::Eq(_) => Self::Eq, | ||
ast::CompBinOp::NotEq(_) => Self::NotEq, | ||
ast::CompBinOp::Lt(_) => Self::Lt, | ||
ast::CompBinOp::LtEq(_) => Self::LtEq, | ||
ast::CompBinOp::Gt(_) => Self::Gt, | ||
ast::CompBinOp::GtEq(_) => Self::GtEq, | ||
} | ||
} | ||
} | ||
|
||
impl LogicalBinOp { | ||
pub(super) fn from_ast(ast: ast::LogicalBinOp) -> Self { | ||
match ast { | ||
ast::LogicalBinOp::And(_) => Self::And, | ||
ast::LogicalBinOp::Or(_) => Self::Or, | ||
} | ||
} | ||
} | ||
|
||
impl UnOp { | ||
pub(super) fn from_ast(ast: ast::UnOp) -> Self { | ||
match ast { | ||
ast::UnOp::Plus(_) => Self::Plus, | ||
ast::UnOp::Minus(_) => Self::Minus, | ||
ast::UnOp::Not(_) => Self::Not, | ||
ast::UnOp::BitNot(_) => Self::BitNot, | ||
} | ||
} | ||
} | ||
|
||
impl CallArg { | ||
fn from_ast(ctxt: &mut BodyCtxt<'_>, ast: ast::CallArg) -> Self { | ||
let label = ast.label().map(|label| IdentId::from_token(ctxt.db, label)); | ||
let expr = Expr::push_to_body_opt(ctxt, ast.expr()); | ||
Self { label, expr } | ||
} | ||
|
||
fn from_ast_opt(ctxt: &mut BodyCtxt<'_>, ast: Option<ast::CallArg>) -> MaybeInvalid<Self> { | ||
ast.map(|ast| Self::from_ast(ctxt, ast)).into() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.