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:
@@ -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<I> {
|
||||
fn to_ir(&self, ctx: &mut Ctx) -> Result<I>;
|
||||
fn to_ir(&self, trans: &mut TranslateAst<'_, '_>) -> Result<I>;
|
||||
}
|
||||
|
||||
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
|
||||
where A: ToIr<I>
|
||||
{
|
||||
fn from_ast(other: &A, ctx: &mut Ctx) -> Result<Self> {
|
||||
other.to_ir(ctx)
|
||||
fn from_ast(other: &A, trans: &mut TranslateAst<'_, '_>) -> Result<Self> {
|
||||
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> {
|
||||
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<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!()
|
||||
}
|
||||
}
|
||||
|
||||
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!()
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Expression conversions
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
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!()
|
||||
}
|
||||
}
|
||||
|
||||
impl ToIr<ir::Lhs> for Expr {
|
||||
fn to_ir(&self, _ctx: &mut Ctx) -> Result<ir::Lhs> {
|
||||
impl ToIr<ir::Expr> for BinExpr {
|
||||
fn to_ir(&self, _trans: &mut TranslateAst<'_, '_>) -> Result<ir::Expr> {
|
||||
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<'_, '_> {
|
||||
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<'_, '_> {
|
||||
type Out = Result<ir::Expr>;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user