Spread out implementations of symbol and attrs-related things, add impl
blocks for TranslateAst Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
@@ -6,8 +6,13 @@ use crate::{
|
||||
// basic block
|
||||
pub enum Block {
|
||||
Body(ir::Body),
|
||||
Blocks(Vec<Block>),
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// TranslateAst
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
pub struct TranslateAst<'c, 't> {
|
||||
ctx: &'c mut Ctx,
|
||||
text: &'t str,
|
||||
@@ -18,7 +23,7 @@ impl<'c, 't> TranslateAst<'c, 't> {
|
||||
TranslateAst { ctx, text }
|
||||
}
|
||||
|
||||
pub fn translate(&mut self, _ast: &Vec<Stmt>) -> Result<ir::Body> {
|
||||
pub fn translate(&mut self, _ast: &Vec<Stmt>) -> Result<Block> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
@@ -34,7 +39,7 @@ impl<'c, 't> TranslateAst<'c, 't> {
|
||||
todo!()
|
||||
//Ok(ir::Lhs::Name(
|
||||
}
|
||||
_ => todo!(),
|
||||
_ => Err(Error::InvalidLhs { span: expr.span() }),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -50,15 +55,97 @@ impl Visit<Stmt> for TranslateAst<'_, '_> {
|
||||
impl Visit<AssignStmt> for TranslateAst<'_, '_> {
|
||||
type Out = Result<ir::Stmt>;
|
||||
|
||||
fn visit(&mut self, _stmt: &AssignStmt) -> Self::Out {
|
||||
todo!()
|
||||
fn visit(&mut self, stmt: &AssignStmt) -> Self::Out {
|
||||
let lhs = self.visit_lhs_expr(&stmt.lhs)?;
|
||||
let rhs = self.visit(&stmt.rhs)?;
|
||||
Ok(ir::Stmt::Assign(lhs, rhs))
|
||||
}
|
||||
}
|
||||
|
||||
impl Visit<Expr> for TranslateAst<'_, '_> {
|
||||
default_visitor!(Expr for TranslateAst<'_, '_> where Out = Result<ir::Expr>);
|
||||
|
||||
impl Visit<BinExpr> for TranslateAst<'_, '_> {
|
||||
type Out = Result<ir::Expr>;
|
||||
|
||||
fn visit(&mut self, _expr: &Expr) -> Self::Out {
|
||||
fn visit(&mut self, _expr: &BinExpr) -> Self::Out {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
impl Visit<UnExpr> for TranslateAst<'_, '_> {
|
||||
type Out = Result<ir::Expr>;
|
||||
|
||||
fn visit(&mut self, _expr: &UnExpr) -> Self::Out {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
impl Visit<FunCallExpr> for TranslateAst<'_, '_> {
|
||||
type Out = Result<ir::Expr>;
|
||||
|
||||
fn visit(&mut self, _expr: &FunCallExpr) -> Self::Out {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
impl Visit<IndexExpr> for TranslateAst<'_, '_> {
|
||||
type Out = Result<ir::Expr>;
|
||||
|
||||
fn visit(&mut self, _expr: &IndexExpr) -> Self::Out {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
impl Visit<FunExpr> for TranslateAst<'_, '_> {
|
||||
type Out = Result<ir::Expr>;
|
||||
|
||||
fn visit(&mut self, _expr: &FunExpr) -> Self::Out {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
impl Visit<BaseExpr> for TranslateAst<'_, '_> {
|
||||
type Out = Result<ir::Expr>;
|
||||
|
||||
fn visit(&mut self, expr: &BaseExpr) -> Self::Out {
|
||||
let base = match &expr.kind {
|
||||
BaseExprKind::Ident => {
|
||||
let _name = self
|
||||
.ctx
|
||||
.name_stack()
|
||||
.get_scoped(expr.text_at(self.text))
|
||||
.unwrap();
|
||||
todo!()
|
||||
}
|
||||
BaseExprKind::Num => {
|
||||
let num_text = expr.text_at(self.text);
|
||||
let _num = if num_text.starts_with("0x") || num_text.starts_with("0X") {
|
||||
i64::from_str_radix(&num_text[2..], 16).unwrap()
|
||||
} else {
|
||||
num_text.parse().unwrap()
|
||||
};
|
||||
todo!()
|
||||
}
|
||||
BaseExprKind::Str => todo!(),
|
||||
BaseExprKind::Sym => {
|
||||
let _sym = self
|
||||
.ctx
|
||||
.syms()
|
||||
.get(&expr.text_at(self.text)[1..])
|
||||
.unwrap();
|
||||
todo!()
|
||||
}
|
||||
BaseExprKind::List(_) => todo!(),
|
||||
BaseExprKind::Object(_) => todo!(),
|
||||
BaseExprKind::Tuple(_) => todo!(),
|
||||
BaseExprKind::Block(block) => ir::Expr::Block(
|
||||
block
|
||||
.iter()
|
||||
.map(|stmt| self.visit(stmt))
|
||||
.collect::<Result<Vec<_>>>()?,
|
||||
),
|
||||
};
|
||||
|
||||
Ok(base)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user