use crate::{ compile::{ctx::Ctx, error::*, ir, visit::*}, syn::{ast::*, op::BinOp, span::*}, }; // basic block pub enum Block { Body(ir::Body), Blocks(Vec), } //////////////////////////////////////////////////////////////////////////////// // TranslateAst //////////////////////////////////////////////////////////////////////////////// pub struct TranslateAst<'c, 't> { ctx: &'c mut Ctx, text: &'t str, } impl<'c, 't> TranslateAst<'c, 't> { pub fn new(ctx: &'c mut Ctx, text: &'t str) -> Self { TranslateAst { ctx, text } } pub fn translate(&mut self, _ast: &Vec) -> Result { todo!() } fn visit_lhs_expr(&mut self, expr: &Expr) -> Result { match expr { Expr::Bin(b) if b.op == BinOp::Dot => todo!(), Expr::Base(BaseExpr { kind: BaseExprKind::Ident, .. }) => { let _name = expr.text_at(self.text); //let name_id = self.ctx. todo!() //Ok(ir::Lhs::Name( } _ => Err(Error::InvalidLhs { span: expr.span() }), } } } impl Visit for TranslateAst<'_, '_> { type Out = Result; fn visit(&mut self, _stmt: &Stmt) -> Self::Out { todo!() } } impl Visit for TranslateAst<'_, '_> { type Out = Result; 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)) } } default_visitor!(Expr for TranslateAst<'_, '_> where Out = Result); impl Visit for TranslateAst<'_, '_> { type Out = Result; fn visit(&mut self, _expr: &BinExpr) -> Self::Out { todo!() } } impl Visit for TranslateAst<'_, '_> { type Out = Result; fn visit(&mut self, _expr: &UnExpr) -> Self::Out { todo!() } } impl Visit for TranslateAst<'_, '_> { type Out = Result; fn visit(&mut self, _expr: &FunCallExpr) -> Self::Out { todo!() } } impl Visit for TranslateAst<'_, '_> { type Out = Result; fn visit(&mut self, _expr: &IndexExpr) -> Self::Out { todo!() } } impl Visit for TranslateAst<'_, '_> { type Out = Result; fn visit(&mut self, _expr: &FunExpr) -> Self::Out { todo!() } } impl Visit for TranslateAst<'_, '_> { type Out = Result; 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::>>()?, ), }; Ok(base) } }