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 <alekratz@gmail.com>
This commit is contained in:
2020-05-27 15:16:45 -04:00
parent b6375a81ff
commit 44411f0392

View File

@@ -1,6 +1,6 @@
use crate::{ use crate::{
compile::{ctx::Ctx, error::*, ir}, compile::{ctx::Ctx, error::*, ir},
syn::ast::*, syn::{ast::prelude::*, span::*},
}; };
// basic block // basic block
@@ -10,18 +10,18 @@ pub enum Block {
} }
trait ToIr<I> { trait ToIr<I> {
fn to_ir(&self, ctx: &mut Ctx) -> Result<I>; fn to_ir(&self, trans: &mut TranslateAst<'_, '_>) -> Result<I>;
} }
trait FromAst<A>: Sized { trait FromAst<A>: Sized {
fn from_ast(other: &A, ctx: &mut Ctx) -> Result<Self>; fn from_ast(other: &A, trans: &mut TranslateAst<'_, '_>) -> Result<Self>;
} }
impl<I, A> FromAst<A> for I impl<I, A> FromAst<A> for I
where A: ToIr<I> where A: ToIr<I>
{ {
fn from_ast(other: &A, ctx: &mut Ctx) -> Result<Self> { fn from_ast(other: &A, trans: &mut TranslateAst<'_, '_>) -> Result<Self> {
other.to_ir(ctx) other.to_ir(trans)
} }
} }
@@ -42,44 +42,84 @@ impl<'c, 't> TranslateAst<'c, 't> {
pub fn translate(&mut self, ast: &Vec<Stmt>) -> Result<ir::Body> { pub fn translate(&mut self, ast: &Vec<Stmt>) -> Result<ir::Body> {
let mut body = Vec::new(); let mut body = Vec::new();
for stmt in ast.iter() { for stmt in ast.iter() {
body.push(stmt.to_ir(self.ctx)?); body.push(stmt.to_ir(self)?);
} }
Ok(body) Ok(body)
} }
} }
////////////////////////////////////////////////////////////////////////////////
// Statement conversions
////////////////////////////////////////////////////////////////////////////////
impl ToIr<ir::Stmt> for Stmt { impl ToIr<ir::Stmt> for Stmt {
fn to_ir(&self, _ctx: &mut Ctx) -> Result<ir::Stmt> { fn to_ir(&self, _trans: &mut TranslateAst<'_, '_>) -> Result<ir::Stmt> {
todo!() todo!()
} }
} }
impl ToIr<ir::Stmt> for AssignStmt { impl ToIr<ir::Stmt> for AssignStmt {
fn to_ir(&self, _ctx: &mut Ctx) -> Result<ir::Stmt> { fn to_ir(&self, _trans: &mut TranslateAst<'_, '_>) -> Result<ir::Stmt> {
todo!() todo!()
} }
} }
////////////////////////////////////////////////////////////////////////////////
// Expression conversions
////////////////////////////////////////////////////////////////////////////////
impl ToIr<ir::Expr> for Expr { impl ToIr<ir::Expr> for Expr {
fn to_ir(&self, _ctx: &mut Ctx) -> Result<ir::Expr> { fn to_ir(&self, _trans: &mut TranslateAst<'_, '_>) -> Result<ir::Expr> {
todo!() todo!()
} }
} }
impl ToIr<ir::Lhs> for Expr { impl ToIr<ir::Expr> for BinExpr {
fn to_ir(&self, _ctx: &mut Ctx) -> Result<ir::Lhs> { fn to_ir(&self, _trans: &mut TranslateAst<'_, '_>) -> Result<ir::Expr> {
todo!() todo!()
} }
} }
////////////////////////////////////////////////////////////////////////////////
// LHS conversions
////////////////////////////////////////////////////////////////////////////////
impl ToIr<ir::Lhs> for Expr {
fn to_ir(&self, _trans: &mut TranslateAst<'_, '_>) -> Result<ir::Lhs> {
todo!()
}
}
impl ToIr<ir::Lhs> for BinExpr {
fn to_ir(&self, trans: &mut TranslateAst<'_, '_>) -> Result<ir::Lhs> {
// 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<ir::Lhs> for BaseExpr {
fn to_ir(&self, trans: &mut TranslateAst<'_, '_>) -> Result<ir::Lhs> {
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<Stmt> for TranslateAst<'_, '_> {
type Out = Result<ir::Stmt>;
fn visit(&mut self, _stmt: &Stmt) -> Self::Out {
todo!()
}
}
impl Visit<AssignStmt> for TranslateAst<'_, '_> { impl Visit<AssignStmt> for TranslateAst<'_, '_> {
type Out = Result<ir::Stmt>; type Out = Result<ir::Stmt>;
@@ -91,48 +131,6 @@ impl Visit<AssignStmt> 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: &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<'_, '_> { impl Visit<BaseExpr> for TranslateAst<'_, '_> {
type Out = Result<ir::Expr>; type Out = Result<ir::Expr>;