2020-05-20 14:41:12 -04:00
|
|
|
use crate::{
|
2020-05-20 15:25:10 -04:00
|
|
|
compile::{ctx::Ctx, error::*, ir, visit::*},
|
2020-05-20 14:41:12 -04:00
|
|
|
syn::{ast::*, op::BinOp, span::*},
|
|
|
|
|
};
|
2020-05-19 15:26:38 -04:00
|
|
|
|
|
|
|
|
// basic block
|
|
|
|
|
pub enum Block {
|
2020-05-20 14:41:12 -04:00
|
|
|
Body(ir::Body),
|
2020-05-21 18:51:54 -04:00
|
|
|
Blocks(Vec<Block>),
|
2020-05-20 14:41:12 -04:00
|
|
|
}
|
|
|
|
|
|
2020-05-21 18:51:54 -04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// TranslateAst
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2020-05-20 14:41:12 -04:00
|
|
|
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 {
|
2020-05-20 15:25:10 -04:00
|
|
|
TranslateAst { ctx, text }
|
2020-05-20 14:41:12 -04:00
|
|
|
}
|
|
|
|
|
|
2020-05-21 18:51:54 -04:00
|
|
|
pub fn translate(&mut self, _ast: &Vec<Stmt>) -> Result<Block> {
|
2020-05-20 14:41:12 -04:00
|
|
|
todo!()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn visit_lhs_expr(&mut self, expr: &Expr) -> Result<ir::Lhs> {
|
|
|
|
|
match expr {
|
|
|
|
|
Expr::Bin(b) if b.op == BinOp::Dot => todo!(),
|
2020-05-20 15:25:10 -04:00
|
|
|
Expr::Base(BaseExpr {
|
|
|
|
|
kind: BaseExprKind::Ident,
|
|
|
|
|
..
|
|
|
|
|
}) => {
|
2020-05-20 15:33:21 -04:00
|
|
|
let _name = expr.text_at(self.text);
|
2020-05-20 14:41:12 -04:00
|
|
|
//let name_id = self.ctx.
|
2020-05-20 15:25:10 -04:00
|
|
|
todo!()
|
2020-05-20 14:41:12 -04:00
|
|
|
//Ok(ir::Lhs::Name(
|
|
|
|
|
}
|
2020-05-21 18:51:54 -04:00
|
|
|
_ => Err(Error::InvalidLhs { span: expr.span() }),
|
2020-05-20 14:41:12 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Visit<Stmt> for TranslateAst<'_, '_> {
|
|
|
|
|
type Out = Result<ir::Stmt>;
|
|
|
|
|
|
2020-05-20 15:33:21 -04:00
|
|
|
fn visit(&mut self, _stmt: &Stmt) -> Self::Out {
|
2020-05-20 14:41:12 -04:00
|
|
|
todo!()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Visit<AssignStmt> for TranslateAst<'_, '_> {
|
|
|
|
|
type Out = Result<ir::Stmt>;
|
|
|
|
|
|
2020-05-21 18:51:54 -04:00
|
|
|
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<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 {
|
2020-05-20 14:41:12 -04:00
|
|
|
todo!()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-21 18:51:54 -04:00
|
|
|
impl Visit<IndexExpr> for TranslateAst<'_, '_> {
|
2020-05-20 14:41:12 -04:00
|
|
|
type Out = Result<ir::Expr>;
|
|
|
|
|
|
2020-05-21 18:51:54 -04:00
|
|
|
fn visit(&mut self, _expr: &IndexExpr) -> Self::Out {
|
2020-05-20 14:41:12 -04:00
|
|
|
todo!()
|
|
|
|
|
}
|
2020-05-19 15:26:38 -04:00
|
|
|
}
|
2020-05-21 18:51:54 -04:00
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|