From 44411f03927f1ef854aab387676054e8c6e3c827 Mon Sep 17 00:00:00 2001 From: Alek Ratzloff Date: Wed, 27 May 2020 15:16:45 -0400 Subject: [PATCH] Update block AST translation to accept a TranslateAst object instead of a Ctx object TranslateAst is the correct context to have when translating the AST, since it holds a Ctx object itself, as well as the text of the file that was parsed. Signed-off-by: Alek Ratzloff --- src/compile/block.rs | 118 +++++++++++++++++++++---------------------- 1 file changed, 58 insertions(+), 60 deletions(-) diff --git a/src/compile/block.rs b/src/compile/block.rs index a23c0b9..cf1e624 100644 --- a/src/compile/block.rs +++ b/src/compile/block.rs @@ -1,6 +1,6 @@ use crate::{ compile::{ctx::Ctx, error::*, ir}, - syn::ast::*, + syn::{ast::prelude::*, span::*}, }; // basic block @@ -10,18 +10,18 @@ pub enum Block { } trait ToIr { - fn to_ir(&self, ctx: &mut Ctx) -> Result; + fn to_ir(&self, trans: &mut TranslateAst<'_, '_>) -> Result; } trait FromAst: Sized { - fn from_ast(other: &A, ctx: &mut Ctx) -> Result; + fn from_ast(other: &A, trans: &mut TranslateAst<'_, '_>) -> Result; } impl FromAst for I where A: ToIr { - fn from_ast(other: &A, ctx: &mut Ctx) -> Result { - other.to_ir(ctx) + fn from_ast(other: &A, trans: &mut TranslateAst<'_, '_>) -> Result { + other.to_ir(trans) } } @@ -42,44 +42,84 @@ impl<'c, 't> TranslateAst<'c, 't> { pub fn translate(&mut self, ast: &Vec) -> Result { let mut body = Vec::new(); for stmt in ast.iter() { - body.push(stmt.to_ir(self.ctx)?); + body.push(stmt.to_ir(self)?); } Ok(body) } } +//////////////////////////////////////////////////////////////////////////////// +// Statement conversions +//////////////////////////////////////////////////////////////////////////////// + impl ToIr for Stmt { - fn to_ir(&self, _ctx: &mut Ctx) -> Result { + fn to_ir(&self, _trans: &mut TranslateAst<'_, '_>) -> Result { todo!() } } impl ToIr for AssignStmt { - fn to_ir(&self, _ctx: &mut Ctx) -> Result { + fn to_ir(&self, _trans: &mut TranslateAst<'_, '_>) -> Result { todo!() } } +//////////////////////////////////////////////////////////////////////////////// +// Expression conversions +//////////////////////////////////////////////////////////////////////////////// + impl ToIr for Expr { - fn to_ir(&self, _ctx: &mut Ctx) -> Result { + fn to_ir(&self, _trans: &mut TranslateAst<'_, '_>) -> Result { todo!() } } -impl ToIr for Expr { - fn to_ir(&self, _ctx: &mut Ctx) -> Result { +impl ToIr for BinExpr { + fn to_ir(&self, _trans: &mut TranslateAst<'_, '_>) -> Result { todo!() } } +//////////////////////////////////////////////////////////////////////////////// +// LHS conversions +//////////////////////////////////////////////////////////////////////////////// + +impl ToIr for Expr { + fn to_ir(&self, _trans: &mut TranslateAst<'_, '_>) -> Result { + todo!() + } +} + +impl ToIr for BinExpr { + fn to_ir(&self, trans: &mut TranslateAst<'_, '_>) -> Result { + // Only dot-expressions + if self.op != BinOp::Dot { + return Err(Error::InvalidLhs { span: self.span() }); + } + + let lhs: ir::Expr = self.lhs.to_ir(trans)?; + let rhs: ir::Lhs = self.rhs.to_ir(trans)?; + //Expr::GetAttr( + todo!() + } +} + +impl ToIr for BaseExpr { + fn to_ir(&self, trans: &mut TranslateAst<'_, '_>) -> Result { + match &self.kind { + BaseExprKind::Ident => { + let name = self.text_at(trans.text); + let name_id = trans.ctx.name_stack() + .get_scoped(name) + .expect("could not find name"); + Ok(ir::Lhs::Name(name_id)) + } + _ => Err(Error::InvalidLhs { span: self.span() }), + } + } +} + /* -impl Visit for TranslateAst<'_, '_> { - type Out = Result; - - fn visit(&mut self, _stmt: &Stmt) -> Self::Out { - todo!() - } -} impl Visit for TranslateAst<'_, '_> { type Out = Result; @@ -91,48 +131,6 @@ impl Visit for TranslateAst<'_, '_> { } } -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;